KickJava   Java API By Example, From Geeks To Geeks.

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


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.CommandLink;
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  * @author <a HREF="mailto:Jiri.Zaloudek@ivancice.cz">Jiri Zaloudek</a> (latest modification by $Author: matzew $)
38  * @version $Revision: 1.1 $ $Date: 2004/12/30 09:37:26 $
39  * $Log: CommandLinkRenderer.java,v $
40  * Revision 1.1 2004/12/30 09:37:26 matzew
41  * added a new RenderKit for WML. Thanks to Jirí Žaloudek
42  *
43  */

44 public class CommandLinkRenderer extends WmlRenderer {
45     private static Log log = LogFactory.getLog(CommandLinkRenderer.class);
46     
47     /** Creates a new instance of TextRenderer */
48     public CommandLinkRenderer() {
49         super();
50         log.debug("created object " + this.getClass().getName());
51     }
52     
53     public boolean getRendersChildren(){
54         return(false);
55     }
56     
57     public void encodeBegin(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
58         log.debug("encodeBegin(" + component.getId() + ")");
59         if (context == null || component == null) {
60             throw new NullPointerException JavaDoc();
61         }
62         if (!component.isRendered()) return;
63         
64         CommandLink comp = (CommandLink)component;
65         ResponseWriter writer = context.getResponseWriter();
66
67         writer.startElement(Attributes.ANCHOR, component);
68         RendererUtils.writeAttribute(Attributes.TITLE, comp.getTitle(), writer);
69         writer.flush();
70     }
71     
72     public void encodeChildren(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
73         log.debug("encodeChildren(" + component.getId() + ")");
74         if (context == null || component == null) {
75             throw new NullPointerException JavaDoc();
76         }
77         if (!component.isRendered()) return;
78
79         RendererUtils.renderChildren(context, component);
80     }
81     
82     public void encodeEnd(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
83         log.debug("encodeEnd(" + component.getId() + ")");
84         if (context == null || component == null) {
85             throw new NullPointerException JavaDoc();
86         }
87         if (!component.isRendered()) return;
88
89         CommandLink comp = (CommandLink)component;
90         UIForm parentForm = getParentForm(context, component);
91         
92         ResponseWriter writer = context.getResponseWriter();
93         
94         String JavaDoc href = RendererUtils.getCurrentUrl(context);
95         
96         // <go HREF='"+ href +"' method='post' >
97
writer.startElement(Attributes.GO, component);
98         writer.writeAttribute(Attributes.HREF, href, null);
99         writer.writeAttribute(Attributes.METHOD, Attributes.POST, null);
100                         
101         Set JavaDoc inputTags = RendererUtils.getInputTags(parentForm.getChildren());
102         Iterator JavaDoc iter = inputTags.iterator();
103         
104         log.debug("Form has " + inputTags.size() + " input tags.");
105         
106         while(iter.hasNext()){
107             UIComponent inputComp = (UIComponent)iter.next();
108             String JavaDoc name = RendererUtils.getAttribute(inputComp, Attributes.NAME);
109
110             // <postfield name='" + name + "' value='$(" + name + ")' />
111
writer.startElement(Attributes.POSTFIELD, component);
112             writer.writeAttribute(Attributes.NAME, name, null);
113             writer.writeAttribute(Attributes.VALUE, "$(" + name + ")", null);
114             writer.endElement(Attributes.POSTFIELD);
115         }
116                 
117         // write hidden input to determine "submited" value on decode parent UIForm component
118
writer.startElement(Attributes.POSTFIELD, component);
119         writer.writeAttribute(Attributes.NAME, parentForm.getClientId(context) + Attributes.POSTFIX_SUBMITED, null);
120         writer.writeAttribute(Attributes.VALUE, "true", null);
121         writer.endElement(Attributes.POSTFIELD);
122                 
123         // write hidden input to determine "activated" value on decode commandLink
124
writer.startElement(Attributes.POSTFIELD, component);
125         writer.writeAttribute(Attributes.NAME, component.getClientId(context) + Attributes.POSTFIX_ACTIVATED, null);
126         writer.writeAttribute(Attributes.VALUE, "true", null);
127         writer.endElement(Attributes.POSTFIELD);
128
129         writer.endElement(Attributes.GO);
130         writer.endElement(Attributes.ANCHOR);
131     }
132     
133     public void decode(FacesContext context, UIComponent component) {
134         log.debug("decode(" + component.getId() + ")");
135         if (component == null || context == null) throw new NullPointerException JavaDoc();
136         if (!component.isRendered()) return;
137
138         Map JavaDoc map = context.getExternalContext().getRequestParameterMap();
139         
140         // was this link activated?
141
if (map.containsKey(component.getClientId(context) + Attributes.POSTFIX_ACTIVATED))
142             component.queueEvent(new ActionEvent(component));
143     }
144     
145     private UIForm getParentForm(FacesContext context, UIComponent component){
146         // find UIForm parent
147
UIComponent parent = component.getParent();
148         while(parent != null && !(parent instanceof UIForm)){
149             parent = parent.getParent();
150         }
151         
152         String JavaDoc formId;
153         if (parent == null){ //parent not found
154
//TODO: create dummy form
155
throw new FacesException("FacesException - tag commandLink must be nested in a UIForm tag");
156         } else {
157            return((UIForm)parent);
158         }
159     }
160     
161 }
162
163
Popular Tags