KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > popup > HtmlPopupRenderer


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.custom.popup;
17
18 import org.apache.myfaces.component.html.util.AddResource;
19 import org.apache.myfaces.renderkit.RendererUtils;
20 import org.apache.myfaces.renderkit.html.HtmlRenderer;
21 import org.apache.myfaces.renderkit.html.HTML;
22
23 import javax.faces.component.UIComponent;
24 import javax.faces.context.FacesContext;
25 import javax.faces.context.ResponseWriter;
26 import java.io.IOException JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * @author Martin Marinschek (latest modification by $Author: mmarinschek $)
31  * @version $Revision: 1.12 $ $Date: 2005/02/11 13:33:58 $
32  * $Log: HtmlPopupRenderer.java,v $
33  * Revision 1.12 2005/02/11 13:33:58 mmarinschek
34  * fix jira 15
35  *
36  * Revision 1.11 2004/12/27 04:11:11 mmarinschek
37  * Data Table stores the state of facets of children; script tag is rendered with type attribute instead of language attribute, popup works better as a column in a data table
38  *
39  * Revision 1.10 2004/12/20 06:13:01 mmarinschek
40  * killed bugs
41  *
42  * Revision 1.9 2004/12/17 22:06:32 grantsmith
43  * Jira MYFACES-57: Changed logging levels to DEBUG from INFO
44  *
45  * Revision 1.8 2004/12/17 13:19:10 mmarinschek
46  * new component jsValueChangeListener
47  *
48  * Revision 1.7 2004/12/03 20:52:11 svieujot
49  * Defer script loading for additional performance.
50  *
51  * Revision 1.6 2004/12/02 22:26:23 svieujot
52  * Simplify the AddResource methods
53  *
54  * Revision 1.5 2004/12/01 16:32:03 svieujot
55  * Convert the Multipart filter in an ExtensionsFilter that provides an additional facility to include resources in a page.
56  * Tested only with javascript resources right now, but should work fine with images too.
57  * Some work to do to include css resources.
58  * The popup component has been converted to use this new Filter.
59  *
60  * Revision 1.4 2004/11/25 08:41:25 matzew
61  * removed unused import-statements
62  *
63  * Revision 1.3 2004/11/23 23:24:04 mmarinschek
64  * Popup tag has now more attributes
65  *
66  * Revision 1.2 2004/11/17 11:25:42 mmarinschek
67  * reviewed version of popup
68  *
69  * Revision 1.1 2004/11/16 16:25:52 mmarinschek
70  * new popup - component; not yet finished
71  *
72  *
73  */

