KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > wap > renderkit > wml > CommandButtonRenderer


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.wap.renderkit.wml;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import javax.faces.FacesException;
23 import javax.faces.component.UIComponent;
24 import javax.faces.component.UIForm;
25 import javax.faces.context.FacesContext;
26 import javax.faces.context.ResponseWriter;
27 import javax.faces.event.ActionEvent;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.myfaces.wap.component.CommandButton;
32 import org.apache.myfaces.wap.renderkit.Attributes;
33 import org.apache.myfaces.wap.renderkit.RendererUtils;
34 import org.apache.myfaces.wap.renderkit.WmlRenderer;
35
36
37 /**
38  * @author <a HREF="mailto:Jiri.Zaloudek@ivancice.cz">Jiri Zaloudek</a> (latest modification by $Author: matzew $)
39  * @version $Revision: 1.1 $ $Date: 2004/12/30 09:37:26 $
40  * $Log: CommandButtonRenderer.java,v $
41  * Revision 1.1 2004/12/30 09:37:26 matzew
42  * added a new RenderKit for WML. Thanks to Jirí Žaloudek
43  *
44  */

45 public class CommandButtonRenderer extends WmlRenderer {
46     private static Log log = LogFactory.getLog(CommandButtonRenderer.class);
47     
48     /** Creates a new instance of FormRenderer */
49     public CommandButtonRenderer() {
50         super();
51         log.debug("created object " + this.getClass().getName());
52     }
53     
54     public void encodeBegin(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
55         log.debug("encodeBegin(" + component.getId() + ")");
56         if (context == null || component == null) {
57             throw new NullPointerException JavaDoc();
58         }
59         if (!component.isRendered()) return;
60         
61     }
62     
63     public void encodeChildren(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
64         log.debug("encodeChildren(" + component.getId() + ")");
65         if (context == null || component == null) {
66             throw new NullPointerException JavaDoc();
67         }
68     }
69     
70     public void encodeEnd(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
71         log.debug("encodeEnd(" + component.getId() + ")");
72         if (context == null || component == null) {
73             throw new NullPointerException JavaDoc();
74         }
75         if (!component.isRendered()) return;
76         
77         ResponseWriter writer = context.getResponseWriter();
78         CommandButton comp = (CommandButton)component;
79         
80         // <do type='accept' label='submit' >
81
writer.startElement(Attributes.DO, component);
82         RendererUtils.writeAttribute(Attributes.ID, comp.getClientId(context), writer);
83         //RenderUtils.writeAttribute(Attributes.STYLE_CLASS, comp.getStyleClass(), writer);
84
//RenderUtils.writeAttribute(Attributes.XML_LANG, comp.getXmllang(), writer);
85
//RenderUtils.writeAttribute(Attributes.NAME, comp.getName(), writer);
86
RendererUtils.writeAttribute(Attributes.LABEL, comp.getValue(), writer);
87         RendererUtils.writeAttribute(Attributes.TYPE, comp.getType(), writer);
88         
89         /* default value is false, write only if value of attribute emptyok is "true" */
90         if (comp.isOptional()) RendererUtils.writeAttribute(Attributes.OPTIONAL, "true", writer);
91                 
92         if (isReset(comp))
93             renderReset(context, comp);
94         else
95             renderAction(context, comp);
96         
97         writer.endElement(Attributes.DO);
98     }
99     
100     
101     public void decode(FacesContext context, UIComponent component) {
102         log.debug("decode(" + component.getId() + ")");
103         if (component == null ) throw new NullPointerException JavaDoc();
104         if (!component.isRendered()) return;
105         
106         Map JavaDoc map = context.getExternalContext().getRequestParameterMap();
107         
108         // was this link activated?
109
if (map.containsKey(component.getClientId(context) + Attributes.POSTFIX_ACTIVATED) && !isReset((CommandButton)component)){
110             log.debug("action queued");
111             component.queueEvent(new ActionEvent(component));
112         }
113     }
114     
115     private void renderAction(FacesContext context, CommandButton comp) throws java.io.IOException JavaDoc {
116         ResponseWriter writer = context.getResponseWriter();
117         
118         // post form data
119
UIForm parentForm = getParentForm(context, comp);
120         String JavaDoc href = RendererUtils.getCurrentUrl(context);
121         
122         // <go HREF='"+ href +"' method='post' >
123
writer.startElement(Attributes.GO, comp);
124         writer.writeAttribute(Attributes.HREF, href, null);
125         writer.writeAttribute(Attributes.METHOD, Attributes.POST, null);
126         
127         Set JavaDoc inputTags = RendererUtils.getInputTags(parentForm.getChildren());
128         Iterator JavaDoc iter = inputTags.iterator();
129         
130         log.debug("Form has " + inputTags.size() + " input tags.");
131         
132         while(iter.hasNext()){
133             UIComponent component = (UIComponent)iter.next();
134             String JavaDoc name = RendererUtils.getAttribute(component, Attributes.NAME);
135             
136             // <postfield name='" + name + "' value='$(" + name + ")' />
137
writer.startElement(Attributes.POSTFIELD, component);
138             writer.writeAttribute(Attributes.NAME, name, null);
139             writer.writeAttribute(Attributes.VALUE, "$(" + name + ")", null);
140             writer.endElement(Attributes.POSTFIELD);
141         }
142         
143         // write hidden input to determine "submited" value on decode parent UIForm component
144
writer.startElement(Attributes.POSTFIELD, comp);
145         writer.writeAttribute(Attributes.NAME, parentForm.getClientId(context) + Attributes.POSTFIX_SUBMITED, null);
146         writer.writeAttribute(Attributes.VALUE, "true", null);
147         writer.endElement(Attributes.POSTFIELD);
148         
149         // write hidden input to determine "activated" value on decode commandLink
150
writer.startElement(Attributes.POSTFIELD, comp);
151         writer.writeAttribute(Attributes.NAME, comp.getClientId(context) + Attributes.POSTFIX_ACTIVATED, null);
152         writer.writeAttribute(Attributes.VALUE, "true", null);
153         writer.endElement(Attributes.POSTFIELD);
154         
155         writer.endElement(Attributes.GO);
156         
157     }
158
159     private void renderReset(FacesContext context, CommandButton comp) throws java.io.IOException JavaDoc {
160         ResponseWriter writer = context.getResponseWriter();
161     
162         writer.startElement(Attributes.REFRESH, comp);
163         
164         UIForm parentForm = getParentForm(context, comp);
165         Set JavaDoc inputTags = RendererUtils.getInputTags(parentForm.getChildren());
166         Iterator JavaDoc iter = inputTags.iterator();
167         
168         while(iter.hasNext()){
169             UIComponent component = (UIComponent)iter.next();
170             
171             // <setvar name='" + name + "' value='$(" + name + ")' />
172
writer.startElement(Attributes.SETVAR, comp);
173             String JavaDoc name = RendererUtils.getAttribute(component, Attributes.NAME);
174             writer.writeAttribute(Attributes.NAME, name, null);
175             
176             String JavaDoc value = RendererUtils.getAttribute(component, Attributes.VALUE);
177             writer.writeAttribute(Attributes.VALUE, value, null);
178             writer.endElement(Attributes.SETVAR);
179         }
180         
181         writer.endElement(Attributes.REFRESH);
182     }
183     
184     private boolean isReset(CommandButton comp){
185         return ("reset".compareToIgnoreCase(comp.getType()) == 0);
186     }
187     
188     
189     private UIForm getParentForm(FacesContext context, UIComponent component){
190         // find UIForm parent
191
UIComponent parent = component.getParent();
192         while(parent != null && !(parent instanceof UIForm)){
193             parent = parent.getParent();
194         }
195         
196         String JavaDoc formId;
197         if (parent == null){ //parent not found
198
//TODO: create dummy form
199
throw new FacesException("FacesException - tag commandButton must be nested in a UIForm tag");
200         } else {
201             return((UIForm)parent);
202         }
203     }
204 }
205     
206
Popular Tags