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 ConfigurableAwtPopupMenu extends PopupMenu implements ConfigurableUI { 16 17 20 protected Hashtable commands = new Hashtable (); 21 22 public ConfigurableAwtPopupMenu() { 23 super(); 24 } 25 26 34 35 public ConfigurableAwtPopupMenu(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 try { 54 System.err.println(vars.getProperty("error.NoSuchResource") + " " + mre.getKey()); 55 } catch (MissingResourceException mretwo) { 56 System.err.println("Unable to load resource " + mre.getKey()); 57 } 58 59 return; 60 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 82 protected MenuItem createMenuItem(String menuID, String menuItemID, VariableBundle vars) { 83 85 if (vars.getProperty(menuID + "." + menuItemID + ".class", "") == "") { 86 87 if (vars.getProperty(menuID + "." + menuItemID, "") != "") { 88 return new ConfigurableAwtMenu(menuID + "." + menuItemID, vars); 89 } 90 91 MenuItem mi; 92 try { 93 mi = new MenuItem(vars.getProperty(menuID + "." + menuItemID + ".Label")); 94 } catch (MissingResourceException mre) { 95 mi = new MenuItem(menuItemID); 96 } 97 98 java.net.URL url = null; 99 100 try { 101 url = this.getClass().getResource(vars.getProperty(menuID + "." + menuItemID + ".Image")); 102 } catch (MissingResourceException mre) { 103 } 104 105 110 String cmd = vars.getProperty(menuID + "." + menuItemID + ".Action", menuItemID); 111 112 mi.setActionCommand(cmd); 113 114 return mi; 115 } else { 116 117 ConfigurableAwtMenu m; 119 120 if (vars.getProperty(menuID + "." + menuItemID + ".class", "").equals("")) { 121 m = new ConfigurableAwtMenu(menuID + "." + menuItemID, vars); 122 123 } else { 124 126 try { 127 Class menuClass = Class.forName(vars.getProperty(menuID + "." + menuItemID + ".class", "net.suberic.util.gui.ConfigurableAwtMenu")); 128 m = (ConfigurableAwtMenu) menuClass.newInstance(); 129 m.configureComponent(menuID + "." + menuItemID, vars); 130 } catch (Exception e) { 131 System.err.println("Unable to create menu with class " + vars.getProperty(menuID + "." + menuItemID + ".class", "net.suberic.util.gui.ConfigurableAwtMenu") + ": " + e.getMessage()); 134 e.printStackTrace(); 135 m = new ConfigurableAwtMenu(menuID + "." + menuItemID, vars); 136 } 137 } 138 139 return m; 140 141 } 142 } 143 144 147 public void setActive(javax.swing.Action [] newActions) { 148 Hashtable tmpHash = new Hashtable (); 149 if (newActions != null && newActions.length > 0) { 150 for (int i = 0; i < newActions.length; i++) { 151 String cmdName = (String )newActions[i].getValue(Action.NAME); 152 tmpHash.put(cmdName, newActions[i]); 153 } 154 } 155 setActive(tmpHash); 156 } 157 158 161 public void setActive(Hashtable newCommands) { 162 clearListeners(); 163 commands = newCommands; 164 setActiveMenuItems(); 165 } 166 167 protected void setActiveMenuItems() { 168 for (int j = 0; j < getItemCount(); j++) { 169 if (getItem(j) instanceof ConfigurableAwtMenu) { 170 ((ConfigurableAwtMenu)getItem(j)).setActive(commands); 171 } else { 172 MenuItem mi = getItem(j); 173 Action a = getAction(mi.getActionCommand()); 174 if (a != null) { 175 mi.addActionListener(a); 177 mi.setEnabled(true); 178 } else { 179 mi.setEnabled(false); 180 } 181 } 182 } 183 } 184 185 188 189 private void clearListeners() { 190 for (int j = 0; j < getItemCount(); j++) { 191 if (getItem(j) instanceof ConfigurableAwtMenu) { 192 ; 195 } else { 196 MenuItem mi = getItem(j); 197 Action a = getAction(mi.getActionCommand()); 198 if (a != null) { 199 mi.removeActionListener(a); 200 } 201 } 202 } 203 } 204 205 209 210 public Action getAction(String command) { 211 return (Action )commands.get(command); 212 } 213 214 215 } 216 | Popular Tags |