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.faces.dt.renderer;
042:
043: import java.io.IOException;
044: import javax.faces.component.UIComponent;
045: import javax.faces.context.FacesContext;
046: import javax.faces.convert.ConverterException;
047: import javax.faces.el.EvaluationException;
048: import javax.faces.el.ValueBinding;
049: import javax.faces.render.Renderer;
050:
051: /**
052: * Base class for design-time renderers that may delegate most rendering
053: * operations to their corresponding run-time renderer.
054: *
055: * @author gjmurphy
056: */
057: public class AbstractDesignTimeRenderer extends Renderer {
058:
059: protected static String UNINITITIALIZED_STYLE_CLASS = "rave-uninitialized-text";
060: protected static String BORDER_STYLE_CLASS = "rave-design-border";
061:
062: // Delagatee renderer
063: protected Renderer renderer;
064:
065: public AbstractDesignTimeRenderer(Renderer renderer) {
066: this .renderer = renderer;
067: }
068:
069: public Object getConvertedValue(FacesContext context,
070: UIComponent component, Object submittedValue)
071: throws ConverterException {
072: return this .renderer.getConvertedValue(context, component,
073: submittedValue);
074: }
075:
076: public void encodeEnd(FacesContext context, UIComponent component)
077: throws IOException {
078: this .renderer.encodeEnd(context, component);
079: }
080:
081: public void encodeChildren(FacesContext context,
082: UIComponent component) throws IOException {
083: this .renderer.encodeChildren(context, component);
084: }
085:
086: public void encodeBegin(FacesContext context, UIComponent component)
087: throws IOException {
088: this .renderer.encodeBegin(context, component);
089: }
090:
091: public void decode(FacesContext context, UIComponent component) {
092: this .renderer.decode(context, component);
093: }
094:
095: public String convertClientId(FacesContext context, String clientId) {
096: return this .renderer.convertClientId(context, clientId);
097: }
098:
099: public boolean getRendersChildren() {
100: return this .renderer.getRendersChildren();
101: }
102:
103: protected static String addStyleClass(String value,
104: String styleClass) {
105: if (value == null) {
106: return styleClass;
107: } else if (value.indexOf(styleClass) >= 0) {
108: return value;
109: } else {
110: return value + " " + styleClass;
111: }
112: }
113:
114: protected static String removeStyleClass(String value,
115: String styleClass) {
116: if (value == null || value.indexOf(styleClass) == -1)
117: return value;
118: int i = value.indexOf(styleClass);
119: while (i > 0 && Character.isSpaceChar(value.charAt(i)))
120: i--;
121: return value.substring(0, i)
122: + value.substring(i + styleClass.length());
123: }
124:
125: protected static Object getDummyData(FacesContext context,
126: ValueBinding vb) {
127: Class type = null;
128: try {
129: type = vb.getType(context);
130: } catch (EvaluationException e) {
131: // FIXME - workaround for [6371691] that should be reconsidered,
132: // as swallowing exceptions is not a good general practice
133: type = Object.class;
134: }
135: return getDummyData(type);
136: }
137:
138: protected static Object getDummyData(Class clazz) {
139: if (clazz.equals(Object.class))
140: return null;
141: return com.sun.data.provider.impl.AbstractDataProvider
142: .getFakeData(clazz);
143: }
144:
145: }
|