KickJava   Java API By Example, From Geeks To Geeks.

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


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.config.MyfacesConfig;
19 import org.apache.myfaces.renderkit.RendererUtils;
20 import org.apache.myfaces.renderkit.html.util.JavascriptUtils;
21
22 import javax.faces.application.ViewHandler;
23 import javax.faces.component.UIComponent;
24 import javax.faces.component.UIForm;
25 import javax.faces.component.html.HtmlForm;
26 import javax.faces.context.FacesContext;
27 import javax.faces.context.ResponseWriter;
28 import java.io.IOException JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33 /**
34  * @author Manfred Geiler (latest modification by $Author: matze $)
35  * @author Thomas Spiegl
36  * @author Anton Koinov
37  * @version $Revision: 1.9 $ $Date: 2004/10/13 11:51:01 $
38  * $Log: HtmlFormRendererBase.java,v $
39  * Revision 1.9 2004/10/13 11:51:01 matze
40  * renamed packages to org.apache
41  *
42  * Revision 1.8 2004/09/08 15:23:12 manolito
43  * Autoscroll feature
44  *
45  * Revision 1.7 2004/07/01 22:00:57 mwessendorf
46  * ASF switch
47  *
48  * Revision 1.6 2004/06/03 12:57:03 o_rossmueller
49  * modified link renderer to use one hidden field for all links according to 1.1 renderkit docs
50  * added onclick=clear_XXX to button
51  *
52  * Revision 1.5 2004/05/18 12:02:29 manolito
53  * getActionURL and getResourceURL must not call encodeActionURL or encodeResourceURL
54  *
55  * Revision 1.4 2004/04/30 09:11:38 manolito
56  * no message
57  *
58  * Revision 1.3 2004/04/29 19:34:38 o_rossmueller
59  * javascript for 'target' attribute handling
60  *
61  * Revision 1.2 2004/04/27 10:32:24 manolito
62  * clear hidden inputs javascript function
63  *
64  * Revision 1.1 2004/04/22 10:20:34 manolito
65  * tree component
66  *
67  */

68 public class HtmlFormRendererBase
69         extends HtmlRenderer
70 {
71     //private static final Log log = LogFactory.getLog(HtmlFormRenderer.class);
72

73     private static final String JavaDoc HIDDEN_SUBMIT_INPUT_SUFFIX = "_SUBMIT";
74     private static final String JavaDoc HIDDEN_SUBMIT_INPUT_VALUE = "1";
75
76     private static final String JavaDoc HIDDEN_COMMAND_INPUTS_SET_ATTR
77             = HtmlFormRendererBase.class.getName() + ".HIDDEN_COMMAND_INPUTS_SET";
78
79
80     public void encodeBegin(FacesContext facesContext, UIComponent component)
81             throws IOException JavaDoc
82     {
83         RendererUtils.checkParamValidity(facesContext, component, UIForm.class);
84
85         UIForm htmlForm = (HtmlForm)component;
86
87         ResponseWriter writer = facesContext.getResponseWriter();
88         ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
89         String JavaDoc viewId = facesContext.getViewRoot().getViewId();
90         String JavaDoc clientId = htmlForm.getClientId(facesContext);
91         String JavaDoc actionURL = viewHandler.getActionURL(facesContext, viewId);
92
93         writer.startElement(HTML.FORM_ELEM, htmlForm);
94         writer.writeAttribute(HTML.ID_ATTR, clientId, null);
95         writer.writeAttribute(HTML.NAME_ATTR, clientId, null);
96         writer.writeAttribute(HTML.METHOD_ATTR, "post", null);
97         writer.writeURIAttribute(HTML.ACTION_ATTR,
98                                  facesContext.getExternalContext().encodeActionURL(actionURL),
99                                  null);
100
101         HtmlRendererUtils.renderHTMLAttributes(writer, htmlForm, HTML.FORM_PASSTHROUGH_ATTRIBUTES);
102         writer.write(""); // forse start element tag to be closed
103
}
104
105
106     public void encodeEnd(FacesContext facesContext, UIComponent component)
107             throws IOException JavaDoc
108     {
109         ResponseWriter writer = facesContext.getResponseWriter();
110
111         //write state marker
112
ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
113         viewHandler.writeState(facesContext);
114
115         //write hidden input to determine "submitted" value on decode
116
writer.startElement(HTML.INPUT_ELEM, null);
117         writer.writeAttribute(HTML.TYPE_ATTR, "hidden", null);
118         writer.writeAttribute(HTML.NAME_ATTR, component.getClientId(facesContext) +
119                                               HIDDEN_SUBMIT_INPUT_SUFFIX, null);
120         writer.writeAttribute(HTML.VALUE_ATTR, HIDDEN_SUBMIT_INPUT_VALUE, null);
121         writer.endElement(HTML.INPUT_ELEM);
122
123         if (MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()).isAutoScroll())
124         {
125             JavascriptUtils.renderAutoScrollHiddenInput(writer);
126         }
127
128         //render hidden command inputs
129
Set JavaDoc set = (Set JavaDoc)component.getAttributes().get(HIDDEN_COMMAND_INPUTS_SET_ATTR);
130         if (set != null && !set.isEmpty())
131         {
132             HtmlRendererUtils.renderHiddenCommandFormParams(writer, set);
133
134             String JavaDoc target;
135             if (component instanceof HtmlForm)
136             {
137                 target = ((HtmlForm)component).getTarget();
138             }
139             else
140             {
141                 target = (String JavaDoc)component.getAttributes().get(HTML.TARGET_ATTR);
142             }
143             HtmlRendererUtils.renderClearHiddenCommandFormParamsFunction(writer,
144                                                                          component.getClientId(facesContext),
145                                                                          set,
146                                                                          target);
147         }
148
149         writer.endElement(HTML.FORM_ELEM);
150     }
151
152
153     public void decode(FacesContext facesContext, UIComponent component)
154     {
155         RendererUtils.checkParamValidity(facesContext, component, UIForm.class);
156
157         /*
158         if (HTMLUtil.isDisabled(component))
159         {
160             return;
161         }
162         */

163
164         UIForm htmlForm = (UIForm)component;
165
166         Map JavaDoc paramMap = facesContext.getExternalContext().getRequestParameterMap();
167         String JavaDoc submittedValue = (String JavaDoc)paramMap.get(component.getClientId(facesContext) +
168                                                      HIDDEN_SUBMIT_INPUT_SUFFIX);
169         if (submittedValue != null && submittedValue.equals(HIDDEN_SUBMIT_INPUT_VALUE))
170         {
171             htmlForm.setSubmitted(true);
172         }
173         else
174         {
175             htmlForm.setSubmitted(false);
176         }
177     }
178
179
180     public static void addHiddenCommandParameter(UIComponent form, String JavaDoc paramName)
181     {
182         Set JavaDoc set = (Set JavaDoc)form.getAttributes().get(HIDDEN_COMMAND_INPUTS_SET_ATTR);
183         if (set == null)
184         {
185             set = new HashSet JavaDoc();
186             form.getAttributes().put(HIDDEN_COMMAND_INPUTS_SET_ATTR, set);
187         }
188         set.add(paramName);
189     }
190
191 }
192
Popular Tags