Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  /*
   * Copyright (C) 2006 Sun Microsystems, Inc. All rights reserved. Use is
   * subject to license terms.
   */ 
  
  package org.jdesktop.application;
  
 import java.util.List;
 import java.util.Set;
An ActionMap class where each entry corresponds to an @Action method from a single actionsClass (i.e. a class that contains one or more @Actions). Each entry's key is the @Action's name (the method name by default), and the value is an ApplicationAction that calls the @Actions method. For example, the code below prints "Hello World":
 public class HelloWorldActions {
     public @Action void Hello() { System.out.print("Hello "); }
     public @Action void World() { System.out.println("World"); }
 }
 // ...
 ApplicationActionMap appAM = new ApplicationActionMap(SimpleActions.class);
 ActionEvent e = new ActionEvent("no src", ActionEvent.ACTION_PERFORMED, "no cmd");
 appAM.get("Hello").actionPerformed(e);
 appAM.get("World").actionPerformed(e);
 

If a ResourceMap is provided then each ApplicationAction's ( putValue, getValue) properties are initialized from the ResourceMap.

TBD: explain use of resourcemap including action types, actionsObject, actionsClass, ProxyActions,

Author(s):
Hans Muller (Hans.Muller@Sun.COM)
See also:
ApplicationAction
ResourceMap
 
 
 public class ApplicationActionMap extends ActionMap {
     private final ApplicationContext context;
     private final ResourceMap resourceMap;
     private final Class actionsClass;
     private final Object actionsObject;
     private final List<ApplicationActionproxyActions;
 
     public ApplicationActionMap(ApplicationContext contextClass actionsClassObject actionsObjectResourceMap 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;
 	this. = actionsClass;
 	this. = actionsObject;
 	this. = resourceMap;
 	addAnnotationActions(resourceMap);
     }
 
     public final ApplicationContext getContext() {
         return ;
     }
 
     public final Class getActionsClass() { 
 	return 
     }
 
     public final Object getActionsObject() {
 	return ;
     }

    
All of the @ProxyActions recursively defined by this ApplicationActionMap and its parent ancestors.

Returns a read-only list of the @ProxyActions defined by this ApplicationActionMap's actionClass and, recursively, by this ApplicationActionMap's parent. If there are no proxyActions, an empty list is returned.

Returns:
a list of all the proxyActions for this ApplicationActionMap
	// TBD: proxyActions that shadow should be merged
	ActionMap parent = getParent();
	while(parent != null) {
	    if (parent instanceof ApplicationActionMap) {
		allProxyActions.addAll(((ApplicationActionMap)parent).);
	    }
	    parent = parent.getParent();
	}
	return Collections.unmodifiableList(allProxyActions);
    }
    private String aString(String sString emptyValue) {
	return (s.length() == 0) ? emptyValue : s;
    }
    private void putAction(String keyApplicationAction action) {
	if (get(key) != null) {
	    // TBD log a warning - two actions with the same key
	}
	put(keyaction);
    }
    /* Add Actions for each actionsClass method with an @Action
     * annotation and for the class's @ProxyActions annotation
     */
    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(thisresourceMapactionNamemenabledPropertyselectedPropertyblock);
		putAction(actionNameappAction);
	    }
	}
	// @ProxyActions
	ProxyActions proxyActionsAnnotation = actionsClass.getAnnotation(ProxyActions.class);
	if (proxyActionsAnnotation != null) {
	    for(String actionName : proxyActionsAnnotation.value()) {
		ApplicationAction appAction = new ApplicationAction(thisresourceMapactionName);
		appAction.setEnabled(false); // will track the enabled property of the Action it's bound to
		putAction(actionNameappAction);
		.add(appAction);
	    }
	}
    }
    /* If any of the ApplicationActions need to track an 
     * enabled or selected property defined in actionsClass, then add our 
     * PropertyChangeListener.  If none of the @Actions in actionClass
     * provide an enabledProperty or selectedProperty argument, then
     * we don't need to do this.
     */
    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 {
		    Class actionsClass = getActionsClass();
		    Method m = actionsClass.getMethod("addPropertyChangeListener"PropertyChangeListener.class);
		    m.invoke(getActionsObject(), new ActionsPCL());
		}
		catch (Exception e) {
		    String s = "addPropertyChangeListener undefined " + ;
		    throw new Error(se);
		}
	    }
	}
    }
    /* When the value of an actionsClass @Action enabledProperty or 
     * selectedProperty changes, forward the PropertyChangeEvent to 
     * the ApplicationAction object itself.
     */
    private class ActionsPCL implements PropertyChangeListener {
	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");
                        }
		    }
		}
	    }
	}
    }
New to GrepCode? Check out our FAQ X