KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > panelpopup > PanelPopupRenderer


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.component.panelpopup;
35
36 import com.icesoft.faces.component.ext.renderkit.GroupRenderer;
37 import com.icesoft.faces.component.ext.taglib.Util;
38 import com.icesoft.faces.component.util.CustomComponentUtils;
39 import com.icesoft.faces.context.DOMContext;
40 import com.icesoft.faces.context.effects.CurrentStyle;
41 import com.icesoft.faces.context.effects.JavascriptContext;
42 import com.icesoft.faces.renderkit.dom_html_basic.HTML;
43 import com.icesoft.faces.renderkit.dom_html_basic.PassThruAttributeRenderer;
44 import com.icesoft.faces.util.CoreUtils;
45
46 import org.apache.commons.logging.Log;
47 import org.apache.commons.logging.LogFactory;
48 import org.w3c.dom.Element JavaDoc;
49 import org.w3c.dom.Node JavaDoc;
50 import org.w3c.dom.NodeList JavaDoc;
51
52 import javax.faces.component.UIComponent;
53 import javax.faces.context.FacesContext;
54 import java.io.IOException JavaDoc;
55
56 /**
57  * <p>PanelPopupRenderer is an extension of ICEfaces D2D GroupRenderer
58  * responsible for rendering the PanelPopup component.</p>
59  */

