KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > gui > ConfigurableComboBox


1 package net.suberic.util.gui;
2 import javax.swing.*;
3 import net.suberic.util.VariableBundle;
4 import java.util.HashMap JavaDoc;
5 import java.util.Hashtable JavaDoc;
6 import java.util.StringTokenizer JavaDoc;
7 import java.util.MissingResourceException JavaDoc;
8 import javax.swing.Action JavaDoc;
9 import java.awt.event.*;
10
11 /**
12  * This is a JComboBox which implements the ConfigurableUI interface, and
13  * therefore may be dynamically created using a VariableBundle and key,
14  * and updated using an array of Actions.
15  */

16
17 public class ConfigurableComboBox extends JComboBox implements ConfigurableUI {
18
19   // the latest commands list. i'm storing this for now because i
20
// can't do a JButton.removeActionListeners().
21

22   protected HashMap JavaDoc selectionMap = new HashMap JavaDoc();
23
24   protected Hashtable JavaDoc commands = new Hashtable JavaDoc();
25
26   String JavaDoc mKey = null;
27
28   int minWidth = -1;
29   int minHeight = -1;
30
31   public ConfigurableComboBox() {
32     super();
33   }
34   
35   /**
36    * This creates a new ConfigurableComboBox using the buttonID as the
37    * configuration key, and vars as the source for the values of all the
38    * properties.
39    *
40    * If buttonID doesn't exist in vars, then this returns an empty
41    * ComboBox.
42    */

43   public ConfigurableComboBox(String JavaDoc buttonID, VariableBundle vars) {
44     super();
45     
46     configureComponent(buttonID, vars);
47   }
48   
49   /**
50    * This configures the ComboBox using the given buttonID and
51    * VariableBundle.
52    *
53    * As defined in interface net.suberic.util.gui.ConfigurableUI.
54    */

55   public void configureComponent(String JavaDoc key, VariableBundle vars) {
56     this.setRenderer(new ConfigurableComboRenderer());
57
58     mKey = key;
59
60     StringTokenizer JavaDoc iKeys = null;
61     try {
62       iKeys = new StringTokenizer JavaDoc(vars.getProperty(key), ":");
63     } catch (MissingResourceException JavaDoc mre) {
64       mre.printStackTrace();
65       try {
66     System.err.println(vars.getProperty("error.NoSuchResource") + " " + mre.getKey());
67       } catch (MissingResourceException JavaDoc mretwo) {
68     System.err.println("Unable to load resource " + mre.getKey());
69     return;
70       }
71       return;
72     }
73     String JavaDoc currentToken;
74
75     while (iKeys.hasMoreTokens()) {
76       currentToken=iKeys.nextToken();
77       Object JavaDoc i = createComboBoxItem(key + "." + currentToken, vars);
78       this.addItem(i);
79     }
80
81     this.addItemListener(new ItemListener() {
82     public void itemStateChanged(ItemEvent e) {
83       if (e.getStateChange() == ItemEvent.SELECTED) {
84         Object JavaDoc selectedItem = e.getItem();
85         String JavaDoc cmd = (String JavaDoc)selectionMap.get(selectedItem);
86         if (cmd != null) {
87           Action JavaDoc action = getAction(cmd);
88           if (action != null) {
89         action.actionPerformed(new ActionEvent(e.getSource(), e.getID(), cmd));
90           }
91         }
92       }
93     }
94       });
95
96     this.setMaximumSize(this.getPreferredSize());
97
98     String JavaDoc toolTip = vars.getProperty(key + ".ToolTip", "");
99     if (toolTip != "") {
100       setToolTipText(toolTip);
101     }
102   }
103
104
105   /**
106    * And this actually creates the menu items themselves.
107    */

108   protected Object JavaDoc createComboBoxItem(String JavaDoc buttonID, VariableBundle vars) {
109
110     ImageIcon returnValue = null;
111
112     IconManager iconManager = IconManager.getIconManager(vars, "IconManager._default");
113     ImageIcon icon = iconManager.getIcon(vars.getProperty(buttonID + ".Image"));
114     if (icon != null) {
115
116       if (minWidth < 0) {
117     minWidth = icon.getIconWidth();
118       } else {
119     minWidth = java.lang.Math.min(minWidth, icon.getIconWidth());
120       }
121       
122       if (minHeight < 0) {
123     minHeight = icon.getIconHeight();
124       } else {
125     minHeight = java.lang.Math.min(minHeight, icon.getIconHeight());
126       }
127       
128       
129       //returnValue.setIcon(icon);
130
returnValue = icon;
131     }
132     
133     String JavaDoc cmd = vars.getProperty(buttonID + ".Action", buttonID);
134     
135     selectionMap.put(returnValue, cmd);
136     
137     return returnValue;
138   }
139   
140   /**
141    * As defined in net.suberic.util.gui.ConfigurableUI
142    */

143   public void setActive(javax.swing.Action JavaDoc[] newActions) {
144     Hashtable JavaDoc tmpHash = new Hashtable JavaDoc();
145     if (newActions != null && newActions.length > 0) {
146       for (int i = 0; i < newActions.length; i++) {
147     String JavaDoc cmdName = (String JavaDoc)newActions[i].getValue(Action.NAME);
148     tmpHash.put(cmdName, newActions[i]);
149       }
150     }
151     setActive(tmpHash);
152   }
153   
154   /**
155    * As defined in net.suberic.util.gui.ConfigurableUI
156    */

157   public void setActive(Hashtable JavaDoc newCommands) {
158     commands = newCommands;
159   }
160   
161   /**
162    * This gets an action from the supported commands. If there is no
163    * supported action, it returns null
164    */

165   
166   public Action JavaDoc getAction(String JavaDoc command) {
167     return (Action JavaDoc)commands.get(command);
168   }
169
170   class ConfigurableComboRenderer extends JLabel implements ListCellRenderer {
171
172     public ConfigurableComboRenderer() {
173       setOpaque(true);
174       setHorizontalAlignment(CENTER);
175       setVerticalAlignment(CENTER);
176     }
177     
178     public java.awt.Component JavaDoc getListCellRendererComponent(
179     JList list,
180         Object JavaDoc value,
181         int index,
182         boolean isSelected,
183         boolean cellHasFocus)
184     {
185       if (isSelected) {
186     setBackground(list.getSelectionBackground());
187     setForeground(list.getSelectionForeground());
188       } else {
189     setBackground(list.getBackground());
190     setForeground(list.getForeground());
191       }
192       
193       ImageIcon icon = (ImageIcon)value;
194       //setText(icon.getDescription());
195
setIcon(icon);
196       return this;
197     }
198   }
199 }
200
Popular Tags