001: package net.suberic.util.gui;
002:
003: import javax.swing.*;
004: import net.suberic.util.VariableBundle;
005: import java.util.Hashtable;
006: import java.util.StringTokenizer;
007: import java.util.MissingResourceException;
008: import javax.swing.Action;
009:
010: /**
011: * This is a JToolbar which implements the ConfigurableUI interface, and
012: * therefore may be dynamically created using a VariableBundle and key,
013: * and updated using an array of Actions.
014: */
015:
016: public class ConfigurableToolbar extends JToolBar implements
017: ConfigurableUI {
018:
019: // the latest commands list. i'm storing this for now because i
020: // can't do a JButton.removeActionListeners().
021:
022: private Hashtable commands = new Hashtable();
023:
024: /**
025: * This creates a new ConfigurableToolbar using the toolbarID as the
026: * configuration key, and vars as the source forthe values of all the
027: * properties.
028: *
029: * If toolbarID doesn't exist in vars, then this returns an empty
030: * Toolbar.
031: */
032:
033: public ConfigurableToolbar(String toolbarID, VariableBundle vars) {
034: super ();
035:
036: configureComponent(toolbarID, vars);
037: }
038:
039: /**
040: * This configures the Toolbar using the given toolbarID and
041: * VariableBundle.
042: *
043: * As defined in interface net.suberic.util.gui.ConfigurableUI.
044: */
045:
046: public void configureComponent(String toolbarID, VariableBundle vars) {
047: if ((toolbarID != null)
048: && (vars.getProperty(toolbarID, "") != "")) {
049: StringTokenizer tokens = new StringTokenizer(vars
050: .getProperty(toolbarID, ""), ":");
051: while (tokens.hasMoreTokens()) {
052: String nextToken = tokens.nextToken();
053: String nextID = toolbarID + "." + nextToken;
054: String nextClass = vars.getProperty(nextID + ".class",
055: "");
056: if (nextClass == "") {
057: JButton b = createToolButton(nextID, vars);
058: if (b != null) {
059: this .add(b);
060: }
061: } else {
062: try {
063: Class buttonClass = Class.forName(nextClass);
064: ConfigurableUI newUI = (ConfigurableUI) buttonClass
065: .newInstance();
066: newUI.configureComponent(nextID, vars);
067: if (newUI instanceof JComponent) {
068: this .add((JComponent) newUI);
069: }
070: } catch (Exception e) {
071: e.printStackTrace();
072: // if we get any errors, don't create anything.
073: }
074:
075: }
076: }
077: }
078: }
079:
080: protected JButton createToolButton(String key, VariableBundle vars) {
081: JButton bi;
082:
083: IconManager iconManager = IconManager.getIconManager(vars,
084: "IconManager._default");
085:
086: try {
087:
088: ImageIcon icon = iconManager.getIcon(vars.getProperty(key
089: + ".Image"));
090:
091: bi = new JButton(icon);
092:
093: bi.setMargin(new java.awt.Insets(1, 1, 1, 1));
094:
095: } catch (MissingResourceException mre) {
096: return null;
097: }
098:
099: try {
100: bi.setToolTipText(vars.getProperty(key + ".ToolTip"));
101: } catch (MissingResourceException mre) {
102: }
103:
104: String cmd = vars.getProperty(key + ".Action", key);
105:
106: bi.setActionCommand(cmd);
107:
108: return bi;
109: }
110:
111: /**
112: * This updates the Actions on the Toolbar.
113: *
114: * As defined in interface net.suberic.util.gui.ConfigurableUI.
115: */
116:
117: public void setActive(Hashtable newCommands) {
118: clearListeners();
119: commands = newCommands;
120: for (int i = 0; i < this .getComponentCount(); i++) {
121: Object component = this .getComponentAtIndex(i);
122: if (component instanceof ConfigurableUI) {
123: ((ConfigurableUI) component).setActive(newCommands);
124: } else if (component instanceof JButton) {
125: JButton bi = (JButton) (component);
126:
127: Action a = getAction(bi.getActionCommand());
128: if (a != null) {
129: bi.addActionListener(a);
130: bi.setEnabled(true);
131: } else {
132: bi.setEnabled(false);
133: }
134: }
135: }
136: }
137:
138: /**
139: * This updates the Actions on the Toolbar.
140: *
141: * As defined in interface net.suberic.util.gui.ConfigurableUI.
142: */
143: public void setActive(Action[] newActions) {
144: clearListeners();
145: Hashtable tmpHash = new Hashtable();
146: if (newActions != null && newActions.length > 0) {
147: for (int i = 0; i < newActions.length; i++) {
148: String cmdName = (String) newActions[i]
149: .getValue(Action.NAME);
150: tmpHash.put(cmdName, newActions[i]);
151: }
152: }
153: setActive(tmpHash);
154: }
155:
156: /**
157: * This clears the current listeners. I think this shouldn't be
158: * necessary--I think that you can only have one listener at a time,
159: * so this shouldn't really be necessary. Still...
160: */
161: private void clearListeners() {
162: for (int i = 0; i < this .getComponentCount(); i++) {
163: if ((this .getComponentAtIndex(i)) instanceof JButton) {
164: JButton button = (JButton) (this .getComponentAtIndex(i));
165: Action a = getAction(button.getActionCommand());
166: if (a != null) {
167: button.removeActionListener(a);
168: }
169: }
170:
171: }
172: }
173:
174: private Action getAction(String key) {
175: try {
176: return (Action) commands.get(key);
177: } catch (ClassCastException cce) {
178: return null;
179: }
180: }
181:
182: }
|