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.component;
042:
043: import java.io.IOException;
044: import javax.faces.component.UIComponent;
045: import javax.faces.context.FacesContext;
046: import javax.faces.el.MethodBinding;
047: import javax.faces.el.ValueBinding;
048:
049: /**
050: * Use the <code>ui:hiddenField</code> tag to create a hidden field,
051: * which is present in the HTML, but not displayed to the user.
052: * Hidden fields are useful for saving state information.
053: *
054: * <h3>HTML Elements and Layout</h3>
055: *
056: * <p>The hiddenField component renders an XHTML <code><input
057: * type="hidden"></code> element. </p>
058: *
059: * <h3>Configuring the <code>ui:hiddenField</code> Tag</h3>
060: *
061: * <p>Use the <code>value</code> attribute to associate
062: * the component with a model object that represents the current value,
063: * by setting the attribute's value to a JavaServer Faces EL expression
064: * that corresponds to a property of a backing bean.</p>
065: *
066: * <h3>Facets</h3>
067: *
068: * <p>This component has no facets.</p>
069: *
070: * <h3>Theme Identifiers</h3>
071: *
072: * <p>This component does not use any style classes from the theme.</p>
073: *
074: * <h3>Client-side JavaScript functions</h3>
075: *
076: * <p>In all the functions below, <code><id></code> should be
077: * the generated id of the HiddenField component.
078: *
079: * <table cellpadding="2" cellspacing="2" border="1"
080: * style="text-align: left; width: 100%;">
081: * <tbody>
082: * <tr>
083: * <td style="vertical-align">
084: * <code>field_setDisabled(<id>, <disabled>)</code>
085: * </td>
086: * <td style="vertical-align: top">
087: * Enable/disable the field. Set <code><disabled></code>
088: * to true to disable the component, or false to enable it.
089: * </td>
090: * </tr>
091: * <tr>
092: * <td style="vertical-align: top">
093: * <code>field_setValue(<id>, <newValue>)</code>
094: * </td>
095: * <td style="vertical-align: top">
096: * Set the value of the field to <code><newValue></code>.
097: * </td>
098: * </tr>
099: * <tr>
100: * <td style="vertical-align: top">
101: * <code>field_getValue(<id>)</code>
102: * </td>
103: * <td style="vertical-align: top">Get the value of the field.</td>
104: * </tr>
105: * <tr>
106: * <td style="vertical-align: top">
107: * <code>field_getInputElement(<id>)</code></td>
108: * <td style="vertical-align: top">
109: * Get hold of a reference to the input element rendered by this
110: * component.
111: * </td>
112: * </tr>
113: * </tbody>
114: * </table>
115: *
116: * <h3>Examples</h3>
117: *
118: * <p>This example uses a backing bean <code>FieldTest</code> with a
119: * property <code>counter</code>. The property is an <code>int</code> but
120: * it is not necessary to specify a converter since the default
121: * JavaServer Faces converter will be used. The value of the hidden
122: * field may be updated through a JavaScript. The tag generates an
123: * HTML input element.</p>
124: * <pre>
125: * <ui:hiddenField id="counter" value="#{FieldTest.counter}"/>
126: * </pre>
127: * <p>Auto-generated component class.
128: * Do <strong>NOT</strong> modify; all changes
129: * <strong>will</strong> be lost!</p>
130: */
131:
132: public abstract class HiddenFieldBase extends
133: javax.faces.component.UIInput {
134:
135: /**
136: * <p>Construct a new <code>HiddenFieldBase</code>.</p>
137: */
138: public HiddenFieldBase() {
139: super ();
140: setRendererType("com.sun.rave.web.ui.HiddenField");
141: }
142:
143: /**
144: * <p>Return the identifier of the component family to which this
145: * component belongs. This identifier, in conjunction with the value
146: * of the <code>rendererType</code> property, may be used to select
147: * the appropriate {@link Renderer} for this component instance.</p>
148: */
149: public String getFamily() {
150: return "com.sun.rave.web.ui.HiddenField";
151: }
152:
153: /**
154: * <p>Return the <code>ValueBinding</code> stored for the
155: * specified name (if any), respecting any property aliases.</p>
156: *
157: * @param name Name of value binding to retrieve
158: */
159: public ValueBinding getValueBinding(String name) {
160: if (name.equals("text")) {
161: return super .getValueBinding("value");
162: }
163: return super .getValueBinding(name);
164: }
165:
166: /**
167: * <p>Set the <code>ValueBinding</code> stored for the
168: * specified name (if any), respecting any property
169: * aliases.</p>
170: *
171: * @param name Name of value binding to set
172: * @param binding ValueBinding to set, or null to remove
173: */
174: public void setValueBinding(String name, ValueBinding binding) {
175: if (name.equals("text")) {
176: super .setValueBinding("value", binding);
177: return;
178: }
179: super .setValueBinding(name, binding);
180: }
181:
182: // disabled
183: private boolean disabled = false;
184: private boolean disabled_set = false;
185:
186: /**
187: * <p>Flag indicating that the hidden field should not send its value to the
188: * server.</p>
189: */
190: public boolean isDisabled() {
191: if (this .disabled_set) {
192: return this .disabled;
193: }
194: ValueBinding _vb = getValueBinding("disabled");
195: if (_vb != null) {
196: Object _result = _vb.getValue(getFacesContext());
197: if (_result == null) {
198: return false;
199: } else {
200: return ((Boolean) _result).booleanValue();
201: }
202: }
203: return false;
204: }
205:
206: /**
207: * <p>Flag indicating that the hidden field should not send its value to the
208: * server.</p>
209: * @see #isDisabled()
210: */
211: public void setDisabled(boolean disabled) {
212: this .disabled = disabled;
213: this .disabled_set = true;
214: }
215:
216: // text
217: /**
218: * <p>Literal value to be rendered in this hidden field.
219: * If this property is specified by a value binding
220: * expression, the corresponding value will be updated
221: * if validation succeeds.</p>
222: */
223: public Object getText() {
224: return getValue();
225: }
226:
227: /**
228: * <p>Literal value to be rendered in this hidden field.
229: * If this property is specified by a value binding
230: * expression, the corresponding value will be updated
231: * if validation succeeds.</p>
232: * @see #getText()
233: */
234: public void setText(Object text) {
235: setValue(text);
236: }
237:
238: /**
239: * <p>Restore the state of this component.</p>
240: */
241: public void restoreState(FacesContext _context, Object _state) {
242: Object _values[] = (Object[]) _state;
243: super .restoreState(_context, _values[0]);
244: this .disabled = ((Boolean) _values[1]).booleanValue();
245: this .disabled_set = ((Boolean) _values[2]).booleanValue();
246: }
247:
248: /**
249: * <p>Save the state of this component.</p>
250: */
251: public Object saveState(FacesContext _context) {
252: Object _values[] = new Object[3];
253: _values[0] = super .saveState(_context);
254: _values[1] = this .disabled ? Boolean.TRUE : Boolean.FALSE;
255: _values[2] = this.disabled_set ? Boolean.TRUE : Boolean.FALSE;
256: return _values;
257: }
258:
259: }
|