KickJava   Java API By Example, From Geeks To Geeks.

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


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.JSFAttr;
20 import org.apache.myfaces.renderkit.RendererUtils;
21 import org.apache.myfaces.renderkit.html.util.DummyFormResponseWriter;
22 import org.apache.myfaces.renderkit.html.util.DummyFormUtils;
23 import org.apache.myfaces.renderkit.html.util.JavascriptUtils;
24
25 import javax.faces.component.UICommand;
26 import javax.faces.component.UIComponent;
27 import javax.faces.component.UIForm;
28 import javax.faces.component.ValueHolder;
29 import javax.faces.component.html.HtmlCommandButton;
30 import javax.faces.context.FacesContext;
31 import javax.faces.context.ResponseWriter;
32 import javax.faces.event.ActionEvent;
33 import java.io.IOException JavaDoc;
34 import java.util.Map JavaDoc;
35
36
37 /**
38  * @author Manfred Geiler (latest modification by $Author: mmarinschek $)
39  * @author Thomas Spiegl
40  * @author Anton Koinov
41  * @version $Revision: 1.9 $ $Date: 2005/03/16 20:41:12 $
42  * $Log: HtmlButtonRendererBase.java,v $
43  * Revision 1.9 2005/03/16 20:41:12 mmarinschek
44  * fix for MYFACES-111, alien commit for Heath Borders
45  *
46  * Revision 1.8 2004/10/13 11:51:01 matze
47  * renamed packages to org.apache
48  *
49  * Revision 1.7 2004/09/08 15:23:12 manolito
50  * Autoscroll feature
51  *
52  * Revision 1.6 2004/09/08 09:32:03 manolito
53  * MyfacesConfig moved to config package
54  *
55  * Revision 1.5 2004/07/26 09:19:08 manolito
56  * removed onclick from passthrough attributes for ButtonRenderer
57  *
58  * Revision 1.4 2004/07/01 22:00:57 mwessendorf
59  * ASF switch
60  *
61  * Revision 1.3 2004/06/03 12:57:03 o_rossmueller
62  * modified link renderer to use one hidden field for all links according to 1.1 renderkit docs
63  * added onclick=clear_XXX to button
64  *
65  * Revision 1.2 2004/06/03 11:45:40 o_rossmueller
66  * added check for .y image button suffix according to 1.1 renderkit docs
67  *
68  * Revision 1.1 2004/05/18 14:31:39 manolito
69  * user role support completely moved to components source tree
70  *
71  */

