001: package net.suberic.util.gui.propedit;
002:
003: import java.io.*;
004: import javax.swing.*;
005: import java.awt.event.*;
006: import net.suberic.util.swing.JFontChooser;
007: import java.awt.Font;
008:
009: /**
010: * This displays the currently selected file (if any), along with a
011: * button which will bring up a FontChooser to choose any other file(s).
012: *
013: * If property._enabledBox is set to true, then this also adds a
014: * checkbox to show whether or not to use this property, or just to use
015: * the defaults.
016: *
017: */
018:
019: public class FontSelectorPane extends SwingPropertyEditor {
020:
021: JLabel label;
022: JTextField valueDisplay;
023: JButton inputButton;
024:
025: boolean useEnabledBox = false;
026: JCheckBox enabledBox = null;
027: boolean origEnabled = false;
028:
029: /**
030: * @param propertyName The property to be edited.
031: * @param template The property that will define the layout of the
032: * editor.
033: * @param manager The PropertyEditorManager that will manage the
034: * changes.
035: * @param isEnabled Whether or not this editor is enabled by default.
036: */
037: public void configureEditor(String propertyName, String template,
038: PropertyEditorManager newManager, boolean isEnabled) {
039: property = propertyName;
040: manager = newManager;
041: editorTemplate = template;
042: originalValue = manager.getProperty(property, "");
043:
044: if (debug) {
045: System.out.println("property is " + property
046: + "; editorTemplate is " + editorTemplate);
047: }
048:
049: label = createLabel();
050:
051: valueDisplay = new JTextField(originalValue);
052:
053: inputButton = createInputButton();
054:
055: valueDisplay.setPreferredSize(new java.awt.Dimension(
056: 150 - inputButton.getPreferredSize().width,
057: valueDisplay.getMinimumSize().height));
058:
059: this .add(label);
060: labelComponent = label;
061: JPanel tmpPanel = new JPanel(new java.awt.FlowLayout(
062: java.awt.FlowLayout.LEFT, 0, 0));
063: tmpPanel.add(valueDisplay);
064: tmpPanel.add(inputButton);
065: tmpPanel.setPreferredSize(new java.awt.Dimension(150,
066: valueDisplay.getMinimumSize().height));
067:
068: useEnabledBox = manager.getProperty(
069: editorTemplate + "._enabledBox", "false")
070: .equalsIgnoreCase("true");
071: if (useEnabledBox) {
072: enabledBox = new JCheckBox();
073: origEnabled = manager.getProperty(property + "._enabled",
074: "false").equalsIgnoreCase("true");
075: enabledBox.setSelected(origEnabled);
076: enabledBox.addItemListener(new ItemListener() {
077: public void itemStateChanged(ItemEvent e) {
078: enabledBoxUpdated(enabledBox.isSelected());
079: }
080: });
081: enabledBoxUpdated(origEnabled);
082: tmpPanel.add(enabledBox);
083: }
084:
085: valueComponent = tmpPanel;
086: //this.add(valueDisplay);
087: //this.add(inputButton);
088: this .add(tmpPanel);
089:
090: this .setEnabled(isEnabled);
091:
092: manager.registerPropertyEditor(property, this );
093: }
094:
095: /**
096: * Creates a button that will bring up a way to select a new Font.
097: */
098: public JButton createInputButton() {
099: try {
100: java.net.URL url = this .getClass().getResource(
101: manager.getProperty(
102: "FontSelectorPane.inputButton.image",
103: "/net/suberic/util/gui/images/More.gif"));
104: if (url != null) {
105: ImageIcon icon = new ImageIcon(url);
106:
107: JButton newButton = new JButton(icon);
108:
109: newButton.setPreferredSize(new java.awt.Dimension(icon
110: .getIconHeight(), icon.getIconWidth()));
111: newButton.addActionListener(new AbstractAction() {
112: public void actionPerformed(ActionEvent e) {
113: selectNewFont();
114: }
115: });
116:
117: return newButton;
118: }
119: } catch (java.util.MissingResourceException mre) {
120: }
121:
122: JButton newButton = new JButton();
123: newButton.addActionListener(new AbstractAction() {
124: public void actionPerformed(ActionEvent e) {
125: selectNewFont();
126: }
127: });
128:
129: return newButton;
130: }
131:
132: /**
133: * This actually brings up a FontChooser to select a new Font for
134: * the value of the property.
135: */
136: public void selectNewFont() {
137: String fontText = valueDisplay.getText();
138: Font f = null;
139: if (fontText != null && fontText.length() > 0) {
140: f = Font.decode(fontText);
141: }
142:
143: String newFontText = JFontChooser.showStringDialog(this ,
144: manager.getProperty("FontEditorPane.Select", "Select"),
145: f);
146:
147: if (newFontText != null) {
148: try {
149: firePropertyChangingEvent(newFontText);
150: firePropertyChangedEvent(newFontText);
151: valueDisplay.setText(newFontText);
152: } catch (PropertyValueVetoException pvve) {
153: manager.getFactory().showError(
154: this ,
155: "Error changing value " + label.getText()
156: + " to " + newFontText + ": "
157: + pvve.getReason());
158: }
159: }
160:
161: }
162:
163: // as defined in net.suberic.util.gui.PropertyEditorUI
164:
165: /**
166: * This writes the currently configured value in the PropertyEditorUI
167: * to the source PropertyEditorManager.
168: */
169: public void setValue() {
170: if (isEnabled() && isChanged()) {
171: //System.err.println("setting value for " + property);
172: manager.setProperty(property, (String) valueDisplay
173: .getText());
174:
175: if (useEnabledBox) {
176: if (enabledBox.isSelected())
177: manager.setProperty(property + "._enabled", "true");
178: else
179: manager
180: .setProperty(property + "._enabled",
181: "false");
182: }
183: }
184: }
185:
186: /**
187: * Returns the current values of the edited properties as a
188: * java.util.Properties object.
189: */
190: public java.util.Properties getValue() {
191: java.util.Properties retProps = new java.util.Properties();
192:
193: retProps.setProperty(property, (String) valueDisplay.getText());
194: if (useEnabledBox) {
195: if (enabledBox.isSelected())
196: retProps.setProperty(property + "._enabled", "true");
197: else
198: retProps.setProperty(property + "._enabled", "false");
199: }
200: return retProps;
201: }
202:
203: /**
204: * This resets the editor to the original (or latest set, if setValue()
205: * has been called) value of the edited property.
206: */
207: public void resetDefaultValue() {
208: valueDisplay.setText(originalValue);
209: if (useEnabledBox)
210: enabledBox.setSelected(origEnabled);
211: }
212:
213: /**
214: * Returns whether or not this editor still has its originally configured
215: * value.
216: */
217: public boolean isChanged() {
218: if (useEnabledBox) {
219: return (enabledBox.isSelected() != origEnabled || !(originalValue
220: .equals(valueDisplay.getText())));
221: } else {
222: return (!(originalValue.equals(valueDisplay.getText())));
223: }
224: }
225:
226: /**
227: * Sets the enabled property of the PropertyEditorUI. Disabled
228: * editors should not be able to do setValue() calls.
229: */
230: public void setEnabled(boolean newValue) {
231: if (useEnabledBox) {
232: enabledBox.setEnabled(newValue);
233: //inputButton.setEnabled(newValue && enabledBox.isSelected());
234:
235: if (inputButton != null) {
236: inputButton.setEnabled(newValue
237: && enabledBox.isSelected());
238: }
239: if (valueDisplay != null) {
240: valueDisplay.setEnabled(newValue
241: && enabledBox.isSelected());
242: }
243:
244: } else {
245: if (inputButton != null) {
246: inputButton.setEnabled(newValue);
247: }
248: if (valueDisplay != null) {
249: valueDisplay.setEnabled(newValue);
250: }
251: }
252: enabled = newValue;
253: }
254:
255: /**
256: * Called when the enabledBox's value is updated.
257: */
258: private void enabledBoxUpdated(boolean newValue) {
259: if (inputButton != null)
260: inputButton.setEnabled(newValue);
261:
262: if (valueDisplay != null)
263: valueDisplay.setEnabled(newValue);
264: }
265:
266: }
|