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.UICommand;
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 extend
052: * {@link org.netbeans.modules.visualweb.web.ui.component.UICommand}, 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 ActionSourceDesignTimeRenderer 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 ActionSourceDesignTimeRenderer(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(UICommand component) {
085: return component.getValue() == null;
086: }
087:
088: public void encodeBegin(FacesContext context, UIComponent component)
089: throws IOException {
090: if (UICommand.class.isAssignableFrom(component.getClass())) {
091: ValueBinding valueBinding = component
092: .getValueBinding(VALUE_PROP); //NOI18N
093: UICommand command = (UICommand) component;
094: Object value = command.getValue();
095: if (valueBinding != null
096: && (value == null || value.toString().length() == 0)) {
097: Object dummyValue = getDummyData(context, valueBinding);
098: command.setValue(dummyValue);
099: isTextSet = true;
100: } else if (needsShadowText(command)) {
101: command.setValue(getShadowText());
102: String styleClass = (String) component.getAttributes()
103: .get(STYLE_CLASS_PROP);
104: component.getAttributes().put(
105: STYLE_CLASS_PROP,
106: addStyleClass(styleClass,
107: UNINITITIALIZED_STYLE_CLASS));
108: isTextSet = true;
109: isStyleSet = true;
110: }
111: }
112: super .encodeBegin(context, component);
113: }
114:
115: public void encodeEnd(FacesContext context, UIComponent component)
116: throws IOException {
117: super .encodeEnd(context, component);
118: if (isTextSet) {
119: UICommand command = (UICommand) component;
120: command.setValue(null);
121: isTextSet = false;
122: }
123: if (isStyleSet) {
124: String styleClass = (String) component.getAttributes().get(
125: STYLE_CLASS_PROP);
126: component.getAttributes().put(
127: STYLE_CLASS_PROP,
128: removeStyleClass(styleClass,
129: UNINITITIALIZED_STYLE_CLASS));
130: isStyleSet = false;
131: }
132: }
133:
134: }
|