View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2008 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.services.gui.swing;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.List;
22  
23  import javax.swing.Action;
24  
25  import org.jdesktop.application.Application;
26  
27  /**
28   * This class holds the Actions context, that enables to resolve the Swing Action given the action name.
29   * It basically consists of an (ordered) list of objects having @Action annotated methods.
30   *
31   * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/swing/src/main/java/ch/elca/el4j/services/gui/swing/ActionsContext.java $
32   *
33   * @author Stefan Wismer (SWI)
34   */
35  public final class ActionsContext {
36  	/**
37  	 * The array containing all instances to search for annotated methods.
38  	 */
39  	private List<Object> m_instancesWithActionMappings;
40  	
41  	/**
42  	 * The parent {@link ActionsContext} if any.
43  	 */
44  	private final ActionsContext m_parentContext;
45  	
46  	/**
47  	 * The private constructor.
48  	 * @param parentContext                  the parent {@link ActionsContext} if any
49  	 * @param instancesWithActionMappings    an array containing all instances to search for annotated methods
50  	 */
51  	private ActionsContext(ActionsContext parentContext, Object... instancesWithActionMappings) {
52  		m_parentContext = parentContext;
53  		m_instancesWithActionMappings = new ArrayList<Object>(Arrays.asList(instancesWithActionMappings));
54  	}
55  	
56  	/**
57  	 * Extend an ActionsContext.
58  	 * @param parentContext                  the parent {@link ActionsContext} to extend.
59  	 * @param instancesWithActionMappings    an array containing all instances to search for annotated methods
60  	 * @return                               the created ActionsContext
61  	 */
62  	public static ActionsContext extend(ActionsContext parentContext, Object... instancesWithActionMappings) {
63  		return new ActionsContext(parentContext, instancesWithActionMappings);
64  	}
65  	
66  	/**
67  	 * Extend the ActionsContext of {@link GUIApplication}.
68  	 * @param instancesWithActionMappings    an array containing all instances to search for annotated methods
69  	 * @return                               the created ActionsContext
70  	 */
71  	public static ActionsContext extendDefault(Object... instancesWithActionMappings) {
72  		return new ActionsContext(GUIApplication.getInstance().getActionsContext(), instancesWithActionMappings);
73  	}
74  	
75  	/**
76  	 * Create an ActionsContext.
77  	 * @param instancesWithActionMappings    an array containing all instances to search for annotated methods
78  	 * @return                               the created ActionsContext
79  	 */
80  	public static ActionsContext create(Object... instancesWithActionMappings) {
81  		return new ActionsContext(null, instancesWithActionMappings);
82  	}
83  	
84  	/**
85  	 * Add an instances to search for annotated methods.
86  	 * @param instanceWithActionMappings    an instances to search for annotated methods
87  	 */
88  	public void add(Object instanceWithActionMappings) {
89  		m_instancesWithActionMappings.add(instanceWithActionMappings);
90  	}
91  	
92  	/**
93  	 * Returns the first action object found for an action name.
94  	 *  (Looks in the internal list of candidate objects.)
95  	 *
96  	 * @param actionName   the action name as String
97  	 * @return             the corresponding action object
98  	 */
99  	public Action getAction(String actionName) {
100 		for (Object candidate : m_instancesWithActionMappings) {
101 			Action foundAction = getAction(candidate, actionName);
102 			if (foundAction != null) {
103 				return foundAction;
104 			}
105 		}
106 		if (m_parentContext != null) {
107 			return m_parentContext.getAction(actionName);
108 		} else {
109 			return null;
110 		}
111 	}
112 	
113 	/**
114 	 * Returns the action object for a specific object and action name.
115 	 * @param object        the object containing actions
116 	 * @param actionName    the action name as String
117 	 * @return              the corresponding action object
118 	 */
119 	public Action getAction(Object object, String actionName) {
120 		org.jdesktop.application.ApplicationContext ac
121 			= Application.getInstance().getContext();
122 		
123 		// it's important to use Object.class here. Otherwise @Action annotated methods in
124 		// super classes are not considered, which normally is not expected
125 		Action action = ac.getActionMap(Object.class, object).get(actionName);
126 		if (action == null && m_parentContext != null) {
127 			return m_parentContext.getAction(object, actionName);
128 		} else {
129 			return action;
130 		}
131 	}
132 }