1 package com.opensymphony.workflow.designer; 2 3 import java.net.URL ; 4 import java.util.*; 5 import javax.swing.*; 6 7 17 public final class ActionManager 18 { 19 private static final String OS_NAME_STRING = System.getProperty("os.name").replace(' ', '_').toLowerCase(); 20 21 private static final String SMALL_GRAY_ICON = "smallGrayIcon"; 22 private static final String DISPLAYED_MNEMONIC_INDEX = "mnemonicIndex"; 23 24 private static final ActionManager INSTANCE = new ActionManager(); 25 26 private final Map actions; 27 private ResourceBundle bundle; 28 29 private ActionManager() 30 { 31 this.actions = new HashMap(50); 32 bundle = ResourceBundle.getBundle("com.opensymphony.workflow.designer.actions"); 33 } 34 35 43 public static Action register(String id, Action action) 44 { 45 if(action == null) 46 throw new NullPointerException ("Registered actions must not be null."); 47 48 boolean exists = ActionReader.readAndPutValues(action, INSTANCE.bundle, id); 49 if(!exists) return null; 50 51 Object oldValue = INSTANCE.actions.put(id, action); 52 if(oldValue != null) 53 System.out.println("WARNING: Duplicate action id: " + id); 54 return action; 55 } 56 57 62 public static Action deregister(String id) 63 { 64 return (Action)INSTANCE.actions.remove(id); 65 } 66 67 72 public static Action get(String id) 73 { 74 Action action = (Action)(INSTANCE.actions.get(id)); 75 if(null == action) 76 { 77 System.out.println("ERROR: No action found for id: " + id); 78 return null; 79 } 80 return action; 81 } 82 83 86 public static Icon getIcon(String id) 87 { 88 Action action = get(id); 89 if(action == null) 90 return null; 91 return (Icon)action.getValue(Action.SMALL_ICON); 92 } 93 94 100 public static void alias(String newKey, String oldKey) 101 { 102 Object oldValue = INSTANCE.actions.put(newKey, INSTANCE.actions.get(oldKey)); 103 if(oldValue != null) 104 System.out.println("WARNING: Duplicate action id: " + newKey); 105 } 106 107 private static class ActionReader 108 { 109 private static final String LABEL = "label"; 110 private static final char MNEMONIC_MARKER = '&'; 111 private static final String DOT_STRING = "..."; 112 private static final String SHORT_DESCRIPTION = "tooltip"; 113 private static final String LONG_DESCRIPTION = "helptext"; 114 private static final String ICON = "icon"; 115 private static final String GRAY_ICON = ICON + ".gray"; 116 private static final String ACCELERATOR = "accelerator"; 117 private static final String COMMAND = "command"; 118 119 private String id; 120 private String name; 121 private Integer mnemonic; 122 private Integer aMnemonicIndex; 123 private String shortDescription; 124 private String longDescription; 125 private ImageIcon icon; 126 private ImageIcon grayIcon; 127 private KeyStroke accelerator; 128 private String command; 129 private boolean exists = true; 130 131 134 static void readValues(ResourceBundle bundle, String id) 135 { 136 new ActionReader(bundle, id); 137 } 138 139 143 static boolean readAndPutValues(Action action, ResourceBundle bundle, String id) 144 { 145 ActionReader reader = new ActionReader(bundle, id); 146 if(!reader.actionExists()) return false; 147 reader.putValues(action); 148 return true; 149 } 150 151 private ActionReader(ResourceBundle bundle, String id) 152 { 153 String iconPath = getString(bundle, id + '.' + ICON, null); 154 if(getString(bundle, id + "." + LABEL, null) == null && iconPath == null) 155 { 156 exists = false; 157 return; 158 } 159 160 this.id = id; 161 String nameWithMnemonic = getString(bundle, id + "." + LABEL, id); 162 int index = mnemonicIndex(nameWithMnemonic); 163 name = stripName(nameWithMnemonic, index); 164 mnemonic = stripMnemonic(nameWithMnemonic, index); 165 aMnemonicIndex = new Integer (index); 166 167 shortDescription = getString(bundle, id + '.' + SHORT_DESCRIPTION, defaultShortDescription(name)); 168 longDescription = getString(bundle, id + '.' + LONG_DESCRIPTION, name); 169 170 URL iconURL = iconPath != null ? getClass().getClassLoader().getResource(iconPath) : null; 171 if(iconURL == null && iconPath != null) 172 { 173 System.out.println("WARNING Invalid icon " + iconPath + " specified in actions.properties for action '" + name + "'"); 174 icon = null; 175 } 176 else 177 { 178 icon = (iconPath == null) ? null : new ImageIcon(iconURL); 179 } 180 181 String grayIconPath = getString(bundle, id + '.' + GRAY_ICON, null); 182 grayIcon = (grayIconPath == null) ? null : new ImageIcon(getClass().getClassLoader().getResource(grayIconPath)); 183 184 String shortcut = getString(bundle, id + '.' + ACCELERATOR + '.' + OS_NAME_STRING, null); 185 if(shortcut == null) 186 { 187 shortcut = getString(bundle, id + '.' + ACCELERATOR, null); 188 } 189 accelerator = getKeyStroke(shortcut); 190 191 command = getString(bundle, id + '.' + COMMAND, null); 192 } 193 194 public boolean actionExists() 195 { 196 return exists; 197 } 198 199 202 private void putValues(Action action) 203 { 204 action.putValue(Action.NAME, name); 205 action.putValue(Action.SHORT_DESCRIPTION, shortDescription); 206 action.putValue(Action.LONG_DESCRIPTION, longDescription); 207 if(icon != null) 208 action.putValue(Action.SMALL_ICON, icon); 209 if(grayIcon != null) 210 action.putValue(ActionManager.SMALL_GRAY_ICON, grayIcon); 211 if(accelerator != null) 212 action.putValue(Action.ACCELERATOR_KEY, accelerator); 213 if(mnemonic != null) 214 action.putValue(Action.MNEMONIC_KEY, mnemonic); 215 if(command != null) 216 action.putValue(Action.ACTION_COMMAND_KEY, command); 217 action.putValue(ActionManager.DISPLAYED_MNEMONIC_INDEX, aMnemonicIndex); 218 } 219 220 private int mnemonicIndex(String nameWithMnemonic) 221 { 222 return nameWithMnemonic.indexOf(MNEMONIC_MARKER); 223 } 224 225 private String stripName(String nameWithMnemonic, int mnemonicIndex) 226 { 227 return mnemonicIndex == -1 ? nameWithMnemonic : nameWithMnemonic.substring(0, mnemonicIndex) + nameWithMnemonic.substring(mnemonicIndex + 1); 228 } 229 230 private Integer stripMnemonic(String nameWithMnemonic, int mnemonicIndex) 231 { 232 return mnemonicIndex == -1 ? null : new Integer (nameWithMnemonic.charAt(mnemonicIndex + 1)); 233 } 234 235 private String defaultShortDescription(String nameWithDots) 236 { 237 return nameWithDots.endsWith(DOT_STRING) ? (nameWithDots.substring(0, nameWithDots.length() - DOT_STRING.length())) : nameWithDots; 238 } 239 240 private KeyStroke getKeyStroke(String accelerator) 241 { 242 if(accelerator == null) 243 { 244 return null; 245 } 246 else 247 { 248 KeyStroke keyStroke = KeyStroke.getKeyStroke(accelerator); 249 if(keyStroke == null) 250 System.out.println("WARNING: Action " + id + " has an invalid accelerator " + accelerator); 251 return keyStroke; 252 } 253 } 254 255 private String getString(ResourceBundle bundle, String key, String defaultString) 256 { 257 try 258 { 259 return bundle.getString(key); 260 } 261 catch(MissingResourceException e) 262 { 263 return defaultString; 264 } 265 } 266 } 267 } | Popular Tags |