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;
18  
19  import java.lang.reflect.Method;
20  
21  import javax.swing.JPanel;
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  import org.w3c.dom.Element;
26  
27  import cookxml.core.DecodeEngine;
28  import cookxml.core.exception.CookXmlException;
29  import cookxml.core.exception.CreatorException;
30  import cookxml.core.interfaces.Creator;
31  
32  /**
33   * The cookSwing creator for general purpose <create-component>s.
34   * The create-method is invoked when the XML tag is opened and
35   * the finish-method when the tag is closed.
36   *
37   * @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/CreateComponentCreator.java $
38   *
39   * @author Stefan Wismer (SWI)
40   */
41  public class CreateComponentCreator implements Creator {
42  	// <create-component> specific attributes
43  	/**
44  	 * The XML attribute name for the create method.
45  	 */
46  	protected static final String CREATE_METHOD = "create-method";
47  	
48  	/**
49  	 * The XML attribute name for the finish method.
50  	 */
51  	protected static final String FINISH_METHOD = "finish-method";
52  	
53  	/**
54  	 * The logger.
55  	 */
56  	private static final Logger s_logger = LoggerFactory
57  		.getLogger(CreateComponentCreator.class);
58  	
59  	/** {@inheritDoc} */
60  	public Object create(String parentNS, String parentTag, Element elm,
61  		Object parentObj, DecodeEngine decodeEngine) throws CreatorException {
62  		
63  		Object form = decodeEngine.getVariable("this");
64  		String methodName = elm.getAttribute(CREATE_METHOD);
65  		
66  		if (!methodName.equals("")) {
67  			return invokeMethod(form, methodName);
68  		} else {
69  			return new JPanel();
70  		}
71  	}
72  
73  	/** {@inheritDoc} */
74  	public Object editFinished(String parentNS, String parentTag, Element elm,
75  		Object parentObj, Object obj, DecodeEngine decodeEngine) throws CookXmlException {
76  		
77  		Object form = decodeEngine.getVariable("this");
78  		String methodName = elm.getAttribute(FINISH_METHOD);
79  		
80  		if (!methodName.equals("")) {
81  			invokeMethod(form, methodName, obj);
82  		}
83  		return obj;
84  	}
85  	
86  	/**
87  	 * Invoke a potentially private method.
88  	 *
89  	 * @param form          the form object
90  	 * @param methodName    the method name
91  	 * @param parameters    optional parameters
92  	 * @return              the return value of the invoked method
93  	 */
94  	protected Object invokeMethod(Object form, String methodName, Object... parameters) {
95  		Class<?> formClass = form.getClass();
96  		while (formClass != Object.class) {
97  			// list all methods to access private methods as well
98  			final Method[] publicMethods = formClass.getDeclaredMethods();
99  			for (int i = 0; i < publicMethods.length; ++i) {
100 				if (methodName.equals(publicMethods[i].getName())) {
101 					try {
102 						publicMethods[i].setAccessible(true);
103 						return publicMethods[i].invoke(form, parameters);
104 					} catch (Exception e) {
105 						// try next method
106 						continue;
107 					}
108 				}
109 			}
110 			formClass = formClass.getSuperclass();
111 		}
112 		s_logger.error("Error processing <create-component>. Could not find method '" + methodName + "'.");
113 		return null;
114 	}
115 }