001: package net.suberic.util.gui;
002:
003: import javax.swing.*;
004: import net.suberic.util.VariableBundle;
005: import java.util.HashMap;
006: import java.util.Hashtable;
007: import java.util.StringTokenizer;
008: import java.util.MissingResourceException;
009: import javax.swing.Action;
010: import java.awt.event.*;
011:
012: /**
013: * This is a JComboBox which implements the ConfigurableUI interface, and
014: * therefore may be dynamically created using a VariableBundle and key,
015: * and updated using an array of Actions.
016: */
017:
018: public class ConfigurableComboBox extends JComboBox implements
019: ConfigurableUI {
020:
021: // the latest commands list. i'm storing this for now because i
022: // can't do a JButton.removeActionListeners().
023:
024: protected HashMap selectionMap = new HashMap();
025:
026: protected Hashtable commands = new Hashtable();
027:
028: String mKey = null;
029:
030: int minWidth = -1;
031: int minHeight = -1;
032:
033: public ConfigurableComboBox() {
034: super ();
035: }
036:
037: /**
038: * This creates a new ConfigurableComboBox using the buttonID as the
039: * configuration key, and vars as the source for the values of all the
040: * properties.
041: *
042: * If buttonID doesn't exist in vars, then this returns an empty
043: * ComboBox.
044: */
045: public ConfigurableComboBox(String buttonID, VariableBundle vars) {
046: super ();
047:
048: configureComponent(buttonID, vars);
049: }
050:
051: /**
052: * This configures the ComboBox using the given buttonID and
053: * VariableBundle.
054: *
055: * As defined in interface net.suberic.util.gui.ConfigurableUI.
056: */
057: public void configureComponent(String key, VariableBundle vars) {
058: this .setRenderer(new ConfigurableComboRenderer());
059:
060: mKey = key;
061:
062: StringTokenizer iKeys = null;
063: try {
064: iKeys = new StringTokenizer(vars.getProperty(key), ":");
065: } catch (MissingResourceException mre) {
066: mre.printStackTrace();
067: try {
068: System.err.println(vars
069: .getProperty("error.NoSuchResource")
070: + " " + mre.getKey());
071: } catch (MissingResourceException mretwo) {
072: System.err.println("Unable to load resource "
073: + mre.getKey());
074: return;
075: }
076: return;
077: }
078: String currentToken;
079:
080: while (iKeys.hasMoreTokens()) {
081: currentToken = iKeys.nextToken();
082: Object i = createComboBoxItem(key + "." + currentToken,
083: vars);
084: this .addItem(i);
085: }
086:
087: this .addItemListener(new ItemListener() {
088: public void itemStateChanged(ItemEvent e) {
089: if (e.getStateChange() == ItemEvent.SELECTED) {
090: Object selectedItem = e.getItem();
091: String cmd = (String) selectionMap
092: .get(selectedItem);
093: if (cmd != null) {
094: Action action = getAction(cmd);
095: if (action != null) {
096: action.actionPerformed(new ActionEvent(e
097: .getSource(), e.getID(), cmd));
098: }
099: }
100: }
101: }
102: });
103:
104: this .setMaximumSize(this .getPreferredSize());
105:
106: String toolTip = vars.getProperty(key + ".ToolTip", "");
107: if (toolTip != "") {
108: setToolTipText(toolTip);
109: }
110: }
111:
112: /**
113: * And this actually creates the menu items themselves.
114: */
115: protected Object createComboBoxItem(String buttonID,
116: VariableBundle vars) {
117:
118: ImageIcon returnValue = null;
119:
120: IconManager iconManager = IconManager.getIconManager(vars,
121: "IconManager._default");
122: ImageIcon icon = iconManager.getIcon(vars.getProperty(buttonID
123: + ".Image"));
124: if (icon != null) {
125:
126: if (minWidth < 0) {
127: minWidth = icon.getIconWidth();
128: } else {
129: minWidth = java.lang.Math.min(minWidth, icon
130: .getIconWidth());
131: }
132:
133: if (minHeight < 0) {
134: minHeight = icon.getIconHeight();
135: } else {
136: minHeight = java.lang.Math.min(minHeight, icon
137: .getIconHeight());
138: }
139:
140: //returnValue.setIcon(icon);
141: returnValue = icon;
142: }
143:
144: String cmd = vars.getProperty(buttonID + ".Action", buttonID);
145:
146: selectionMap.put(returnValue, cmd);
147:
148: return returnValue;
149: }
150:
151: /**
152: * As defined in net.suberic.util.gui.ConfigurableUI
153: */
154: public void setActive(javax.swing.Action[] newActions) {
155: Hashtable tmpHash = new Hashtable();
156: if (newActions != null && newActions.length > 0) {
157: for (int i = 0; i < newActions.length; i++) {
158: String cmdName = (String) newActions[i]
159: .getValue(Action.NAME);
160: tmpHash.put(cmdName, newActions[i]);
161: }
162: }
163: setActive(tmpHash);
164: }
165:
166: /**
167: * As defined in net.suberic.util.gui.ConfigurableUI
168: */
169: public void setActive(Hashtable newCommands) {
170: commands = newCommands;
171: }
172:
173: /**
174: * This gets an action from the supported commands. If there is no
175: * supported action, it returns null
176: */
177:
178: public Action getAction(String command) {
179: return (Action) commands.get(command);
180: }
181:
182: class ConfigurableComboRenderer extends JLabel implements
183: ListCellRenderer {
184:
185: public ConfigurableComboRenderer() {
186: setOpaque(true);
187: setHorizontalAlignment(CENTER);
188: setVerticalAlignment(CENTER);
189: }
190:
191: public java.awt.Component getListCellRendererComponent(
192: JList list, Object value, int index,
193: boolean isSelected, boolean cellHasFocus) {
194: if (isSelected) {
195: setBackground(list.getSelectionBackground());
196: setForeground(list.getSelectionForeground());
197: } else {
198: setBackground(list.getBackground());
199: setForeground(list.getForeground());
200: }
201:
202: ImageIcon icon = (ImageIcon) value;
203: //setText(icon.getDescription());
204: setIcon(icon);
205: return this;
206: }
207: }
208: }
|