KickJava   Java API By Example, From Geeks To Geeks.

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


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 ConfigurablePopupMenu extends JPopupMenu 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 ConfigurablePopupMenu() {
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 ConfigurablePopupMenu(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     JMenuItem 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 JMenuItem 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 ConfigurableMenu(menuID + "." + menuItemID, vars);
89       }
90       
91       JMenuItem mi;
92       try {
93     mi = new JMenuItem(vars.getProperty(menuID + "." + menuItemID + ".Label"));
94       } catch (MissingResourceException JavaDoc mre) {
95     mi = new JMenuItem(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(JButton.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       // this means that we have a submenu.
117
ConfigurableMenu m;
118       
119       if (vars.getProperty(menuID + "." + menuItemID + ".class", "").equals("")) {
120     m = new ConfigurableMenu(menuID + "." + menuItemID, vars);
121     
122       } else {
123     // this means we're using a custom Menu.
124

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

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

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

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

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