1 package net.suberic.util.gui.propedit; 2 import javax.swing.*; 3 import net.suberic.util.*; 4 import java.awt.FlowLayout ; 5 import java.awt.event.*; 6 import java.util.*; 7 8 11 public class ListEditorPane extends LabelValuePropertyEditor { 12 protected int originalIndex; 13 protected String mOriginalValue; 14 protected JLabel label; 15 protected JComboBox inputField; 16 JButton addButton; 17 protected HashMap labelToValueMap = new HashMap(); 18 protected int currentIndex = -1; 19 20 static String INCLUDE_ADD_BUTTON = "_includeAddButton"; 22 static String ALLOWED_VALUES = "allowedValues"; 23 static String LIST_MAPPING = "listMapping"; 24 static String INCLUDE_DEFAULT_OPTION = "_includeDefault"; 25 static String INCLUDE_NEW_OPTION = "_includeNew"; 26 public static String SELECTION_DEFAULT = "__default"; 27 public static String SELECTION_NEW = "__new"; 28 29 36 public void configureEditor(String propertyName, String template, String propertyBaseName, PropertyEditorManager newManager) { 37 configureBasic(propertyName, template, propertyBaseName, newManager); 38 39 label = createLabel(); 40 41 inputField = createComboBox(); 42 43 Box inputBox = new Box(BoxLayout.X_AXIS); 44 inputField.setPreferredSize(inputField.getMinimumSize()); 45 inputField.setMaximumSize(inputField.getMinimumSize()); 46 inputBox.add(inputField); 47 inputBox.add(Box.createGlue()); 48 49 if (manager.getProperty(editorTemplate + "." + INCLUDE_ADD_BUTTON, "false").equalsIgnoreCase("true")) { 50 addButton = createAddButton(); 51 inputBox.add(addButton); 52 } 53 54 inputBox.setMaximumSize(new java.awt.Dimension (Integer.MAX_VALUE, inputBox.getMinimumSize().height)); 55 56 this.add(label); 57 this.add(inputBox); 58 updateEditorEnabled(); 59 60 labelComponent = label; 61 valueComponent = inputBox; 62 63 manager.registerPropertyEditor(property, this); 64 65 if (isChanged()) { 68 String newValue = (String )labelToValueMap.get(inputField.getSelectedItem()); 69 try { 70 firePropertyChangingEvent(newValue); 71 firePropertyChangedEvent(newValue); 72 } catch (PropertyValueVetoException pvve) { 73 manager.getFactory().showError(inputField, "Error changing value " + label.getText() + " to " + newValue + ": " + pvve.getReason()); 74 inputField.setSelectedIndex(currentIndex); 75 } 76 } 77 } 78 79 82 protected JComboBox createComboBox() { 83 String originalValue = manager.getProperty(property, ""); 84 mOriginalValue = originalValue; 87 if (originalValue.equalsIgnoreCase("")) 89 originalValue = manager.getProperty(editorTemplate, ""); 90 String currentItem; 91 originalIndex=-1; 92 Vector items = new Vector(); 93 List<String > tokens; 94 95 String allowedValuesString = manager.getProperty(editorTemplate + "." + ALLOWED_VALUES, ""); 96 if (manager.getProperty(allowedValuesString, "") != "") { 97 tokens = manager.getPropertyAsList(allowedValuesString, ""); 98 manager.addPropertyEditorListener(allowedValuesString, new ListEditorListener()); 99 if (manager.getProperty(editorTemplate + "." + INCLUDE_NEW_OPTION, "false").equalsIgnoreCase("true")) { 102 tokens.add(0, SELECTION_NEW); 103 } 104 if (tokens != null && tokens.size() > 0) { 105 if (manager.getProperty(editorTemplate + "." + INCLUDE_DEFAULT_OPTION, "false").equalsIgnoreCase("true")) { 106 tokens.add(0, SELECTION_DEFAULT); 107 } 108 } 109 } else { 110 if (manager.getProperty(editorTemplate + "." + INCLUDE_NEW_OPTION, "false").equalsIgnoreCase("true")) { 111 tokens = new ArrayList<String >(); 112 tokens.add(0, SELECTION_NEW); 113 } else { 114 tokens = manager.getPropertyAsList(editorTemplate + "." + ALLOWED_VALUES, ""); 115 } 116 } 117 118 for (int i = 0; i < tokens.size(); i++) { 119 currentItem = tokens.get(i); 120 121 String itemLabel = manager.getProperty(editorTemplate + "." + LIST_MAPPING + "." + currentItem.toString() + ".label", ""); 122 String itemValue = manager.getProperty(editorTemplate + "." + LIST_MAPPING + "." + currentItem.toString() + ".value", ""); 123 124 if (currentItem.equals(SELECTION_DEFAULT)) { 126 if (itemLabel.length() < 1) 127 itemLabel = manager.getProperty("ListEditorPane.button.default", "< Default Value >"); 128 129 if (itemValue.length() < 1) 130 itemValue = currentItem; 131 132 if (originalValue.length() < 1) { 134 originalValue = itemValue; 135 } 136 } else if (currentItem.equals(SELECTION_NEW)) { 137 if (itemLabel.length() < 1) 138 itemLabel = manager.getProperty("ListEditorPane.button.new", "< Create New Value >"); 139 140 if (itemValue.length() < 1) 141 itemValue = currentItem; 142 } else { 143 if (itemLabel.length() < 1) 145 itemLabel = currentItem; 146 147 if (itemValue.length() < 1) 148 itemValue = currentItem; 149 150 } 151 if (itemValue.equals(originalValue)) { 152 originalIndex=i; 153 currentIndex=i; 154 } 155 items.add(itemLabel); 156 labelToValueMap.put(itemLabel, itemValue); 157 } 158 159 if (originalIndex == -1) { 160 if (! (manager.getProperty(editorTemplate + "." + INCLUDE_NEW_OPTION, "false").equalsIgnoreCase("true") && SELECTION_DEFAULT.equalsIgnoreCase(originalValue))) { 163 items.add(originalValue); 164 labelToValueMap.put(originalValue, originalValue); 165 originalIndex = items.size() - 1; 166 } else { 167 originalIndex = 0; 168 } 169 } 170 171 JComboBox jcb = new JComboBox(items); 172 jcb.setSelectedIndex(originalIndex); 173 174 jcb.addItemListener(new ItemListener() { 175 public void itemStateChanged(ItemEvent e) { 176 int newIndex = inputField.getSelectedIndex(); 177 if (newIndex != currentIndex) { 178 String newValue = (String )labelToValueMap.get(inputField.getSelectedItem()); 179 try { 180 firePropertyChangingEvent(newValue); 181 firePropertyChangedEvent(newValue); 182 currentIndex = newIndex; 183 } catch (PropertyValueVetoException pvve) { 184 manager.getFactory().showError(inputField, "Error changing value " + label.getText() + " to " + newValue + ": " + pvve.getReason()); 185 inputField.setSelectedIndex(currentIndex); 186 } 187 } 188 } 189 }); 190 191 return jcb; 192 } 193 194 197 private void updateComboBox(String newValue) { 198 Vector items = new Vector(); 199 StringTokenizer tokens = new StringTokenizer(newValue, ":"); 200 String currentValue = (String ) inputField.getSelectedItem(); 201 202 String currentItem; 203 for (int i=0; tokens.hasMoreTokens(); i++) { 204 currentItem = tokens.nextToken(); 205 206 String itemLabel = manager.getProperty(editorTemplate + "." + LIST_MAPPING + "." + currentItem.toString() + ".label", ""); 207 if (itemLabel.length() < 1) 208 itemLabel = currentItem.toString(); 209 210 String itemValue = manager.getProperty(editorTemplate + "." + LIST_MAPPING + "." + currentItem.toString() + ".value", ""); 211 if (itemValue.length() < 1) 212 itemValue = currentItem.toString(); 213 214 if (itemValue.equals(originalValue)) { 215 originalIndex=i; 216 } 217 if (itemValue.equals(currentValue)) { 218 currentIndex=i; 219 } 220 items.add(itemLabel); 221 labelToValueMap.put(itemLabel, itemValue); 222 } 223 224 ComboBoxModel newModel = new DefaultComboBoxModel(items); 225 newModel.setSelectedItem(currentValue); 226 inputField.setModel(newModel); 227 228 } 229 230 233 public JButton createAddButton() { 234 JButton returnValue = new JButton(manager.getProperty("button.add", "Add")); 235 returnValue.addActionListener(new AbstractAction() { 236 public void actionPerformed(ActionEvent e) { 237 addNewEntry(); 238 } 239 }); 240 241 return returnValue; 242 } 243 244 247 public void addNewEntry() { 248 String editedProperty = manager.getProperty(editorTemplate + "." + ALLOWED_VALUES, ""); 249 PropertyEditorUI sourceEditor = manager.getPropertyEditor(editedProperty); 252 if (sourceEditor == null) { 253 sourceEditor = manager.getFactory().createEditor(editedProperty, editedProperty, manager); 254 } 255 if (sourceEditor instanceof MultiEditorPane) { 256 MultiEditorPane multiEditor = (MultiEditorPane) sourceEditor; 257 multiEditor.addNewValue(multiEditor.getNewValueName(), this.getPropertyEditorPane().getContainer()); 258 } 259 } 260 261 263 267 public void setValue() throws PropertyValueVetoException { 268 int newIndex = inputField.getSelectedIndex(); 269 String currentValue = (String )labelToValueMap.get(inputField.getSelectedItem()); 270 271 if (isEditorEnabled() && isChanged()) { 272 manager.setProperty(property, currentValue); 273 mOriginalValue = (String )labelToValueMap.get(inputField.getSelectedItem()); 274 } 275 } 276 277 278 281 public void validateProperty() throws PropertyValueVetoException { 282 int newIndex = inputField.getSelectedIndex(); 283 String currentValue = (String )labelToValueMap.get(inputField.getSelectedItem()); 284 if (newIndex != currentIndex) { 285 firePropertyChangingEvent(currentValue); 286 firePropertyChangedEvent(currentValue); 287 currentIndex = newIndex; 288 } 289 firePropertyCommittingEvent(currentValue); 290 } 291 292 296 public java.util.Properties getValue() { 297 java.util.Properties retProps = new java.util.Properties (); 298 299 retProps.setProperty(property, (String )labelToValueMap.get(inputField.getSelectedItem())); 300 301 return retProps; 302 } 303 304 308 public void resetDefaultValue() { 309 312 inputField.setSelectedIndex(originalIndex); 313 } 314 315 319 public boolean isChanged() { 320 if (mOriginalValue == null) 321 return (mOriginalValue != (String )labelToValueMap.get(inputField.getSelectedItem())); 322 else 323 return (! mOriginalValue.equals((String )labelToValueMap.get(inputField.getSelectedItem()))); 324 325 } 326 327 330 protected void updateEditorEnabled() { 331 if (inputField != null) { 332 inputField.setEnabled(isEditorEnabled()); 333 } 334 if (addButton != null) { 335 addButton.setEnabled(isEditorEnabled()); 336 } 337 } 338 339 344 public class ListEditorListener extends PropertyEditorAdapter { 345 346 349 public void propertyChanged(PropertyEditorUI ui, String property, String newValue) { 350 updateComboBox(newValue); 351 } 352 353 } 354 } 355 | Popular Tags |