1 7 package org.jdesktop.swing.actions; 8 9 import java.beans.PropertyChangeListener ; 10 11 import java.io.PrintStream ; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 import java.util.Set ; 17 18 import javax.swing.*; 19 20 76 public class ActionManager { 77 78 80 private Map actionMap; 82 83 private ActionContainerFactory factory; 85 86 89 private static ActionManager INSTANCE; 90 91 96 private static boolean DEBUG = false; 97 98 103 public ActionManager() { 104 } 105 106 110 public ActionContainerFactory getFactory() { 111 if (factory == null) { 112 factory = new ActionContainerFactory(this); 113 } 114 return factory; 115 } 116 117 121 public void setFactory(ActionContainerFactory factory) { 122 this.factory = factory; 123 } 124 125 132 public static ActionManager getInstance() { 133 if (INSTANCE == null) { 134 INSTANCE = new ActionManager(); 135 } 136 return INSTANCE; 137 } 138 139 142 public static void setInstance(ActionManager manager) { 143 INSTANCE = manager; 144 } 145 146 157 public Set getActionIDs() { 158 if (actionMap == null) { 159 return null; 160 } 161 return actionMap.keySet(); 162 } 163 164 public Action addAction(Action action) { 165 return addAction(action.getValue(Action.ACTION_COMMAND_KEY), action); 166 } 167 168 174 public Action addAction(Object id, Action action) { 175 if (actionMap == null) { 176 actionMap = new HashMap (); 177 } 178 actionMap.put(id, action); 179 180 return action; 181 } 182 183 189 public Action getAction(Object id) { 190 if (actionMap != null) { 191 return (Action)actionMap.get(id); 192 } 193 return null; 194 } 195 196 202 public TargetableAction getTargetableAction(Object id) { 203 Action a = getAction(id); 204 if (a instanceof TargetableAction) { 205 return (TargetableAction)a; 206 } 207 return null; 208 } 209 210 216 public BoundAction getBoundAction(Object id) { 217 Action a = getAction(id); 218 if (a instanceof BoundAction) { 219 return (BoundAction)a; 220 } 221 return null; 222 } 223 224 230 public ServerAction getServerAction(Object id) { 231 Action a = getAction(id); 232 if (a instanceof ServerAction) { 233 return (ServerAction)a; 234 } 235 return null; 236 } 237 238 244 public CompositeAction getCompositeAction(Object id) { 245 Action a = getAction(id); 246 if (a instanceof CompositeAction) { 247 return (CompositeAction)a; 248 } 249 return null; 250 } 251 252 258 private AbstractActionExt getStateChangeAction(Object id) { 259 Action a = getAction(id); 260 if (a != null && a instanceof AbstractActionExt) { 261 AbstractActionExt aa = (AbstractActionExt)a; 262 if (aa.isStateAction()) { 263 return aa; 264 } 265 } 266 return null; 267 } 268 269 278 public void setEnabled(Object id, boolean enabled) { 279 Action action = getAction(id); 280 if (action != null) { 281 action.setEnabled(enabled); 282 } 283 } 284 285 286 295 public boolean isEnabled(Object id) { 296 Action action = getAction(id); 297 if (action != null) { 298 return action.isEnabled(); 299 } 300 return false; 301 } 302 303 310 public void setSelected(Object id, boolean selected) { 311 AbstractActionExt action = getStateChangeAction(id); 312 if (action != null) { 313 action.setSelected(selected); 314 } 315 } 316 317 325 public boolean isSelected(Object id) { 326 AbstractActionExt action = getStateChangeAction(id); 327 if (action != null) { 328 return action.isSelected(); 329 } 330 return false; 331 } 332 333 337 static void printAction(PrintStream stream, Action action) { 338 stream.println("Attributes for " + action.getValue(Action.ACTION_COMMAND_KEY)); 339 340 if (action instanceof AbstractAction) { 341 Object [] keys = ((AbstractAction)action).getKeys(); 342 343 for (int i = 0; i < keys.length; i++) { 344 stream.println("\tkey: " + keys[i] + "\tvalue: " + 345 action.getValue((String )keys[i])); 346 } 347 } 348 } 349 350 358 public void registerCallback(Object id, Object handler, String method) { 359 BoundAction action = getBoundAction(id); 360 if (action != null) { 361 action.registerCallback(handler, method); 362 } 363 } 364 365 383 384 388 395 public boolean isStateAction(Object id) { 396 Action action = getAction(id); 397 if (action != null && action instanceof AbstractActionExt) { 398 return ((AbstractActionExt)action).isStateAction(); 399 } 400 return false; 401 } 402 403 406 public boolean isTargetableAction(Object id) { 407 return (getTargetableAction(id) != null); 408 } 409 410 413 public boolean isBoundAction(Object id) { 414 return (getBoundAction(id) != null); 415 } 416 417 420 public boolean isCompositeAction(Object id) { 421 return (getCompositeAction(id) != null); 422 } 423 424 427 public boolean isServerAction(Object id) { 428 return (getServerAction(id) != null); 429 } 430 } 431 | Popular Tags |