KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.util.gui;
2 import java.awt.*;
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 Menu 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 ConfigurableAwtMenu extends Menu 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   protected Hashtable JavaDoc commands = new Hashtable JavaDoc();
21
22   public ConfigurableAwtMenu() {
23     super();
24   }
25
26   /**
27    * This creates a new ConfigurableAwtMenu using the menuID as the
28    * configuration key, and vars as the source for the values of all the
29    * properties.
30    *
31    * If menuID doesn't exist in vars, then this returns an empty
32    * Menu.
33    */

34
35   public ConfigurableAwtMenu(String JavaDoc menuID, VariableBundle vars) {
36     super();
37
38     configureComponent(menuID, vars);
39   }
40
41   /**
42    * This configures the Menu using the given menuID and
43    * VariableBundle.
44    *
45    * As defined in interface net.suberic.util.gui.ConfigurableUI.
46    */

47
48   public void configureComponent(String JavaDoc key, VariableBundle vars) {
49     StringTokenizer JavaDoc iKeys = null;
50     try {
51       iKeys = new StringTokenizer JavaDoc(vars.getProperty(key), ":");
52     } catch (MissingResourceException JavaDoc mre) {
53       mre.printStackTrace();
54       try {
55         System.err.println(vars.getProperty("error.NoSuchResource") + " " + mre.getKey());
56       } catch (MissingResourceException JavaDoc mretwo) {
57         System.err.println("Unable to load resource " + mre.getKey());
58         return;
59       }
60       return;
61     }
62     String JavaDoc currentToken;
63
64     try {
65       setLabel(vars.getProperty(key + ".Label"));
66     } catch (MissingResourceException JavaDoc mre) {
67     }
68
69     while (iKeys.hasMoreTokens()) {
70       currentToken=iKeys.nextToken();
71       if (currentToken.equals("-")) {
72         this.addSeparator();
73       } else {
74         MenuItem mi = createMenuItem(key, currentToken, vars);
75         this.add(mi);
76       }
77     }
78
79
80     //String keyBinding = vars.getProperty(key + ".KeyBinding", "");
81
//if (!keyBinding.equals("")) {
82
// this.setMnemonic(keyBinding.charAt(0));
83
//}
84

85   }
86
87   /**
88    * And this actually creates the menu items themselves.
89    */

90   protected MenuItem createMenuItem(String JavaDoc menuID, String JavaDoc menuItemID, VariableBundle vars) {
91     // TODO: should also make these undo-able.
92

93     if (vars.getProperty(menuID + "." + menuItemID + ".class", "") == "") {
94
95       if (vars.getProperty(menuID + "." + menuItemID, "") != "") {
96   return new ConfigurableAwtMenu(menuID + "." + menuItemID, vars);
97       }
98
99       MenuItem mi;
100       try {
101         mi = new MenuItem(vars.getProperty(menuID + "." + menuItemID + ".Label"));
102       } catch (MissingResourceException JavaDoc mre) {
103         mi = new MenuItem(menuItemID);
104       }
105
106       java.net.URL JavaDoc url = null;
107
108       try {
109         url = this.getClass().getResource(vars.getProperty(menuID + "." + menuItemID + ".Image"));
110       } catch (MissingResourceException JavaDoc mre) {
111       }
112
113 // if (url != null) {
114
// mi.setHorizontalTextPosition(Button.RIGHT);
115
// mi.setIcon(new ImageIcon(url));
116
// }
117

118       String JavaDoc cmd = vars.getProperty(menuID + "." + menuItemID + ".Action", menuItemID);
119
120       mi.setActionCommand(cmd);
121
122       String JavaDoc keyBinding = vars.getProperty(menuID + "." + menuItemID + ".KeyBinding", "");
123       //if (!keyBinding.equals(""))
124
// mi.setMnemonic(keyBinding.charAt(0));
125

126       String JavaDoc accelBinding = vars.getProperty(menuID + "." + menuItemID + ".Accelerator", "");
127       if (!accelBinding.equals("")) {
128         mi.setShortcut(new MenuShortcut(AWTKeyStroke.getAWTKeyStroke(accelBinding).getKeyCode()));
129       }
130
131       return mi;
132     } else {
133       // this means that we have a submenu.
134
ConfigurableAwtMenu m;
135
136       if (vars.getProperty(menuID + "." + menuItemID + ".class", "").equals("")) {
137         m = new ConfigurableAwtMenu(menuID + "." + menuItemID, vars);
138
139       } else {
140         // this means we're using a custom Menu.
141

142         try {
143           Class JavaDoc menuClass = Class.forName(vars.getProperty(menuID + "." + menuItemID + ".class", "net.suberic.util.gui.ConfigurableAwtMenu"));
144           m = (ConfigurableAwtMenu) menuClass.newInstance();
145           m.configureComponent(menuID + "." + menuItemID, vars);
146         } catch (Exception JavaDoc e) {
147           e.printStackTrace();
148           // if we get any errors, just create a plain
149
// ConfigurableAwtMenu.
150
m = new ConfigurableAwtMenu(menuID + "." + menuItemID, vars);
151         }
152       }
153
154       return m;
155
156     }
157   }
158
159   /**
160    * As defined in net.suberic.util.gui.ConfigurableUI
161    */

162   public void setActive(javax.swing.Action JavaDoc[] newActions) {
163     Hashtable JavaDoc tmpHash = new Hashtable JavaDoc();
164     if (newActions != null && newActions.length > 0) {
165       for (int i = 0; i < newActions.length; i++) {
166         String JavaDoc cmdName = (String JavaDoc)newActions[i].getValue(Action.NAME);
167         tmpHash.put(cmdName, newActions[i]);
168       }
169     }
170     setActive(tmpHash);
171   }
172
173   /**
174    * As defined in net.suberic.util.gui.ConfigurableUI
175    */

176   public void setActive(Hashtable JavaDoc newCommands) {
177     clearListeners();
178     commands = newCommands;
179     setActiveMenuItems();
180   }
181
182   protected void setActiveMenuItems() {
183     for (int j = 0; j < getItemCount(); j++) {
184       if (getItem(j) instanceof ConfigurableAwtMenu) {
185         ((ConfigurableAwtMenu)getItem(j)).setActive(commands);
186       } else {
187         MenuItem mi = getItem(j);
188         Action JavaDoc a = getAction(mi.getActionCommand());
189         if (a != null) {
190           //mi.removeActionListener(a);
191
mi.addActionListener(a);
192           mi.setEnabled(true);
193         } else {
194           mi.setEnabled(false);
195         }
196       }
197     }
198   }
199
200   /**
201    * This clears all of the current listeners on the Menu.
202    */

203
204   private void clearListeners() {
205     for (int j = 0; j < getItemCount(); j++) {
206       if (getItem(j) instanceof ConfigurableAwtMenu) {
207         // we don't have to clear the listeners here because
208
// it will be done in setActive().
209
;
210       } else {
211         MenuItem mi = getItem(j);
212         Action JavaDoc a = getAction(mi.getActionCommand());
213         if (a != null) {
214           mi.removeActionListener(a);
215         }
216       }
217     }
218   }
219
220   /**
221    * This gets an action from the supported commands. If there is no
222    * supported action, it returns null
223    */

224
225   public Action JavaDoc getAction(String JavaDoc command) {
226     return (Action JavaDoc)commands.get(command);
227   }
228
229
230 }
231
Popular Tags