private void maybeAddActionsPCL() { boolean needsPCL = false; Object[] keys = keys(); if (keys != null) { for (Object key : keys) { javax.swing.Action value = get(key); if (value instanceof ApplicationAction) { ApplicationAction actionAdapter = (ApplicationAction) value; if ((actionAdapter.getEnabledProperty() != null) || (actionAdapter.getSelectedProperty() != null)) { needsPCL = true; break; } } } if (needsPCL) { try { final Class actionsClass = getActionsClass(); final Method m = actionsClass.getMethod("addPropertyChangeListener", PropertyChangeListener.class); m.invoke(getActionsObject(), new ActionsPCL()); } catch (Exception e) { final String s = "addPropertyChangeListener undefined " + actionsClass; throw new Error(s, e); } } } }
private ApplicationActionMap createActionMapChain( Class startClass, Class stopClass, Object actionsObject, ResourceMap resourceMap) { // All of the classes from stopClass to startClass, inclusive. List<Class> classes = new ArrayList<Class>(); for(Class c = startClass; ; c = c.getSuperclass()) { classes.add(c); if (c.equals(stopClass)) { break; } } Collections.reverse(classes); // Create the ActionMap chain, one per class ApplicationContext ctx = getContext(); ApplicationActionMap parent = null; for(Class cls : classes) { ApplicationActionMap appAM = new ApplicationActionMap(ctx, cls, actionsObject, resourceMap); appAM.setParent(parent); parent = appAM; } return parent; }
public ApplicationActionMap(ApplicationContext context, Class actionsClass, Object actionsObject, ResourceMap resourceMap) { if (context == null) { throw new IllegalArgumentException("null context"); } if (actionsClass == null) { throw new IllegalArgumentException("null actionsClass"); } if (actionsObject == null) { throw new IllegalArgumentException("null actionsObject"); } if (!(actionsClass.isInstance(actionsObject))) { throw new IllegalArgumentException("actionsObject not an instanceof actionsClass"); } this.context = context; this.actionsClass = actionsClass; this.actionsObject = actionsObject; this.resourceMap = resourceMap; this.proxyActions = new ArrayList<ApplicationAction>(); addAnnotationActions(resourceMap); maybeAddActionsPCL(); }
private void addAnnotationActions(ResourceMap resourceMap) { Class<?> actionsClass = getActionsClass(); // @Action for (Method m : actionsClass.getDeclaredMethods()) { Action action = m.getAnnotation(Action.class); if (action != null) { String methodName = m.getName(); String enabledProperty = aString(action.enabledProperty(), null); String selectedProperty = aString(action.selectedProperty(), null); String actionName = aString(action.name(), methodName); Task.BlockingScope block = action.block(); ApplicationAction appAction = new ApplicationAction(this, resourceMap, actionName, m, enabledProperty, selectedProperty, block); putAction(actionName, appAction); } } // @ProxyActions ProxyActions proxyActionsAnnotation = actionsClass.getAnnotation(ProxyActions.class); if (proxyActionsAnnotation != null) { for(String actionName : proxyActionsAnnotation.value()) { ApplicationAction appAction = new ApplicationAction(this, resourceMap, actionName); appAction.setEnabled(false); // will track the enabled property of the Action it's bound to putAction(actionName, appAction); proxyActions.add(appAction); } } }
@Override public void propertyChange(PropertyChangeEvent event) { String propertyName = event.getPropertyName(); Object[] keys = keys(); if (keys != null) { for (Object key : keys) { javax.swing.Action value = get(key); if (value instanceof ApplicationAction) { ApplicationAction appAction = (ApplicationAction) value; if (propertyName.equals(appAction.getEnabledProperty())) { appAction.forwardPropertyChangeEvent(event, "enabled"); } else if (propertyName.equals(appAction.getSelectedProperty())) { appAction.forwardPropertyChangeEvent(event, "selected"); } } } } } }
private Error newInvokeError(Method m, Exception e, Object... args) { String argsString = (args.length == 0) ? "" : args[0].toString(); for(int i = 1; i < args.length; i++) { argsString += ", " + args[i]; } String actionsClassName = appAM.getActionsObject().getClass().getName(); String msg = String.format("%s.%s(%s) failed", actionsClassName, m, argsString); return new Error(msg, e); }
private IllegalArgumentException newNoSuchPropertyException(String propertyName) { String actionsClassName = appAM.getActionsClass().getName(); String msg = String.format("no property named %s in %s", propertyName, actionsClassName); return new IllegalArgumentException(msg); }
Object target = appAM.getActionsObject(); taskObject = actionMethod.invoke(target, arguments); task.setInputBlocker(createInputBlocker(task, actionEvent)); ApplicationContext ctx = appAM.getContext(); ctx.getTaskService().execute(task);
private void updateProxyActions(ApplicationActionMap appAM, ActionMap ownerActionMap, JComponent focusOwner) { for (ApplicationAction proxyAction : appAM.getProxyActions()) { String proxyActionName = proxyAction.getName(); javax.swing.Action proxy = ownerActionMap.get(proxyActionName); if (proxy != null) { proxyAction.setProxy(proxy); proxyAction.setProxySource(focusOwner); } else { proxyAction.setProxy(null); proxyAction.setProxySource(null); } } } }
/** * All of the {@code @ProxyActions} recursively defined by this * {@code ApplicationActionMap} and its parent ancestors. * <p> * Returns a read-only list of the {@code @ProxyActions} defined * by this {@code ApplicationActionMap's} {@code actionClass} * and, recursively, by this {@code ApplicationActionMap's} parent. * If there are no proxyActions, an empty list is returned. * * @return a list of all the proxyActions for this {@code ApplicationActionMap} */ public List<ApplicationAction> getProxyActions() { // TBD: proxyActions that shadow should be merged ArrayList<ApplicationAction> allProxyActions = new ArrayList<ApplicationAction>(proxyActions); ActionMap parent = getParent(); while(parent != null) { if (parent instanceof ApplicationActionMap) { allProxyActions.addAll(((ApplicationActionMap)parent).proxyActions); } parent = parent.getParent(); } return Collections.unmodifiableList(allProxyActions); }
argument = resourceMap; } else if (pType == ApplicationContext.class) { argument = appAM.getContext(); } else if (pType == Application.class) { argument = appAM.getContext().getApplication(); } else { Exception e = new IllegalArgumentException("unrecognized @Action method parameter");
private void addAnnotationActions(ResourceMap resourceMap) { final Class<?> actionsClass = getActionsClass(); if (action != null) { final String methodName = m.getName(); final String enabledProperty = aString(action.enabledProperty(), null); final String disabledProperty = aString(action.disabledProperty(), null); final String selectedProperty = aString(action.selectedProperty(), null); final String actionName = aString(action.name(), methodName); final String taskService = aString(action.taskService(), TaskService.DEFAULT_NAME); final Task.BlockingScope block = action.block(); new ApplicationAction(this, resourceMap, actionName, m, inverted?disabledProperty:enabledProperty, inverted, selectedProperty, taskService, block); putAction(actionName, appAction); ApplicationAction appAction = new ApplicationAction(this, resourceMap, actionName); appAction.setEnabled(false); // will track the enabled property of the Action it's bound to putAction(actionName, appAction); proxyActions.add(appAction);
ApplicationActionMap actionMap = OmrGui.getApplication().getContext().getActionMap(this); repoMenu.add(new JMenuItem(actionMap.get("refresh"))); repoMenu.add(new JMenuItem(actionMap.get("loadImages"))); repoMenu.add(new JMenuItem(actionMap.get("checkRepository"))); if (SampleRepository.USE_TRIBES) { repoMenu.add(new JMenuItem(actionMap.get("checkTribes"))); repoMenu.add(new JMenuItem(actionMap.get("launchTrainer"))); } else { repoMenu.add(new JMenuItem(actionMap.get("pushRepository"))); repoMenu.add(new JMenuItem(actionMap.get("save"))); repoMenu.add(new JMenuItem(actionMap.get("exportFeatures"))); repoMenu.add(new JMenuItem(actionMap.get("purgeSheets")));
public void propertyChange(PropertyChangeEvent event) { String propertyName = event.getPropertyName(); Object[] keys = keys(); if (keys != null) { for (Object key : keys) { javax.swing.Action value = get(key); if (value instanceof ApplicationAction) { ApplicationAction appAction = (ApplicationAction)value; if (propertyName.equals(appAction.getEnabledProperty())) { appAction.forwardPropertyChangeEvent(event, "enabled"); } else if (propertyName.equals(appAction.getSelectedProperty())) { appAction.forwardPropertyChangeEvent(event, "selected"); } } } } } }
private Error newInvokeError(Method m, Exception e, Object... args) { StringBuilder argsString = new StringBuilder((args.length == 0) ? "" : args[0].toString()); for (int i = 1; i < args.length; i++) { argsString.append(", ").append(args[i].toString()); } String actionsClassName = appAM.getActionsObject().getClass().getName(); String msg = String.format("%s.%s(%s) failed", actionsClassName, m, argsString.toString()); return new Error(msg, e); }
private IllegalArgumentException newNoSuchPropertyException(String propertyName) { String actionsClassName = appAM.getActionsClass().getName(); String msg = String.format("no property named %s in %s", propertyName, actionsClassName); return new IllegalArgumentException(msg); }
Object target = appAM.getActionsObject(); taskObject = actionMethod.invoke(target, arguments); } catch (Exception e) { task.setInputBlocker(createInputBlocker(task, actionEvent)); final ApplicationContext ctx = appAM.getContext(); final TaskService ts = ctx.getTaskService(taskService); if (ts != null) {