001: package net.suberic.util.gui;
002:
003: import javax.swing.*;
004: import net.suberic.util.VariableBundle;
005: import java.util.Hashtable;
006: import java.util.StringTokenizer;
007: import java.util.MissingResourceException;
008: import javax.swing.Action;
009:
010: /**
011: * This is a JMenuBar which implements the ConfigurableUI interface, and
012: * therefore may be dynamically created using a VariableBundle and key,
013: * and updated using an array of Actions.
014: */
015:
016: public class ConfigurableMenuBar extends JMenuBar implements
017: ConfigurableUI {
018:
019: // the latest commands list. i'm storing this for now because i
020: // can't do a JButton.removeActionListeners().
021:
022: private Hashtable commands = new Hashtable();
023:
024: /**
025: * This creates a new ConfigurableMenuBar using the menubarID as the
026: * configuration key, and vars as the source for the values of all the
027: * properties.
028: *
029: * If menubarID doesn't exist in vars, then this returns an empty
030: * Menubar.
031: */
032:
033: public ConfigurableMenuBar(String menuBarID, VariableBundle vars) {
034: super ();
035:
036: configureComponent(menuBarID, vars);
037: }
038:
039: /**
040: * This configures the Menubar using the given menubarID and
041: * VariableBundle.
042: *
043: * As defined in interface net.suberic.util.gui.ConfigurableUI.
044: */
045:
046: public void configureComponent(String menubarID, VariableBundle vars) {
047: if ((menubarID != null)
048: && (vars.getProperty(menubarID, "") != "")) {
049: StringTokenizer tokens = new StringTokenizer(vars
050: .getProperty(menubarID, ""), ":");
051: while (tokens.hasMoreTokens()) {
052: String currentMenu = tokens.nextToken();
053: ConfigurableMenu m;
054: if (vars.getProperty(
055: menubarID + "." + currentMenu + ".class", "")
056: .equals("")) {
057: m = new ConfigurableMenu(menubarID + "."
058: + currentMenu, vars);
059: } else {
060: // this means we're using a custom Menu.
061:
062: try {
063: Class menuClass = Class
064: .forName(vars
065: .getProperty(menubarID + "."
066: + currentMenu
067: + ".class",
068: "net.suberic.util.gui.ConfigurableMenu"));
069: m = (ConfigurableMenu) menuClass.newInstance();
070: m.configureComponent(menubarID + "."
071: + currentMenu, vars);
072: } catch (Exception e) {
073: // if we get any errors, just create a plain
074: // ConfigurableMenu.
075: m = new ConfigurableMenu(menubarID + "."
076: + currentMenu, vars);
077: }
078: }
079: if (m != null) {
080: this .add(m);
081: }
082: }
083: }
084: }
085:
086: /**
087: * As defined in net.suberic.util.gui.ConfigurableUI
088: */
089: public void setActive(javax.swing.Action[] newActions) {
090: Hashtable tmpHash = new Hashtable();
091: if (newActions != null && newActions.length > 0) {
092: for (int i = 0; i < newActions.length; i++) {
093: String cmdName = (String) newActions[i]
094: .getValue(Action.NAME);
095: tmpHash.put(cmdName, newActions[i]);
096: }
097: }
098: setActive(tmpHash);
099: }
100:
101: /**
102: * As defined in net.suberic.util.gui.ConfigurableUI
103: */
104: public void setActive(Hashtable newCommands) {
105: commands = newCommands;
106: setActiveMenus();
107: }
108:
109: private void setActiveMenus() {
110: for (int i = 0; i < getMenuCount(); i++) {
111: ((ConfigurableMenu) getMenu(i)).setActive(commands);
112: }
113: }
114:
115: /**
116: * This gets an action from the supported commands. If there is no
117: * supported action, it returns null
118: */
119:
120: public Action getAction(String command) {
121: return (Action) commands.get(command);
122: }
123:
124: }
|