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 java.io.IOException;
044: import java.util.Map;
045:
046: import javax.faces.FacesException;
047: import javax.faces.component.EditableValueHolder;
048: import javax.faces.component.UIComponent;
049: import javax.faces.context.ResponseWriter;
050: import javax.faces.context.FacesContext;
051: import javax.faces.render.Renderer;
052: import com.sun.rave.web.ui.component.Field;
053: import com.sun.rave.web.ui.component.HiddenField;
054: import com.sun.rave.web.ui.util.ConversionUtilities;
055: import com.sun.rave.web.ui.util.MessageUtil;
056:
057: /**
058: * <p>Renderer for HiddenFieldRenderer {@link HiddenField} component.</p>
059: */
060:
061: public class HiddenFieldRenderer extends Renderer {
062:
063: private static final boolean DEBUG = false;
064:
065: public void encodeEnd(FacesContext context, UIComponent component)
066: throws IOException {
067:
068: if (!(component instanceof HiddenField)) {
069: Object[] params = { component.toString(),
070: this .getClass().getName(),
071: HiddenField.class.getName() };
072: String message = MessageUtil.getMessage(
073: "com.sun.rave.web.ui.resources.LogMessages", //NOI18N
074: "Renderer.component", params); //NOI18N
075: throw new FacesException(message);
076: }
077:
078: HiddenField field = (HiddenField) component;
079: ResponseWriter writer = context.getResponseWriter();
080: writer.startElement("input", field); //NOI18N
081: writer.writeAttribute("type", "hidden", null); //NOI18N
082: String id = field.getClientId(context);
083: writer.writeAttribute("id", id, null); //NOI18N
084: writer.writeAttribute("name", id, null); //NOI18N
085:
086: // Record the value that is rendered.
087: // Note that getValueAsString conforms to JSF conventions
088: // for NULL values, in that it returns "" if the component
089: // value is NULL. This value cannot be trusted since
090: // the fidelity of the data must be preserved, i.e. if the
091: // value is null, it must remain null if the component is unchanged
092: // by the user..
093: //
094: // What should be done in the case of submittedValue != null ?
095: // This call to getValue may not be value is used by
096: // getValueAsString, it may use the submittedValue.
097: // Then should the previously set rendered value be
098: // preserved ?
099: //
100: // If submittedValue is not null then the component's
101: // model value or local value has not been updated
102: // therefore assume that this is an immediate or premature
103: // render response. Therefore just assume that if the rendered
104: // value was null, the saved information is still valid.
105: //
106: if (((HiddenField) component).getSubmittedValue() == null) {
107: ConversionUtilities.setRenderedValue(component,
108: ((HiddenField) component).getValue());
109: }
110:
111: // Still call the component's getValueAsString method
112: // in order to render it.
113: //
114: String value = field.getValueAsString(context);
115: writer.writeAttribute("value", value, "value"); //NOI18N
116:
117: if (field.isDisabled()) {
118: writer.writeAttribute("disabled", "disabled", null); //NOI18N
119: }
120: writer.endElement("input");
121: }
122:
123: public void decode(FacesContext context, UIComponent component) {
124: decodeInput(context, component);
125: }
126:
127: /**
128: * Decode the component component
129: * @param context The FacesContext associated with this request
130: * @param component The TextField component to decode
131: */
132: static void decodeInput(FacesContext context, UIComponent component) {
133:
134: if (DEBUG)
135: log("decodeInput()");
136:
137: String id = component.getClientId(context);
138: Map params = context.getExternalContext()
139: .getRequestParameterMap();
140: Object valueObject = params.get(id);
141: String value = null;
142:
143: if (valueObject == null && component instanceof Field) {
144: id = id.concat(((Field) component).INPUT_ID);
145: valueObject = params.get(id);
146: }
147:
148: if (valueObject != null) {
149: value = (String) valueObject;
150: if (DEBUG)
151: log("Submitted value is " + value);
152:
153: if (component instanceof Field
154: && ((Field) component).isTrim()) {
155: value = value.toString().trim();
156: if (DEBUG)
157: log("Trimmed value is " + String.valueOf(value));
158: }
159: } else if (DEBUG)
160: log("\tNo relevant input parameter");
161:
162: ((EditableValueHolder) component).setSubmittedValue(value);
163: }
164:
165: static protected void log(String s) {
166: System.out.println(s);
167: }
168: }
|