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) 2005 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.cookswing.action;
18  
19  import javax.swing.AbstractButton;
20  import javax.swing.Action;
21  
22  import org.jdesktop.application.Application;
23  import org.jdesktop.application.ApplicationContext;
24  
25  import ch.elca.el4j.services.gui.swing.ActionsContext;
26  
27  import cookxml.cookswing.CookSwing;
28  import cookxml.core.DecodeEngine;
29  import cookxml.core.exception.SetterException;
30  import cookxml.core.interfaces.Setter;
31  
32  /**
33   * This class is a cookXml setter, which sets actions defined by "@Action"
34   * for buttons.
35   *
36   * @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/cookswing/action/ButtonActionSetter.java $
37   *
38   * @author Stefan Wismer (SWI)
39   */
40  public class ButtonActionSetter implements Setter {
41  	/**
42  	 * The class containing "@Action" annotated methods.
43  	 */
44  	private Object m_actionHolder;
45  	
46  	/**
47  	 * The default constructor: actionHolder is the associated class.
48  	 * @see CookSwing#CookSwing(Object)
49  	 */
50  	public ButtonActionSetter() { }
51  	
52  	/**
53  	 * @param actionHolder    the class containing "@Action" annotated methods
54  	 */
55  	public ButtonActionSetter(Object actionHolder) {
56  		m_actionHolder = actionHolder;
57  	}
58  	
59  	/** {@inheritDoc} */
60  	public void setAttribute(String ns, String tag,
61  		String attrNS, String attr, Object obj, Object value,
62  		DecodeEngine decodeEngine) throws SetterException {
63  		
64  		Object actionHolder = m_actionHolder;
65  		if (actionHolder == null) {
66  			actionHolder = decodeEngine.getVariable("this");
67  		}
68  		
69  		Action action = null;
70  		
71  		String actionName = (String) value;
72  		// Check attrValue if it contains class name
73  		String[] parsedValues = actionName.split("#");
74  		Class<?> cls = null;
75  		if (parsedValues.length == 2) {
76  			try {
77  				cls =  Class.forName(parsedValues[0]);
78  				ApplicationContext ac = Application.getInstance().getContext();
79  				action = ac.getActionMap(cls, actionHolder).get(parsedValues[1]);
80  			} catch (ClassNotFoundException e) {
81  				action = null;
82  			}
83  		}
84  		
85  		if (action == null) {
86  			ActionsContext actionsContext;
87  			if (actionHolder instanceof ActionsContextAware) {
88  				actionsContext = ((ActionsContextAware) actionHolder).getActionsContext();
89  			} else {
90  				// use ActionsContext of GUIApplication as parent context and extend it with the actionHolder
91  				actionsContext = ActionsContext.extendDefault(actionHolder);
92  			}
93  			
94  			action = actionsContext.getAction(actionName);
95  			if (action == null) {
96  				throw new ActionNotFoundException(actionName);
97  			}
98  		}
99  		
100 		AbstractButton button = (AbstractButton) obj;
101 		button.setAction(action);
102 	}
103 }