KickJava   Java API By Example, From Geeks To Geeks.

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


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 JMenu 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 ConfigurableAwtPopupMenu extends PopupMenu implements ConfigurableUI {
16
17   // the latest commands list. i'm storing this for now because i
18
// can't do a Button.removeActionListeners().
19

20   protected Hashtable JavaDoc commands = new Hashtable JavaDoc();
21
22   public ConfigurableAwtPopupMenu() {
23     super();
24   }
25
26   /**
27    * This creates a new ConfigurableAwtPopupMenu 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 ConfigurableAwtPopupMenu(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       try {
54         System.err.println(vars.getProperty("error.NoSuchResource") + " " + mre.getKey());
55       } catch (MissingResourceException JavaDoc mretwo) {
56         System.err.println("Unable to load resource " + mre.getKey());
57       }
58
59       return;
60
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    * And this actually creates the menu items themselves.
81    */

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

85     if (vars.getProperty(menuID + "." + menuItemID + ".class", "") == "") {
86
87       if (vars.getProperty(menuID + "." + menuItemID, "") != "") {
88         return new ConfigurableAwtMenu(menuID + "." + menuItemID, vars);
89       }
90
91       MenuItem mi;
92       try {
93         mi = new MenuItem(vars.getProperty(menuID + "." + menuItemID + ".Label"));
94       } catch (MissingResourceException JavaDoc mre) {
95         mi = new MenuItem(menuItemID);
96       }
97
98       java.net.URL JavaDoc url = null;
99
100       try {
101         url = this.getClass().getResource(vars.getProperty(menuID + "." + menuItemID + ".Image"));
102       } catch (MissingResourceException JavaDoc mre) {
103       }
104
105 // if (url != null) {
106
// //mi.setHorizontalTextPosition(Button.RIGHT);
107
// mi.setIcon(new ImageIcon(url));
108
// }
109

110       String JavaDoc cmd = vars.getProperty(menuID + "." + menuItemID + ".Action", menuItemID);
111
112       mi.setActionCommand(cmd);
113
114       return mi;
115     } else {
116
117       // this means that we have a submenu.
118
ConfigurableAwtMenu m;
119
120       if (vars.getProperty(menuID + "." + menuItemID + ".class", "").equals("")) {
121         m = new ConfigurableAwtMenu(menuID + "." + menuItemID, vars);
122
123       } else {
124         // this means we're using a custom Menu.
125

126         try {
127           Class JavaDoc menuClass = Class.forName(vars.getProperty(menuID + "." + menuItemID + ".class", "net.suberic.util.gui.ConfigurableAwtMenu"));
128           m = (ConfigurableAwtMenu) menuClass.newInstance();
129           m.configureComponent(menuID + "." + menuItemID, vars);
130         } catch (Exception JavaDoc e) {
131           // if we get any errors, just create a plain
132
// ConfigurableAwtMenu.
133
System.err.println("Unable to create menu with class " + vars.getProperty(menuID + "." + menuItemID + ".class", "net.suberic.util.gui.ConfigurableAwtMenu") + ": " + e.getMessage());
134           e.printStackTrace();
135           m = new ConfigurableAwtMenu(menuID + "." + menuItemID, vars);
136         }
137       }
138
139       return m;
140
141     }
142   }
143
144   /**
145    * As defined in net.suberic.util.gui.ConfigurableUI
146    */

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

161   public void setActive(Hashtable JavaDoc newCommands) {
162     clearListeners();
163     commands = newCommands;
164     setActiveMenuItems();
165   }
166
167   protected void setActiveMenuItems() {
168     for (int j = 0; j < getItemCount(); j++) {
169       if (getItem(j) instanceof ConfigurableAwtMenu) {
170         ((ConfigurableAwtMenu)getItem(j)).setActive(commands);
171       } else {
172         MenuItem mi = getItem(j);
173         Action JavaDoc a = getAction(mi.getActionCommand());
174         if (a != null) {
175           //mi.removeActionListener(a);
176
mi.addActionListener(a);
177           mi.setEnabled(true);
178         } else {
179           mi.setEnabled(false);
180         }
181       }
182     }
183   }
184
185   /**
186    * This clears all of the current listeners on the Menu.
187    */

188
189   private void clearListeners() {
190     for (int j = 0; j < getItemCount(); j++) {
191       if (getItem(j) instanceof ConfigurableAwtMenu) {
192         // we don't have to clear the listeners here because
193
// it will be done in setActive().
194
;
195       } else {
196         MenuItem mi = getItem(j);
197         Action JavaDoc a = getAction(mi.getActionCommand());
198         if (a != null) {
199           mi.removeActionListener(a);
200         }
201       }
202     }
203   }
204
205   /**
206    * This gets an action from the supported commands. If there is no
207    * supported action, it returns null
208    */

209
210   public Action JavaDoc getAction(String JavaDoc command) {
211     return (Action JavaDoc)commands.get(command);
212   }
213
214
215 }
216
Popular Tags