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 com.sun.rave.web.ui.component.Field;
044: import com.sun.rave.web.ui.component.Icon;
045: import org.netbeans.modules.visualweb.web.ui.dt.component.util.DesignMessageUtil;
046: import com.sun.rave.web.ui.theme.Theme;
047: import com.sun.rave.web.ui.theme.ThemeImages;
048: import com.sun.rave.web.ui.util.ConversionUtilities;
049: import com.sun.rave.web.ui.util.RenderingUtilities;
050: import com.sun.rave.web.ui.util.ThemeUtilities;
051: import java.io.IOException;
052: import javax.faces.render.Renderer;
053: import javax.faces.component.UIComponent;
054: import javax.faces.context.FacesContext;
055: import javax.faces.context.ResponseWriter;
056: import javax.faces.render.Renderer;
057:
058: /**
059: * A delegating renderer for components based on {@link org.netbeans.modules.visualweb.web.ui.component.Field}.
060: * This delegating renderer takes over when the component is read-only, since the
061: * field renderers replace the field component with a proxy component when the
062: * <code>readOnly</code> property is true, which makes the resulting HTML unselectable
063: * on the designer. This delegating renderer also provides a shadow text value
064: * when the component is read-only and there is no text value.
065: *
066: * @author gjmurphy
067: */
068: public class FieldDesignTimeRenderer extends AbstractDesignTimeRenderer {
069:
070: /** Creates a new instance of FieldDesignTimeRenderer */
071: public FieldDesignTimeRenderer(Renderer renderer) {
072: super (renderer);
073: }
074:
075: protected String getShadowText(FacesContext context, Field field) {
076: return DesignMessageUtil.getMessage(
077: FieldDesignTimeRenderer.class, "field.readOnly.value");
078: }
079:
080: public void encodeBegin(FacesContext context, UIComponent component)
081: throws IOException {
082: Field field = (Field) component;
083: if (field.isReadOnly()) {
084: ResponseWriter writer = context.getResponseWriter();
085: Object value = field.getText();
086: writer.startElement("span", field); // NOI18N
087: writer.writeAttribute("id", field.getId(), "id"); //NOI18N
088: String style = field.getStyle();
089: if (style != null && style.length() > 0)
090: writer.writeAttribute("style", style, null); //NOI18N
091: String styleClass = field.getStyleClass();
092: StringBuffer styleClassBuffer = new StringBuffer();
093: if (styleClass != null)
094: styleClassBuffer.append(styleClass);
095: UIComponent label = field.getLabelComponent(context, "");
096: if (label != null) {
097: writer.writeAttribute("class", styleClassBuffer
098: .toString(), null); // NOI18N
099: styleClassBuffer.setLength(0);
100: RenderingUtilities.renderComponent(label, context);
101: Theme theme = ThemeUtilities.getTheme(context);
102: Icon icon = theme.getIcon(ThemeImages.DOT);
103: icon.setId(component.getId().concat("_spacer")); //NOI18N
104: icon.setHeight(1);
105: icon.setWidth(10);
106: RenderingUtilities.renderComponent(icon, context);
107: writer.startElement("span", field); // NOI18N
108: writer.writeAttribute("id", field.getId().concat(
109: "_readOnly"), "id"); //NOI18N
110: }
111: if (value == null) {
112: if (styleClassBuffer.length() > 0)
113: styleClassBuffer.append(' ');
114: styleClassBuffer.append(UNINITITIALIZED_STYLE_CLASS);
115: writer.writeAttribute("class", styleClassBuffer
116: .toString(), null); // NOI18N
117: writer.writeText(getShadowText(context, field), null); // NOI18N
118: } else {
119: writer.writeAttribute("class", styleClassBuffer
120: .toString(), null); // NOI18N
121: writer.writeText(ConversionUtilities
122: .convertValueToString(field, value), null);
123: }
124: writer.endElement("span"); // NOI18N
125: } else {
126: super .encodeEnd(context, component);
127: }
128: }
129:
130: public void encodeEnd(FacesContext context, UIComponent component)
131: throws IOException {
132: Field field = (Field) component;
133: if (!field.isReadOnly())
134: super.encodeBegin(context, component);
135: }
136:
137: }
|