KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.DOMUtils;
38
39 import org.w3c.dom.Element JavaDoc;
40 import org.w3c.dom.Text JavaDoc;
41
42 import javax.faces.FacesException;
43 import javax.faces.component.NamingContainer;
44 import javax.faces.component.UICommand;
45 import javax.faces.component.UIComponent;
46 import javax.faces.component.UIViewRoot;
47 import javax.faces.context.FacesContext;
48 import javax.faces.event.ActionEvent;
49
50 import java.beans.Beans JavaDoc;
51 import java.io.IOException JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.Map JavaDoc;
54
55 public class CommandLinkRenderer extends DomBasicRenderer {
56
57     private static final String JavaDoc HIDDEN_FIELD_NAME = "cl";
58
59     public void decode(FacesContext facesContext, UIComponent uiComponent) {
60         validateParameters(facesContext, uiComponent, UICommand.class);
61         if (isStatic(uiComponent)) {
62             return;
63         }
64         String JavaDoc commandLinkClientId = uiComponent.getClientId(facesContext);
65         Map JavaDoc requestParameterMap =
66                 facesContext.getExternalContext().getRequestParameterMap();
67         String JavaDoc commonCommandLinkHiddenFieldName = deriveCommonHiddenFieldName(
68                 facesContext, (UICommand) uiComponent);
69         String JavaDoc hiddenFieldNameInRequestMap = (String JavaDoc) requestParameterMap
70                 .get(commonCommandLinkHiddenFieldName);
71         if (hiddenFieldNameInRequestMap == null
72             || hiddenFieldNameInRequestMap.equals("")
73             || !commandLinkClientId.equals(hiddenFieldNameInRequestMap)) {
74             // this command link did not invoke the submit
75
return;
76         }
77         // this command link caused the submit so queue an event
78
ActionEvent actionEvent = new ActionEvent(uiComponent);
79         uiComponent.queueEvent(actionEvent);
80     }
81
82     protected static String JavaDoc deriveCommonHiddenFieldName(
83             FacesContext facesContext,
84             UIComponent uiComponent) {
85         
86         if (Beans.isDesignTime()){
87             return "";
88         }
89
90         UIComponent parentNamingContainer = findForm(uiComponent);
91         String JavaDoc parentClientId = parentNamingContainer.getClientId(facesContext);
92         String JavaDoc hiddenFieldName = parentClientId
93                                  + NamingContainer.SEPARATOR_CHAR
94                                  + UIViewRoot.UNIQUE_ID_PREFIX
95                                  + HIDDEN_FIELD_NAME;
96         return hiddenFieldName;
97     }
98
99     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
100             throws IOException JavaDoc {
101
102         validateParameters(facesContext, uiComponent, UICommand.class);
103
104         DOMContext domContext =
105                 DOMContext.attachDOMContext(facesContext, uiComponent);
106
107         if (!domContext.isInitialized()) {
108             Element JavaDoc root = domContext.createElement("a");
109             domContext.setRootNode(root);
110             setRootElementId(facesContext, root, uiComponent);
111             root.setAttribute("href", "# ");
112         }
113         Element JavaDoc root = (Element JavaDoc) domContext.getRootNode();
114         DOMContext.removeChildren(root);
115         renderLinkText(uiComponent, domContext, root);
116
117         Map JavaDoc parameterMap = getParameterMap(uiComponent);
118         renderOnClick(facesContext, uiComponent, root, parameterMap);
119
120         String JavaDoc styleClass =
121                 (String JavaDoc) uiComponent.getAttributes().get("styleClass");
122         if (styleClass != null) {
123             root.setAttribute("class", styleClass);
124         }
125
126         PassThruAttributeRenderer.renderAttributes(facesContext, uiComponent,
127                                                    new String JavaDoc[]{"onclick"});
128
129         FormRenderer.addHiddenField(facesContext,
130                                     deriveCommonHiddenFieldName(
131                                             facesContext,
132                                             (UICommand) uiComponent));
133         Iterator JavaDoc parameterKeys = parameterMap.keySet().iterator();
134         while (parameterKeys.hasNext()) {
135             String JavaDoc nextKey = (String JavaDoc) parameterKeys.next();
136             FormRenderer.addHiddenField(facesContext, nextKey);
137         }
138         domContext.stepInto(uiComponent);
139     }
140
141
142     /**
143      * @param uiComponent
144      * @param domContext
145      * @param root
146      */

147     private void renderLinkText(UIComponent uiComponent, DOMContext
148             domContext, Element JavaDoc root) {
149         Object JavaDoc currentValue = ((UICommand) uiComponent).getValue();
150         String JavaDoc linkText = null;
151         if (currentValue != null) {
152             linkText = DOMUtils.escapeAnsi(currentValue.toString());
153         }
154         // create a new or update the old text node for the label
155
if (linkText != null && linkText.length() != 0) {
156             Text JavaDoc labelNode = (Text JavaDoc) root.getFirstChild();
157             if (labelNode == null) {
158                 labelNode = domContext.getDocument().createTextNode(linkText);
159                 root.appendChild(labelNode);
160             } else {
161                 labelNode.setData(linkText);
162             }
163         }
164     }
165
166     /**
167      * @param facesContext
168      * @param uiComponent
169      * @param root
170      * @param parameters
171      */

172     protected void renderOnClick(FacesContext facesContext,
173                                  UIComponent uiComponent,
174                                  Element JavaDoc root, Map JavaDoc parameters) {
175         UIComponent uiForm = findForm(uiComponent);
176         if (uiForm == null) {
177             throw new FacesException("CommandLink must be contained in a form");
178         }
179         String JavaDoc uiFormClientId = uiForm.getClientId(facesContext);
180         Object JavaDoc onClick = uiComponent.getAttributes().get(HTML.ONCLICK_ATTR);
181
182         //if onClick attribute set by the user, pre append it.
183
if (onClick != null) {
184             onClick = onClick.toString() + ";" +
185                       getJavaScriptOnClickString(facesContext,
186                                                  uiComponent, uiFormClientId,
187                                                  parameters);
188         } else {
189             onClick = getJavaScriptOnClickString(facesContext,
190                                                  uiComponent, uiFormClientId,
191                                                  parameters);
192         }
193         root.setAttribute("onclick",
194                           onClick.toString()); // replaced command w/component
195
}
196
197     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
198             throws IOException JavaDoc {
199         validateParameters(facesContext, uiComponent, UICommand.class);
200         DOMContext domContext =
201                 DOMContext.getDOMContext(facesContext, uiComponent);
202         domContext.stepOver();
203         domContext.streamWrite(facesContext, uiComponent);
204     }
205
206
207     private String JavaDoc getJavaScriptOnClickString(FacesContext facesContext,
208                                               UIComponent uiComponent,
209                                               String JavaDoc formClientId,
210                                               Map JavaDoc parameters) {
211         return getJavascriptHiddenFieldSetters(facesContext,
212                                                (UICommand) uiComponent,
213                                                formClientId, parameters)
214                + "iceSubmit("
215                + " document.forms['" + formClientId + "'],"
216                + " this,event); "
217                + "return false;";
218     }
219
220     /**
221      * @param facesContext
222      * @param uiCommand
223      * @param formClientId
224      * @param parameters
225      * @return
226      */

227     protected static String JavaDoc getJavascriptHiddenFieldSetters(
228             FacesContext facesContext,
229             UICommand uiCommand, String JavaDoc formClientId, Map JavaDoc parameters) {
230         StringBuffer JavaDoc buffer;
231         buffer = new StringBuffer JavaDoc("document.forms['" + formClientId + "']['");
232         buffer.append(deriveCommonHiddenFieldName(facesContext, uiCommand));
233         buffer.append(
234                 "'].value='" + uiCommand.getClientId(facesContext) + "';");
235         Iterator JavaDoc parameterKeys = parameters.keySet().iterator();
236         while (parameterKeys.hasNext()) {
237             String JavaDoc nextParamName = (String JavaDoc) parameterKeys.next();
238             Object JavaDoc nextParamValue = parameters.get(nextParamName);
239             buffer.append("document.forms['" + formClientId + "']['");
240             buffer.append(nextParamName);
241             buffer.append("'].value='");
242             buffer.append(nextParamValue);
243             buffer.append("';");
244         }
245         return buffer.toString();
246     }
247
248     public boolean getRendersChildren() {
249         return true;
250     }
251
252     public void encodeChildren(FacesContext facesContext,
253                                UIComponent uiComponent)
254             throws IOException JavaDoc {
255         validateParameters(facesContext, uiComponent, UICommand.class);
256         Iterator JavaDoc children = uiComponent.getChildren().iterator();
257         while (children.hasNext()) {
258             UIComponent nextChild = (UIComponent) children.next();
259             nextChild.encodeBegin(facesContext);
260             if (nextChild.getRendersChildren()) {
261                 nextChild.encodeChildren(facesContext);
262             }
263             nextChild.encodeEnd(facesContext);
264         }
265     }
266 }
267
Popular Tags