KickJava   Java API By Example, From Geeks To Geeks.

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


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 choice of three:
10  * true, false, or default.
11  */

12 public class DefaultBooleanEditorPane extends ListEditorPane {
13
14   /**
15    * Creates the JComboBox with the appropriate options.
16    */

17   protected JComboBox createComboBox() {
18     String JavaDoc originalValue = manager.getProperty(property, "");
19     originalIndex=-1;
20     currentIndex = -1;
21     Vector items = new Vector();
22     
23     HashMap valueMap = new HashMap();
24     valueMap.put("False", "False");
25     valueMap.put("True", "True");
26     valueMap.put("Default", "");
27     
28     Set keys = valueMap.keySet();
29     Iterator keyIter = keys.iterator();
30     for (int i = 0; keyIter.hasNext(); i++) {
31       String JavaDoc currentLabel = (String JavaDoc) keyIter.next();
32       String JavaDoc currentValue = (String JavaDoc) valueMap.get(currentLabel);
33       if (currentValue.equals(originalValue)) {
34     originalIndex=i;
35     currentIndex=i;
36       }
37       items.add(currentLabel);
38     }
39     
40     if (originalIndex == -1) {
41       items.add(originalValue);
42       labelToValueMap.put(originalValue, originalValue);
43       originalIndex = items.size() - 1;
44     }
45       
46     JComboBox jcb = new JComboBox(items);
47     jcb.setSelectedIndex(originalIndex);
48
49     labelToValueMap = valueMap;
50
51     jcb.addItemListener(new ItemListener() {
52     public void itemStateChanged(ItemEvent e) {
53       int newIndex = inputField.getSelectedIndex();
54       if (newIndex != currentIndex) {
55         String JavaDoc newValue = (String JavaDoc)labelToValueMap.get(inputField.getSelectedItem());
56         try {
57           firePropertyChangingEvent(newValue);
58           firePropertyChangedEvent(newValue);
59           currentIndex = newIndex;
60         } catch (PropertyValueVetoException pvve) {
61           manager.getFactory().showError(inputField, "Error changing value " + label.getText() + " to " + newValue + ": " + pvve.getReason());
62           inputField.setSelectedIndex(currentIndex);
63         }
64       }
65     }
66       });
67
68     return jcb;
69   }
70   
71 }
72
Popular Tags