74 public class HtmlPopupRenderer
75     extends HtmlRenderer
76 {
77     public static final String JavaDoc RENDERER_TYPE = "org.apache.myfaces.Popup";
78     //private static final Log log = LogFactory.getLog(HtmlListRenderer.class);
79

80     public boolean getRendersChildren()
81     {
82         return true;
83     }
84
85     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException JavaDoc
86     {
87     }
88
89     public void encodeChildren(FacesContext facesContext, UIComponent component) throws IOException JavaDoc
90     {
91
92     }
93
94
95     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException JavaDoc
96     {
97         RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlPopup.class);
98
99         HtmlPopup popup = (HtmlPopup) uiComponent;
100
101         UIComponent popupFacet = popup.getPopup();
102
103         String JavaDoc popupId = writePopupScript(
104                 facesContext, popup.getClientId(facesContext),
105                 popup.getDisplayAtDistanceX(),popup.getDisplayAtDistanceY());
106
107         //writeMouseOverAndOutAttribs(popupId, popupFacet.getChildren());
108

109         writeMouseOverAttribs(popupId, uiComponent.getChildren(),
110             popup.getClosePopupOnExitingElement()==null ||
111                     popup.getClosePopupOnExitingElement().booleanValue());
112
113         RendererUtils.renderChildren(facesContext, uiComponent);
114
115         ResponseWriter writer = facesContext.getResponseWriter();
116
117         writer.startElement(HTML.DIV_ELEM, popup);
118         writer.writeAttribute(HTML.STYLE_ATTR,(popup.getStyle()!=null?(popup.getStyle()+
119                 (popup.getStyle().trim().endsWith(";")?"":";")):"")+
120                 "position:absolute;display:none;",null);
121         if(popup.getStyleClass()!=null)
122         {
123             writer.writeAttribute(HTML.CLASS_ATTR,popup.getStyleClass(),null);
124         }
125         writer.writeAttribute(HTML.ID_ATTR, popup.getClientId(facesContext),null);
126         writer.writeAttribute(HTML.ONMOUSEOVER_ATTR, new String JavaDoc(popupId+".redisplay();"),null);
127
128         Boolean JavaDoc closeExitPopup = popup.getClosePopupOnExitingPopup();
129
130         if(closeExitPopup==null || closeExitPopup.booleanValue())
131             writer.writeAttribute(HTML.ONMOUSEOUT_ATTR, new String JavaDoc(popupId+".hide();"),null);
132
133         RendererUtils.renderChildren(facesContext, popupFacet);
134         writer.endElement(HTML.DIV_ELEM);
135     }
136
137     private void writeMouseOverAttribs(String JavaDoc popupId, List JavaDoc children, boolean renderMouseOut)
138     {
139         for (int i = 0; i < children.size(); i++)
140         {
141             UIComponent uiComponent = (UIComponent) children.get(i);
142
143             callMethod(uiComponent,"onmouseover",new String JavaDoc(popupId+".display(event);"));
144
145             if(renderMouseOut)
146                 callMethod(uiComponent,"onmouseout",new String JavaDoc(popupId+".hide(event);"));
147
148             writeMouseOverAttribs(popupId, uiComponent.getChildren(),renderMouseOut);
149         }
150     }
151
152     private String JavaDoc writePopupScript(FacesContext context, String JavaDoc clientId,
153                                     Integer JavaDoc displayAtDistanceX, Integer JavaDoc displayAtDistanceY)
154         throws IOException JavaDoc
155     {
156         AddResource.addJavaScriptToHeader(
157                 HtmlPopupRenderer.class, "JSPopup.js", false, context);
158
159         String JavaDoc popupId = (clientId+"Popup").replaceAll(":","_");
160
161         while(popupId.startsWith("_"))
162         {
163             popupId = popupId.substring(1);
164         }
165
166         ResponseWriter writer = context.getResponseWriter();
167         writer.startElement(HTML.SCRIPT_ELEM,null);
168         writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR,HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT,null);
169         writer.writeText("var "+popupId+"=new orgApacheMyfacesPopup('"+clientId+"',"+
170                 (displayAtDistanceX==null?-5:displayAtDistanceX.intValue())+","+
171                 (displayAtDistanceY==null?-5:displayAtDistanceY.intValue())+");",null);
172         writer.endElement(HTML.SCRIPT_ELEM);
173
174         return popupId;
175     }
176
177     private void writeMouseOverAndOutAttribs(String JavaDoc popupId, List JavaDoc children)
178     {
179         for (int i = 0; i < children.size(); i++)
180         {
181             UIComponent uiComponent = (UIComponent) children.get(i);
182
183             callMethod(uiComponent,"onmouseover",new String JavaDoc(popupId+".redisplay();"));
184             callMethod(uiComponent,"onmouseout",new String JavaDoc(popupId+".hide();"));
185
186             writeMouseOverAndOutAttribs(popupId, uiComponent.getChildren());
187         }
188     }
189
190     private void callMethod(UIComponent uiComponent, String JavaDoc propName, String JavaDoc value)
191     {
192         Object JavaDoc oldValue = uiComponent.getAttributes().get(propName);
193
194         String JavaDoc oldValueStr = "";
195
196         String JavaDoc genCommentary = "/* generated code */";
197
198         if(oldValue != null)
199         {
200             oldValueStr = oldValue.toString().trim();
201
202             int genCommentaryIndex;
203
204             //check if generated code has already been added...
205
if((genCommentaryIndex=oldValueStr.indexOf(genCommentary))!=-1)
206             {
207                 oldValueStr = oldValueStr.substring(0,genCommentaryIndex);
208             }
209
210             if(oldValueStr.length()>0 && !oldValueStr.endsWith(";"))
211                 oldValueStr +=";";
212         }
213
214         value = oldValueStr + genCommentary+value;
215
216         uiComponent.getAttributes().put(propName, value);
217     }
218 }
219
Popular Tags