1 package net.suberic.util.gui; 2 import java.awt.*; 3 import net.suberic.util.VariableBundle; 4 import java.util.Hashtable ; 5 import java.util.StringTokenizer ; 6 import java.util.MissingResourceException ; 7 import javax.swing.Action ; 8 9 14 15 public class ConfigurableAwtMenu extends Menu implements ConfigurableUI { 16 17 20 protected Hashtable commands = new Hashtable (); 21 22 public ConfigurableAwtMenu() { 23 super(); 24 } 25 26 34 35 public ConfigurableAwtMenu(String menuID, VariableBundle vars) { 36 super(); 37 38 configureComponent(menuID, vars); 39 } 40 41 47 48 public void configureComponent(String key, VariableBundle vars) { 49 StringTokenizer iKeys = null; 50 try { 51 iKeys = new StringTokenizer (vars.getProperty(key), ":"); 52 } catch (MissingResourceException mre) { 53 mre.printStackTrace(); 54 try { 55 System.err.println(vars.getProperty("error.NoSuchResource") + " " + mre.getKey()); 56 } catch (MissingResourceException mretwo) { 57 System.err.println("Unable to load resource " + mre.getKey()); 58 return; 59 } 60 return; 61 } 62 String currentToken; 63 64 try { 65 setLabel(vars.getProperty(key + ".Label")); 66 } catch (MissingResourceException 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 85 } 86 87 90 protected MenuItem createMenuItem(String menuID, String menuItemID, VariableBundle vars) { 91 93 if (vars.getProperty(menuID + "." + menuItemID + ".class", "") == "") { 94 95 if (vars.getProperty(menuID + "." + menuItemID, "") != "") { 96 return new ConfigurableAwtMenu(menuID + "." + menuItemID, vars); 97 } 98 99 MenuItem mi; 100 try { 101 mi = new MenuItem(vars.getProperty(menuID + "." + menuItemID + ".Label")); 102 } catch (MissingResourceException mre) { 103 mi = new MenuItem(menuItemID); 104 } 105 106 java.net.URL url = null; 107 108 try { 109 url = this.getClass().getResource(vars.getProperty(menuID + "." + menuItemID + ".Image")); 110 } catch (MissingResourceException mre) { 111 } 112 113 118 String cmd = vars.getProperty(menuID + "." + menuItemID + ".Action", menuItemID); 119 120 mi.setActionCommand(cmd); 121 122 String keyBinding = vars.getProperty(menuID + "." + menuItemID + ".KeyBinding", ""); 123 126 String accelBinding = vars.getProperty(menuID + "." + menuItemID + ".Accelerator", ""); 127 if (!accelBinding.equals("")) { 128 mi.setShortcut(new MenuShortcut(AWTKeyStroke.getAWTKeyStroke(accelBinding).getKeyCode())); 129 } 130 131 return mi; 132 } else { 133 ConfigurableAwtMenu m; 135 136 if (vars.getProperty(menuID + "." + menuItemID + ".class", "").equals("")) { 137 m = new ConfigurableAwtMenu(menuID + "." + menuItemID, vars); 138 139 } else { 140 142 try { 143 Class menuClass = Class.forName(vars.getProperty(menuID + "." + menuItemID + ".class", "net.suberic.util.gui.ConfigurableAwtMenu")); 144 m = (ConfigurableAwtMenu) menuClass.newInstance(); 145 m.configureComponent(menuID + "." + menuItemID, vars); 146 } catch (Exception e) { 147 e.printStackTrace(); 148 m = new ConfigurableAwtMenu(menuID + "." + menuItemID, vars); 151 } 152 } 153 154 return m; 155 156 } 157 } 158 159 162 public void setActive(javax.swing.Action [] newActions) { 163 Hashtable tmpHash = new Hashtable (); 164 if (newActions != null && newActions.length > 0) { 165 for (int i = 0; i < newActions.length; i++) { 166 String cmdName = (String )newActions[i].getValue(Action.NAME); 167 tmpHash.put(cmdName, newActions[i]); 168 } 169 } 170 setActive(tmpHash); 171 } 172 173 176 public void setActive(Hashtable newCommands) { 177 clearListeners(); 178 commands = newCommands; 179 setActiveMenuItems(); 180 } 181 182 protected void setActiveMenuItems() { 183 for (int j = 0; j < getItemCount(); j++) { 184 if (getItem(j) instanceof ConfigurableAwtMenu) { 185 ((ConfigurableAwtMenu)getItem(j)).setActive(commands); 186 } else { 187 MenuItem mi = getItem(j); 188 Action a = getAction(mi.getActionCommand()); 189 if (a != null) { 190 mi.addActionListener(a); 192 mi.setEnabled(true); 193 } else { 194 mi.setEnabled(false); 195 } 196 } 197 } 198 } 199 200 203 204 private void clearListeners() { 205 for (int j = 0; j < getItemCount(); j++) { 206 if (getItem(j) instanceof ConfigurableAwtMenu) { 207 ; 210 } else { 211 MenuItem mi = getItem(j); 212 Action a = getAction(mi.getActionCommand()); 213 if (a != null) { 214 mi.removeActionListener(a); 215 } 216 } 217 } 218 } 219 220 224 225 public Action getAction(String command) { 226 return (Action )commands.get(command); 227 } 228 229 230 } 231 | Popular Tags |