1 package net.suberic.util.gui; 2 import javax.swing.*; 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 ConfigurablePopupMenu extends JPopupMenu implements ConfigurableUI { 16 17 20 protected Hashtable commands = new Hashtable (); 21 22 public ConfigurablePopupMenu() { 23 super(); 24 } 25 26 34 35 public ConfigurablePopupMenu(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 JMenuItem mi = createMenuItem(key, currentToken, vars); 75 this.add(mi); 76 } 77 } 78 } 79 82 protected JMenuItem 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 ConfigurableMenu(menuID + "." + menuItemID, vars); 89 } 90 91 JMenuItem mi; 92 try { 93 mi = new JMenuItem(vars.getProperty(menuID + "." + menuItemID + ".Label")); 94 } catch (MissingResourceException mre) { 95 mi = new JMenuItem(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 if (url != null) { 106 mi.setHorizontalTextPosition(JButton.RIGHT); 107 mi.setIcon(new ImageIcon(url)); 108 } 109 110 String cmd = vars.getProperty(menuID + "." + menuItemID + ".Action", menuItemID); 111 112 mi.setActionCommand(cmd); 113 114 return mi; 115 } else { 116 ConfigurableMenu m; 118 119 if (vars.getProperty(menuID + "." + menuItemID + ".class", "").equals("")) { 120 m = new ConfigurableMenu(menuID + "." + menuItemID, vars); 121 122 } else { 123 125 try { 126 Class 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 e) { 130 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 146 public void setActive(javax.swing.Action [] newActions) { 147 Hashtable tmpHash = new Hashtable (); 148 if (newActions != null && newActions.length > 0) { 149 for (int i = 0; i < newActions.length; i++) { 150 String cmdName = (String )newActions[i].getValue(Action.NAME); 151 tmpHash.put(cmdName, newActions[i]); 152 } 153 } 154 setActive(tmpHash); 155 } 156 157 160 public void setActive(Hashtable 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 a = getAction(mi.getActionCommand()); 173 if (a != null) { 174 mi.addActionListener(a); 176 mi.setEnabled(true); 177 } else { 178 mi.setEnabled(false); 179 } 180 } 181 } 182 } 183 184 187 188 private void clearListeners() { 189 for (int j = 0; j < getSubElements().length; j++) { 190 if (getComponent(j) instanceof ConfigurableMenu) { 191 ; 194 } else { 195 JMenuItem mi = (JMenuItem)getComponent(j); 196 Action a = getAction(mi.getActionCommand()); 197 if (a != null) { 198 mi.removeActionListener(a); 199 } 200 } 201 } 202 } 203 204 208 209 public Action getAction(String command) { 210 return (Action )commands.get(command); 211 } 212 213 214 } 215 | Popular Tags |