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 ConfigurableMenu extends JMenu implements ConfigurableUI { 16 17 20 protected Hashtable commands = new Hashtable (); 21 22 public ConfigurableMenu() { 23 super(); 24 } 25 26 34 35 public ConfigurableMenu(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 setText(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 String keyBinding = vars.getProperty(key + ".KeyBinding", ""); 80 if (!keyBinding.equals("")) { 81 this.setMnemonic(keyBinding.charAt(0)); 82 } 83 84 } 85 86 89 protected JMenuItem createMenuItem(String menuID, String menuItemID, VariableBundle vars) { 90 92 if (vars.getProperty(menuID + "." + menuItemID + ".class", "") == "") { 93 94 if (vars.getProperty(menuID + "." + menuItemID, "") != "") { 95 return new ConfigurableMenu(menuID + "." + menuItemID, vars); 96 } 97 98 JMenuItem mi; 99 try { 100 mi = new JMenuItem(vars.getProperty(menuID + "." + menuItemID + ".Label")); 101 } catch (MissingResourceException mre) { 102 mi = new JMenuItem(menuItemID); 103 } 104 105 java.net.URL url = null; 106 107 try { 108 url = this.getClass().getResource(vars.getProperty(menuID + "." + menuItemID + ".Image")); 109 } catch (MissingResourceException mre) { 110 } 111 112 if (url != null) { 113 mi.setHorizontalTextPosition(JButton.RIGHT); 114 mi.setIcon(new ImageIcon(url)); 115 } 116 117 String cmd = vars.getProperty(menuID + "." + menuItemID + ".Action", menuItemID); 118 119 mi.setActionCommand(cmd); 120 121 String keyBinding = vars.getProperty(menuID + "." + menuItemID + ".KeyBinding", ""); 122 if (!keyBinding.equals("")) 123 mi.setMnemonic(keyBinding.charAt(0)); 124 125 String accelBinding = vars.getProperty(menuID + "." + menuItemID + ".Accelerator", ""); 126 if (!accelBinding.equals("")) { 127 mi.setAccelerator(KeyStroke.getKeyStroke(accelBinding)); 128 } 129 130 return mi; 131 } else { 132 ConfigurableMenu m; 134 135 if (vars.getProperty(menuID + "." + menuItemID + ".class", "").equals("")) { 136 m = new ConfigurableMenu(menuID + "." + menuItemID, vars); 137 138 } else { 139 141 try { 142 Class menuClass = Class.forName(vars.getProperty(menuID + "." + menuItemID + ".class", "net.suberic.util.gui.ConfigurableMenu")); 143 m = (ConfigurableMenu) menuClass.newInstance(); 144 m.configureComponent(menuID + "." + menuItemID, vars); 145 } catch (Exception e) { 146 e.printStackTrace(); 147 m = new ConfigurableMenu(menuID + "." + menuItemID, vars); 150 } 151 } 152 153 return m; 154 155 } 156 } 157 158 161 public void setActive(javax.swing.Action [] newActions) { 162 Hashtable tmpHash = new Hashtable (); 163 if (newActions != null && newActions.length > 0) { 164 for (int i = 0; i < newActions.length; i++) { 165 String cmdName = (String )newActions[i].getValue(Action.NAME); 166 tmpHash.put(cmdName, newActions[i]); 167 } 168 } 169 setActive(tmpHash); 170 } 171 172 175 public void setActive(Hashtable newCommands) { 176 clearListeners(); 177 commands = newCommands; 178 setActiveMenuItems(); 179 } 180 181 protected void setActiveMenuItems() { 182 for (int j = 0; j < getItemCount(); j++) { 183 if (getItem(j) instanceof ConfigurableMenu) { 184 ((ConfigurableMenu)getItem(j)).setActive(commands); 185 } else { 186 JMenuItem mi = getItem(j); 187 Action a = getAction(mi.getActionCommand()); 188 if (a != null) { 189 mi.addActionListener(a); 191 mi.setEnabled(true); 192 } else { 193 mi.setEnabled(false); 194 } 195 } 196 } 197 } 198 199 202 203 private void clearListeners() { 204 for (int j = 0; j < getItemCount(); j++) { 205 if (getItem(j) instanceof ConfigurableMenu) { 206 ; 209 } else { 210 JMenuItem mi = getItem(j); 211 Action a = getAction(mi.getActionCommand()); 212 if (a != null) { 213 mi.removeActionListener(a); 214 } 215 } 216 } 217 } 218 219 223 224 public Action getAction(String command) { 225 return (Action )commands.get(command); 226 } 227 228 229 } 230 | Popular Tags |