72 public class HtmlButtonRendererBase
73     extends HtmlRenderer
74 {
75     private static final String JavaDoc IMAGE_BUTTON_SUFFIX_X = ".x";
76     private static final String JavaDoc IMAGE_BUTTON_SUFFIX_Y = ".y";
77
78     public void decode(FacesContext facesContext, UIComponent uiComponent)
79     {
80         RendererUtils.checkParamValidity(facesContext, uiComponent, UICommand.class);
81
82         //super.decode must not be called, because value is handled here
83
if (!isReset(uiComponent) && isSubmitted(facesContext, uiComponent))
84         {
85             uiComponent.queueEvent(new ActionEvent(uiComponent));
86         }
87     }
88
89     private static boolean isReset(UIComponent uiComponent)
90     {
91         return "reset".equalsIgnoreCase((String JavaDoc) uiComponent.getAttributes().get(HTML.TYPE_ATTR));
92     }
93
94     private static boolean isSubmitted(FacesContext facesContext, UIComponent uiComponent)
95     {
96         String JavaDoc clientId = uiComponent.getClientId(facesContext);
97         Map JavaDoc paramMap = facesContext.getExternalContext().getRequestParameterMap();
98         return paramMap.containsKey(clientId) || paramMap.containsKey(clientId + IMAGE_BUTTON_SUFFIX_X) || paramMap.containsKey(clientId + IMAGE_BUTTON_SUFFIX_Y);
99     }
100
101     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
102             throws IOException JavaDoc
103     {
104         RendererUtils.checkParamValidity(facesContext, uiComponent, UICommand.class);
105
106         String JavaDoc clientId = uiComponent.getClientId(facesContext);
107
108         ResponseWriter writer = facesContext.getResponseWriter();
109
110         writer.startElement(HTML.INPUT_ELEM, uiComponent);
111
112         writer.writeAttribute(HTML.ID_ATTR, clientId, null);
113         writer.writeAttribute(HTML.NAME_ATTR, clientId, null);
114
115         String JavaDoc image = getImage(uiComponent);
116
117         if (image != null)
118         {
119             writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_IMAGE, null);
120             writer.writeAttribute(HTML.SRC_ATTR, image, JSFAttr.IMAGE_ATTR);
121         }
122         else
123         {
124             String JavaDoc type = getType(uiComponent);
125
126             if (type == null)
127             {
128                 type = HTML.INPUT_TYPE_SUBMIT;
129             }
130             writer.writeAttribute(HTML.TYPE_ATTR, type, JSFAttr.TYPE_ATTR);
131             Object JavaDoc value = getValue(uiComponent);
132             if (value != null)
133             {
134                 writer.writeAttribute(HTML.VALUE_ATTR, value, null);
135             }
136         }
137         if (JavascriptUtils.isJavascriptAllowed(facesContext.getExternalContext()))
138         {
139             StringBuffer JavaDoc onClick = buildOnClick(uiComponent, facesContext, writer);
140             writer.writeAttribute(HTML.ONCLICK_ATTR, onClick.toString(), null);
141             HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
142                                                    HTML.BUTTON_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED_AND_ONCLICK);
143         }
144         else
145         {
146             HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
147                                                    HTML.BUTTON_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED);
148         }
149
150         if (isDisabled(facesContext, uiComponent))
151         {
152             writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, null);
153         }
154
155         writer.endElement(HTML.INPUT_ELEM);
156     }
157
158
159     protected StringBuffer JavaDoc buildOnClick(UIComponent uiComponent, FacesContext facesContext, ResponseWriter writer)
160         throws IOException JavaDoc
161     {
162         //Find form
163
UIComponent parent = uiComponent.getParent();
164         while (parent != null && !(parent instanceof UIForm))
165         {
166             parent = parent.getParent();
167         }
168
169         UIForm nestingForm = null;
170         String JavaDoc formName;
171         DummyFormResponseWriter dummyFormResponseWriter;
172         if (parent != null)
173         {
174             //link is nested inside a form
175
nestingForm = (UIForm)parent;
176             formName = nestingForm.getClientId(facesContext);
177             dummyFormResponseWriter = null;
178         }
179         else
180         {
181             //not nested in form, we must add a dummy form at the end of the document
182
formName = DummyFormUtils.DUMMY_FORM_NAME;
183             dummyFormResponseWriter = DummyFormUtils.getDummyFormResponseWriter(facesContext);
184             dummyFormResponseWriter.setWriteDummyForm(true);
185         }
186         StringBuffer JavaDoc onClick = new StringBuffer JavaDoc();
187         String JavaDoc commandOnClick = (String JavaDoc)uiComponent.getAttributes().get(HTML.ONCLICK_ATTR);
188
189         if (commandOnClick != null)
190         {
191             onClick.append(commandOnClick);
192             onClick.append(';');
193         }
194
195         //call the clear_<formName> method
196
onClick.append(HtmlRendererUtils.getClearHiddenCommandFormParamsFunctionName(formName)).append("();");
197
198         if (MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()).isAutoScroll())
199         {
200             JavascriptUtils.appendAutoScrollAssignment(onClick, formName);
201         }
202
203         //add hidden field for the case there is no commandLink in the form
204
String JavaDoc hiddenFieldName = HtmlRendererUtils.getHiddenCommandLinkFieldName(formName);
205         if (nestingForm != null)
206         {
207             HtmlFormRendererBase.addHiddenCommandParameter(nestingForm, hiddenFieldName);
208         }
209         else
210         {
211             dummyFormResponseWriter.addDummyFormParameter(hiddenFieldName);
212         }
213
214         return onClick;
215     }
216
217
218
219     protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
220     {
221         //TODO: overwrite in extended HtmlButtonRenderer and check for enabledOnUserRole
222
if (uiComponent instanceof HtmlCommandButton)
223         {
224             return ((HtmlCommandButton)uiComponent).isDisabled();
225         }
226         else
227         {
228             return RendererUtils.getBooleanAttribute(uiComponent, HTML.DISABLED_ATTR, false);
229         }
230     }
231
232
233     private String JavaDoc getImage(UIComponent uiComponent)
234     {
235         if (uiComponent instanceof HtmlCommandButton)
236         {
237             return ((HtmlCommandButton)uiComponent).getImage();
238         }
239         return (String JavaDoc)uiComponent.getAttributes().get(JSFAttr.IMAGE_ATTR);
240     }
241
242     private String JavaDoc getType(UIComponent uiComponent)
243     {
244         if (uiComponent instanceof HtmlCommandButton)
245         {
246             return ((HtmlCommandButton)uiComponent).getType();
247         }
248         return (String JavaDoc)uiComponent.getAttributes().get(JSFAttr.TYPE_ATTR);
249     }
250
251     private Object JavaDoc getValue(UIComponent uiComponent)
252     {
253         if (uiComponent instanceof ValueHolder)
254         {
255             return ((ValueHolder)uiComponent).getValue();
256         }
257         return uiComponent.getAttributes().get(JSFAttr.VALUE_ATTR);
258     }
259 }
260
Popular Tags