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 ; 7 8 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 35 public void configureEditor(String propertyName, String template, String 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 (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 (150, valueDisplay.getMinimumSize().height)); 68 tmpPanel.setMaximumSize(new java.awt.Dimension (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(tmpPanel); 88 89 updateEditorEnabled(); 90 91 manager.registerPropertyEditor(property, this); 92 } 93 94 97 public JButton createInputButton() { 98 try { 99 java.net.URL 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 (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 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 131 public void selectNewFont() { 132 String fontText = valueDisplay.getText(); 133 Font f = null; 134 if (fontText != null && fontText.length() > 0) { 135 f = Font.decode(fontText); 136 } 137 138 String 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 156 157 161 public void setValue() { 162 if (isEditorEnabled() && isChanged()) { 163 manager.setProperty(property, (String )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 firePropertyCommittingEvent((String )valueDisplay.getText()); 182 } 183 } 184 185 189 public java.util.Properties getValue() { 190 java.util.Properties retProps = new java.util.Properties (); 191 192 retProps.setProperty(property, (String )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 206 public void resetDefaultValue() { 207 valueDisplay.setText(originalValue); 208 if (useEnabledBox) 209 enabledBox.setSelected(origEnabled); 210 } 211 212 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 227 protected void updateEditorEnabled() { 228 if (useEnabledBox) { 229 enabledBox.setEnabled(isEditorEnabled()); 230 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 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 |