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.web.ui.dt.renderer;
042:
043: import java.io.IOException;
044: import javax.faces.component.UIComponent;
045: import javax.faces.component.ValueHolder;
046: import javax.faces.context.FacesContext;
047: import javax.faces.el.ValueBinding;
048: import javax.faces.render.Renderer;
049:
050: /**
051: * A delegating renderer for components that implement
052: * {@link org.netbeans.modules.visualweb.web.ui.component.ValueHolder}, which does two things. If
053: * the value property is not set, temporarily sets the value and style to
054: * cause the component to be rendered with a default "shadow label". If the
055: * value property is bound, but the value is null or the empty string,
056: * temporarily sets the value to a "dummy string" that is keyed to the type
057: * of the value.
058: *
059: * @author gjmurphy
060: */
061: public abstract class ValueHolderDesignTimeRenderer extends
062: AbstractDesignTimeRenderer {
063:
064: protected static String STYLE_CLASS_PROP = "styleClass"; //NOI18N
065: protected static String VALUE_PROP = "value"; //NOI18N
066:
067: boolean isTextSet;
068: boolean isStyleSet;
069:
070: public ValueHolderDesignTimeRenderer(Renderer renderer) {
071: super (renderer);
072: }
073:
074: /**
075: * Returns a display string to set as the component's value if shadowed
076: * text is required.
077: */
078: protected abstract String getShadowText();
079:
080: /**
081: * Determines if shadowed text is required. Default implementation is
082: * simply to check for a null value.
083: */
084: protected boolean needsShadowText(ValueHolder component) {
085: return component.getValue() == null;
086: }
087:
088: public void encodeBegin(FacesContext context, UIComponent component)
089: throws IOException {
090: if (ValueHolder.class.isAssignableFrom(component.getClass())) {
091: ValueBinding valueBinding = component
092: .getValueBinding(VALUE_PROP); //NOI18N
093: ValueHolder valueHolder = (ValueHolder) component;
094: Object value = valueHolder.getValue();
095: if (valueBinding != null
096: && (value == null
097: || value.getClass().equals(Object.class) || value
098: .toString().length() == 0)) {
099: Object dummyValue = getDummyData(context, valueBinding);
100: valueHolder.setValue(dummyValue);
101: isTextSet = true;
102: } else if (needsShadowText(valueHolder)) {
103: valueHolder.setValue(getShadowText());
104: String styleClass = (String) component.getAttributes()
105: .get(STYLE_CLASS_PROP);
106: component.getAttributes().put(
107: STYLE_CLASS_PROP,
108: addStyleClass(styleClass,
109: UNINITITIALIZED_STYLE_CLASS));
110: isTextSet = true;
111: isStyleSet = true;
112: }
113: }
114: super .encodeBegin(context, component);
115: }
116:
117: public void encodeEnd(FacesContext context, UIComponent component)
118: throws IOException {
119: super .encodeEnd(context, component);
120: if (isTextSet) {
121: ValueHolder valueHolder = (ValueHolder) component;
122: valueHolder.setValue(null);
123: isTextSet = false;
124: }
125: if (isStyleSet) {
126: String styleClass = (String) component.getAttributes().get(
127: STYLE_CLASS_PROP);
128: component.getAttributes().put(
129: STYLE_CLASS_PROP,
130: removeStyleClass(styleClass,
131: UNINITITIALIZED_STYLE_CLASS));
132: isStyleSet = false;
133: }
134: }
135:
136: }
|