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 com.sun.rave.web.ui.util.ComponentUtilities;
044: import java.beans.Beans;
045: import javax.faces.component.UIComponent;
046: import javax.faces.component.UIOutput;
047: import javax.faces.context.FacesContext;
048:
049: /**
050: *
051: * @author avk
052: */
053: public class Field extends FieldBase implements ComplexComponent {
054:
055: public static final String READONLY_ID = "_readOnly"; //NOI18N
056: public static final String LABEL_ID = "_label"; //NOI18N
057: public static final String INPUT_ID = "_field"; //NOI18N
058: public static final String READONLY_FACET = "readOnly"; //NOI18N
059: public static final String LABEL_FACET = "label";
060:
061: private static final boolean DEBUG = false;
062:
063: /** Creates a new instance of FieldBase */
064: public Field() {
065: }
066:
067: // Labels
068: public UIComponent getLabelComponent(FacesContext context,
069: String style) {
070:
071: if (DEBUG)
072: log("getReadOnlyComponent()");
073:
074: // Check if the page author has defined a label facet
075: UIComponent labelComponent = getFacet(LABEL_FACET); //NOI18N
076:
077: // If the page author has not defined a label facet,
078: // check if the page author specified a label.
079: if (labelComponent == null) {
080: String label = getLabel();
081: if (label != null && label.length() > 0) {
082: labelComponent = createLabel(label, style, context); //NOI18N\
083: }
084: } else if (DEBUG) {
085: log("\tFound facet."); //NOI18N
086: }
087:
088: return labelComponent;
089: }
090:
091: // Readonly value
092: public UIComponent getReadOnlyComponent(FacesContext context) {
093:
094: if (DEBUG)
095: log("getListLabelComponent()");
096:
097: String id = getId();
098:
099: // Check if the page author has defined a label facet
100: UIComponent textComponent = getFacet(READONLY_FACET); //NOI18N
101:
102: // If the page author has not defined a label facet,
103: // check if the page author specified a label.
104: if (textComponent == null) {
105: textComponent = createText(getReadOnlyValueString(context)); //NOI18N
106: //<RAVE>
107: } else {
108: if (DEBUG) {
109: log("\tFound facet."); //NOI18N
110: }
111: //CR 6391493
112: //The readonly component was cached. Make sure its value (text) property is up to date.
113: if (textComponent instanceof UIOutput) {
114: UIOutput outputComponent = (UIOutput) textComponent;
115: outputComponent
116: .setValue(getReadOnlyValueString(context));
117: }
118: //</RAVE>
119: }
120:
121: return textComponent;
122: }
123:
124: private UIComponent createLabel(String labelString, String style,
125: FacesContext context) {
126:
127: if (DEBUG)
128: log("createLabel()");
129:
130: // If we find a label, define a component and add it to the
131: // children, unless it has been added in a previous cycle
132: // (the component is being redisplayed).
133:
134: if (labelString == null || labelString.length() < 1) {
135: if (DEBUG)
136: log("\tNo label");
137: return null;
138: } else if (DEBUG) {
139: log("\tLabel is " + labelString); //NOI18N
140: }
141:
142: Label label = new Label();
143: label.setId(getId().concat(LABEL_ID));
144: label.setLabelLevel(getLabelLevel());
145: label.setStyleClass(style);
146: label.setText(labelString);
147: label.setLabeledComponent(this );
148: // <RAVE>
149: // this.getFacets().put(LABEL_FACET, label);
150: if (!Beans.isDesignTime())
151: this .getFacets().put(LABEL_FACET, label);
152: // </RAVE>
153: return label;
154: }
155:
156: private UIComponent createText(String string) {
157:
158: if (DEBUG)
159: log("createText()");
160:
161: // If we find a label, define a component and add it to the
162: // children, unless it has been added in a previous cycle
163: // (the component is being redisplayed).
164:
165: if (string == null || string.length() < 1) {
166: // TODO - maybe print a default?
167: string = new String();
168: }
169: StaticText text = new StaticText();
170: text.setText(string);
171: text.setId(getId().concat(READONLY_ID));
172: // <RAVE>
173: // this.getFacets().put(READONLY_FACET, text);
174: if (!Beans.isDesignTime())
175: this .getFacets().put(READONLY_FACET, text);
176: // </RAVE>
177: return text;
178: }
179:
180: /**
181: * Log an error - only used during development time.
182: */
183: protected void log(String s) {
184: System.out.println(this .getClass().getName() + "::" + s); //NOI18N
185: }
186:
187: // <RAVE>
188: // Merged INF http://inf.central/inf/integrationReport.jsp?id=82661 from braveheart.
189: // It fixes bug 6349156: File upload label's "for" attribute value is incorrect
190:
191: /**
192: * Retrieves the DOM ID for the HTML input element. To be used by
193: * Label component as a value for the "for" attribute.
194: */
195: public String getPrimaryElementID(FacesContext context) {
196:
197: // Check for a public facet
198: String clntId = this .getClientId(context);
199: UIComponent facet = getFacet(LABEL_FACET);
200: if (facet != null) {
201: return clntId.concat(this .INPUT_ID);
202: }
203: // Need to check for the private facet as well. Note that
204: // this is not ideal - unless getLabelComponent has been invoked
205: // first, the private facet will be null. (Is there a reason
206: // we can't just invoke getLabelComponent instead of getFacet?).
207:
208: // Pass "false" since we don't want to get null if the id's
209: // don't match. We don't care at this point, in fact they
210: // should match because getLabelComponent must have been
211: // called in order for this to even work.
212: //
213: facet = ComponentUtilities.getPrivateFacet(this , LABEL_FACET,
214: false);
215: if (facet == null) {
216: return getClientId(context);
217: }
218: return this .getClientId(context).concat(this .INPUT_ID);
219: }
220:
221: // <RAVE>
222:
223: public int getColumns() {
224:
225: int columns = super .getColumns();
226: if (columns < 1) {
227: columns = 20;
228: super .setColumns(20);
229: }
230: return columns;
231: }
232:
233: // <RAVE>
234: public void setText(Object text) {
235: if (this .isReadOnly()) {
236: UIComponent facet = (UIComponent) getFacets().get(
237: READONLY_FACET);
238: if (facet == null || !(facet instanceof StaticText)) {
239: StaticText staticText = new StaticText();
240: staticText.setId(getId().concat(READONLY_ID));
241: getFacets().put(READONLY_FACET, staticText);
242: facet = staticText;
243: }
244: ((StaticText) facet).setText(text);
245: }
246: super .setText(text);
247: }
248: // </RAVE>
249:
250: }
|