KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > renderkit > html > HtmlLabelRenderer


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.renderkit.html;
17
18 import org.apache.myfaces.renderkit.JSFAttr;
19 import org.apache.myfaces.renderkit.RendererUtils;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import javax.faces.component.UIComponent;
25 import javax.faces.component.UINamingContainer;
26 import javax.faces.component.ValueHolder;
27 import javax.faces.component.html.HtmlOutputLabel;
28 import javax.faces.context.FacesContext;
29 import javax.faces.context.ResponseWriter;
30 import java.io.IOException JavaDoc;
31
32
33 /**
34  * @author Thomas Spiegl (latest modification by $Author: mmarinschek $)
35  * @author Anton Koinov
36  * @author Martin Marinschek
37  * @version $Revision: 1.17 $ $Date: 2005/01/09 18:15:12 $
38  * $Log: HtmlLabelRenderer.java,v $
39  * Revision 1.17 2005/01/09 18:15:12 mmarinschek
40  * small changes - better error handling, label renderer supports more hooks for sub-classes
41  *
42  * Revision 1.16 2005/01/09 09:18:54 mmarinschek
43  * Exception thrown
44  *
45  * Revision 1.15 2005/01/09 09:10:56 mmarinschek
46  * prepare call-back methods for renderer
47  *
48  * Revision 1.14 2004/12/23 13:03:08 mmarinschek
49  * id's not rendered (or not conditionally rendered); changes in jslistener to support both ie and firefox now
50  *
51  * Revision 1.13 2004/12/20 06:13:02 mmarinschek
52  * killed bugs
53  *
54  * Revision 1.12 2004/10/13 11:51:00 matze
55  * renamed packages to org.apache
56  *
57  * Revision 1.11 2004/07/01 22:05:07 mwessendorf
58  * ASF switch
59  *
60  * Revision 1.10 2004/04/13 10:42:03 manolito
61  * introduced commons codecs and fileupload
62  *
63  * Revision 1.9 2004/04/05 15:02:47 manolito
64  * for-attribute issues
65  *
66  */

67 public class HtmlLabelRenderer
68 extends HtmlRenderer
69 {
70     private static final Log log = LogFactory.getLog(HtmlLabelRenderer.class);
71
72     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
73             throws IOException JavaDoc
74     {
75         super.encodeBegin(facesContext, uiComponent); //check for NP
76

77         ResponseWriter writer = facesContext.getResponseWriter();
78
79         encodeBefore(facesContext, writer, uiComponent);
80
81         writer.startElement(HTML.LABEL_ELEM, uiComponent);
82         HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext);
83         HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.LABEL_PASSTHROUGH_ATTRIBUTES);
84
85         String JavaDoc forAttr = getFor(uiComponent);
86         if (forAttr == null)
87         {
88             throw new NullPointerException JavaDoc("Attribute 'for' of label component with id " + uiComponent.getClientId(facesContext));
89         }
90
91         UIComponent forComponent = uiComponent.findComponent(forAttr);
92         if (forComponent == null)
93         {
94             if (log.isWarnEnabled())
95             {
96                 log.warn("Unable to find component '" + forAttr + "' (calling findComponent on component '" + uiComponent.getClientId(facesContext) + "')");
97             }
98             if (forAttr.length() > 0 && forAttr.charAt(0) == UINamingContainer.SEPARATOR_CHAR)
99             {
100                 //absolute id path
101
writer.writeAttribute(HTML.FOR_ATTR, forAttr.substring(1), JSFAttr.FOR_ATTR);
102             }
103             else
104             {
105                 //relative id path, we assume a component on the same level as the label component
106
String JavaDoc labelClientId = uiComponent.getClientId(facesContext);
107                 int colon = labelClientId.lastIndexOf(UINamingContainer.SEPARATOR_CHAR);
108                 if (colon == -1)
109                 {
110                     writer.writeAttribute(HTML.FOR_ATTR, forAttr, JSFAttr.FOR_ATTR);
111                 }
112                 else
113                 {
114                     writer.writeAttribute(HTML.FOR_ATTR, labelClientId.substring(0, colon + 1) + forAttr, JSFAttr.FOR_ATTR);
115                 }
116             }
117         }
118         else
119         {
120             writer.writeAttribute(HTML.FOR_ATTR, forComponent.getClientId(facesContext), JSFAttr.FOR_ATTR);
121         }
122
123
124         //MyFaces extension: Render a label text given by value
125
//TODO: Move to extended component
126
if (uiComponent instanceof ValueHolder)
127         {
128             String JavaDoc text = RendererUtils.getStringValue(facesContext, uiComponent);
129             if(text != null)
130             {
131                 writer.writeText(text, "value");
132             }
133         }
134
135         writer.flush(); // close start tag
136

137         encodeAfterStart(facesContext,writer,uiComponent);
138     }
139
140     protected void encodeAfterStart(FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent)
141         throws IOException JavaDoc
142     {
143     }
144
145     protected void encodeBefore(FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent)
146         throws IOException JavaDoc
147     {
148     }
149
150
151     protected String JavaDoc getFor(UIComponent component)
152     {
153         if (component instanceof HtmlOutputLabel)
154         {
155             return ((HtmlOutputLabel)component).getFor();
156         }
157         else
158         {
159             return (String JavaDoc)component.getAttributes().get(JSFAttr.FOR_ATTR);
160         }
161     }
162
163
164     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
165             throws IOException JavaDoc
166     {
167         super.encodeEnd(facesContext, uiComponent); //check for NP
168

169         ResponseWriter writer = facesContext.getResponseWriter();
170
171         encodeBeforeEnd(facesContext, writer, uiComponent);
172
173         writer.endElement(HTML.LABEL_ELEM);
174
175         encodeAfter(facesContext, writer, uiComponent);
176     }
177
178     protected void encodeBeforeEnd(FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent)
179         throws IOException JavaDoc
180     {
181     }
182
183     protected void encodeAfter(FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent)
184         throws IOException JavaDoc
185     {
186     }
187 }
188
Popular Tags