KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

20   private Hashtable JavaDoc commands = new Hashtable JavaDoc();
21
22   /**
23    * This creates a new ConfigurableToolbar using the toolbarID as the
24    * configuration key, and vars as the source forthe values of all the
25    * properties.
26    *
27    * If toolbarID doesn't exist in vars, then this returns an empty
28    * Toolbar.
29    */

30
31   public ConfigurableToolbar(String JavaDoc toolbarID, VariableBundle vars) {
32     super();
33
34     configureComponent(toolbarID, vars);
35   }
36
37   /**
38    * This configures the Toolbar using the given toolbarID and
39    * VariableBundle.
40    *
41    * As defined in interface net.suberic.util.gui.ConfigurableUI.
42    */

43
44   public void configureComponent(String JavaDoc toolbarID, VariableBundle vars) {
45     if (toolbarID != null) {
46       if (vars.getProperty(toolbarID + "._floatable", "true").equalsIgnoreCase("false")) {
47         setFloatable(false);
48       }
49       if (vars.getProperty(toolbarID, "") != "") {
50         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(vars.getProperty(toolbarID, ""), ":");
51         while (tokens.hasMoreTokens()) {
52           String JavaDoc nextToken = tokens.nextToken();
53           String JavaDoc nextID = toolbarID + "." + nextToken;
54           String JavaDoc nextClass = vars.getProperty(nextID + ".class", "");
55           if (nextClass == "") {
56             JButton b = createToolButton(nextID, vars);
57             if (b != null) {
58               this.add(b);
59             }
60           } else {
61             try {
62               Class JavaDoc buttonClass = Class.forName(nextClass);
63               ConfigurableUI newUI = (ConfigurableUI) buttonClass.newInstance();
64               newUI.configureComponent(nextID, vars);
65               if (newUI instanceof JComponent) {
66                 this.add((JComponent)newUI);
67               }
68             } catch (Exception JavaDoc e) {
69               e.printStackTrace();
70               // if we get any errors, don't create anything.
71
}
72
73           }
74         }
75       }
76     }
77   }
78
79   protected JButton createToolButton(String JavaDoc key, VariableBundle vars) {
80     JButton bi;
81
82     IconManager iconManager = IconManager.getIconManager(vars, "IconManager._default");
83
84     try {
85
86       ImageIcon icon = iconManager.getIcon(vars.getProperty(key + ".Image"));
87
88       bi = new JButton(icon);
89
90       bi.setMargin(new java.awt.Insets JavaDoc(1, 1, 1, 1));
91
92     } catch (MissingResourceException JavaDoc mre) {
93       return null;
94     }
95
96     try {
97       bi.setToolTipText(vars.getProperty(key+ ".ToolTip"));
98     } catch (MissingResourceException JavaDoc mre) {
99     }
100
101     String JavaDoc cmd = vars.getProperty(key + ".Action", key);
102
103     bi.setActionCommand(cmd);
104
105     return bi;
106   }
107
108   /**
109    * This updates the Actions on the Toolbar.
110    *
111    * As defined in interface net.suberic.util.gui.ConfigurableUI.
112    */

113
114   public void setActive(Hashtable JavaDoc newCommands) {
115     clearListeners();
116     commands=newCommands;
117     for (int i = 0; i < this.getComponentCount(); i++) {
118       Object JavaDoc component = this.getComponentAtIndex(i);
119       if (component instanceof ConfigurableUI) {
120         ((ConfigurableUI) component).setActive(newCommands);
121       } else if (component instanceof JButton) {
122         JButton bi = (JButton)(component);
123
124         Action JavaDoc a = getAction(bi.getActionCommand());
125         if (a != null) {
126           bi.addActionListener(a);
127           bi.setEnabled(true);
128         } else {
129           bi.setEnabled(false);
130         }
131       }
132     }
133   }
134
135   /**
136    * This updates the Actions on the Toolbar.
137    *
138    * As defined in interface net.suberic.util.gui.ConfigurableUI.
139    */

140   public void setActive(Action JavaDoc[] newActions) {
141     clearListeners();
142     Hashtable JavaDoc tmpHash = new Hashtable JavaDoc();
143     if (newActions != null && newActions.length > 0) {
144       for (int i = 0; i < newActions.length; i++) {
145         String JavaDoc cmdName = (String JavaDoc)newActions[i].getValue(Action.NAME);
146         tmpHash.put(cmdName, newActions[i]);
147       }
148     }
149     setActive(tmpHash);
150   }
151
152   /**
153    * This clears the current listeners. I think this shouldn't be
154    * necessary--I think that you can only have one listener at a time,
155    * so this shouldn't really be necessary. Still...
156    */

157   private void clearListeners() {
158     for (int i = 0; i < this.getComponentCount(); i++) {
159       if ((this.getComponentAtIndex(i)) instanceof JButton) {
160         JButton button = (JButton)(this.getComponentAtIndex(i));
161         Action JavaDoc a = getAction(button.getActionCommand());
162         if (a != null) {
163           button.removeActionListener(a);
164         }
165       }
166
167     }
168   }
169
170   private Action JavaDoc getAction(String JavaDoc key) {
171     try {
172       return (Action JavaDoc)commands.get(key);
173     } catch (ClassCastException JavaDoc cce) {
174       return null;
175     }
176   }
177
178
179 }
180
181
182
Popular Tags