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 org.netbeans.modules.visualweb.insync.beans;
042:
043: import org.netbeans.modules.visualweb.insync.java.DelegatorMethod;
044: import org.netbeans.modules.visualweb.insync.java.EventMethod;
045: import org.netbeans.modules.visualweb.insync.java.JavaClass;
046: import java.beans.MethodDescriptor;
047:
048: import org.netbeans.modules.visualweb.extension.openide.util.Trace;
049: import java.util.List;
050:
051: /**
052: * Representation of a wiring for a single event handler within an EventSet's listener adapter in
053: * the init block.
054: *
055: * @author cquinn
056: */
057: public class Event extends BeansNode {
058:
059: public static final Event[] EMPTY_ARRAY = {};
060:
061: // General event fields
062: protected final EventSet set; // our parent event set
063: final MethodDescriptor descriptor;
064: protected String defaultBody;
065: protected String[] parameterNames;
066: protected String[] requiredImports;
067:
068: // Java source-based event fields
069: protected DelegatorMethod delegator;
070: protected EventMethod handler;
071: private String name;
072:
073: //--------------------------------------------------------------------------------- Construction
074:
075: /**
076: * Partially construct an event to be fully populated later.
077: *
078: * @param set The EventSet that this event is a member of.
079: * @param descriptor The MethodDescriptor that defines the hendler method signature+
080: */
081: protected Event(EventSet set, MethodDescriptor descriptor) {
082: super (set.getUnit());
083: this .set = set;
084: this .descriptor = descriptor;
085: }
086:
087: /**
088: * Construct an event bound to existing method & its bean. Called only from factory below.
089: *
090: * @param set The EventSet that this event is a member of.
091: * @param descriptor The MethodDescriptor that defines the handler method signature+.
092: * @param delegator The adaptor method that delegates to our handler.
093: * @param call The Apply expression in the delegator that calls our handler.
094: * @param handler Our handler method that is populated by the user.
095: */
096: private Event(EventSet set, MethodDescriptor descriptor,
097: DelegatorMethod delegator,
098: Object/*MethodInvocationTree*/call, EventMethod handler) {
099: this (set, descriptor);
100: this .delegator = delegator;
101: this .handler = handler;
102: assert Trace
103: .trace("insync.beans", "E new bound Event: " + this );
104: }
105:
106: /**
107: * Create an event bound to a specific statement adapter method.
108: *
109: * new adapter() {
110: * void delegateMethod(ActionEvent ae) {
111: * handlerCall(ae);
112: * }
113: * ...
114: * }
115: *
116: * @param set The EventSet that this event is a member of.
117: * @param md The MethodDescriptor that defines the handler method signature+.
118: * @param am The adapter
119: * @return the new bound property if bindable, else null
120: */
121: static Event newBoundInstance(EventSet set, MethodDescriptor md,
122: DelegatorMethod method) {
123: String mname = method.getDelegateName();
124: if (mname != null) {
125: EventMethod handler = set.getUnit().getThisClass()
126: .getEventMethod(mname,
127: md.getMethod().getParameterTypes());
128: return new Event(set, md, method, null, handler);
129: }
130:
131: return null;
132: }
133:
134: /**
135: * Construct a new Event, creating the underlying delegating method & body, and handler method
136: *
137: * @param set The EventSet that this event is a member of.
138: * @param descriptor The MethodDescriptor that defines the handler method signature+.
139: * @param handler Our handler method that is populated by the user.
140: */
141: Event(EventSet set, MethodDescriptor descriptor, String name) {
142: this (set, descriptor);
143: this .name = name;
144: assert Trace.trace("insync.beans", "E new created Event: "
145: + this );
146: }
147:
148: /**
149: * Set the default body to be used for this event when it is created.
150: * If not set, a generic comment will be used.
151: * @param defaultBody The Java source for the method body
152: */
153: public void setDefaultBody(String defaultBody) {
154: this .defaultBody = defaultBody;
155: }
156:
157: /**
158: * Set the parameter names to be used when initially creating this
159: * event. It has no effect if the event already exists.
160: * @param parameterNames An array of names to be assigned to the
161: * parameters. The array must have at least as many names as there
162: * are arguments in the method.
163: */
164: public void setParameterNames(String[] parameterNames) {
165: this .parameterNames = parameterNames;
166: }
167:
168: /**
169: * Set the list of required imports requested by this event.
170: * @param An array of fully qualified class names that should be
171: * imported when an event handler for this event is created
172: */
173: public void setRequiredImports(String[] requiredImports) {
174: this .requiredImports = requiredImports;
175: }
176:
177: /**
178: * Wire up the delegation for this event to the handler method, inserting the methods and
179: * statements as needed.
180: *
181: * @param delegate name of delegate, pass null to use a tmp name & set later
182: */
183: protected void insertEntry() {
184: Class[] pTypes = descriptor.getMethod().getParameterTypes();
185: String[] pNames = Naming.paramNames(pTypes, descriptor
186: .getParameterDescriptors());
187: boolean noreturn = false;
188: if (descriptor.getMethod().getReturnType().getName().equals(
189: "void")) {
190: noreturn = true;
191: }
192: set.getDelegatorMethod(descriptor).addDelegateStatement(name,
193: pNames, noreturn);
194: }
195:
196: /**
197: * Remove this event's delegation method from the adapter. This event instance is dead & should
198: * not be used.
199: *
200: * @return true iff the source entry for this event was actually removed.
201: */
202: protected boolean removeEntry() {
203: JavaClass adapter = set.getAdapter();
204: if (delegator != null) {
205: set.removeDelegatorMethod(delegator);
206: delegator = null;
207: return true;
208: }
209: return false;
210: }
211:
212: //------------------------------------------------------------------------------------ Accessors
213:
214: /**
215: * Get the (method) descriptor for this event
216: *
217: * @return
218: */
219: public MethodDescriptor getDescriptor() {
220: return descriptor;
221: }
222:
223: /**
224: * Get the owning event set for this event.
225: *
226: * @return the owning event set.
227: */
228: public EventSet getEventSet() {
229: return set;
230: }
231:
232: /**
233: * Get the name of this event
234: *
235: * @return The name of this event.
236: */
237: public String getName() {
238: return descriptor.getName();
239: }
240:
241: /**
242: * Set the name of the handler method for this event
243: *
244: * @param name The new handler method name.
245: */
246: public void setHandler() {
247: setHandler(name);
248: }
249:
250: /**
251: * Set the name of the handler method for this event
252: *
253: * @param name The new handler method name.
254: */
255: public void setHandler(String name) {
256: if (handler != null) {
257: if (!getUnit().hasEventMethod(descriptor, name)) {
258: //rename existing method
259: handler.rename(name);
260: //re-initialize handler
261: Class[] params = descriptor.getMethod()
262: .getParameterTypes();
263: handler = handler.getJavaClass().getEventMethod(name,
264: params);
265: }
266: } else {
267: handler = getUnit().ensureEventMethod(descriptor, name,
268: defaultBody, parameterNames, requiredImports);
269: }
270: if (delegator != null) {
271: delegator.setDelegateName(name);
272: }
273: }
274:
275: /**
276: * Get the name of the handler method for this event.
277: * @return The handler method name.
278: */
279: public String getHandlerName() {
280: if (handler != null) {
281: return handler.getName();
282: } else if (delegator != null) {
283: return delegator.getDelegateName();
284: }
285:
286: return name;
287: }
288:
289: /**
290: * Get the handler method for this event.
291: *
292: * @return The handler method itself.
293: */
294: public EventMethod getHandlerMethod() {
295: return handler;
296: }
297:
298: //--------------------------------------------------------------------------------------- Object
299:
300: /*
301: * @see org.netbeans.modules.visualweb.insync.beans.BeansNode#toString(java.lang.StringBuffer)
302: */
303: public void toString(StringBuffer sb) {
304: sb.append(" name:");
305: sb.append(getName());
306: sb.append("=>");
307: sb.append(getHandlerName());
308: }
309: }
|