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.live;
042:
043: import java.beans.EventSetDescriptor;
044: import java.beans.MethodDescriptor;
045: import java.beans.PropertyDescriptor;
046: import java.lang.reflect.Method;
047: import javax.el.MethodExpression;
048:
049: import javax.faces.application.Application;
050: import javax.faces.component.UIInput;
051: import javax.faces.context.FacesContext;
052: import javax.faces.el.MethodBinding;
053: import javax.faces.event.ActionEvent;
054: import javax.faces.event.ActionListener;
055: import javax.faces.event.ValueChangeEvent;
056: import javax.faces.event.ValueChangeListener;
057: import javax.faces.validator.Validator;
058:
059: import org.netbeans.modules.visualweb.extension.openide.util.Trace;
060:
061: import com.sun.rave.designtime.EventDescriptor;
062: import org.netbeans.modules.visualweb.insync.faces.FacesPageUnit;
063:
064: /**
065: * A pseudo-event that encapsulates an EL MethodBinding and is implemented on top of a property
066: * modeled with MethodBindDesignProperty. Does not use beans.Event, nor the faces.ReflectedEvent*
067: * classes.
068: *
069: * NOTE: this is OBSOLETE now in favor of using the true beans.Event / faces.FacesEvent classes
070: *
071: * @author Carl Quinn
072: */
073: public class FacesDesignEvent extends SourceDesignEvent {
074:
075: MethodBindDesignProperty reference; // the persisted event state if set, i.e. not default, null if not set
076:
077: static FacesDesignEvent newInstance(BeansDesignBean bean,
078: MethodBindDesignProperty p) {
079: PropertyDescriptor pd = p.getPropertyDescriptor();
080: String name = pd.getName();
081: EventDescriptor ed = null;
082: try {
083: if (name.equals("action")) {
084: Method m = Method.class.getDeclaredMethod("getName",
085: new Class[] {});
086: ed = newBoundEventDescriptor("action", m);
087: } else if (name.equals("actionListener")) {
088: Method m = ActionListener.class.getDeclaredMethod(
089: "processAction",
090: new Class[] { ActionEvent.class });
091: ed = newBoundEventDescriptor("actionListener", m);
092: } else if (name.equals("validator")) {
093: Method m = Validator.class.getDeclaredMethod(
094: "validate", new Class[] { FacesContext.class,
095: UIInput.class });
096: ed = newBoundEventDescriptor("validator", m);
097: } else if (name.equals("valueChangeListener")) {
098: Method m = ValueChangeListener.class.getDeclaredMethod(
099: "processValueChange",
100: new Class[] { ValueChangeEvent.class });
101: ed = newBoundEventDescriptor("valueChangeListener", m);
102: }
103: } catch (NoSuchMethodException e) {
104: }
105: return ed != null ? new FacesDesignEvent(ed, bean, p) : null;
106: }
107:
108: static EventDescriptor newBoundEventDescriptor(String name, Method m) {
109: MethodDescriptor md = new MethodDescriptor(m);
110: md.setName(name);
111: md.setDisplayName(name + "#{}");
112: //md.setShortDescription();
113: return new EventDescriptor(null, md);
114: }
115:
116: static EventDescriptor newDescriptor(EventSetDescriptor esd) {
117: MethodDescriptor md = new MethodDescriptor(esd
118: .getListenerMethods()[0]);
119: md.setName(esd.getName());
120: md.setDisplayName(esd.getDisplayName());
121: md.setShortDescription(esd.getShortDescription());
122: return new EventDescriptor(esd, md);
123: }
124:
125: /**
126: * @param esd
127: * @param bean
128: * @param reference
129: */
130: public FacesDesignEvent(EventSetDescriptor esd,
131: BeansDesignBean bean, MethodBindDesignProperty reference) {
132: super (newDescriptor(esd), bean);
133: this .reference = reference;
134: assert Trace.trace("insync.live", "FLE event:" + reference);
135: }
136:
137: /**
138: * @param ed
139: * @param bean
140: * @param reference
141: */
142: public FacesDesignEvent(EventDescriptor ed, BeansDesignBean bean,
143: MethodBindDesignProperty reference) {
144: super (ed, bean);
145: this .reference = reference;
146: assert Trace.trace("insync.live", "FLE event:" + reference);
147: }
148:
149: String scopePrefixed(String methodName) {
150: return liveBean.unit.rootContainer.getInstanceName() + "."
151: + methodName;
152: }
153:
154: String scopeStripped(String refName) {
155: return refName.substring(refName.indexOf('.') + 1);
156: }
157:
158: /*
159: * Get the unqualified event handler method name
160: * @see com.sun.rave.designtime.DesignEvent#getHandlerName()
161: */
162: public String getHandlerName() {
163: String binding = null;
164: if (reference.getValue() instanceof MethodBinding) {
165: MethodBinding mb = (MethodBinding) reference.getValue();
166: if (mb != null) {
167: binding = mb.getExpressionString();
168: }
169: } else if (reference.getValue() instanceof MethodExpression) {
170: MethodExpression me = (MethodExpression) reference
171: .getValue();
172: if (me != null) {
173: binding = me.getExpressionString();
174: }
175: }
176: if (binding == null)
177: return null;
178: if (binding.startsWith("#{"))
179: binding = binding.substring(2, binding.length() - 1);
180: return scopeStripped(binding);
181: }
182:
183: /*
184: * Set the event handler method as an unqualified name string
185: * @see com.sun.rave.designtime.DesignEvent#getHandlerName()
186: */
187: public boolean setHandlerName(String methodName) {
188: if (methodName == null)
189: methodName = getDefaultHandlerName();
190:
191: String binding = "#{" + scopePrefixed(methodName) + "}";
192: Application app = ((FacesPageUnit) liveBean.unit.sourceUnit)
193: .getFacesApplication();
194: MethodBinding mb = app.createMethodBinding(binding,
195: new Class[] {});
196: reference.setValue(mb);
197:
198: MethodDescriptor lmd = descriptor.getListenerMethodDescriptor();
199: //!CQ move this into the Event impl
200: //!TN Do we need the default body here? Doesn't seem like this could be called
201: // before Event.setHandler anyway
202: // XXX YES: Here's how you reproduce: Double click on a button to
203: // generate the specialized event handler: then delete this in source,
204: // then go back and double click again. You now get the generic
205: // handler because it comes through this interface
206: liveBean.unit.sourceUnit.ensureEventMethod(lmd, methodName,
207: null, null, null);
208:
209: return true;
210: }
211:
212: public boolean isHandled() {
213: return reference.isModified();
214: }
215:
216: public boolean removeHandler() {
217: return reference.unset();
218: }
219:
220: //!JOE - this is just a stub! This needs to bang in the source code to the handler method
221: String handlerMethodBody;
222:
223: public void setHandlerMethodSource(String methodBody)
224: throws IllegalArgumentException {
225: this .handlerMethodBody = methodBody;
226: }
227:
228: public String getHandlerMethodSource() {
229: return handlerMethodBody;
230: }
231: }
|