KickJava   Java API By Example, From Geeks To Geeks.

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


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 JMenuBar 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 ConfigurableMenuBar extends JMenuBar 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 ConfigurableMenuBar using the menubarID as the
24      * configuration key, and vars as the source for the values of all the
25      * properties.
26      *
27      * If menubarID doesn't exist in vars, then this returns an empty
28      * Menubar.
29      */

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

43
44     public void configureComponent(String JavaDoc menubarID, VariableBundle vars) {
45     if ((menubarID != null) && (vars.getProperty(menubarID, "") != "")) {
46         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(vars.getProperty(menubarID, ""), ":");
47         while (tokens.hasMoreTokens()) {
48         String JavaDoc currentMenu = tokens.nextToken();
49         ConfigurableMenu m;
50         if (vars.getProperty(menubarID + "." + currentMenu + ".class", "").equals("")) {
51             m = new ConfigurableMenu(menubarID + "." + currentMenu, vars);
52         } else {
53             // this means we're using a custom Menu.
54

55             try {
56             Class JavaDoc menuClass = Class.forName(vars.getProperty(menubarID + "." + currentMenu + ".class", "net.suberic.util.gui.ConfigurableMenu"));
57             m = (ConfigurableMenu) menuClass.newInstance();
58             m.configureComponent(menubarID + "." + currentMenu, vars);
59             } catch (Exception JavaDoc e) {
60             // if we get any errors, just create a plain
61
// ConfigurableMenu.
62
m = new ConfigurableMenu(menubarID + "." + currentMenu, vars);
63             }
64         }
65         if (m != null) {
66             this.add(m);
67         }
68         }
69     }
70     }
71     
72     
73     /**
74      * As defined in net.suberic.util.gui.ConfigurableUI
75      */

76     public void setActive(javax.swing.Action JavaDoc[] newActions) {
77     Hashtable JavaDoc tmpHash = new Hashtable JavaDoc();
78     if (newActions != null && newActions.length > 0) {
79         for (int i = 0; i < newActions.length; i++) {
80         String JavaDoc cmdName = (String JavaDoc)newActions[i].getValue(Action.NAME);
81         tmpHash.put(cmdName, newActions[i]);
82         }
83     }
84     setActive(tmpHash);
85     }
86
87     /**
88      * As defined in net.suberic.util.gui.ConfigurableUI
89      */

90     public void setActive(Hashtable JavaDoc newCommands) {
91     commands = newCommands;
92     setActiveMenus();
93     }
94
95     private void setActiveMenus() {
96     for (int i = 0; i < getMenuCount(); i++) {
97         ((ConfigurableMenu)getMenu(i)).setActive(commands);
98     }
99     }
100
101     /**
102      * This gets an action from the supported commands. If there is no
103      * supported action, it returns null
104      */

105     
106     public Action JavaDoc getAction(String JavaDoc command) {
107     return (Action JavaDoc)commands.get(command);
108     }
109
110
111 }
112
Popular Tags