KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > gui > propedit > FontSelectorPane


1 package net.suberic.util.gui.propedit;
2 import java.io.*;
3 import javax.swing.*;
4 import java.awt.event.*;
5 import net.suberic.util.swing.JFontChooser;
6 import java.awt.Font JavaDoc;
7
8 /**
9  * This displays the currently selected file (if any), along with a
10  * button which will bring up a FontChooser to choose any other file(s).
11  *
12  * If property._enabledBox is set to true, then this also adds a
13  * checkbox to show whether or not to use this property, or just to use
14  * the defaults.
15  *
16  */

17
18 public class FontSelectorPane extends LabelValuePropertyEditor {
19
20   JLabel label;
21   JTextField valueDisplay;
22   JButton inputButton;
23
24   boolean useEnabledBox = false;
25   JCheckBox enabledBox = null;
26   boolean origEnabled = false;
27
28   /**
29    * @param propertyName The property to be edited.
30    * @param template The property that will define the layout of the
31    * editor.
32    * @param manager The PropertyEditorManager that will manage the
33    * changes.
34    */

35   public void configureEditor(String JavaDoc propertyName, String JavaDoc template, String JavaDoc propertyBaseName, PropertyEditorManager newManager) {
36     property=propertyName;
37     manager=newManager;
38     editorTemplate = template;
39     propertyBase=propertyBaseName;
40     originalValue = manager.getProperty(property, "");
41
42     getLogger().fine("property is " + property + "; editorTemplate is " + editorTemplate);
43
44     label = createLabel();
45
46     valueDisplay = new JTextField(originalValue);
47
48     inputButton = createInputButton();
49
50     valueDisplay.setPreferredSize(new java.awt.Dimension JavaDoc(150 - inputButton.getPreferredSize().width, valueDisplay.getMinimumSize().height));
51
52     this.add(label);
53     labelComponent = label;
54     JPanel tmpPanel = new JPanel();
55     SpringLayout layout = new SpringLayout();
56     tmpPanel.setLayout(layout);
57
58     tmpPanel.add(valueDisplay);
59     tmpPanel.add(inputButton);
60
61     layout.putConstraint(SpringLayout.NORTH, valueDisplay, 0, SpringLayout.NORTH, tmpPanel);
62     layout.putConstraint(SpringLayout.WEST, valueDisplay, 0, SpringLayout.WEST, tmpPanel);
63     layout.putConstraint(SpringLayout.SOUTH, tmpPanel, 0, SpringLayout.SOUTH, valueDisplay);
64     layout.putConstraint(SpringLayout.WEST, inputButton, 5, SpringLayout.EAST, valueDisplay);
65     layout.putConstraint(SpringLayout.EAST, tmpPanel, 5, SpringLayout.EAST, inputButton);
66
67     tmpPanel.setPreferredSize(new java.awt.Dimension JavaDoc(150, valueDisplay.getMinimumSize().height));
68     tmpPanel.setMaximumSize(new java.awt.Dimension JavaDoc(Integer.MAX_VALUE, valueDisplay.getMinimumSize().height));
69
70     useEnabledBox = manager.getProperty(editorTemplate + "._enabledBox", "false").equalsIgnoreCase("true");
71     if (useEnabledBox) {
72       enabledBox = new JCheckBox();
73       origEnabled = manager.getProperty(property + "._enabled", "false").equalsIgnoreCase("true");
74       enabledBox.setSelected(origEnabled);
75       enabledBox.addItemListener(new ItemListener() {
76           public void itemStateChanged(ItemEvent e) {
77             enabledBoxUpdated(enabledBox.isSelected());
78           }
79         });
80       enabledBoxUpdated(origEnabled);
81       tmpPanel.add(enabledBox);
82     }
83
84     valueComponent = tmpPanel;
85     //this.add(valueDisplay);
86
//this.add(inputButton);
87
this.add(tmpPanel);
88
89     updateEditorEnabled();
90
91     manager.registerPropertyEditor(property, this);
92   }
93
94   /**
95    * Creates a button that will bring up a way to select a new Font.
96    */

97   public JButton createInputButton() {
98     try {
99       java.net.URL JavaDoc url = this.getClass().getResource(manager.getProperty("FontSelectorPane.inputButton.image", "/net/suberic/util/gui/images/More.gif"));
100       if (url != null) {
101         ImageIcon icon = new ImageIcon(url);
102
103         JButton newButton = new JButton(icon);
104
105         newButton.setPreferredSize(new java.awt.Dimension JavaDoc(icon.getIconHeight(), icon.getIconWidth()));
106         newButton.addActionListener(new AbstractAction() {
107             public void actionPerformed(ActionEvent e) {
108               selectNewFont();
109             }
110           });
111
112         return newButton;
113       }
114     } catch (java.util.MissingResourceException JavaDoc mre) {
115     }
116
117     JButton newButton = new JButton();
118     newButton.addActionListener(new AbstractAction() {
119         public void actionPerformed(ActionEvent e) {
120           selectNewFont();
121         }
122       });
123
124     return newButton;
125   }
126
127   /**
128    * This actually brings up a FontChooser to select a new Font for
129    * the value of the property.
130    */

131   public void selectNewFont() {
132     String JavaDoc fontText = valueDisplay.getText();
133     Font JavaDoc f = null;
134     if (fontText != null && fontText.length() > 0) {
135       f = Font.decode(fontText);
136     }
137
138     String JavaDoc newFontText = JFontChooser.showStringDialog(this,
139                                                        manager.getProperty("FontEditorPane.Select",
140                                                                            "Select"), f);
141
142     if (newFontText != null) {
143       try {
144         firePropertyChangingEvent(newFontText);
145         firePropertyChangedEvent(newFontText);
146         valueDisplay.setText(newFontText);
147       } catch (PropertyValueVetoException pvve) {
148         manager.getFactory().showError(this, "Error changing value " + label.getText() + " to " + newFontText + ": " + pvve.getReason());
149       }
150     }
151
152   }
153
154   // as defined in net.suberic.util.gui.PropertyEditorUI
155

156
157   /**
158    * This writes the currently configured value in the PropertyEditorUI
159    * to the source PropertyEditorManager.
160    */

161   public void setValue() {
162     if (isEditorEnabled() && isChanged()) {
163       //System.err.println("setting value for " + property);
164
manager.setProperty(property, (String JavaDoc)valueDisplay.getText());
165       originalValue = valueDisplay.getText();
166
167       if (useEnabledBox) {
168         if (enabledBox.isSelected())
169           manager.setProperty(property + "._enabled", "true");
170         else
171           manager.setProperty(property + "._enabled", "false");
172
173         origEnabled = enabledBox.isSelected();
174       }
175     }
176   }
177
178   public void validateProperty() throws PropertyValueVetoException {
179     if (isEditorEnabled()) {
180       //System.err.println("setting value for " + property);
181
firePropertyCommittingEvent((String JavaDoc)valueDisplay.getText());
182     }
183   }
184
185   /**
186    * Returns the current values of the edited properties as a
187    * java.util.Properties object.
188    */

189   public java.util.Properties JavaDoc getValue() {
190     java.util.Properties JavaDoc retProps = new java.util.Properties JavaDoc();
191
192     retProps.setProperty(property, (String JavaDoc)valueDisplay.getText());
193     if (useEnabledBox) {
194       if (enabledBox.isSelected())
195         retProps.setProperty(property + "._enabled", "true");
196       else
197         retProps.setProperty(property + "._enabled", "false");
198     }
199     return retProps;
200   }
201
202   /**
203    * This resets the editor to the original (or latest set, if setValue()
204    * has been called) value of the edited property.
205    */

206   public void resetDefaultValue() {
207     valueDisplay.setText(originalValue);
208     if (useEnabledBox)
209       enabledBox.setSelected(origEnabled);
210   }
211
212   /**
213    * Returns whether or not this editor still has its originally configured
214    * value.
215    */

216   public boolean isChanged() {
217     if (useEnabledBox) {
218       return (enabledBox.isSelected() != origEnabled || !(originalValue.equals(valueDisplay.getText())));
219     } else {
220       return (!(originalValue.equals(valueDisplay.getText())));
221     }
222   }
223
224   /**
225    * Run when the PropertyEditor may have changed enabled states.
226    */

227   protected void updateEditorEnabled() {
228     if (useEnabledBox) {
229       enabledBox.setEnabled(isEditorEnabled());
230       //inputButton.setEnabled(newValue && enabledBox.isSelected());
231

232       if (inputButton != null) {
233         inputButton.setEnabled(isEditorEnabled() && enabledBox.isSelected());
234       }
235       if (valueDisplay != null) {
236         valueDisplay.setEnabled(isEditorEnabled() && enabledBox.isSelected());
237       }
238
239     } else {
240       if (inputButton != null) {
241         inputButton.setEnabled(isEditorEnabled());
242       }
243       if (valueDisplay != null) {
244         valueDisplay.setEnabled(isEditorEnabled());
245       }
246     }
247   }
248
249   /**
250    * Called when the enabledBox's value is updated.
251    */

252   private void enabledBoxUpdated(boolean newValue) {
253     if (inputButton != null)
254       inputButton.setEnabled(newValue);
255
256     if (valueDisplay != null)
257       valueDisplay.setEnabled(newValue);
258   }
259
260 }
261
Popular Tags