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.beans.Beans;
044: import java.util.Iterator;
045: import java.util.List;
046: import javax.faces.application.FacesMessage;
047: import javax.faces.component.EditableValueHolder;
048: import javax.faces.component.NamingContainer;
049: import javax.faces.component.UIComponent;
050: import javax.faces.context.FacesContext;
051: import com.sun.rave.web.ui.theme.Theme;
052: import com.sun.rave.web.ui.theme.ThemeImages;
053: import com.sun.rave.web.ui.util.ConversionUtilities;
054: import com.sun.rave.web.ui.util.LogUtil;
055:
056: /**
057: * <p>Component that represents an input field label.</p>
058: */
059:
060: public class Label extends LabelBase {
061:
062: public static final String REQUIRED_ID = "_required";
063: public static final String REQUIRED_FACET = "required";
064: public static final String ERROR_ID = "_error";
065: public static final String ERROR_FACET = "error";
066:
067: private EditableValueHolder labeledComponent = null;
068:
069: private String element = "span"; //NOI18N
070:
071: private static final boolean DEBUG = false;
072:
073: public void setLabeledComponent(UIComponent comp) {
074:
075: if (DEBUG)
076: log("setLabeledComponent");
077: if (comp == null) {
078: if (DEBUG)
079: log("component is null");
080: this .labeledComponent = null;
081: } else if (comp instanceof EditableValueHolder) {
082: if (DEBUG)
083: log("Component is EditableValueHolder");
084: this .labeledComponent = (EditableValueHolder) comp;
085: // <RAVE>
086: // this.setFor(comp.getClientId(FacesContext.getCurrentInstance()));
087: if (!Beans.isDesignTime())
088: this .setFor(comp.getClientId(FacesContext
089: .getCurrentInstance()));
090: // </RAVE>
091: element = "label";
092: } else {
093: if (DEBUG)
094: log("Component is not an EditableValueHolder");
095: if (LogUtil.infoEnabled(Label.class)) {
096: FacesContext context = FacesContext
097: .getCurrentInstance();
098:
099: LogUtil.info(Label.class, "Label.invalidFor",
100: new Object[] { getId(),
101: context.getViewRoot().getViewId(),
102: comp.getId() });
103: }
104:
105: this .labeledComponent = null;
106: element = "label";
107: }
108: }
109:
110: public EditableValueHolder getLabeledComponent() {
111:
112: if (DEBUG)
113: log("getLabeledComponent for label "
114: + String.valueOf(getText()));
115: if (labeledComponent != null) {
116: if (DEBUG)
117: log("Found component ");
118: if (DEBUG)
119: log(((UIComponent) labeledComponent).getId());
120: return labeledComponent;
121: }
122: if (DEBUG)
123: log("labelled component is null, try something else");
124: String id = getFor();
125:
126: if (DEBUG && id != null) {
127: log("\tfor attribute set to " + id);
128: }
129:
130: if (id == null) {
131: if (DEBUG)
132: log("\tID is not set, find children ");
133: setLabeledComponent(findLabeledChild());
134: } else {
135: if (DEBUG)
136: log("\tID found");
137: if (id.indexOf(":") > -1 && !id.startsWith(":")) {
138: id = ":" + id;
139: }
140: setLabeledComponent(findComponent(id));
141: element = "label";
142: }
143: return labeledComponent;
144: }
145:
146: public String getLabeledComponentId(FacesContext context) {
147:
148: String id = null;
149:
150: if (labeledComponent != null) {
151: if (labeledComponent instanceof ComplexComponent) {
152: ComplexComponent compComp = (ComplexComponent) labeledComponent;
153: id = compComp.getPrimaryElementID(context);
154: } else {
155: UIComponent comp = ((UIComponent) labeledComponent);
156: id = comp.getClientId(context);
157: }
158: } else {
159: id = getFor();
160: if (id != null && id.indexOf(":") == -1) {
161: UIComponent comp = this .getParent();
162: if (comp instanceof NamingContainer) {
163: id = comp.getClientId(context) + ":" + id;
164: }
165: }
166: }
167: return id;
168: }
169:
170: private UIComponent findLabeledChild() {
171:
172: if (DEBUG)
173: log("findLabeledChild");
174: List kids = getChildren();
175: if (DEBUG && kids.size() == 0) {
176: log("No children!");
177: }
178: for (int i = 0; i < kids.size(); i++) {
179: Object kid = kids.get(i);
180: if (kid instanceof EditableValueHolder) {
181: if (DEBUG)
182: log("Found good child " + kid.toString());
183: return (UIComponent) kid;
184: }
185: }
186: if (DEBUG)
187: log("\tReturning null...");
188: return null;
189: }
190:
191: public UIComponent getRequiredIcon(Theme theme, FacesContext context) {
192:
193: UIComponent comp = getFacet(REQUIRED_FACET);
194: if (comp == null) {
195: comp = theme.getIcon(ThemeImages.LABEL_REQUIRED_ICON);
196: comp.setId(getId().concat(REQUIRED_ID));
197: ((Icon) comp).setBorder(0);
198: //((Icon)comp).setLongDesc("TODO: Required");
199: // <RAVE>
200: // getFacets().put(REQUIRED_FACET, comp);
201: if (!Beans.isDesignTime())
202: getFacets().put(REQUIRED_FACET, comp);
203: // </RAVE>
204: }
205: return comp;
206: }
207:
208: public UIComponent getErrorIcon(Theme theme, FacesContext context,
209: boolean valid) {
210:
211: UIComponent comp = getFacet(ERROR_FACET);
212: if (comp == null) {
213:
214: comp = theme.getIcon(ThemeImages.LABEL_INVALID_ICON);
215: comp.setId(getId().concat(ERROR_ID));
216: ((Icon) comp).setBorder(0);
217: //((Icon)comp).setLongDesc("TODO: Invalid");
218:
219: }
220: if (comp instanceof Icon) {
221: if (valid) {
222: ((Icon) comp).setIcon(ThemeImages.DOT);
223: } else if (labeledComponent != null) {
224: String labeledCompID = ((UIComponent) labeledComponent)
225: .getClientId(context);
226: Iterator messages = context.getMessages(labeledCompID);
227: FacesMessage fm = null;
228: StringBuffer msgBuffer = new StringBuffer(200);
229: while (messages.hasNext()) {
230: fm = (FacesMessage) (messages.next());
231: msgBuffer.append(fm.getDetail());
232: msgBuffer.append(" "); //NOI18N
233: }
234: ((Icon) comp).setAlt(msgBuffer.toString());
235: ((Icon) comp).setToolTip(msgBuffer.toString());
236: }
237: }
238: return comp;
239: }
240:
241: public String getElement() {
242: return element;
243: }
244:
245: private void log(String s) {
246: System.out.println(getClass().getName() + "::" + s);
247: }
248:
249: public int getLabelLevel() {
250:
251: int level = super .getLabelLevel();
252: if (level < 1 || level > 3) {
253: level = 2;
254: super.setLabelLevel(level);
255: }
256: return level;
257: }
258: }
|