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 com.sun.rave.web.ui.renderer;
042:
043: import com.sun.rave.web.ui.component.StaticText;
044: import com.sun.rave.web.ui.theme.Theme;
045: import com.sun.rave.web.ui.util.ConversionUtilities;
046:
047: import java.io.IOException;
048: import java.text.MessageFormat;
049: import java.util.ArrayList;
050: import java.util.Iterator;
051:
052: import javax.faces.component.EditableValueHolder;
053: import javax.faces.component.NamingContainer;
054: import javax.faces.component.UIComponent;
055: import javax.faces.component.UIParameter;
056: import javax.faces.context.FacesContext;
057: import javax.faces.context.ResponseWriter;
058:
059: /**
060: * <p>Renderer for a {@link StaticText} component.</p>
061: */
062:
063: public class StaticTextRenderer extends AbstractRenderer {
064:
065: // ======================================================== Static Variables
066:
067: /**
068: * <p>The set of String pass-through attributes to be rendered.</p>
069: */
070: private static final String stringAttributes[] = { "onClick",
071: "onDblClick", "onMouseUp", //NOI18N
072: "onMouseDown", "onMouseMove", "onMouseOut", "onMouseOver" }; //NOI18N
073:
074: // -------------------------------------------------------- Renderer Methods
075:
076: /**
077: * <p>Render the appropriate element start, depending on whether the
078: * <code>for</code> property is set or not.</p>
079: *
080: * @param context <code>FacesContext</code> for the current request
081: * @param component StaticText component
082: * @param writer <code>ResponseWriter</code> to which the element
083: * start should be rendered
084: *
085: * @exception IOException if an input/output error occurs
086: */
087: protected void renderStart(FacesContext context,
088: UIComponent component, ResponseWriter writer)
089: throws IOException {
090:
091: writer.startElement("span", component);
092: }
093:
094: /**
095: * <p>Render the appropriate element attributes, followed by the
096: * label content, depending on whether the <code>for</code> property
097: * is set or not.</p>
098: *
099: * @param context <code>FacesContext</code> for the current request
100: * @param component StaticText component
101: * @param writer <code>ResponseWriter</code> to which the element
102: * start should be rendered
103: *
104: * @exception IOException if an input/output error occurs
105: */
106: protected void renderAttributes(FacesContext context,
107: UIComponent component, ResponseWriter writer)
108: throws IOException {
109:
110: StaticText st = (StaticText) component;
111: addCoreAttributes(context, component, writer, null);
112: addStringAttributes(context, component, writer,
113: stringAttributes);
114: if (st.getToolTip() != null) {
115: writer.writeAttribute("title", st.getToolTip(), null);
116: }
117: }
118:
119: /**
120: * <p>Render the appropriate element end, depending on whether the
121: * <code>for</code> property is set or not.</p>
122: *
123: * @param context <code>FacesContext</code> for the current request
124: * @param component <code>EditableValueHolder</code> component whose
125: * submitted value is to be stored
126: * @param writer <code>ResponseWriter</code> to which the element
127: * start should be rendered
128: *
129: * @exception IOException if an input/output error occurs
130: */
131: protected void renderEnd(FacesContext context,
132: UIComponent component, ResponseWriter writer)
133: throws IOException {
134: StaticText staticText = (StaticText) component;
135:
136: String currentValue = ConversionUtilities.convertValueToString(
137: component, staticText.getText());
138: String style = staticText.getStyle();
139: String styleClass = staticText.getStyleClass();
140:
141: // <RAVE>
142: // Object currentObj = getAsString(context, component);
143: // if (currentObj != null) {
144: // if (currentObj instanceof String) {
145: // currentValue = (String) currentObj;
146: // } else {
147: // currentValue = currentObj.toString();
148: // }
149: // }
150: // </RAVE>
151: if (currentValue != null) {
152: java.util.ArrayList parameterList = new ArrayList();
153:
154: // get UIParameter children...
155:
156: java.util.Iterator kids = component.getChildren()
157: .iterator();
158: while (kids.hasNext()) {
159: UIComponent kid = (UIComponent) kids.next();
160:
161: //PENDING(rogerk) ignore if child is not UIParameter?
162:
163: if (!(kid instanceof UIParameter)) {
164: continue;
165: }
166:
167: parameterList.add(((UIParameter) kid).getValue());
168: }
169:
170: // If at least one substitution parameter was specified,
171: // use the string as a MessageFormat instance.
172: String message = null;
173: if (parameterList.size() > 0) {
174: message = MessageFormat.format(currentValue,
175: parameterList.toArray(new Object[parameterList
176: .size()]));
177: } else {
178: message = currentValue;
179: }
180:
181: if (message != null) {
182: if (staticText.isEscape()) {
183: writer.writeText(message, "value");
184: } else {
185: writer.write(message);
186: }
187: }
188: }
189: writer.endElement("span");
190: }
191:
192: // --------------------------------------------------------- Private Methods
193:
194: }
|