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.propertyeditors.binding.nodes;
042:
043: import java.beans.BeanInfo;
044: import java.beans.Introspector;
045: import java.beans.PropertyDescriptor;
046: import java.util.ArrayList;
047: import java.awt.Image;
048: import javax.swing.Icon;
049: import javax.swing.ImageIcon;
050: import com.sun.rave.designtime.DesignBean;
051: import com.sun.rave.designtime.faces.FacesDesignContext;
052: import com.sun.rave.designtime.markup.MarkupDesignBean;
053: import org.netbeans.modules.visualweb.propertyeditors.binding.BindingTargetNode;
054: import org.netbeans.modules.visualweb.propertyeditors.binding.BindingTargetPanel;
055: import org.netbeans.modules.visualweb.propertyeditors.binding.PropertyBindingHelper;
056: import org.netbeans.modules.visualweb.propertyeditors.util.Bundle;
057:
058: public class PropertyTargetNode extends BindingTargetNode {
059: private static Bundle bundle = Bundle
060: .getBundle(PropertyTargetNode.class);
061:
062: public PropertyTargetNode(BindingTargetNode parent,
063: DesignBean bean, PropertyDescriptor[] propPath,
064: Object propInstance) {
065: super (parent);
066: this .bean = bean;
067: this .propPath = propPath;
068: this .propInstance = propInstance;
069: this .displayTextEnabled = getDisplayText(true);
070: this .displayTextDisabled = getDisplayText(false);
071: }
072:
073: protected DesignBean bean;
074:
075: public DesignBean getBean() {
076: return bean;
077: }
078:
079: protected PropertyDescriptor[] propPath;
080:
081: public PropertyDescriptor[] getPropPath() {
082: return propPath;
083: }
084:
085: protected Object propInstance;
086:
087: public Object getPropInstance() {
088: return propInstance;
089: }
090:
091: public boolean lazyLoad() {
092: lazyLoadCustomTargetNodes();
093: if (propPath == null) {
094: lazyLoadBeanTargetNodes();
095: }
096: if (isValidBindingTarget()) {
097: lazyLoadPropertyTargetNodes();
098: }
099: return true;
100: }
101:
102: public void lazyLoadCustomTargetNodes() {
103: // subclasses can put their stuff here
104: }
105:
106: public void lazyLoadBeanTargetNodes() {
107: if (bean.isContainer()) {
108: DesignBean[] kids = bean.getChildBeans();
109: for (int i = 0; kids != null && i < kids.length; i++) {
110: super .add(_createTargetNode(this , kids[i], null,
111: kids[i].getInstance()));
112: }
113: }
114: }
115:
116: public void lazyLoadPropertyTargetNodes() {
117: try {
118: BeanInfo bi = Introspector
119: .getBeanInfo(getTargetTypeClass());
120: PropertyDescriptor[] pds = bi.getPropertyDescriptors();
121: for (int i = 0; pds != null && i < pds.length; i++) {
122: if ((pds[i].getReadMethod() != null)
123: && !pds[i].getReadMethod().getName().equals(
124: "getClass")) {
125: ArrayList pdList = new ArrayList();
126: for (int j = 0; propPath != null
127: && j < propPath.length; j++) {
128: pdList.add(propPath[j]);
129: }
130: pdList.add(pds[i]);
131: PropertyDescriptor[] pda = (PropertyDescriptor[]) pdList
132: .toArray(new PropertyDescriptor[pdList
133: .size()]);
134: BindingTargetNode btn = _createTargetNode(this ,
135: bean, pda, null);
136: super .add(btn);
137: }
138: }
139: } catch (Exception x) {
140: x.printStackTrace();
141: }
142: }
143:
144: protected String displayTextEnabled;
145: protected String displayTextDisabled;
146:
147: public String getDisplayText(boolean enableNode) {
148: if (enableNode && displayTextEnabled != null) {
149: return displayTextEnabled;
150: } else if (!enableNode && displayTextDisabled != null) {
151: return displayTextDisabled;
152: }
153: PropertyDescriptor pd = (propPath != null && propPath.length > 0) ? propPath[propPath.length - 1]
154: : null;
155: StringBuffer sb = new StringBuffer();
156: sb.append("<html>"); //NOI18N
157: if (!enableNode) {
158: sb.append("<font color=\"gray\">"); //NOI18N
159: }
160: if (pd != null) {
161: sb.append(bundle.getMessage("property")); //NOI18N
162: sb.append(" "); //NOI18N
163: }
164: if (enableNode) {
165: sb.append("<b>"); //NOI18N
166: }
167: if (pd != null) {
168: sb.append(pd.getName());
169: } else {
170: sb.append(bean.getInstanceName());
171: }
172: if (enableNode) {
173: sb.append("</b>"); //NOI18N
174: }
175: sb.append(" <font><i>"); //NOI18N
176: sb.append(getTargetTypeDisplayName());
177: sb.append("</i></font>"); //NOI18N
178: if (!enableNode) {
179: sb.append("</font>"); //NOI18N
180: }
181: sb.append("</html>"); //NOI18N
182: return sb.toString();
183: }
184:
185: public boolean isValidBindingTarget() {
186: if (propPath == null
187: && bean.getDesignContext() instanceof FacesDesignContext) {
188: return ((FacesDesignContext) bean.getDesignContext())
189: .isValidBindingTarget(bean);
190: }
191: return true;
192: }
193:
194: public String getBindingExpressionPart() {
195: if (propPath != null && propPath.length > 0) {
196: return propPath[propPath.length - 1].getName();
197: }
198: return bean.getInstanceName();
199: }
200:
201: public Class getTargetTypeClass() {
202: if (propInstance == null) {
203: propInstance = PropertyBindingHelper.getPropInstance(bean,
204: propPath);
205: }
206: if (propInstance != null) {
207: if (!propInstance.getClass().isPrimitive()) {
208: return propInstance.getClass();
209: }
210: }
211: return propPath != null && propPath.length > 0 ? propPath[propPath.length - 1]
212: .getPropertyType()
213: : bean.getInstance() != null ? bean.getInstance()
214: .getClass() : bean.getBeanInfo() != null ? bean
215: .getBeanInfo().getBeanDescriptor()
216: .getBeanClass() : null;
217: }
218:
219: boolean iconChecked = false;
220:
221: public boolean hasDisplayIcon() {
222: if (!iconChecked) {
223: displayIcon = getDisplayIcon(true);
224: iconChecked = true;
225: }
226: return displayIcon != null;
227: }
228:
229: Icon displayIcon = null;
230:
231: public Icon getDisplayIcon(boolean enableNode) {
232: if (displayIcon == null) {
233: if (propInstance == null) {
234: propInstance = PropertyBindingHelper.getPropInstance(
235: bean, propPath);
236: }
237: if (propInstance != null) {
238: try {
239: BeanInfo bi = Introspector.getBeanInfo(propInstance
240: .getClass());
241: Image img = bi.getIcon(BeanInfo.ICON_COLOR_16x16);
242: if (img != null) {
243: displayIcon = new ImageIcon(img);
244: }
245: } catch (Exception x) {
246: }
247: if (displayIcon == null
248: && (propPath == null || propPath.length == 0)) {
249: if (bean instanceof MarkupDesignBean
250: && ((MarkupDesignBean) bean).getElement() != null) {
251: displayIcon = BindingTargetPanel.TAG_ICON;
252: }
253: }
254: }
255: }
256: if (displayIcon == null/* && propPath == null*/) {
257: displayIcon = BindingTargetPanel.BEAN_ICON;
258: }
259: return displayIcon;
260: }
261: }
|