source: branches/2.2/jabberit_messenger/java_source/src/nu/fw/jeti/plugins/xhtml/fontchooser/FontPanel.java @ 3102

Revision 3102, 16.7 KB checked in by amuller, 14 years ago (diff)

Ticket #986 - Efetuado merge para o Branch 2.2( atualizacao do modulo)

  • Property svn:executable set to *
Line 
1/*
2 * SimplyHTML, a word processor based on Java, HTML and CSS
3 * Copyright (C) 2002 Ulrich Hilger
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20package nu.fw.jeti.plugins.xhtml.fontchooser;
21
22import java.awt.*;
23import java.util.Enumeration;
24import java.util.Vector;
25
26import javax.swing.JPanel;
27import javax.swing.JTextField;
28import javax.swing.SwingConstants;
29import javax.swing.border.EtchedBorder;
30import javax.swing.border.TitledBorder;
31import javax.swing.text.AttributeSet;
32import javax.swing.text.SimpleAttributeSet;
33import javax.swing.text.StyleConstants;
34import javax.swing.text.html.CSS;
35import javax.swing.text.html.StyleSheet;
36
37import nu.fw.jeti.util.I18N;
38
39/**
40 * A panel for showing and manipulating font information.
41 * <p>
42 * <code>FontPanel</code> shows and manipulates CSS attributes.
43 *  To set it to HTML attributes, methods setAttributes and getAttributes have to be overridden.
44 * </p>
45 *
46 * @author Ulrich Hilger
47 * @author Light Development
48 * @author <a href="http://www.lightdev.com">http://www.lightdev.com </a>
49 * @author <a href="mailto:info@lightdev.com">info@lightdev.com </a>
50 * @author published under the terms and conditions of the GNU General Public License,
51 *  for details see file gpl.txt in the distribution package of this software
52 * @version stage 9, release 4, January 12, 2003
53 */
54
55public class FontPanel extends JPanel implements TitledPickList.TitledPickListListener, ColorPanel.ColorPanelListener
56{
57
58        /** a text field to show a sample of the selected font attributes */
59        JTextField sample = new JTextField();
60
61        /** table for automatic font component value read/write */
62        private Vector fontComponents = new Vector(0);
63
64        public FontPanel()
65        {
66                setLayout(new BorderLayout(5, 5));
67
68                /** create a label for previewing font selections */
69                sample.setText("");
70                sample.setEditable(false);
71                sample.setPreferredSize(new Dimension(200, 50));
72                sample.setHorizontalAlignment(SwingConstants.CENTER);
73                sample.setText(I18N.gettext("xhtml.The_quick_brown_fox_jumped_over_the_lazy_dogs"));
74                JPanel previewPanel = new JPanel(new BorderLayout());
75                previewPanel.add(sample, BorderLayout.CENTER);
76                previewPanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), I18N.gettext("xhtml.Preview")));
77
78                /**
79                 * create a pick list for family filled with available font family names
80                 */
81                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
82                FamilyPickList family = new FamilyPickList(ge.getAvailableFontFamilyNames(), I18N.gettext("xhtml.Font_Family"));
83                family.addTitledPickListListener(this);
84                fontComponents.add(family);
85
86                /** create a pick list for font size */
87                String[] fontSizes = new String[] { "8", "10", "12", "14", "18", "24"};
88                SizePickList size = new SizePickList(fontSizes, (I18N.gettext("xhtml.Size")));
89                size.addTitledPickListListener(this);
90                fontComponents.add(size);
91
92                /** wrap together family and size */
93                JPanel familySizePanel = new JPanel(new BorderLayout(5, 5));
94                familySizePanel.add(family, BorderLayout.CENTER);
95                familySizePanel.add(size, BorderLayout.EAST);
96
97                /** create a panel to put font parts family, size and stlye in */
98                JPanel fontPartsPanel = new JPanel(new BorderLayout(5, 5));
99                fontPartsPanel.add(familySizePanel, BorderLayout.CENTER);
100                String[] fontStyles = new String[] { I18N.gettext("xhtml.Plain"), I18N.gettext("xhtml.Bold"), I18N.gettext("xhtml.Italic"),
101                                I18N.gettext("xhtml.BoldItalic")};
102                StylePickList style = new StylePickList(fontStyles, I18N.gettext("xhtml.Style"));
103                style.addTitledPickListListener(this);
104                fontPartsPanel.add(style, BorderLayout.EAST);
105                fontComponents.add(style);
106
107                /** create a panel for underline / line through */
108                EffectPanel linePanel = new EffectPanel();
109                fontComponents.add(linePanel);
110
111                /** create a panel for color choices */
112                JPanel colorPanel = new JPanel(new GridLayout(2, 1, 3, 3));
113                colorPanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), I18N.gettext("xhtml.Color")));
114                ColorPanel fCol = new ColorPanel(I18N.gettext("xhtml.foreground"), Color.black);
115                fCol.addColorPanelListener(this);
116                fontComponents.add(fCol);
117                ColorPanel bCol = new ColorPanel(I18N.gettext("xhtml.background"), Color.white);
118                bCol.addColorPanelListener(this);
119                fontComponents.add(bCol);
120                colorPanel.add(fCol);
121                colorPanel.add(bCol);
122
123                sample.setForeground(Color.black);
124                sample.setBackground(Color.white);
125
126                /** create a panel to combine line and color choices */
127                JPanel eastPanel = new JPanel(new BorderLayout());
128                eastPanel.add(linePanel, BorderLayout.NORTH);
129                eastPanel.add(colorPanel, BorderLayout.SOUTH);
130
131                /** add all font controls to our font panel */
132                add(fontPartsPanel, BorderLayout.CENTER);
133                add(eastPanel, BorderLayout.EAST);
134                add(previewPanel, BorderLayout.SOUTH);
135                add(new JPanel(), BorderLayout.NORTH);
136                add(new JPanel(), BorderLayout.WEST);
137        }
138
139        /**
140         * construct a FontPanel and display a set of attributes
141         *
142         * @param frame the main frame having the ResourceBundle
143         * @param a the set of attributes to display
144         */
145        public FontPanel(AttributeSet a)
146        {
147                this();
148
149                /** set the new FontPanel to display our set of attributes */
150                setAttributes(a);
151        }
152
153        /**
154         * handle ColorChangeEvents from one of our color panels
155         *
156         * @param e the ColorPanelEvent to handle
157         */
158        public void colorChanged(ColorPanel.ColorPanelEvent e)
159        {
160                ColorPanel source = (ColorPanel) e.getSource();
161                if (source.getAttributeKey() == CSS.Attribute.COLOR)
162                {
163                        sample.setForeground(source.getColor());
164                } else if (source.getAttributeKey() == CSS.Attribute.BACKGROUND_COLOR)
165                {
166                        sample.setBackground(source.getColor());
167                }
168        }
169
170        /**
171         * set all components of this FontPanel to reflect a set of attributes.
172         *
173         * @param a the set of attributes to show
174         */
175        public void setAttributes(AttributeSet a)
176        {
177                Enumeration components = fontComponents.elements();
178                while (components.hasMoreElements())
179                {
180                        ((AttributeComponent) components.nextElement()).setValue(a);
181                }
182        }
183
184        /**
185         * get the set of attributes resulting from the settings on this FontPanel.
186         *
187         * @return the set of attributes set in this FontPanel
188         */
189        public AttributeSet getAttributes()
190        {
191                SimpleAttributeSet attributes = new SimpleAttributeSet();
192                Enumeration components = fontComponents.elements();
193                while (components.hasMoreElements())
194                {
195                        attributes.addAttributes(((AttributeComponent) components.nextElement()).getValue());
196                }
197                return attributes;
198        }
199
200        public AttributeSet getAttributes(boolean includeUnchanged)
201        {
202                if (includeUnchanged)
203                {
204                        SimpleAttributeSet attributes = new SimpleAttributeSet();
205                        Enumeration components = fontComponents.elements();
206                        while (components.hasMoreElements())
207                        {
208                                attributes.addAttributes(((AttributeComponent) components.nextElement()).getValue(includeUnchanged));
209                        }
210                        return attributes;
211                } else
212                {
213                        return getAttributes();
214                }
215        }
216
217        public void reset()
218        {
219                Object c;
220                for (int i = 0; i < fontComponents.size(); i++)
221                {
222                        c = fontComponents.get(i);
223                        if (c instanceof FamilyPickList)
224                        {
225                                ((FamilyPickList) c).reset();
226                        } else if (c instanceof SizePickList)
227                        {
228                                ((SizePickList) c).reset();
229                        } else if (c instanceof StylePickList)
230                        {
231                                ((StylePickList) c).reset();
232                        }
233                }
234        }
235
236        /**
237         * if another value was picked from a list, update the sample
238         */
239        public void valueChanged(TitledPickList.TitledPickListEvent e)
240        {
241                Object source = e.getSource();
242                Font saveFont = sample.getFont();
243                if (source instanceof FamilyPickList)
244                {
245                        sample.setFont(new Font(((FamilyPickList) source).getFamily(), saveFont.getStyle(), saveFont.getSize()));
246                } else if (source instanceof SizePickList)
247                {
248                        sample.setFont(new Font(saveFont.getFamily(), saveFont.getStyle(), Integer.parseInt((String) ((SizePickList) source).getSelection())));
249                        /* adjustFontSize(Integer.parseInt((String) ((SizePickList) source).getSelection())))); */
250                } else if (source instanceof StylePickList)
251                {
252                        sample.setFont(new Font(saveFont.getFamily(), ((StylePickList) source).getFontStyle(), saveFont.getSize()));
253                }
254        }
255
256        /**
257         * extend <code>TitledPickList</code> with a way to set values special to font family values
258         */
259        static class FamilyPickList extends TitledPickList implements AttributeComponent
260        {
261
262                private int setValCount = 0;
263                private Object originalValue;
264
265                /**
266                 * constructor
267                 *
268                 * @param options the options to be selectable in this list
269                 * @param titleText the title for the pick list
270                 */
271                FamilyPickList(String[] options, String titleText)
272                {
273                        super(options, titleText);
274                }
275
276                /**
277                 * set the value of this <code>TitledPickList</code>
278                 *
279                 * @param a the set of attributes possibly having a font family attribute this pick list could display
280                 * @return true, if the set of attributes had a font family attribute, false if not
281                 */
282                public boolean setValue(AttributeSet a)
283                {
284                        boolean success = false;
285                        ignoreTextChanges = true;
286                        Object newSelection;
287                        //      if(a.isDefined(CSS.Attribute.FONT_FAMILY)) {
288                        //        newSelection = a.getAttribute(CSS.Attribute.FONT_FAMILY);
289                        //      setSelection(a.getAttribute(CSS.Attribute.FONT_FAMILY));
290                        //      success = true;
291                        //      }
292                        //a.isDefined(StyleConstants.FontFamily)
293                        String family = StyleConstants.getFontFamily(a);
294                        if (family != null)
295                        {
296
297                                newSelection = family;
298                                setSelection(family);
299                                success = true;
300                        } else
301                        {
302                                newSelection = "SansSerif";
303                                setSelection(newSelection);
304                        }
305                        ignoreTextChanges = false;
306                        if (++setValCount < 2)
307                        {
308                                originalValue = newSelection;
309                        }
310                        return success;
311                }
312
313                public AttributeSet getValue()
314                {
315                        SimpleAttributeSet set = new SimpleAttributeSet();
316                        Object value = getSelection();
317                        //System.out.println("FamilyPicker getValue originalValue=" + originalValue);
318                        //System.out.println("FamilyPicker getValue value=" + value);
319                        if (((originalValue == null) && (value != null))
320                                        || ((originalValue != null) && (value != null) && (!originalValue.toString().equalsIgnoreCase(value.toString()))))
321                        {
322                                //Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_FAMILY, value.toString());
323                                StyleConstants.setFontFamily(set, value.toString());
324                        }
325                        return set;
326                }
327
328                public AttributeSet getValue(boolean includeUnchanged)
329                {
330                        if (includeUnchanged)
331                        {
332                                SimpleAttributeSet set = new SimpleAttributeSet();
333                                Object value = getSelection();
334                                //System.out.println("FamilyPicker getValue originalValue=" + originalValue);
335                                //System.out.println("FamilyPicker getValue value=" + value);
336                                //Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_FAMILY, value.toString());
337                                StyleConstants.setFontFamily(set, value.toString());
338                                return set;
339                        } else
340                        {
341                                return getValue();
342                        }
343                }
344
345                public String getFamily()
346                {
347                        return (String) getSelection();
348                }
349
350                public void reset()
351                {
352                        setValCount = 0;
353                        originalValue = null;
354                }
355        }
356
357        /**
358         * extend <code>TitledPickList</code> with a way to set values special to font size values
359         */
360        static class SizePickList extends TitledPickList implements AttributeComponent
361        {
362
363                // private Object key;
364                private int setValCount = 0;
365                private String originalValue;
366
367                /**
368                 * constructor
369                 *
370                 * @param options the options to be selectable in this list
371                 * @param titleText the title for the pick list
372                 */
373                SizePickList(String[] options, String titleText)
374                {
375                        super(options, titleText);
376                        //this.key = key;
377                }
378
379                /**
380                 * set the value of this <code>TitledPickList</code>
381                 *
382                 * @param a the set of attributes possibly having a font size attribute this pick list could display
383                 * @return true, if the set of attributes had a font size attribute, false if not
384                 */
385                public boolean setValue(AttributeSet a)
386                {
387                        ignoreTextChanges = true;
388                        boolean success = false;
389                        //Object attr = a.getAttribute(key);
390                        String newSelection;
391                        if (a != null)
392                        {
393                                //LengthValue lv = new LengthValue(a.getAttribute(key));
394                                //int val = new Float(lv.getAttrValue(attr.toString(), LengthValue.pt)).intValue();
395                                int val = StyleConstants.getFontSize(a); //(int) Util.getAttrValue(a.getAttribute(key));
396                                if (val > 0)
397                                {
398                                        success = true;
399                                        newSelection = new Integer(val).toString();
400                                        setSelection(newSelection);
401                                } else
402                                {
403                                        newSelection = "12";
404                                        setSelection(newSelection);
405                                }
406                        } else
407                        {
408                                newSelection = "12";
409                                setSelection(newSelection);
410                        }
411                        ignoreTextChanges = false;
412                        if (++setValCount < 2)
413                        {
414                                originalValue = newSelection;
415                        }
416                        return success;
417                }
418
419                public AttributeSet getValue()
420                {
421                        SimpleAttributeSet set = new SimpleAttributeSet();
422                        String value = (String) getSelection();
423                        if (((originalValue == null) && (value != null)) || ((originalValue != null) && (!originalValue.equalsIgnoreCase(value))))
424                        {
425                                //       Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_SIZE,
426                                //  set.addAttribute("size", (String) getSelection() /*+ "pt"*/);
427                                StyleConstants.setFontSize(set, Integer.parseInt((String) getSelection()));
428                        }
429                        return set;
430                }
431
432                public AttributeSet getValue(boolean includeUnchanged)
433                {
434                        if (includeUnchanged)
435                        {
436                                SimpleAttributeSet set = new SimpleAttributeSet();
437
438                                //Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_SIZE,
439                                //(String) getSelection() /*+ "pt"*/);
440                                StyleConstants.setFontSize(set, Integer.parseInt((String) getSelection()));
441                                return set;
442                        } else
443                        {
444                                return getValue();
445                        }
446                }
447
448                public void reset()
449                {
450                        setValCount = 0;
451                        originalValue = null;
452                }
453        }
454
455        private static StyleSheet styleSheet = new StyleSheet();
456        /**
457         * extend <code>TitledPickList</code> with a way to set values special to font style values
458         */
459        static class StylePickList extends TitledPickList implements AttributeComponent
460        {
461
462                private int setValCount = 0;
463
464                /**
465                 * constructor
466                 *
467                 * @param options the options to be selectable in this list
468                 * @param titleText the title for the pick list
469                 */
470                StylePickList(String[] options, String titleText)
471                {
472                        super(options, titleText);
473                }
474
475                /**
476                 * set the value of this <code>TitledPickList</code>
477                 *
478                 * @param a the set of attributes possibly having a font style attribute this pick list could display
479                 * @return true, if the set of attributes had a font style attribute, false if not
480                 */
481                public boolean setValue(AttributeSet a)
482                {
483                        ignoreTextChanges = true;
484                        boolean success = false;
485                        int styleNo = 0;
486                        String value;
487                        if (StyleConstants.isBold(a))
488                        {
489                                styleNo++;
490                        }
491                        if (StyleConstants.isItalic(a))
492                        {
493                                styleNo += 2;
494                        }
495                        setSelection(styleNo);
496                        if (++setValCount < 2)
497                        {
498
499                        }
500                        ignoreTextChanges = false;
501                        return success;
502                }
503
504                public AttributeSet getValue()
505                {
506                        SimpleAttributeSet set = new SimpleAttributeSet();
507                        int styleNo = getIndex();
508                        switch (styleNo)
509                        {
510                                case 1:
511                                        StyleConstants.setBold(set,true);
512                                        break;
513                                case 2:
514                                        StyleConstants.setItalic(set,true);
515                                        break;
516                                case 3:
517                                        StyleConstants.setBold(set,true);
518                                        StyleConstants.setItalic(set,true);
519                                        break;
520                        }
521                        return set;
522                }
523
524                public AttributeSet getValue(boolean includeUnchanged)
525                {
526//                      if (includeUnchanged)
527//                      {
528//                              String value = "CSS_ATTRIBUTE_NORMAL";
529//                              SimpleAttributeSet set = new SimpleAttributeSet();
530//                              int styleNo = getIndex();
531//                              switch (styleNo)
532//                              {
533//                                      case 0:
534//                                              styleSheet.addCSSAttribute(set, CSS.Attribute.FONT_STYLE, value);
535//                                              break;
536//                                      case 1:
537//                                              styleSheet.addCSSAttribute(set, CSS.Attribute.FONT_WEIGHT, StyleConstants.Bold.toString());
538//                                              break;
539//                                      case 2:
540//                                              styleSheet.addCSSAttribute(set, CSS.Attribute.FONT_STYLE, StyleConstants.Italic.toString());
541//                                              break;
542//                                      case 3:
543//                                              styleSheet.addCSSAttribute(set, CSS.Attribute.FONT_WEIGHT, StyleConstants.Bold.toString());
544//                                              styleSheet.addCSSAttribute(set, CSS.Attribute.FONT_STYLE, StyleConstants.Italic.toString());
545//                                              break;
546//                              }
547//                              return set;
548//                      } else
549                        {
550                                return getValue();
551                        }
552                }
553
554                public int getFontStyle()
555                {
556                        int fontStyle = 0;
557                        switch (getIndex())
558                        {
559                                case 0:
560                                        fontStyle = Font.PLAIN;
561                                        break;
562                                case 1:
563                                        fontStyle = Font.BOLD;
564                                        break;
565                                case 2:
566                                        fontStyle = Font.ITALIC;
567                                        break;
568                                case 3:
569                                        fontStyle = Font.BOLD | Font.ITALIC;
570                                        break;
571                        }
572                        return fontStyle;
573                }
574
575                public void reset()
576                {
577                        setValCount = 0;
578
579                }
580        }
581}
582/*
583 * Overrides for emacs
584 * Local variables:
585 * tab-width: 4
586 * End:
587 */
Note: See TracBrowser for help on using the repository browser.