01: package net.suberic.util.gui.propedit;
02:
03: import javax.swing.*;
04: import net.suberic.util.*;
05: import java.awt.FlowLayout;
06: import java.awt.event.*;
07: import java.util.*;
08:
09: /**
10: * An EditorPane which allows a user to select from a choice of three:
11: * true, false, or default.
12: */
13: public class DefaultBooleanEditorPane extends ListEditorPane {
14:
15: /**
16: * Creates the JComboBox with the appropriate options.
17: */
18: protected JComboBox createComboBox() {
19: String originalValue = manager.getProperty(property, "");
20: originalIndex = -1;
21: currentIndex = -1;
22: Vector items = new Vector();
23:
24: HashMap valueMap = new HashMap();
25: valueMap.put("False", "False");
26: valueMap.put("True", "True");
27: valueMap.put("Default", "");
28:
29: Set keys = valueMap.keySet();
30: Iterator keyIter = keys.iterator();
31: for (int i = 0; keyIter.hasNext(); i++) {
32: String currentLabel = (String) keyIter.next();
33: String currentValue = (String) valueMap.get(currentLabel);
34: if (currentValue.equals(originalValue)) {
35: originalIndex = i;
36: currentIndex = i;
37: }
38: items.add(currentLabel);
39: }
40:
41: if (originalIndex == -1) {
42: items.add(originalValue);
43: labelToValueMap.put(originalValue, originalValue);
44: originalIndex = items.size() - 1;
45: }
46:
47: JComboBox jcb = new JComboBox(items);
48: jcb.setSelectedIndex(originalIndex);
49:
50: labelToValueMap = valueMap;
51:
52: jcb.addItemListener(new ItemListener() {
53: public void itemStateChanged(ItemEvent e) {
54: int newIndex = inputField.getSelectedIndex();
55: if (newIndex != currentIndex) {
56: String newValue = (String) labelToValueMap
57: .get(inputField.getSelectedItem());
58: try {
59: firePropertyChangingEvent(newValue);
60: firePropertyChangedEvent(newValue);
61: currentIndex = newIndex;
62: } catch (PropertyValueVetoException pvve) {
63: manager.getFactory().showError(
64: inputField,
65: "Error changing value "
66: + label.getText() + " to "
67: + newValue + ": "
68: + pvve.getReason());
69: inputField.setSelectedIndex(currentIndex);
70: }
71: }
72: }
73: });
74:
75: return jcb;
76: }
77:
78: }
|