KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > renderkit > html > util > DummyFormUtils


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.util;
17
18 import org.apache.myfaces.config.MyfacesConfig;
19 import org.apache.myfaces.renderkit.html.HTML;
20 import org.apache.myfaces.renderkit.html.HtmlRendererUtils;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import javax.faces.application.StateManager;
26 import javax.faces.application.ViewHandler;
27 import javax.faces.context.FacesContext;
28 import javax.faces.context.ResponseWriter;
29 import java.io.IOException JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33 /**
34  * @author Manfred Geiler (latest modification by $Author: matze $)
35  * @version $Revision: 1.8 $ $Date: 2004/10/13 11:51:01 $
36  * $Log: DummyFormUtils.java,v $
37  * Revision 1.8 2004/10/13 11:51:01 matze
38  * renamed packages to org.apache
39  *
40  * Revision 1.7 2004/09/08 15:23:11 manolito
41  * Autoscroll feature
42  *
43  * Revision 1.6 2004/07/01 22:00:53 mwessendorf
44  * ASF switch
45  *
46  * Revision 1.5 2004/05/18 12:02:29 manolito
47  * getActionURL and getResourceURL must not call encodeActionURL or encodeResourceURL
48  *
49  */

50 public class DummyFormUtils
51 {
52     private static final Log log = LogFactory.getLog(DummyFormUtils.class);
53
54     private static final String JavaDoc DUMMY_FORM_RESPONSE_WRITER_WRAPPER_REQ_PARAM
55             = DummyFormUtils.class.getName() + ".INSTANCE";
56
57     public static final String JavaDoc DUMMY_FORM_NAME = "linkDummyForm";
58     private static final String JavaDoc DUMMY_FORM_ID = "linkDummyForm";
59
60     /**
61      * Returns a DummyFormResponseWriter.
62      * Replaces current ResponseWriter by a DummyFormResponseWriter if necessary.
63      * @param facesContext
64      * @return a DummyFormResponseWriter instance
65      */

66     public static DummyFormResponseWriter getDummyFormResponseWriter(FacesContext facesContext)
67     {
68         ResponseWriter currentWriter = facesContext.getResponseWriter();
69         if (currentWriter instanceof DummyFormResponseWriter)
70         {
71             // current ResponseWriter is a DummyFormResponseWriter
72
//((DummyFormResponseWriter)currentWriter).setWriteDummyForm(true);
73
return (DummyFormResponseWriter)currentWriter;
74         }
75         else
76         {
77             // current ResponseWriter is not a DummyFormResponseWriter
78
// and therfore must be wrapped
79
Map JavaDoc requestMap = facesContext.getExternalContext().getRequestMap();
80             DummyFormResponseWriterWrapper writer
81                     = (DummyFormResponseWriterWrapper)requestMap.get(DUMMY_FORM_RESPONSE_WRITER_WRAPPER_REQ_PARAM);
82             if (writer != null)
83             {
84                 //There is already a wrapped response writer
85
return writer;
86             }
87             else
88             {
89                 //We must wrap current writer and replace it in the FacesContext
90
writer = new DummyFormResponseWriterWrapper(currentWriter);
91                 facesContext.setResponseWriter(writer);
92                 requestMap.put(DUMMY_FORM_RESPONSE_WRITER_WRAPPER_REQ_PARAM, writer);
93                 log.debug("DummyFormResponseWriterWrapper installed");
94                 return writer;
95             }
96         }
97     }
98
99
100     public static void writeDummyForm(ResponseWriter writer,
101                                       Set JavaDoc dummyFormParams) throws IOException JavaDoc
102     {
103         FacesContext facesContext = FacesContext.getCurrentInstance();
104         ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
105         String JavaDoc viewId = facesContext.getViewRoot().getViewId();
106         String JavaDoc actionURL = viewHandler.getActionURL(facesContext, viewId);
107
108         //write out dummy form
109
writer.startElement(HTML.FORM_ELEM, null);
110         writer.writeAttribute(HTML.ID_ATTR, DUMMY_FORM_ID, null);
111         writer.writeAttribute(HTML.NAME_ATTR, DUMMY_FORM_NAME, null);
112         writer.writeAttribute(HTML.STYLE_ATTR, "display:inline", null);
113         writer.writeAttribute(HTML.METHOD_ATTR, "post", null);
114         writer.writeURIAttribute(HTML.ACTION_ATTR,
115                                  facesContext.getExternalContext().encodeActionURL(actionURL),
116                                  null);
117         writer.flush();
118
119         StateManager stateManager = facesContext.getApplication().getStateManager();
120         if (stateManager.isSavingStateInClient(facesContext))
121         {
122             //render state parameters
123
//TODO: Optimize saveSerializedView call, because serialized view is built twice!
124
StateManager.SerializedView serializedView = stateManager.saveSerializedView(facesContext);
125             stateManager.writeState(facesContext, serializedView);
126         }
127
128         if (MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()).isAutoScroll())
129         {
130             JavascriptUtils.renderAutoScrollHiddenInput(writer);
131         }
132
133         if (dummyFormParams != null)
134         {
135             HtmlRendererUtils.renderHiddenCommandFormParams(writer, dummyFormParams);
136             HtmlRendererUtils.renderClearHiddenCommandFormParamsFunction(writer,
137                                                                          DUMMY_FORM_NAME,
138                                                                          dummyFormParams, null);
139         }
140
141         writer.endElement(HTML.FORM_ELEM);
142     }
143
144
145 }
146
Popular Tags