1 package net.suberic.util.gui.propedit; 2 import net.suberic.util.gui.*; 3 import java.awt.Color ; 4 import javax.swing.*; 5 import java.awt.event.*; 6 7 8 20 21 public class ColorSelectorPane extends LabelValuePropertyEditor { 22 23 JLabel label; 24 JButton inputButton; 25 26 int originalRgb = -1; 27 Color currentColor; 28 29 boolean useEnabledBox = false; 30 JCheckBox enabledBox = null; 31 boolean origEnabled = false; 32 33 40 public void configureEditor(String propertyName, String template, String propertyBaseName, PropertyEditorManager newManager) { 41 property=propertyName; 42 manager=newManager; 43 editorTemplate = template; 44 propertyBase=propertyBaseName; 45 46 label = createLabel(); 47 48 inputButton = createInputButton(); 49 50 int defaultValue = inputButton.getBackground().getRGB(); 51 52 originalValue = manager.getProperty(property + ".rgb", Integer.toString(defaultValue)); 53 originalRgb = Integer.parseInt(originalValue); 54 55 setCurrentColor(new Color (originalRgb)); 56 57 inputButton.setPreferredSize(new java.awt.Dimension (150, label.getMinimumSize().height)); 58 59 this.add(label); 60 labelComponent = label; 61 62 JPanel tmpPanel = new JPanel(); 63 SpringLayout layout = new SpringLayout(); 64 tmpPanel.setLayout(layout); 65 66 tmpPanel.add(inputButton); 67 layout.putConstraint(SpringLayout.NORTH, inputButton, 0, SpringLayout.NORTH, tmpPanel); 68 layout.putConstraint(SpringLayout.WEST, inputButton, 0, SpringLayout.WEST, tmpPanel); 69 layout.putConstraint(SpringLayout.SOUTH, tmpPanel, 0, SpringLayout.SOUTH, inputButton); 70 71 useEnabledBox = manager.getProperty(editorTemplate + "._enabledBox", "false").equalsIgnoreCase("true"); 72 if (useEnabledBox) { 73 enabledBox = new JCheckBox(); 74 origEnabled = manager.getProperty(property + "._enabled", "false").equalsIgnoreCase("true"); 75 enabledBox.setSelected(origEnabled); 76 enabledBox.addItemListener(new ItemListener() { 77 public void itemStateChanged(ItemEvent e) { 78 enabledBoxUpdated(enabledBox.isSelected()); 79 } 80 }); 81 tmpPanel.add(enabledBox); 82 layout.putConstraint(SpringLayout.WEST, enabledBox, 5, SpringLayout.EAST, inputButton); 83 layout.putConstraint(SpringLayout.EAST, tmpPanel, 5, SpringLayout.EAST, enabledBox); 84 } else { 85 layout.putConstraint(SpringLayout.EAST, tmpPanel, 5, SpringLayout.EAST, inputButton); 86 } 87 88 tmpPanel.setPreferredSize(new java.awt.Dimension (150, inputButton.getMinimumSize().height)); 89 tmpPanel.setMaximumSize(new java.awt.Dimension (Integer.MAX_VALUE, inputButton.getMinimumSize().height)); 90 91 valueComponent = tmpPanel; 92 this.add(tmpPanel); 93 94 updateEditorEnabled(); 95 96 manager.registerPropertyEditor(property, this); 97 } 98 99 102 public JButton createInputButton() { 103 JButton newButton = new JButton(); 104 105 newButton.addActionListener(new AbstractAction() { 106 public void actionPerformed(ActionEvent e) { 107 selectNewColor(); 108 } 109 }); 110 111 return newButton; 112 } 113 114 118 public void selectNewColor() { 119 Color newColor = JColorChooser.showDialog(this, "title", currentColor); 120 if (newColor != null) { 121 setCurrentColor(newColor); 122 } 123 } 124 125 128 public void setCurrentColor(Color newColor) { 129 String newValue = Integer.toString(newColor.getRGB()); 130 try { 131 if (currentColor != newColor) { 132 firePropertyChangingEvent(newValue); 133 firePropertyChangedEvent(newValue); 134 currentColor = newColor; 135 inputButton.setBackground(currentColor); 136 } 137 } catch (PropertyValueVetoException pvve) { 138 manager.getFactory().showError(this, "Error changing value " + label.getText() + " to " + newColor.toString() + ": " + pvve.getReason()); 139 140 } 141 } 142 143 146 public Color getCurrentColor() { 147 return currentColor; 148 } 149 150 152 153 157 public void setValue() { 158 if (isEditorEnabled() && isChanged()) { 159 manager.setProperty(property + ".rgb", Integer.toString(currentColor.getRGB())); 160 originalValue = Integer.toString(currentColor.getRGB()); 161 if (useEnabledBox) { 162 if (enabledBox.isSelected()) 163 manager.setProperty(property + "._enabled", "true"); 164 else 165 manager.setProperty(property + "._enabled", "false"); 166 167 origEnabled = enabledBox.isSelected(); 168 } 169 } 170 } 171 172 public void validateProperty() throws PropertyValueVetoException { 173 if (isEditorEnabled()) { 174 firePropertyCommittingEvent(Integer.toString(currentColor.getRGB())); 175 } 176 } 177 178 182 public java.util.Properties getValue() { 183 java.util.Properties retProps = new java.util.Properties (); 184 185 retProps.setProperty(property + ".rgb", Integer.toString(currentColor.getRGB())); 186 if (useEnabledBox) { 187 if (enabledBox.isSelected()) 188 retProps.setProperty(property + "._enabled", "true"); 189 else 190 retProps.setProperty(property + "._enabled", "false"); 191 } 192 return retProps; 193 } 194 195 199 public void resetDefaultValue() { 200 setCurrentColor(new Color (originalRgb)); 201 202 if (useEnabledBox) 203 enabledBox.setSelected(origEnabled); 204 } 205 206 209 public boolean isChanged() { 210 if (useEnabledBox) { 211 return (! (enabledBox.isSelected() == origEnabled && originalValue.equals(Integer.toString(currentColor.getRGB())))); 212 } else { 213 return (!(originalValue.equals(Integer.toString(currentColor.getRGB())))); 214 } 215 } 216 217 220 protected void updateEditorEnabled() { 221 if (useEnabledBox) { 222 enabledBox.setEnabled(isEditorEnabled()); 223 if (inputButton != null) { 224 inputButton.setEnabled(isEditorEnabled() && enabledBox.isSelected()); 225 } 226 } else { 227 if (inputButton != null) { 228 inputButton.setEnabled(isEditorEnabled()); 229 } 230 } 231 } 232 233 236 private void enabledBoxUpdated(boolean newValue) { 237 inputButton.setEnabled(newValue); 238 } 239 240 } 241 | Popular Tags |