KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > renderkit > dom_html_basic > ButtonRenderer


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.renderkit.dom_html_basic;
35
36 import com.icesoft.faces.context.DOMContext;
37 import com.icesoft.faces.context.effects.JavascriptContext;
38 import org.w3c.dom.Element JavaDoc;
39
40 import javax.faces.component.UICommand;
41 import javax.faces.component.UIComponent;
42 import javax.faces.context.FacesContext;
43 import javax.faces.event.ActionEvent;
44 import java.io.IOException JavaDoc;
45 import java.util.Map JavaDoc;
46
47 public class ButtonRenderer extends DomBasicRenderer {
48
49     public void decode(FacesContext facesContext, UIComponent uiComponent) {
50         validateParameters(facesContext, uiComponent, UICommand.class);
51         if (isStatic(uiComponent)) {
52             return;
53         }
54         boolean thisButtonInvokedSubmit =
55                 didThisButtonInvokeSubmit(facesContext, uiComponent);
56         if (!thisButtonInvokedSubmit) {
57             return;
58         }
59         String JavaDoc type = ((String JavaDoc) uiComponent.getAttributes().get("type"))
60                 .toLowerCase();
61         if ((type != null) && (type.equals("reset"))) {
62             return;
63         }
64         ActionEvent actionEvent = new ActionEvent(uiComponent);
65         uiComponent.queueEvent(actionEvent);
66     }
67
68     /**
69      * @param facesContext
70      * @param uiComponent
71      * @return boolean
72      */

73     private boolean didThisButtonInvokeSubmit(
74             FacesContext facesContext, UIComponent uiComponent) {
75         boolean thisButtonInvokedSubmit = false;
76         //find if the form submitted by a textField, workaround to deal with the default behaviour of the browser
77
//(e.g.) if a form has a button on it, and enter key pressed on a text field, form submitted by the first intance of button
78
if (!isTextFieldInvokedSubmit(facesContext)) {
79             Map JavaDoc requestParameterMap =
80                     facesContext.getExternalContext().getRequestParameterMap();
81             String JavaDoc componentClientId = uiComponent.getClientId(facesContext);
82             String JavaDoc clientIdInRequestMap =
83                     (String JavaDoc) requestParameterMap.get(componentClientId);
84             thisButtonInvokedSubmit = clientIdInRequestMap != null;
85             if (!thisButtonInvokedSubmit) {
86                 // detect whether this button is an image button
87
// that invoked the submit
88
String JavaDoc clientIdXCoordinateInRequestMap =
89                         componentClientId + ".x";
90                 String JavaDoc clientIdYCoordinateInRequestMap =
91                         componentClientId + ".y";
92                 if (requestParameterMap.get(componentClientId + ".x") != null
93                     || requestParameterMap.get(componentClientId + ".y") !=
94                        null) {
95                     thisButtonInvokedSubmit = true;
96                 }
97             }
98         }
99         return thisButtonInvokedSubmit;
100     }
101
102     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
103             throws IOException JavaDoc {
104         validateParameters(facesContext, uiComponent, UICommand.class);
105
106         DOMContext domContext =
107                 DOMContext.attachDOMContext(facesContext, uiComponent);
108         String JavaDoc clientId = uiComponent.getClientId(facesContext);
109
110         if (!domContext.isInitialized()) {
111             Element JavaDoc root = domContext.createElement("input");
112             domContext.setRootNode(root);
113             root.setAttribute("name", clientId);
114         }
115         Element JavaDoc root = (Element JavaDoc) domContext.getRootNode();
116
117         setRootElementId(facesContext, root, uiComponent);
118
119         // according to the Sun documentation for ButtonRenderer,
120
// type: valid values are "submit" and "reset"; default to "submit".
121
// But the docs for the image attribute declare that 'image' is also
122
// a valid value for the type attribute.
123
// The logic here holds true when there is no image attribute
124
// But suns JSF implementation allows for type button, we allow it as well.
125
String JavaDoc typeAttribute =
126                 ((String JavaDoc) uiComponent.getAttributes().get("type"))
127                         .toLowerCase();
128         if (typeAttribute == null || (!typeAttribute.equals("reset") &&
129                                       (!typeAttribute.equals("button")))) {
130             typeAttribute = "submit";
131         }
132         uiComponent.getAttributes().put("type", typeAttribute);
133
134         // If image attribute is specified, the type is "image"
135
// otherwise take the type from the type attribute and set a label
136
// with value specified by the value attribute
137
String JavaDoc imageAttribute =
138                 (String JavaDoc) uiComponent.getAttributes().get("image");
139         if (imageAttribute != null) {
140             typeAttribute = "image";
141             root.setAttribute("type", typeAttribute);
142
143             //Paths to resources can be dependent on the environment so
144
//we should use proper JSF techniques to resolve them.
145
String JavaDoc pathToImage = getResourceURL(facesContext, imageAttribute);
146             
147             root.setAttribute("src", pathToImage);
148             root.removeAttribute("value");
149         } else {
150             root.setAttribute("type", typeAttribute);
151             String JavaDoc label = "";
152             Object JavaDoc componentValue = ((UICommand) uiComponent).getValue();
153             if (componentValue != null) {
154                 label = componentValue.toString();
155             } else {
156                 label = "";
157             }
158             root.setAttribute("value", label);
159             root.removeAttribute("src");
160         }
161
162         String JavaDoc styleClass =
163                 (String JavaDoc) uiComponent.getAttributes().get("styleClass");
164         if (styleClass != null) {
165             root.setAttribute("class", styleClass);
166         }
167
168         JavascriptContext.fireEffect(uiComponent, facesContext);
169
170         PassThruAttributeRenderer
171                 .renderAttributes(facesContext, uiComponent, null);
172         //add iceSubmit for image and submit button only
173
if (typeAttribute.equals("submit") || typeAttribute.equals("image")) {
174             renderOnClick(uiComponent, root);
175         }
176
177         domContext.stepOver();
178         domContext.streamWrite(facesContext, uiComponent);
179     }
180
181     /**
182      * @param uiComponent
183      * @param root
184      */

185     protected void renderOnClick(UIComponent uiComponent, Element JavaDoc root) {
186         String JavaDoc onclick = (String JavaDoc) uiComponent.getAttributes().get("onclick");
187         String JavaDoc submitCode = this.ICESUBMIT + "return false;";
188         if (onclick == null) {
189             onclick = submitCode;
190         } else {
191             onclick += submitCode;
192         }
193         root.setAttribute("onclick", onclick);
194     }
195
196     public void encodeChildren(FacesContext facesContext,
197                                UIComponent uiComponent)
198             throws IOException JavaDoc {
199         validateParameters(facesContext, uiComponent, UICommand.class);
200     }
201
202     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
203             throws IOException JavaDoc {
204         validateParameters(facesContext, uiComponent, UICommand.class);
205     }
206
207     private boolean isTextFieldInvokedSubmit(FacesContext facesContext) {
208         Object JavaDoc textFieldfocus =
209                 facesContext.getExternalContext().getRequestParameterMap()
210                         .get(FormRenderer.FOCUS_HIDDEN_FIELD);
211         if (textFieldfocus == null) {
212             return false;
213         }
214
215         if (textFieldfocus.toString().length() > 0) {
216             return true;
217         } else {
218             return false;
219         }
220     }
221 }
222
Popular Tags