KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > editors2 > FontEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.form.editors2;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.beans.*;
24 import java.io.IOException JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.util.ResourceBundle JavaDoc;
27 import javax.swing.*;
28 import javax.swing.border.Border JavaDoc;
29 import javax.swing.event.ChangeEvent JavaDoc;
30 import javax.swing.event.ChangeListener JavaDoc;
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33
34 import org.jdesktop.layout.GroupLayout;
35 import org.jdesktop.layout.LayoutStyle;
36
37 import org.openide.awt.Mnemonics;
38 import org.openide.explorer.propertysheet.ExPropertyEditor;
39 import org.openide.explorer.propertysheet.PropertyEnv;
40 import org.openide.explorer.propertysheet.editors.XMLPropertyEditor;
41 import org.openide.util.NbBundle;
42
43 import org.netbeans.modules.form.*;
44 import org.netbeans.modules.form.codestructure.CodeVariable;
45
46 /**
47  * Font property editor that supports relative changes of the font.
48  *
49  * @author Jan Stola
50  */

51 public class FontEditor implements ExPropertyEditor, XMLPropertyEditor,
52     FormAwareEditor, PropertyChangeListener {
53     /**
54      * Property editor delegate used for static/absolute values.
55      */

56     private PropertyEditor delegate;
57     /**
58      * The current value of the property maintained by this property editor.
59      */

60     private NbFont propertyValue;
61     /**
62      * Edited property.
63      */

64     private RADProperty property;
65     /**
66      * Property change support.
67      */

68     private PropertyChangeSupport propChangeSupport;
69     /**
70      * Determines whether (internal) updates should be ignored.
71      */

72     private boolean ignoreUpdates = true;
73     
74     public FontEditor() {
75         propertyValue = new NbFont();
76         propChangeSupport = new PropertyChangeSupport(this);
77         delegate = PropertyEditorManager.findEditor(Font.class);
78         if (delegate == null) {
79             throw new IllegalStateException JavaDoc("FontEditor delegate not found."); // NOI18N
80
}
81         if (!(delegate instanceof XMLPropertyEditor)) {
82             throw new IllegalStateException JavaDoc("FontEditor delegate doesn't implement XMLPropertyEditor."); // NOI18N
83
}
84         delegate.addPropertyChangeListener(this);
85     }
86
87     public void setValue(Object JavaDoc value) {
88         if ((value instanceof Font) || (value == null)) {
89             propertyValue = new NbFont();
90             propertyValue.absolute = true;
91             propertyValue.font = (Font)value;
92         } else if (value instanceof NbFont) {
93             propertyValue = ((NbFont)value).copy();
94         } else {
95             throw new IllegalArgumentException JavaDoc();
96         }
97         if (property != null) {
98             propertyValue.property = property;
99         }
100         delegate.setValue(propertyValue.getDesignValue());
101     }
102
103     public Object JavaDoc getValue() {
104         return propertyValue.absolute ? propertyValue.font : propertyValue;
105     }
106
107     public boolean isPaintable() {
108         return delegate.isPaintable();
109     }
110
111     public void paintValue(Graphics gfx, Rectangle box) {
112         delegate.paintValue(gfx, box);
113     }
114
115     public String JavaDoc getJavaInitializationString() {
116         String JavaDoc exp;
117         if (propertyValue.absolute) {
118             exp = delegate.getJavaInitializationString();
119         } else {
120             RADComponent comp = property.getRADComponent();
121             CodeVariable var = comp.getCodeExpression().getVariable();
122             String JavaDoc varName = (var == null) ? null : var.getName();
123             String JavaDoc readMethod = property.getPropertyDescriptor().getReadMethod().getName();
124             String JavaDoc getter = readMethod + "()"; // NOI18N
125
if (varName != null) {
126                 getter = varName + '.' + getter;
127             }
128             exp = getter + ".deriveFont("; // NOI18N
129
boolean styleChanged = (propertyValue.italic != null) || (propertyValue.bold != null);
130             if (styleChanged) {
131                 String JavaDoc styleExp = null;
132                 if (propertyValue.italic != null) {
133                     styleExp = getter + ".getStyle()"; // NOI18N
134
if (Boolean.TRUE.equals(propertyValue.italic)) {
135                         styleExp += " | "; // NOI18N
136
} else{
137                         styleExp += " & ~"; // NOI18N
138
}
139                     styleExp += "java.awt.Font.ITALIC"; // NOI18N
140
}
141                 if (styleExp == null) {
142                     styleExp = getter + ".getStyle()"; // NOI18N
143
} else {
144                     styleExp = "(" + styleExp + ")"; // NOI18N
145
}
146                 if (propertyValue.bold != null) {
147                     if (Boolean.TRUE.equals(propertyValue.bold)) {
148                         styleExp += " | "; // NOI18N
149
} else{
150                         styleExp += " & ~"; // NOI18N
151
}
152                     styleExp += "java.awt.Font.BOLD"; // NOI18N
153
}
154                 exp += styleExp;
155             }
156             if (propertyValue.absoluteSize) {
157                 exp += styleChanged ? ", " : "(float)"; // NOI18N
158
exp += propertyValue.size + ")"; // NOI18N
159
} else {
160                 if (propertyValue.size == 0) {
161                     if (styleChanged) {
162                         exp += ')';
163                     } else {
164                         exp = getter;
165                     }
166                 } else {
167                     if (styleChanged) {
168                         exp += ", "; // NOI18N
169
}
170                     exp += getter + ".getSize()"; // NOI18N
171
if (propertyValue.size > 0) {
172                         exp += '+';
173                     }
174                     exp += propertyValue.size;
175                     if (!styleChanged) exp += "f"; // NOI18N
176
exp += ")"; // NOI18N
177
}
178             }
179         }
180         return exp;
181     }
182
183     public String JavaDoc getAsText() {
184         return propertyValue.absolute ? delegate.getAsText() : propertyValue.getDescription();
185     }
186
187     public void setAsText(String JavaDoc text) {
188         return;
189     }
190
191     public String JavaDoc[] getTags() {
192         return null;
193     }
194
195     // Hack for ugly behaviour of property sheet - it sometimes calls
196
// getCustomEditor() before attachEnv()
197
private WeakReference JavaDoc lastSwitchBox;
198     public Component getCustomEditor() {
199         // The custom editor changes iternals of NbFont
200
// We must edit another instance because user can cancel the changes
201
// and they would remain in the original NbFont
202
propertyValue = ((NbFont)propertyValue).copy();
203         final Component absolute = propertyValue.absolute ? delegate.getCustomEditor() : null;
204         String JavaDoc switchBoxText = NbBundle.getMessage(FontEditor.class, "CTL_DeriveFont"); // NOI18N
205
final JCheckBox switchBox = new JCheckBox();
206         lastSwitchBox = new WeakReference JavaDoc(switchBox);
207         if (property == null) {
208             switchBox.setVisible(false);
209         } else {
210             Mnemonics.setLocalizedText(switchBox, switchBoxText);
211             switchBox.setSelected(!propertyValue.absolute);
212         }
213         final RelativeFontPanel relative = new RelativeFontPanel();
214         Component pane = propertyValue.absolute ? absolute : relative;
215         if (!propertyValue.absolute) relative.updateFromPropertyValue();
216
217         final JPanel editor = new JPanel();
218         final GroupLayout layout = new GroupLayout(editor);
219         editor.setLayout(layout);
220         layout.setHorizontalGroup(layout.createParallelGroup()
221             .add(layout.createSequentialGroup()
222                 .addContainerGap()
223                 .add(switchBox))
224             .add(pane));
225         layout.setVerticalGroup(layout.createSequentialGroup()
226             .addContainerGap()
227             .add(switchBox)
228             .addPreferredGap(LayoutStyle.RELATED)
229             .add(pane)
230             .addContainerGap());
231
232         switchBox.addItemListener(new ItemListener() {
233             private Component absoluteInLayout = absolute;
234             public void itemStateChanged(ItemEvent e) {
235                 if (switchBox.isSelected()) {
236                     layout.replace(absoluteInLayout, relative);
237                     propertyValue.absolute = false;
238                     convertToRelative();
239                     relative.updateFromPropertyValue();
240                 } else {
241                     absoluteInLayout = delegate.getCustomEditor();
242                     layout.replace(relative, absoluteInLayout);
243                     propertyValue.absolute = true;
244                 }
245                 firePropertyChange();
246                 editor.revalidate();
247                 editor.repaint();
248             }
249         });
250
251         return editor;
252     }
253
254     private void convertToRelative() {
255         if (propertyValue.font == null) return;
256         Font defaultFont = (Font)property.getDefaultValue();
257         if (propertyValue.absoluteSize) {
258             propertyValue.size = propertyValue.font.getSize();
259         } else {
260             if (defaultFont == null) return;
261             propertyValue.size = propertyValue.font.getSize() - defaultFont.getSize();
262         }
263         if (defaultFont == null) return;
264         int absoluteStyle = propertyValue.font.getStyle();
265         int defaultStyle = defaultFont.getStyle();
266         boolean aItalic = ((absoluteStyle & Font.ITALIC) != 0);
267         boolean dItalic = ((defaultStyle & Font.ITALIC) != 0);
268         if (aItalic && !dItalic) {
269             propertyValue.italic = Boolean.TRUE;
270         }
271         if (!aItalic && dItalic) {
272             propertyValue.italic = Boolean.FALSE;
273         }
274         if ((propertyValue.italic != null) && (aItalic == dItalic)
275             && (aItalic != propertyValue.italic.booleanValue())) {
276             propertyValue.italic = null;
277         }
278         boolean aBold = ((absoluteStyle & Font.BOLD) != 0);
279         boolean dBold = ((defaultStyle & Font.BOLD) != 0);
280         if (aBold && !dBold) {
281             propertyValue.bold = Boolean.TRUE;
282         }
283         if (!aBold && dBold) {
284             propertyValue.bold = Boolean.FALSE;
285         }
286         if ((propertyValue.bold != null) && (aBold == dBold)
287             && (aBold != propertyValue.bold.booleanValue())) {
288             propertyValue.bold = null;
289         }
290     }
291
292     public boolean supportsCustomEditor() {
293         return true;
294     }
295
296     public void addPropertyChangeListener(PropertyChangeListener listener) {
297         propChangeSupport.addPropertyChangeListener(listener);
298     }
299
300     public void removePropertyChangeListener(PropertyChangeListener listener) {
301         propChangeSupport.removePropertyChangeListener(listener);
302     }
303
304     // PropertyChangeListener implementation
305
public void propertyChange(PropertyChangeEvent evt) {
306         Font font = (Font)delegate.getValue();
307         propertyValue.font = font;
308         if (!ignoreUpdates) {
309             firePropertyChange();
310         }
311     }
312
313     private void updateDelegate() {
314         ignoreUpdates = true;
315         delegate.setValue(propertyValue.getDesignValue());
316         ignoreUpdates = false;
317         firePropertyChange();
318     }
319
320     private void firePropertyChange() {
321         propChangeSupport.firePropertyChange("", null, null); // NOI18N
322
}
323
324     // ExPropertyEditor implementation
325
public void attachEnv(PropertyEnv env) {
326         FeatureDescriptor prop = env.getFeatureDescriptor();
327         // Don't support relative changes for nested properties
328
// (for example Font property of TitledBorder)
329
if ((prop instanceof RADProperty) && (env.getBeans().length == 1)) {
330             property = (RADProperty)prop;
331             if (propertyValue != null) {
332                 propertyValue.property = property;
333             }
334         } else {
335             if (lastSwitchBox != null) { // Hack - see comment for lastSwitchBox field
336
Object JavaDoc switchBox = lastSwitchBox.get();
337                 if (switchBox != null) {
338                     AbstractButton button = ((AbstractButton)switchBox);
339                     button.setVisible(false);
340                     if (button.isSelected()) button.setSelected(false);
341                 }
342             }
343             property = null;
344         }
345     }
346
347     // XMLPropertyEditor implementation
348
/** Root of the XML representation of the font. */
349     public static final String JavaDoc XML_FONT_ROOT = "FontInfo"; // NOI18N
350
/** Element with information about the font. */
351     public static final String JavaDoc XML_FONT = "Font"; // NOI18N
352
/** Determines whether the font is relative. */
353     public static final String JavaDoc ATTR_RELATIVE = "relative"; // NOI18N
354
/** Determines whether the font size is relative to the default value. */
355     public static final String JavaDoc ATTR_RELATIVE_SIZE = "relativeSize"; // NOI18N
356
/** Attribute for the font size. */
357     public static final String JavaDoc ATTR_SIZE = "size"; // NOI18N
358
/** Attribute for the change of italic. */
359     public static final String JavaDoc ATTR_ITALIC_CHANGE = "italic"; // NOI18N
360
/** Attribute for the change of thickness. */
361     public static final String JavaDoc ATTR_BOLD_CHANGE = "bold"; // NOI18N
362
/** Name of the component this value belongs to. */
363     public static final String JavaDoc ATTR_COMP_NAME = "component"; // NOI18N
364
/** Name of the property this value belongs to. */
365     public static final String JavaDoc ATTR_PROP_NAME = "property"; // NOI18N
366

367     public void readFromXML(Node JavaDoc element) throws IOException JavaDoc {
368         if (!XML_FONT_ROOT.equals(element.getNodeName())) {
369             // Backward compatibility with openide's FontEditor
370
((XMLPropertyEditor)delegate).readFromXML(element);
371             setValue(delegate.getValue());
372             return;
373         }
374         org.w3c.dom.NamedNodeMap JavaDoc attributes = element.getAttributes();
375         boolean relative = Boolean.valueOf(attributes.getNamedItem(ATTR_RELATIVE).getNodeValue()).booleanValue();
376         propertyValue = new NbFont();
377         propertyValue.absolute = !relative;
378         org.w3c.dom.NodeList JavaDoc subnodes = element.getChildNodes();
379         for (int i=0; i<subnodes.getLength(); i++){
380             org.w3c.dom.Node JavaDoc subnode = subnodes.item(i);
381             if (subnode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
382                 if (relative) {
383                     if (!XML_FONT.equals(subnode.getNodeName())) {
384                         throw new java.io.IOException JavaDoc();
385                     }
386                     attributes = subnode.getAttributes();
387                     propertyValue.absoluteSize = !Boolean.valueOf(attributes.getNamedItem(ATTR_RELATIVE_SIZE).getNodeValue()).booleanValue();
388                     propertyValue.size = Integer.parseInt(attributes.getNamedItem(ATTR_SIZE).getNodeValue());
389                     org.w3c.dom.Node JavaDoc italicChange = attributes.getNamedItem(ATTR_ITALIC_CHANGE);
390                     if (italicChange != null) {
391                         propertyValue.italic = Boolean.valueOf(italicChange.getNodeValue());
392                     }
393                     org.w3c.dom.Node JavaDoc boldChange = attributes.getNamedItem(ATTR_BOLD_CHANGE);
394                     if (boldChange != null) {
395                         propertyValue.bold = Boolean.valueOf(boldChange.getNodeValue());
396                     }
397                     String JavaDoc compName = attributes.getNamedItem(ATTR_COMP_NAME).getNodeValue();
398                     RADComponent component = formModel.findRADComponent(compName);
399                     String JavaDoc propName = attributes.getNamedItem(ATTR_PROP_NAME).getNodeValue();
400                     property = (RADProperty)component.getPropertyByName(propName);
401                     propertyValue.property = property;
402                     delegate.setValue(propertyValue.getDesignValue());
403                 } else {
404                     ((XMLPropertyEditor)delegate).readFromXML(subnode);
405                     propertyValue.font = (Font)delegate.getValue();
406                 }
407                 break;
408             }
409         }
410     }
411
412     public Node JavaDoc storeToXML(Document JavaDoc doc) {
413         org.w3c.dom.Element JavaDoc el = doc.createElement(XML_FONT_ROOT);
414         el.setAttribute(ATTR_RELATIVE, Boolean.toString(!propertyValue.absolute));
415         if (propertyValue.absolute) {
416             org.w3c.dom.Node JavaDoc absNode = ((XMLPropertyEditor)delegate).storeToXML(doc);
417             el.appendChild(absNode);
418         } else {
419             org.w3c.dom.Element JavaDoc subel = doc.createElement(XML_FONT);
420             el.appendChild(subel);
421             subel.setAttribute(ATTR_RELATIVE_SIZE, Boolean.toString(!propertyValue.absoluteSize));
422             subel.setAttribute(ATTR_SIZE, Integer.toString(propertyValue.size));
423             if (propertyValue.italic != null) {
424                 subel.setAttribute(ATTR_ITALIC_CHANGE, propertyValue.italic.toString());
425             }
426             if (propertyValue.bold != null) {
427                 subel.setAttribute(ATTR_BOLD_CHANGE, propertyValue.bold.toString());
428             }
429             subel.setAttribute(ATTR_COMP_NAME, property.getRADComponent().getName());
430             subel.setAttribute(ATTR_PROP_NAME, property.getName());
431         }
432         return el;
433     }
434
435     // FormAwarePropertyEditor implementation
436
private FormModel formModel;
437     public void setFormModel(FormModel model) {
438         this.formModel = model;
439     }
440
441     static class NbFont implements FormDesignValue {
442         /**
443          * Determines whether this is an absolute/static value
444          * or relative/dynamic value.
445          */

446         boolean absolute = false;
447         /**
448          * Absolute/static value of the font.
449          */

450         Font font;
451         /**
452          * Describes the relative change of italic.
453          * <code>true = add italic, false = remove italic, null = leave it as it is</code>
454          */

455         Boolean JavaDoc italic;
456         /**
457          * Describes the relative change of bold.
458          * <code>true = add bold, false = remove false, null = leave it as it is</code>
459          */

460         Boolean JavaDoc bold;
461         /**
462          * Determines whether the change of the font size is relative or absolute.
463          */

464         boolean absoluteSize;
465         /**
466          * Describes the change of the font size.
467          * <code>size = (absoluteSize) ? absolute size : relative change</code>
468          */

469         int size;
470         /**
471          * Property that contains this value.
472          */

473         FormProperty property;
474
475         public Object JavaDoc getDesignValue() {
476             Font value;
477             if (absolute) {
478                 value = font;
479             } else {
480                 value = defaultValue(property);
481                 if (value != null) {
482                     int origStyle = value.getStyle();
483                     int style = origStyle;
484                     if (italic != null) {
485                         if (italic.booleanValue()) {
486                             style |= Font.ITALIC;
487                         } else {
488                             style &= ~Font.ITALIC;
489                         }
490                     }
491                     if (bold != null) {
492                         if (bold.booleanValue()) {
493                             style |= Font.BOLD;
494                         } else {
495                             style &= ~Font.BOLD;
496                         }
497                     }
498                     int origSize = value.getSize();
499                     int newSize = (absoluteSize) ? size : (size + origSize);
500                     if ((style != origStyle) || (origSize != newSize)) {
501                         value = value.deriveFont(style, newSize);
502                     }
503                 }
504             }
505             return value;
506         }
507         
508         private Font defaultValue(FormProperty property) {
509             if ((property instanceof RADProperty) && FormLAF.getUsePreviewDefaults()) {
510                 RADProperty radProp = (RADProperty)property;
511                 PropertyDescriptor propDesc = radProp.getPropertyDescriptor();
512                 java.lang.reflect.Method JavaDoc readMethod = propDesc.getReadMethod();
513                 if (readMethod != null) {
514                     try {
515                         Class JavaDoc clazz = radProp.getRADComponent().getBeanClass();
516                         Object JavaDoc beanInstance = BeanSupport.createBeanInstance(clazz);
517                         return (Font)readMethod.invoke(beanInstance, new Object JavaDoc [0]);
518                     } catch (Exception JavaDoc e) {
519                     }
520                 }
521             }
522             return (Font)property.getDefaultValue();
523         }
524
525         public String JavaDoc getDescription() {
526             ResourceBundle JavaDoc bundle = NbBundle.getBundle(FontEditor.class);
527             String JavaDoc description;
528             if (absolute) {
529                 String JavaDoc style = null;
530                 switch (font.getStyle()) {
531                     case Font.PLAIN: style = bundle.getString("CTL_FontStylePlain"); break; // NOI18N
532
case Font.BOLD: style = bundle.getString("CTL_FontStyleBold"); break; // NOI18N
533
case Font.ITALIC: style = bundle.getString("CTL_FontStyleItalic"); break; // NOI18N
534
case Font.BOLD|Font.ITALIC: style = bundle.getString("CTL_FontStyleBoldItalic"); break; // NOI18N
535
default: style = Integer.toString(font.getStyle()); break;
536                 }
537                 description = font.getName() + ' ' + font.getSize() + ' ' + style;
538             } else {
539                 description = Integer.toString(size);
540                 if (!absoluteSize && (size > 0)) {
541                     description = '+' + description;
542                 }
543                 if (italic != null) {
544                     description += " " + (italic.booleanValue() ? '+' : '-') + bundle.getString("CTL_FontStyleItalic"); // NOI18N
545
}
546                 if (bold != null) {
547                     description += " " + (bold.booleanValue() ? '+' : '-') + bundle.getString("CTL_FontStyleBold"); // NOI18N
548
}
549                 if (description.charAt(0) == '0') description = description.substring(Math.min(2, description.length()));
550             }
551             return description;
552         }
553
554         public FormDesignValue copy(FormProperty targetFormProperty) {
555             NbFont copy = copy();
556             copy.property = targetFormProperty;
557             return copy;
558         }
559
560         NbFont copy() {
561             NbFont newValue = new NbFont();
562             newValue.absolute = absolute;
563             newValue.font = font;
564             newValue.italic = italic;
565             newValue.bold = bold;
566             newValue.absoluteSize = absoluteSize;
567             newValue.size = size;
568             newValue.property = property;
569             return newValue;
570         }
571
572     }
573
574     /**
575      * Panel used to configurate the relative change.
576      */

577     private class RelativeFontPanel extends JPanel {
578         private JRadioButton absoluteChoice;
579         private JSpinner absoluteSize;
580         private JRadioButton addBoldChoice;
581         private JRadioButton addItalicChoice;
582         private JCheckBox italicCheckBox;
583         private JRadioButton relativeChoice;
584         private JSpinner relativeSize;
585         private JRadioButton removeBoldChoice;
586         private JRadioButton removeItalicChoice;
587         private JCheckBox thicknessCheckBox;
588
589         RelativeFontPanel() {
590             initComponents();
591         }
592
593         void updateFromPropertyValue() {
594             ignoreUpdates = true;
595             boolean changeItalic = (propertyValue.italic != null);
596             italicCheckBox.setSelected(changeItalic);
597             addItalicChoice.setEnabled(changeItalic);
598             removeItalicChoice.setEnabled(changeItalic);
599             if (!changeItalic) {
600                 addItalicChoice.setSelected(true);
601             } else if (Boolean.TRUE.equals(propertyValue.italic)) {
602                 addItalicChoice.setSelected(true);
603             } else if (Boolean.FALSE.equals(propertyValue.italic)) {
604                 removeItalicChoice.setSelected(true);
605             }
606             boolean changeBold = (propertyValue.bold != null);
607             thicknessCheckBox.setSelected(changeBold);
608             addBoldChoice.setEnabled(changeBold);
609             removeBoldChoice.setEnabled(changeBold);
610             if (!changeBold) {
611                 addBoldChoice.setSelected(true);
612             } else if (Boolean.TRUE.equals(propertyValue.bold)) {
613                 addBoldChoice.setSelected(true);
614             } else if (Boolean.FALSE.equals(propertyValue.bold)) {
615                 removeBoldChoice.setSelected(true);
616             }
617             absoluteSize.setEnabled(propertyValue.absoluteSize);
618             relativeSize.setEnabled(!propertyValue.absoluteSize);
619             if (propertyValue.absoluteSize) {
620                 absoluteSize.setValue(new Integer JavaDoc(propertyValue.size));
621                 absoluteChoice.setSelected(true);
622                 synchronizeSizeControls();
623             } else {
624                 relativeSize.setValue(new Integer JavaDoc(propertyValue.size));
625                 relativeChoice.setSelected(true);
626                 synchronizeSizeControls();
627             }
628             ignoreUpdates = false;
629         }
630
631         private void synchronizeSizeControls() {
632             if (propertyValue.absoluteSize) {
633                 Font defaultFont = (Font)property.getDefaultValue();
634                 if (defaultFont != null) {
635                     relativeSize.setValue(new Integer JavaDoc(propertyValue.size - defaultFont.getSize()));
636                 }
637             } else {
638                 Font font = (Font)propertyValue.getDesignValue();
639                 absoluteSize.setValue(new Integer JavaDoc(font == null ? 12 : font.getSize()));
640             }
641         }
642
643         private void initComponents() {
644             relativeSize = new JSpinner(new SpinnerNumberModel(0, Short.MIN_VALUE, Short.MAX_VALUE, 1));
645             relativeSize.setEditor(new JSpinner.NumberEditor(relativeSize, "+#;-#")); // NOI18N
646
absoluteSize = new JSpinner(new SpinnerNumberModel(12, 1, Short.MAX_VALUE, 1));
647
648             ResourceBundle JavaDoc bundle = NbBundle.getBundle(FontEditor.class);
649             JLabel fontSizeLabel = new JLabel(bundle.getString("CTL_FontSize")); // NOI18N
650
JLabel fontStyleLabel = new JLabel(bundle.getString("CTL_FontStyle")); // NOI18N
651

652             absoluteChoice = new JRadioButton();
653             Mnemonics.setLocalizedText(absoluteChoice, bundle.getString("CTL_AbsoluteFontSize")); // NOI18N
654

655             relativeChoice = new JRadioButton();
656             Mnemonics.setLocalizedText(relativeChoice, bundle.getString("CTL_RelativeFontSize")); // NOI18N
657

658             italicCheckBox = new JCheckBox();
659             Mnemonics.setLocalizedText(italicCheckBox, bundle.getString("CTL_ChangeItalic")); // NOI18N
660

661             addItalicChoice = new JRadioButton();
662             Mnemonics.setLocalizedText(addItalicChoice, bundle.getString("CTL_AddItalic")); // NOI18N
663

664             removeItalicChoice = new JRadioButton();
665             Mnemonics.setLocalizedText(removeItalicChoice, bundle.getString("CTL_RemoveItalic")); // NOI18N
666

667             thicknessCheckBox = new JCheckBox();
668             Mnemonics.setLocalizedText(thicknessCheckBox, bundle.getString("CTL_ChangeBold")); // NOI18N
669

670             addBoldChoice = new JRadioButton();
671             Mnemonics.setLocalizedText(addBoldChoice, bundle.getString("CTL_AddBold")); // NOI18N
672

673             removeBoldChoice = new JRadioButton();
674             Mnemonics.setLocalizedText(removeBoldChoice, bundle.getString("CTL_RemoveBold")); // NOI18N
675

676             // Listener
677
Listener JavaDoc listener = new Listener JavaDoc();
678             relativeChoice.addItemListener(listener);
679             thicknessCheckBox.addItemListener(listener);
680             italicCheckBox.addItemListener(listener);
681             relativeSize.addChangeListener(listener);
682             absoluteSize.addChangeListener(listener);
683             addItalicChoice.addItemListener(listener);
684             addBoldChoice.addItemListener(listener);
685
686             // Radio button groups
687
ButtonGroup italicGroup = new ButtonGroup();
688             italicGroup.add(addItalicChoice);
689             italicGroup.add(removeItalicChoice);
690
691             ButtonGroup thicknessGroup = new ButtonGroup();
692             thicknessGroup.add(addBoldChoice);
693             thicknessGroup.add(removeBoldChoice);
694
695             ButtonGroup fontSizeGroup = new ButtonGroup();
696             fontSizeGroup.add(absoluteChoice);
697             fontSizeGroup.add(relativeChoice);
698
699             // Eliminate redundant borders
700
Border JavaDoc emptyBorder = BorderFactory.createEmptyBorder(0, 0, 0, 0);
701             Insets emptyInsets = new Insets(0, 0, 0, 0);
702
703             absoluteChoice.setBorder(emptyBorder);
704             absoluteChoice.setMargin(emptyInsets);
705             relativeChoice.setBorder(emptyBorder);
706             relativeChoice.setMargin(emptyInsets);
707             italicCheckBox.setBorder(emptyBorder);
708             italicCheckBox.setMargin(emptyInsets);
709             addItalicChoice.setBorder(emptyBorder);
710             addItalicChoice.setMargin(emptyInsets);
711             removeItalicChoice.setBorder(emptyBorder);
712             removeItalicChoice.setMargin(emptyInsets);
713             thicknessCheckBox.setBorder(emptyBorder);
714             thicknessCheckBox.setMargin(emptyInsets);
715             addBoldChoice.setBorder(emptyBorder);
716             addBoldChoice.setMargin(emptyInsets);
717             removeBoldChoice.setBorder(emptyBorder);
718             removeBoldChoice.setMargin(emptyInsets);
719
720             GroupLayout layout = new GroupLayout(this);
721             setLayout(layout);
722             layout.setHorizontalGroup(layout.createSequentialGroup()
723                 .addContainerGap()
724                 .add(layout.createParallelGroup()
725                     .add(fontSizeLabel)
726                     .add(layout.createSequentialGroup()
727                         .add(layout.createParallelGroup()
728                             .add(relativeChoice)
729                             .add(absoluteChoice))
730                         .addPreferredGap(LayoutStyle.RELATED)
731                         .add(layout.createParallelGroup()
732                             .add(relativeSize, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
733                             .add(absoluteSize, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE))))
734                 .addPreferredGap(LayoutStyle.UNRELATED)
735                 .add(layout.createParallelGroup()
736                     .add(fontStyleLabel)
737                     .add(layout.createSequentialGroup()
738                         .add(layout.createParallelGroup()
739                             .add(italicCheckBox)
740                             .add(layout.createSequentialGroup()
741                                 .add(17)
742                                 .add(layout.createParallelGroup()
743                                     .add(addItalicChoice)
744                                     .add(removeItalicChoice))))
745                         .addPreferredGap(LayoutStyle.RELATED)
746                         .add(layout.createParallelGroup()
747                             .add(thicknessCheckBox)
748                             .add(layout.createSequentialGroup()
749                                 .add(17)
750                                 .add(layout.createParallelGroup()
751                                     .add(removeBoldChoice)
752                                     .add(addBoldChoice))))))
753                     .addContainerGap());
754             layout.setVerticalGroup(layout.createSequentialGroup()
755                 .addContainerGap()
756                 .add(layout.createParallelGroup(GroupLayout.BASELINE)
757                     .add(fontSizeLabel)
758                     .add(fontStyleLabel))
759                 .addPreferredGap(LayoutStyle.RELATED)
760                 .add(layout.createParallelGroup(GroupLayout.BASELINE)
761                     .add(relativeChoice)
762                     .add(relativeSize, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
763                     .add(italicCheckBox)
764                     .add(thicknessCheckBox))
765                 .addPreferredGap(LayoutStyle.RELATED)
766                 .add(layout.createParallelGroup(GroupLayout.BASELINE)
767                     .add(absoluteChoice)
768                     .add(absoluteSize, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
769                     .add(addItalicChoice)
770                     .add(addBoldChoice))
771                 .addPreferredGap(LayoutStyle.RELATED)
772                 .add(layout.createParallelGroup(GroupLayout.BASELINE)
773                     .add(removeItalicChoice)
774                     .add(removeBoldChoice))
775                 .addContainerGap());
776         }
777
778         private class Listener implements ItemListener, ChangeListener JavaDoc {
779             public void itemStateChanged(ItemEvent e) {
780                 if (ignoreUpdates) return;
781                 ignoreUpdates = true;
782                 Object JavaDoc source = e.getSource();
783                 if (source == relativeChoice) {
784                     boolean relative = relativeChoice.isSelected();
785                     relativeSize.setEnabled(relative);
786                     absoluteSize.setEnabled(!relative);
787                     propertyValue.absoluteSize = !relative;
788                     propertyValue.size = ((Number JavaDoc)((relative ? relativeSize : absoluteSize).getValue())).intValue();
789                 } else if (source == italicCheckBox) {
790                     boolean changeItalic = italicCheckBox.isSelected();
791                     addItalicChoice.setEnabled(changeItalic);
792                     removeItalicChoice.setEnabled(changeItalic);
793                     propertyValue.italic = changeItalic ? Boolean.valueOf(addItalicChoice.isSelected()) : null;
794                 } else if (source == thicknessCheckBox) {
795                     boolean changeBold = thicknessCheckBox.isSelected();
796                     addBoldChoice.setEnabled(changeBold);
797                     removeBoldChoice.setEnabled(changeBold);
798                     propertyValue.bold = changeBold ? Boolean.valueOf(addBoldChoice.isSelected()) : null;
799                 } else if (source == addBoldChoice) {
800                     propertyValue.bold = Boolean.valueOf(addBoldChoice.isSelected());
801                 } else if (source == addItalicChoice) {
802                     propertyValue.italic = Boolean.valueOf(addItalicChoice.isSelected());
803                 }
804                 ignoreUpdates = false;
805                 updateDelegate();
806             }
807
808             public void stateChanged(ChangeEvent JavaDoc e) {
809                 if (ignoreUpdates) return;
810                 ignoreUpdates = true;
811                 Object JavaDoc source = e.getSource();
812                 if (source == relativeSize) {
813                     propertyValue.size = ((Number JavaDoc)relativeSize.getValue()).intValue();
814                     synchronizeSizeControls();
815                 } else if(source == absoluteSize) {
816                     propertyValue.size = ((Number JavaDoc)absoluteSize.getValue()).intValue();
817                     synchronizeSizeControls();
818                 }
819                 ignoreUpdates = false;
820                 updateDelegate();
821             }
822         }
823     }
824     
825 }
Popular Tags