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.faces;
042:
043: import java.beans.EventSetDescriptor;
044: import java.beans.MethodDescriptor;
045: import java.beans.PropertyDescriptor;
046: import javax.el.MethodExpression;
047:
048: import javax.faces.el.MethodBinding;
049:
050: import org.netbeans.modules.visualweb.extension.openide.util.Trace;
051:
052: import org.netbeans.modules.visualweb.insync.beans.Event;
053: import org.netbeans.modules.visualweb.insync.beans.EventSet;
054: import org.netbeans.modules.visualweb.insync.beans.Property;
055:
056: /**
057: * An EventSet that
058: */
059: public class MethodBindEventSet extends EventSet {
060:
061: MarkupProperty bindingProperty; // the MarkupProperty (attr) that defines our event wiring
062: MethodBindEvent event; // The Event instance that represents the wiring
063:
064: /**
065: * Determins if a given property can and does represent a method binding.
066: *
067: * @param p The property to check.
068: * @return True iff the property can and does represent a method binding.
069: */
070: static boolean isMethodBindProperty(Property p) {
071: PropertyDescriptor pd = p.getDescriptor();
072: if (MethodBinding.class.isAssignableFrom(pd.getPropertyType())
073: || MethodExpression.class.isAssignableFrom(pd
074: .getPropertyType())) {
075: String bind = p.getValueSource();
076: if (bind.startsWith("#{") && bind.endsWith("}"))
077: return bind.indexOf('.', 2) >= 0;
078: }
079: return false;
080: }
081:
082: //--------------------------------------------------------------------------------- Construction
083:
084: /**
085: * Construct an MethodBindEventSet bound to an existing specific method binding MarkupProperty
086: *
087: * @param bean
088: * @param descriptor
089: * @param bindingProperty
090: */
091: protected MethodBindEventSet(FacesBean bean,
092: EventSetDescriptor descriptor,
093: MarkupProperty bindingProperty) {
094: super (bean, descriptor); // won't try to bind
095: this .bindingProperty = bindingProperty;
096:
097: bindEvents();
098:
099: assert Trace.trace("insync.faces",
100: "MBES new bound MethodBindEventSet: " + this );
101: }
102:
103: /**
104: * Construct a new MethodBindEventSet, creating the underlying statement methods
105: *
106: * @param bean
107: * @param name
108: */
109: MethodBindEventSet(FacesBean bean, EventSetDescriptor descriptor,
110: PropertyDescriptor pd) {
111: super (bean, descriptor);
112: bindingProperty = (MarkupProperty) bean.setProperty(pd
113: .getName(), null, "");
114: bindEvents();
115: assert Trace.trace("insync.beans",
116: "MBES new created MethodBindEventSet: " + this );
117: }
118:
119: /**
120: * Called when the property has changed, decoupling from this event & we just need to clean up.
121: * @see org.netbeans.modules.visualweb.insync.beans.EventSet#releaseEntry()
122: */
123: public void releaseEntry() {
124: if (bindingProperty != null) {
125: event = null;
126: bindingProperty = null;
127: }
128: }
129:
130: /**
131: * @see org.netbeans.modules.visualweb.insync.beans.EventSet#insertEntry()
132: */
133: public void insertEntry() {
134: }
135:
136: /*
137: * @see org.netbeans.modules.visualweb.insync.beans.EventSet#removeEntry()
138: */
139: public boolean removeEntry() {
140: assert Trace.trace("insync.beans", "MBES.removeEntry: " + this );
141: if (bindingProperty != null) {
142: event = null;
143: bean.unsetProperty(bindingProperty);
144: bindingProperty = null;
145: return true;
146: }
147: return false;
148: }
149:
150: //--------------------------------------------------------------------------------------- Events
151:
152: /**
153: * This set always contains exactly one event which we already know about.
154: * @see org.netbeans.modules.visualweb.insync.beans.EventSet#newCreatedEvent(java.beans.MethodDescriptor, java.lang.String)
155: */
156: protected Event newCreatedEvent(MethodDescriptor md, String value) {
157: return null;
158: }
159:
160: /**
161: * Bind our always-present, exactly-one Event
162: * @see org.netbeans.modules.visualweb.insync.beans.EventSet#bindEvents()
163: */
164: protected void bindEvents() {
165: MethodDescriptor[] lmds = descriptor
166: .getListenerMethodDescriptors();
167: event = new MethodBindEvent(this , lmds[0]);
168: events.add(event);
169: }
170:
171: //--------------------------------------------------------------------------------------- Object
172:
173: /*
174: * @see org.netbeans.modules.visualweb.insync.beans.BeansNode#toString(java.lang.StringBuffer)
175: */
176: public void toString(StringBuffer sb) {
177: sb.append(" bindingProperty:");
178: sb.append(bindingProperty);
179: super.toString(sb);
180: }
181: }
|