KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.util.gui.propedit;
2 import javax.swing.*;
3 import net.suberic.util.*;
4 import java.awt.FlowLayout JavaDoc;
5 import java.awt.event.*;
6 import java.util.*;
7
8 /**
9  * An EditorPane which allows a user to select from a list of choices.
10  */

11 public class ListEditorPane extends LabelValuePropertyEditor {
12   protected int originalIndex;
13   protected String JavaDoc 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   // configuration settings
21
static String JavaDoc INCLUDE_ADD_BUTTON = "_includeAddButton";
22   static String JavaDoc ALLOWED_VALUES = "allowedValues";
23   static String JavaDoc LIST_MAPPING = "listMapping";
24   static String JavaDoc INCLUDE_DEFAULT_OPTION = "_includeDefault";
25   static String JavaDoc INCLUDE_NEW_OPTION = "_includeNew";
26   public static String JavaDoc SELECTION_DEFAULT = "__default";
27   public static String JavaDoc SELECTION_NEW = "__new";
28
29   /**
30    * @param propertyName The property to be edited.
31    * @param template The property that will define the layout of the
32    * editor.
33    * @param manager The PropertyEditorManager that will manage the
34    * changes.
35    */

36   public void configureEditor(String JavaDoc propertyName, String JavaDoc template, String JavaDoc 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 JavaDoc(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 we're showing something other than the actually configured
66
// value, fire an event.
67
if (isChanged()) {
68       String JavaDoc newValue = (String JavaDoc)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   /**
80    * Creates the JComboBox with the appropriate options.
81    */

82   protected JComboBox createComboBox() {
83     String JavaDoc originalValue = manager.getProperty(property, "");
84     // we set this to the real original value, rather than the default
85
// value if none is set.
86
mOriginalValue = originalValue;
87     // now we get the default value.
88
if (originalValue.equalsIgnoreCase(""))
89       originalValue = manager.getProperty(editorTemplate, "");
90     String JavaDoc currentItem;
91     originalIndex=-1;
92     Vector items = new Vector();
93     List<String JavaDoc> tokens;
94
95     String JavaDoc allowedValuesString = manager.getProperty(editorTemplate + "." + ALLOWED_VALUES, "");
96     if (manager.getProperty(allowedValuesString, "") != "") {
97       tokens = manager.getPropertyAsList(allowedValuesString, "");
98       manager.addPropertyEditorListener(allowedValuesString, new ListEditorListener());
99       // if we're loading this from a property list, check for options for
100
// default values and adding new values.
101
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 JavaDoc>();
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 JavaDoc itemLabel = manager.getProperty(editorTemplate + "." + LIST_MAPPING + "." + currentItem.toString() + ".label", "");
122       String JavaDoc itemValue = manager.getProperty(editorTemplate + "." + LIST_MAPPING + "." + currentItem.toString() + ".value", "");
123
124       // special cases
125
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         // and set a blank entry as equal to the default option.
133
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         // default case
144
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 there are no valid values and we have a new value option, then
161
// don't add the current value if it's the default option.
162
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 JavaDoc newValue = (String JavaDoc)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   /**
195    * Updates the combo box with the new value(s).
196    */

197   private void updateComboBox(String JavaDoc newValue) {
198     Vector items = new Vector();
199     StringTokenizer tokens = new StringTokenizer(newValue, ":");
200     String JavaDoc currentValue = (String JavaDoc) inputField.getSelectedItem();
201
202     String JavaDoc currentItem;
203     for (int i=0; tokens.hasMoreTokens(); i++) {
204       currentItem = tokens.nextToken();
205
206       String JavaDoc itemLabel = manager.getProperty(editorTemplate + "." + LIST_MAPPING + "." + currentItem.toString() + ".label", "");
207       if (itemLabel.length() < 1)
208         itemLabel = currentItem.toString();
209
210       String JavaDoc 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   /**
231    * Creates a button to add a new value to the List.
232    */

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   /**
245    * Opens up an editor to add a new Item to the List.
246    */

247   public void addNewEntry() {
248     String JavaDoc editedProperty = manager.getProperty(editorTemplate + "." + ALLOWED_VALUES, "");
249     //
250
//manager.getFactory().showNewEditorWindow("Add property", editedProperty, editedProperty, manager);
251
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   // as defined in net.suberic.util.gui.PropertyEditorUI
262

263   /**
264    * This writes the currently configured value in the PropertyEditorUI
265    * to the source VariableBundle.
266    */

267   public void setValue() throws PropertyValueVetoException {
268     int newIndex = inputField.getSelectedIndex();
269     String JavaDoc currentValue = (String JavaDoc)labelToValueMap.get(inputField.getSelectedItem());
270
271     if (isEditorEnabled() && isChanged()) {
272       manager.setProperty(property, currentValue);
273       mOriginalValue = (String JavaDoc)labelToValueMap.get(inputField.getSelectedItem());
274     }
275   }
276
277
278   /**
279    * This checks that the currently configured value is valid.
280    */

281   public void validateProperty() throws PropertyValueVetoException {
282     int newIndex = inputField.getSelectedIndex();
283     String JavaDoc currentValue = (String JavaDoc)labelToValueMap.get(inputField.getSelectedItem());
284     if (newIndex != currentIndex) {
285       firePropertyChangingEvent(currentValue);
286       firePropertyChangedEvent(currentValue);
287       currentIndex = newIndex;
288     }
289     firePropertyCommittingEvent(currentValue);
290   }
291
292   /**
293    * Returns the current values of the edited properties as a
294    * java.util.Properties object.
295    */

296   public java.util.Properties JavaDoc getValue() {
297     java.util.Properties JavaDoc retProps = new java.util.Properties JavaDoc();
298
299     retProps.setProperty(property, (String JavaDoc)labelToValueMap.get(inputField.getSelectedItem()));
300
301     return retProps;
302   }
303
304   /**
305    * This resets the editor to the original (or latest set, if setValue()
306    * has been called) value of the edited property.
307    */

308   public void resetDefaultValue() {
309     // this will be handled by the ItemListener we have on the inputField,
310
// so we don't have to notify listeners here.
311

312     inputField.setSelectedIndex(originalIndex);
313   }
314
315   /**
316    * Returns whether or not the current list selection has changed from
317    * the last save.
318    */

319   public boolean isChanged() {
320     if (mOriginalValue == null)
321       return (mOriginalValue != (String JavaDoc)labelToValueMap.get(inputField.getSelectedItem()));
322     else
323       return (! mOriginalValue.equals((String JavaDoc)labelToValueMap.get(inputField.getSelectedItem())));
324
325   }
326
327   /**
328    * Run when the PropertyEditor may have changed enabled states.
329    */

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   /**
340    * This listens to the property that it currently providing the list
341    * of allowed values for this List. If it changes, then the allowed
342    * values list also is updated.
343    */

344   public class ListEditorListener extends PropertyEditorAdapter {
345
346     /**
347      * Called after a property changes.
348      */

349     public void propertyChanged(PropertyEditorUI ui, String JavaDoc property, String JavaDoc newValue) {
350       updateComboBox(newValue);
351     }
352
353   }
354 }
355
Popular Tags