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.BeanInfo;
044: import java.beans.PropertyDescriptor;
045: import java.lang.reflect.Method;
046:
047: import org.openide.util.NbBundle;
048: import org.openide.ErrorManager;
049: import org.openide.awt.StatusDisplayer;
050:
051: import com.sun.rave.designtime.Constants;
052: import com.sun.rave.designtime.EventDescriptor;
053: import com.sun.rave.designtime.DesignInfo;
054: import com.sun.rave.designtime.DesignProperty;
055: import org.netbeans.modules.visualweb.insync.UndoEvent;
056: import org.netbeans.modules.visualweb.insync.beans.Bean;
057: import org.netbeans.modules.visualweb.insync.beans.Naming;
058: import org.netbeans.modules.visualweb.insync.faces.FacesBean;
059: import java.util.Iterator;
060:
061: /**
062: * A live bean implementation that uses a beans.Bean (or faces.FacesBean) to persist the bean's state.
063: * Knows about both Java and markup based persistance.
064: *
065: * @author Carl Quinn
066: * @version 1.0
067: */
068: public class BeansDesignBean extends SourceDesignBean {
069:
070: protected final Bean bean;
071:
072: /**
073: * @param unit
074: * @param beanInfo
075: * @param liveBeanInfo
076: * @param parent
077: * @param instance
078: * @param bean
079: */
080: public BeansDesignBean(LiveUnit unit, BeanInfo beanInfo,
081: DesignInfo liveBeanInfo, SourceDesignBean parent,
082: Object instance, Bean bean) {
083: super (unit, beanInfo, liveBeanInfo, parent, instance);
084: this .bean = bean;
085:
086: if (bean.isParentCapable()) {
087: setChildCapable();
088: }
089: }
090:
091: public Bean getBean() {
092: return bean;
093: }
094:
095: /**
096: *
097: */
098: public void invokeCleanupMethod() {
099: if (instance != null) {
100: String cmn = bean.getCleanupMethod();
101: if (cmn != null) {
102: try {
103: Method cm = instance.getClass().getMethod(cmn,
104: new Class[] {});
105: cm.invoke(instance);
106: } catch (Exception e) {
107: //e.printStackTrace();
108: }
109: }
110: }
111: }
112:
113: //------------------------------------------------------------------------------- SourceDesignBean
114:
115: public ClipImage getClipImage() {
116: ClipImage image = super .getClipImage();
117: image.facetName = getFacetName();
118: return image;
119: }
120:
121: /**
122: * @return true iff a property given by descriptor should be handled in markup
123: */
124: protected boolean isMarkupProperty(PropertyDescriptor pd) {
125: return bean.isMarkupProperty(pd);
126: }
127:
128: /*
129: * Create the right kind of Property handler depending on the type of the property
130: */
131: protected SourceDesignProperty newDesignProperty(
132: PropertyDescriptor pd) {
133: if (bean instanceof FacesBean && isMarkupProperty(pd)) {
134: if (MethodBindDesignProperty.isMethodBindingProperty(pd))
135: return new MethodBindDesignProperty(pd, this );
136: if (FacesDesignProperty.isBindingProperty(pd))
137: return new FacesDesignProperty(pd, this );
138: }
139: return new BeansDesignProperty(pd, this );
140: }
141:
142: /*
143: * Create the right kind of Event handler--a Java source based one or a MethodBound one
144: */
145: protected SourceDesignEvent newDesignEvent(EventDescriptor ed) {
146: if (bean instanceof FacesBean) {
147: Object pdO = ed.getEventSetDescriptor().getValue(
148: Constants.EventSetDescriptor.BINDING_PROPERTY);
149: if (pdO instanceof PropertyDescriptor
150: && MethodBindDesignProperty
151: .isMethodBindingProperty((PropertyDescriptor) pdO)) {
152: DesignProperty prop = getProperty(((PropertyDescriptor) pdO)
153: .getName());
154: if (prop instanceof MethodBindDesignProperty)
155: return new MethodBindDesignEvent(ed, this ,
156: (MethodBindDesignProperty) prop);
157: }
158: }
159: return new BeansDesignEvent(ed, this );
160: }
161:
162: public String getInstanceName() {
163: return bean.getName();
164: }
165:
166: public boolean canSetInstanceName() {
167: return bean.canSetName();
168: }
169:
170: public boolean setInstanceName(String name, boolean autoNumber) {
171: if (Naming.isJavaKeyWord(name)) {
172: String msg = NbBundle.getMessage(BeansDesignBean.class,
173: "IdIsJavaKeyWord", //NOI18N
174: new Object[] { name, getInstanceName() });
175: StatusDisplayer.getDefault().setStatusText(msg);
176: return false;
177: }
178:
179: // If asked to name the bean the existing name, just say we did
180: String beanname = bean.getName();
181: if (name != null && name.equals(beanname))
182: return true;
183:
184: // make sure we can get a good name
185: String goodName = Naming.makeValidIdentifier(name);
186: if (goodName == null)
187: return false;
188:
189: // Grab the old name for the instanceNameChanged event
190: String oldName = bean.getName();
191:
192: // now bang it in
193: UndoEvent event = null;
194: try {
195: String description = NbBundle.getMessage(
196: BeansDesignProperty.class, "SetInstanceName",
197: beanname); // NOI18N
198: event = unit.model.writeLock(description);
199: String oldname = bean.getName();
200: String newname = bean.setName(goodName, autoNumber, this );
201: if (newname == null)
202: return false; // oh, bad name. wasn't changed.
203: // fix event handler methods that are still in the form <oldname>_event
204: Iterator ei = (events != null) ? events.iterator() : null;
205: while (ei != null && ei.hasNext()) {
206: BeansDesignEvent bde = (BeansDesignEvent) ei.next();
207: String hname = bde.getHandlerName();
208: if (hname != null) {
209: int delim = hname.indexOf("_");
210: if (delim > 0) {
211: String nameLeft = hname.substring(0, delim);
212: String nameRight = hname.substring(delim + 1);
213: if (nameLeft.equals(oldname)) {
214: bde.setHandlerName(newname + "_"
215: + nameRight);
216: //Update the property sheet
217: fireDesignEventChanged(bde);
218: }
219: }
220: }
221: }
222: //org.netbeans.modules.visualweb.insync.java.Statement holds on to bean name because of bug #96387
223: //Until that bug is fixed, this is a workaround to fix #103122
224: Iterator pi = (properties != null) ? properties.iterator()
225: : null;
226: while (ei != null && pi.hasNext()) {
227: BeansDesignProperty bdp = (BeansDesignProperty) pi
228: .next();
229: if (bdp.property != null
230: && bdp.getDesignBean().getInstanceName()
231: .equals(newname)) {
232: bdp.property.setBeanName(newname);
233: }
234: }
235:
236: if (!newname.equals(oldname)) {
237: String scope = unit.getBeansUnit().getBeanName() + ".";
238: unit.model.updateAllBeanElReferences(scope + oldname,
239: scope + newname);
240: }
241: } finally {
242: unit.model.writeUnlock(event);
243: }
244:
245: fireDesignBeanInstanceNameChanged(oldName);
246: fireDesignBeanChanged();
247: return true;
248: }
249:
250: /**
251: * @return
252: */
253: public String getFacetName() {
254: return (bean instanceof FacesBean) ? ((FacesBean) bean)
255: .getFacetName() : null;
256: }
257:
258: //--------------------------------------------------------------------------------------- Object
259:
260: public void toString(StringBuffer sb) {
261: sb.append(" instanceName:");
262: sb.append(getInstanceName());
263: super.toString(sb);
264: }
265: }
|