60 public class PanelPopupRenderer extends GroupRenderer {
61     private static Log log = LogFactory.getLog(PanelPopupRenderer.class);
62
63     /* (non-Javadoc)
64     * @see com.icesoft.faces.renderkit.dom_html_basic.GroupRenderer#getRendersChildren()
65     */

66     public boolean getRendersChildren() {
67         return true;
68     }
69
70     /* (non-Javadoc)
71      * @see com.icesoft.faces.component.ext.renderkit.GroupRenderer#encodeBegin(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
72      */

73     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
74             throws IOException JavaDoc {
75         validateParameters(facesContext, uiComponent, PanelPopup.class);
76
77         String JavaDoc styleClass =
78                 (String JavaDoc) uiComponent.getAttributes().get("styleClass");
79         String JavaDoc headerClass =
80                 (String JavaDoc) uiComponent.getAttributes().get("headerClass");
81         String JavaDoc bodyClass =
82                 (String JavaDoc) uiComponent.getAttributes().get("bodyClass");
83         Boolean JavaDoc resizable =
84                 null; // resizable functionality has not been implemented yet.
85
Boolean JavaDoc modal = (Boolean JavaDoc) uiComponent.getAttributes().get("modal");
86         if (log.isTraceEnabled()) {
87             log.trace("Value of modal is [" + modal + "]");
88         }
89         Boolean JavaDoc visible = (Boolean JavaDoc) uiComponent.getAttributes().get("visible");
90
91         String JavaDoc dndType = getDndType(uiComponent);
92
93         DOMContext domContext =
94                 DOMContext.attachDOMContext(facesContext, uiComponent);
95
96         // initialize DOMContext
97
PanelPopup panelPopup = (PanelPopup) uiComponent;
98
99         String JavaDoc clientId = uiComponent.getClientId(facesContext);
100
101         if (!domContext.isInitialized()) {
102             Element JavaDoc rootDiv = domContext.createRootElement(HTML.DIV_ELEM);
103             setRootElementId(facesContext, rootDiv, uiComponent);
104             rootDiv.setAttribute(HTML.NAME_ATTR, clientId);
105             Element JavaDoc table = domContext.createElement(HTML.TABLE_ELEM);
106             table.setAttribute(HTML.CELLPADDING_ATTR, "0");
107             table.setAttribute(HTML.CELLSPACING_ATTR, "0");
108             table.setAttribute(HTML.WIDTH_ATTR, "100%");
109
110             rootDiv.appendChild(table);
111             if (modal != null && modal.booleanValue()) {
112                 dndType = null;
113             }
114             // extracted from GroupRenderer encodeBegin
115
if (dndType != null) {
116                 // Drag an drop needs some hidden fields
117
Element JavaDoc statusField = createHiddenField(domContext,
118                                                         facesContext,
119                                                         uiComponent, STATUS);
120                 rootDiv.appendChild(statusField);
121                 Element JavaDoc targetID = createHiddenField(domContext, facesContext,
122                                                      uiComponent, DROP);
123                 rootDiv.appendChild(targetID);
124             }
125             // Write Modal Javascript so that on refresh it will still be modal.
126
String JavaDoc script =
127                     modalJavascript(modal, visible, facesContext, clientId);
128             if (script != null) {
129                 Element JavaDoc scriptEle = domContext.createElement(HTML.SCRIPT_ELEM);
130                 scriptEle.setAttribute(HTML.SCRIPT_LANGUAGE_ATTR,
131                                        HTML.SCRIPT_LANGUAGE_JAVASCRIPT);
132                 script = "window.onLoad(function(){" + script + "});";
133                 Node JavaDoc node = domContext.createTextNode(script);
134                 scriptEle.appendChild(node);
135                 rootDiv.appendChild(scriptEle);
136             }
137         }
138
139         Element JavaDoc root = (Element JavaDoc) domContext.getRootNode();
140         String JavaDoc style = ((PanelPopup) uiComponent).getStyle();
141         if(style != null && style.length() > 0)
142             root.setAttribute(HTML.STYLE_ATTR, style);
143         else
144             root.removeAttribute(HTML.STYLE_ATTR);
145         try {
146             root.setAttribute(HTML.CLASS_ATTR, styleClass);
147             String JavaDoc script =
148                     modalJavascript(modal, visible, facesContext, clientId);
149             if (script != null) {
150                 JavascriptContext.addJavascriptCall(facesContext, script);
151             }
152         } catch (Exception JavaDoc e) {
153             log.error("Error rendering Modal Panel Popup ", e);
154         }
155         // get tables , our table is the first and only one
156
NodeList JavaDoc tables = root.getElementsByTagName(HTML.TABLE_ELEM);
157         // assumption we want the first table in tables. there should only be one
158
Element JavaDoc table = (Element JavaDoc) tables.item(0);
159         // clean out child nodes and build a fresh selectinputdate
160
DOMContext.removeChildrenByTagName(table, HTML.TR_ELEM);
161
162         PassThruAttributeRenderer
163                 .renderAttributes(facesContext, uiComponent, null);
164         String JavaDoc handleId = null;
165         if (panelPopup.getHeader() != null) {
166             Element JavaDoc headerTr = domContext.createElement(HTML.TR_ELEM);
167             Element JavaDoc headerTd = domContext.createElement(HTML.TD_ELEM);
168             headerTd.setAttribute(HTML.CLASS_ATTR, headerClass);
169             handleId = uiComponent.getClientId(facesContext) + "Handle";
170             headerTd.setAttribute(HTML.ID_ATTR, handleId);
171             headerTr.appendChild(headerTd);
172             // add header facet to header tr and add to table
173
table.appendChild(headerTr);
174             // set the cursor parent to the new table row Element
175
// to the new table row Element
176
domContext.setCursorParent(headerTd);
177
178             UIComponent header = panelPopup.getHeader();
179
180             domContext.streamWrite(facesContext, uiComponent,
181                                    domContext.getRootNode(), headerTd);
182
183             CustomComponentUtils.renderChild(facesContext, header);
184         }
185
186         if (panelPopup.getBody() != null) {
187
188             Element JavaDoc bodyTr = domContext.createElement(HTML.TR_ELEM);
189             Element JavaDoc bodyTd = domContext.createElement(HTML.TD_ELEM);
190
191
192             bodyTd.setAttribute(HTML.CLASS_ATTR, bodyClass);
193             bodyTr.appendChild(bodyTd);
194             // add body facet to body tr then add to table
195
table.appendChild(bodyTr);
196             // set the cursor parent to the new table row Element
197
// this will cause the renderChild method to append the child nodes
198
// to the new table row Element
199
domContext.setCursorParent(bodyTd);
200
201             UIComponent body = panelPopup.getBody();
202
203             domContext.streamWrite(facesContext, uiComponent,
204                                    domContext.getRootNode(), bodyTd);
205
206             CustomComponentUtils.renderChild(facesContext, body);
207         }
208         // if the popup is resizable render a resize handle
209
if (resizable != null && resizable.booleanValue()) {
210             Element JavaDoc footerTr = domContext.createElement(HTML.TR_ELEM);
211             footerTr.setAttribute(HTML.HEIGHT_ATTR, "15px");
212             footerTr.setAttribute(HTML.STYLE_ATTR,
213                                   "text-align: right; float: right;");
214             Element JavaDoc footerTd = domContext.createElement(HTML.TD_ELEM);
215             footerTd.setAttribute(HTML.STYLE_CLASS_ATTR, "panelPopupFooter");
216             Element JavaDoc img = domContext.createElement(HTML.IMG_ELEM);
217             img.setAttribute(HTML.SRC_ATTR, CoreUtils
218                     .resolveResourceURL(facesContext,
219                     "/xmlhttp/css/xp/css-images/resize.gif"));
220             img.setAttribute(HTML.STYLE_ATTR, "cursor: se-resize");
221             footerTd.appendChild(img);
222             footerTr.appendChild(footerTd);
223             table.appendChild(footerTr);
224         }
225
226         domContext.stepOver();
227         domContext.streamWrite(facesContext, uiComponent);
228         CurrentStyle.apply(facesContext, uiComponent);
229         // Rebroadcast Javascript to survive refresh
230
if (dndType != null) {
231             addJavascriptCalls(uiComponent, "DRAG", handleId, facesContext);
232         }
233     }
234
235     private String JavaDoc modalJavascript(Boolean JavaDoc modal, Boolean JavaDoc visible,
236                                    FacesContext facesContext, String JavaDoc clientId) {
237         String JavaDoc call = null;
238         if (modal != null) {
239             if (modal.booleanValue() && visible.booleanValue()) {
240                 call = "Ice.modal.start('" + clientId + "');";
241                 if (log.isTraceEnabled()) {
242                     log.trace("Starting Modal Function");
243                 }
244             } else {
245                 call = "Ice.modal.stop('" + clientId + "');";
246                 if (log.isTraceEnabled()) {
247                     log.trace("Stopping modal function");
248                 }
249             }
250         }
251         return call;
252     }
253
254     /* (non-Javadoc)
255      * @see com.icesoft.faces.renderkit.dom_html_basic.GroupRenderer#encodeChildren(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
256      */

257     public void encodeChildren(FacesContext facesContext,
258                                UIComponent uiComponent) throws IOException JavaDoc {
259     }
260
261     /* (non-Javadoc)
262      * @see com.icesoft.faces.component.ext.renderkit.GroupRenderer#encodeEnd(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
263      */

264     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
265             throws IOException JavaDoc {
266         if (log.isTraceEnabled()) {
267             log.trace("Encode End Called");
268         }
269     }
270 }
Popular Tags