001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package com.sun.rave.web.ui.component.util.event;
042:
043: import java.lang.reflect.Method;
044: import java.lang.reflect.Modifier;
045: import java.util.ArrayList;
046: import java.util.HashMap;
047: import java.util.List;
048: import java.util.Map;
049:
050: /**
051: * <P> A HandlerDefinition defines a "handler" that may be invoked in the
052: * process of executing an event. A HandlerDefinition has an
053: * <strong>id</strong>, <strong>java method</strong>, <strong>input
054: * definitions</strong>, <strong>output definitions</strong>, and
055: * <strong>child handlers</strong>.</P>
056: *
057: * <P> The <strong>java method</strong> to be invoked must have the
058: * following method signature:</P>
059: *
060: * <P> <BLOCKQUOTE></CODE>
061: * public void beginDisplay(HandlerContext handlerCtx)
062: * </CODE></BLOCKQUOTE></P>
063: *
064: * <P> <code>void</code> above can return a value. Depending on the type of
065: * event, return values may be handled differently.</P>
066: *
067: * @author Ken Paulsen (ken.paulsen@sun.com)
068: */
069: public class HandlerDefinition implements java.io.Serializable {
070:
071: /**
072: * Constructor
073: */
074: public HandlerDefinition(String id) {
075: _id = id;
076: }
077:
078: /**
079: * This method returns the id for this handler.
080: */
081: public String getId() {
082: return _id;
083: }
084:
085: /**
086: * For future tool support
087: */
088: public String getDescription() {
089: return _description;
090: }
091:
092: /**
093: * For future tool support
094: */
095: public void setDescription(String desc) {
096: _description = desc;
097: }
098:
099: /**
100: * <P> This method sets the event handler (method) to be invoked. The
101: * method should be public and accept a prameter of type
102: * "HandlerContext" Example:</P>
103: *
104: * <P> <BLOCKQUOTE>
105: * public void beginDisplay(HandlerContext handlerCtx)
106: * </BLOCKQUOTE></P>
107: *
108: * @param cls The full class name containing method
109: * @param methodName The method name of the handler within class
110: */
111: public void setHandlerMethod(String cls, String methodName) {
112: if ((cls == null) || (methodName == null)) {
113: throw new IllegalArgumentException(
114: "Class name and method name must be non-null!");
115: }
116: _methodClass = cls;
117: _methodName = methodName;
118: }
119:
120: /**
121: *
122: */
123: public void setHandlerMethod(Method method) {
124: if (method != null) {
125: _methodName = method.getName();
126: _methodClass = method.getDeclaringClass().getName();
127: } else {
128: _methodName = null;
129: _methodClass = null;
130: }
131: _method = method;
132: }
133:
134: /**
135: * <p> This method determines if the handler is static.</p>
136: */
137: public boolean isStatic() {
138: if (_static == null) {
139: _static = Boolean.valueOf(Modifier
140: .isStatic(getHandlerMethod().getModifiers()));
141: }
142: return _static.booleanValue();
143: }
144:
145: /**
146: *
147: */
148: public Method getHandlerMethod() {
149: if (_method != null) {
150: // return cached Method
151: return _method;
152: }
153:
154: // See if we have the info to find it
155: if ((_methodClass != null) && (_methodName != null)) {
156: // Find the class
157: Class clzz = null;
158: try {
159: clzz = Class.forName(_methodClass);
160: } catch (ClassNotFoundException ex) {
161: throw new RuntimeException("'" + _methodClass
162: + "' not found!", ex);
163: }
164:
165: // Find the method on the class
166: Method method = null;
167: try {
168: method = clzz.getMethod(_methodName, EVENT_ARGS);
169: } catch (NoSuchMethodException ex) {
170: throw new RuntimeException("Method '" + _methodName
171: + "' not found!", ex);
172: }
173:
174: // Cache the _method
175: _method = method;
176: }
177:
178: // Return the Method if there is one
179: return _method;
180: }
181:
182: /**
183: * This method adds an IODescriptor to the list of input descriptors.
184: * These descriptors define the input parameters to this handler.
185: *
186: * @param desc The input IODescriptor to add
187: */
188: public void addInputDef(IODescriptor desc) {
189: _inputDefs.put(desc.getName(), desc);
190: }
191:
192: /**
193: * This method sets the input IODescriptors for this handler.
194: *
195: * @param inputDefs The Map of IODescriptors
196: */
197: public void setInputDefs(Map inputDefs) {
198: if (inputDefs == null) {
199: throw new IllegalArgumentException(
200: "inputDefs cannot be null!");
201: }
202: _inputDefs = inputDefs;
203: }
204:
205: /**
206: * This method retrieves the Map of input IODescriptors.
207: *
208: * @return The Map of IODescriptors
209: */
210: public Map getInputDefs() {
211: return _inputDefs;
212: }
213:
214: /**
215: * This method returns the requested IODescriptor, null if not found.
216: */
217: public IODescriptor getInputDef(String name) {
218: return (IODescriptor) _inputDefs.get(name);
219: }
220:
221: /**
222: * This method adds an IODescriptor to the list of output descriptors.
223: * These descriptors define the output parameters to this handler.
224: *
225: * @param desc The IODescriptor to add
226: */
227: public void addOutputDef(IODescriptor desc) {
228: _outputDefs.put(desc.getName(), desc);
229: }
230:
231: /**
232: * This method sets the output IODescriptors for this handler.
233: *
234: * @param outputDefs The Map of output IODescriptors
235: */
236: public void setOutputDefs(Map outputDefs) {
237: if (outputDefs == null) {
238: throw new IllegalArgumentException(
239: "outputDefs cannot be null!");
240: }
241: _outputDefs = outputDefs;
242: }
243:
244: /**
245: * This method retrieves the Map of output IODescriptors.
246: *
247: * @return The Map of output IODescriptors
248: */
249: public Map getOutputDefs() {
250: return _outputDefs;
251: }
252:
253: /**
254: * This method returns the requested IODescriptor, null if not found.
255: */
256: public IODescriptor getOutputDef(String name) {
257: return (IODescriptor) _outputDefs.get(name);
258: }
259:
260: /**
261: * This method adds a Handler to the list of child handlers. Child
262: * Handlers are executed PRIOR to this handler executing.
263: *
264: * @param desc The Handler to add
265: */
266: public void addChildHandler(Handler desc) {
267: _childHandlers.add(desc);
268: }
269:
270: /**
271: * This method sets the List of child Handlers for this HandlerDefinition.
272: *
273: * @param childHandlers The List of child Handler objects
274: */
275: public void setChildHandlers(List childHandlers) {
276: if (childHandlers == null) {
277: throw new IllegalArgumentException(
278: "childHandlers cannot be null!");
279: }
280: _childHandlers = childHandlers;
281: }
282:
283: /**
284: * This method retrieves the List of child Handler.
285: *
286: * @return The List of child Handler for this handler.
287: */
288: public List getChildHandlers() {
289: return _childHandlers;
290: }
291:
292: public static final Class[] EVENT_ARGS = new Class[] { HandlerContext.class };
293:
294: private String _id = null;
295: private String _description = null;
296: private String _methodClass = null;
297: private String _methodName = null;
298: private transient Method _method = null;
299: private Map _inputDefs = new HashMap(5);
300: private Map _outputDefs = new HashMap(5);
301: private List _childHandlers = new ArrayList(5);
302: private transient Boolean _static = null;
303:
304: private static final long serialVersionUID = 0xA8B7C6D5E4F30211L;
305: }
|