KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 ConfigurableMenu extends JMenu 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 ConfigurableMenu() {
23     super();
24   }
25
26   /**
27    * This creates a new ConfigurableMenu 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 ConfigurableMenu(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       setText(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         JMenuItem mi = createMenuItem(key, currentToken, vars);
75         this.add(mi);
76       }
77     }
78
79     String JavaDoc keyBinding = vars.getProperty(key + ".KeyBinding", "");
80     if (!keyBinding.equals("")) {
81       this.setMnemonic(keyBinding.charAt(0));
82     }
83
84   }
85
86   /**
87    * And this actually creates the menu items themselves.
88    */

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

92     if (vars.getProperty(menuID + "." + menuItemID + ".class", "") == "") {
93
94       if (vars.getProperty(menuID + "." + menuItemID, "") != "") {
95         return new ConfigurableMenu(menuID + "." + menuItemID, vars);
96       }
97
98       JMenuItem mi;
99       try {
100         mi = new JMenuItem(vars.getProperty(menuID + "." + menuItemID + ".Label"));
101       } catch (MissingResourceException JavaDoc mre) {
102         mi = new JMenuItem(menuItemID);
103       }
104
105       java.net.URL JavaDoc url = null;
106
107       try {
108         url = this.getClass().getResource(vars.getProperty(menuID + "." + menuItemID + ".Image"));
109       } catch (MissingResourceException JavaDoc mre) {
110       }
111
112       if (url != null) {
113         mi.setHorizontalTextPosition(JButton.RIGHT);
114         mi.setIcon(new ImageIcon(url));
115       }
116
117       String JavaDoc cmd = vars.getProperty(menuID + "." + menuItemID + ".Action", menuItemID);
118
119       mi.setActionCommand(cmd);
120
121       String JavaDoc keyBinding = vars.getProperty(menuID + "." + menuItemID + ".KeyBinding", "");
122       if (!keyBinding.equals(""))
123         mi.setMnemonic(keyBinding.charAt(0));
124
125       String JavaDoc accelBinding = vars.getProperty(menuID + "." + menuItemID + ".Accelerator", "");
126       if (!accelBinding.equals("")) {
127         mi.setAccelerator(KeyStroke.getKeyStroke(accelBinding));
128       }
129
130       return mi;
131     } else {
132       // this means that we have a submenu.
133
ConfigurableMenu m;
134
135       if (vars.getProperty(menuID + "." + menuItemID + ".class", "").equals("")) {
136         m = new ConfigurableMenu(menuID + "." + menuItemID, vars);
137
138       } else {
139         // this means we're using a custom Menu.
140

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

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

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

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

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