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.PropertyDescriptor;
044: import javax.el.MethodExpression;
045:
046: import javax.faces.application.Application;
047: import javax.faces.context.FacesContext;
048: import javax.faces.el.MethodBinding;
049:
050: import com.sun.rave.designtime.Constants;
051: import com.sun.rave.designtime.markup.AttributeDescriptor;
052: import org.netbeans.modules.visualweb.extension.openide.util.Trace;
053: import org.netbeans.modules.visualweb.insync.java.Statement;
054: import org.w3c.dom.Attr;
055: import org.w3c.dom.Element;
056:
057: import com.sun.faces.util.ConstantMethodBinding;
058: import org.netbeans.modules.visualweb.insync.beans.Property;
059:
060: /**
061: * A source property setting that is persisted as an XML element attribute
062: */
063: public class MarkupProperty extends Property {
064:
065: public static final MarkupProperty[] EMPTY_ARRAY = {};
066:
067: protected final Element element; // the element in which this attr-based property will reside
068:
069: protected final AttributeDescriptor attributeDescriptor;
070:
071: protected Attr attr; // the attr holding this property when set, null when not
072:
073: //--------------------------------------------------------------------------------- Construction
074:
075: /**
076: * Construct a property bound to existing element attribute
077: *
078: * @param bean
079: * @param pd
080: * @param element
081: * @param attr
082: */
083: MarkupProperty(MarkupBean bean, PropertyDescriptor pd,
084: Element element, Attr attr) {
085: super (bean, pd, false);
086: attributeDescriptor = (AttributeDescriptor) pd
087: .getValue(Constants.PropertyDescriptor.ATTRIBUTE_DESCRIPTOR);
088: this .element = element;
089: this .attr = attr;
090: assert Trace.trace("insync.faces", "new bound MarkupProperty: "
091: + this );
092: }
093:
094: /**
095: * Create a property setting bound to a specific statement. Always defers to super since
096: * statement based properties are never markup based
097: *
098: * @param unit
099: * @param s
100: * @return the new bound property if bindable, else null
101: */
102: static Property newBoundInstance(FacesPageUnit unit, Statement s) {
103: return Property.newBoundInstance(unit, s);
104: }
105:
106: /**
107: * Construct a new property, creating the underlying tag attrs if needed
108: *
109: * @param beansUnit
110: */
111: MarkupProperty(MarkupBean bean, PropertyDescriptor pd,
112: Element element) {
113: super (bean, pd, false); // partial construction--no java statements created
114: attributeDescriptor = (AttributeDescriptor) pd
115: .getValue(Constants.PropertyDescriptor.ATTRIBUTE_DESCRIPTOR);
116: this .element = element;
117: String attribName = null;
118: if (attributeDescriptor != null) {
119: attribName = attributeDescriptor.getName();
120: } else {
121: //Use property name from descriptor if ATTRIBUTE_DESCRIPTOR is not available
122: attribName = pd.getName();
123: }
124: element.setAttribute(attribName, "");
125: this .attr = element.getAttributeNode(attribName);
126: assert Trace.trace("insync.faces", "new MarkupProperty: "
127: + this );
128: }
129:
130: /*
131: * @see org.netbeans.modules.visualweb.insync.beans.Property#getValue(java.lang.Class)
132: */
133: public Object getValue(Class type) {
134: if (attr == null)
135: return null;
136:
137: String value = attr.getValue();
138: if (value == null || type == String.class
139: || type == Object.class)
140: return value;
141: if (type == Boolean.class || type == Boolean.TYPE)
142: return Boolean.valueOf(value);
143: if (type == Character.class || type == Character.TYPE)
144: return new Character(value.length() > 0 ? value.charAt(0)
145: : ' ');
146: if (type == Byte.class || type == Byte.TYPE)
147: return Byte.valueOf(value);
148: if (type == Short.class || type == Short.TYPE)
149: return Short.valueOf(value);
150: if (type == Integer.class || type == Integer.TYPE)
151: return Integer.valueOf(value);
152: if (type == Long.class || type == Long.TYPE)
153: return Long.valueOf(value);
154: if (type == Float.class || type == Float.TYPE)
155: return Float.valueOf(value);
156: if (type == Double.class || type == Double.TYPE)
157: return Double.valueOf(value);
158: // EAT: Not sure how safe this is, will have to see
159: if (type == MethodBinding.class) {
160: if (value.startsWith("#{")) { //NOI18N
161: // This is not the right thing to do here, but its working
162: // original code stolen from MethodBindingPropertyEditor
163: if (getUnit() instanceof FacesPageUnit) {
164: FacesContext facesContext = ((FacesPageUnit) getUnit())
165: .getFacesContext();
166: Application app = facesContext.getApplication();
167: return app.createMethodBinding(value,
168: new Class[] {});
169: }
170: } else if (value.length() > 0) {
171: return new ConstantMethodBinding(value);
172: }
173: }
174: if (type == MethodExpression.class) {
175: if (value.startsWith("#{")) { //NOI18N
176: if (getUnit() instanceof FacesPageUnit) {
177: FacesContext facesContext = ((FacesPageUnit) getUnit())
178: .getFacesContext();
179: Application app = facesContext.getApplication();
180: return app.getExpressionFactory()
181: .createMethodExpression(
182: FacesContext.getCurrentInstance()
183: .getELContext(), value,
184: String.class, new Class[] {});
185: }
186: }
187: }
188: assert Trace.trace("insync.faces", "Unconvertable markup type:"
189: + type + " attr:" + attr.getName() + " value:" + value);
190: return null;
191: }
192:
193: /*
194: * @see org.netbeans.modules.visualweb.insync.beans.Property#isMarkupProperty()
195: */
196: public boolean isMarkupProperty() {
197: return true;
198: }
199:
200: /**
201: * Get the markup source text value of this property.
202: *
203: * @see org.netbeans.modules.visualweb.insync.beans.Property#getValueSource()
204: */
205: public String getValueSource() {
206: return attr != null ? attr.getValue() : "";
207: }
208:
209: /**
210: * Set the value of this property as markup source text (touching DOM only if needed)
211: *
212: * @see org.netbeans.modules.visualweb.insync.beans.Property#setValue(java.lang.Object, java.lang.String)
213: */
214: public void setValue(Object value, String valueSource) {
215: //System.err.println("FP.setValue: valueSource:" + valueSource + " to:" + attr);
216: if (!attr.getValue().equals(valueSource))
217: attr.setValue(valueSource);
218: }
219:
220: /**
221: * Remove this property's attr from the element
222: *
223: * @return true iff the source entry for this property was actually removed.
224: * @see org.netbeans.modules.visualweb.insync.beans.Property#removeEntry()
225: */
226: protected boolean removeEntry() {
227: if (super .removeEntry())
228: return true;
229: if (element != null && attr != null) {
230: element.removeAttributeNode(attr);
231: attr = null;
232: return true;
233: }
234: return false;
235: }
236:
237: /*
238: * @see org.netbeans.modules.visualweb.insync.beans.BeansNode#toString(java.lang.StringBuffer)
239: */
240: public void toString(StringBuffer sb) {
241: super.toString(sb);
242: }
243: }
|