KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > panelborder > PanelBorderRenderer


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.panelborder;
35
36 import com.icesoft.faces.component.util.CustomComponentUtils;
37 import com.icesoft.faces.context.DOMContext;
38 import com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer;
39 import com.icesoft.faces.renderkit.dom_html_basic.HTML;
40 import com.icesoft.faces.renderkit.dom_html_basic.PassThruAttributeRenderer;
41 import org.w3c.dom.Element JavaDoc;
42
43 import javax.faces.component.UIComponent;
44 import javax.faces.context.FacesContext;
45 import java.io.IOException JavaDoc;
46 import java.util.List JavaDoc;
47
48 /**
49  * This PanelBorderRenderer is responsible for rendering PanelBorder
50  * components.
51  */

52 public class PanelBorderRenderer extends DomBasicRenderer {
53     public static final String JavaDoc DEFAULT_LAYOUT = "default";
54     public static final String JavaDoc REVERSE_W_E = "horizontal reverse";
55     public static final String JavaDoc REVERSE_N_S = "vertical reverse";
56     public static final String JavaDoc CENTER_ONLY = "center only";
57     public static final String JavaDoc HIDE_N = "hide north";
58     public static final String JavaDoc HIDE_E = "hide east";
59     public static final String JavaDoc HIDE_S = "hide south";
60     public static final String JavaDoc HIDE_W = "hide west";
61
62     /* (non-Javadoc)
63      * @see javax.faces.render.Renderer#getRendersChildren()
64      */

65     public boolean getRendersChildren() {
66         return true;
67     }
68
69     /* (non-Javadoc)
70      * @see javax.faces.render.Renderer#encodeChildren(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
71      */

72     public void encodeChildren(FacesContext facesContext,
73                                UIComponent uiComponent) throws IOException JavaDoc {
74         super.encodeChildren(facesContext, uiComponent);
75     }
76
77     /* (non-Javadoc)
78      * @see com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer#encodeEnd(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
79      */

80     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
81             throws IOException JavaDoc {
82         validateParameters(facesContext, uiComponent, PanelBorder.class);
83
84         DOMContext domContext =
85                 DOMContext.attachDOMContext(facesContext, uiComponent);
86         PanelBorder borderLayout = (PanelBorder) uiComponent;
87         List JavaDoc layout = borderLayout.getLayoutAsList();
88         String JavaDoc clientId = uiComponent.getClientId(facesContext);
89         if (!domContext.isInitialized()) {
90             Element JavaDoc table = domContext.createRootElement(HTML.TABLE_ELEM);
91             setRootElementId(facesContext, table, uiComponent);
92             table.setAttribute(HTML.NAME_ATTR, clientId);
93         }
94
95         Element JavaDoc table = (Element JavaDoc) domContext.getRootNode();
96         PassThruAttributeRenderer
97                 .renderAttributes(facesContext, uiComponent, null);
98         table.setAttribute(HTML.CLASS_ATTR, borderLayout.getStyleClass());
99         String JavaDoc style = borderLayout.getStyle();
100         if(style != null && style.length() > 0)
101             table.setAttribute(HTML.STYLE_ATTR, style);
102         else
103             table.removeAttribute(HTML.STYLE_ATTR);
104
105         DOMContext.removeChildren(table);
106
107         renderPanel(facesContext, layout, table, borderLayout, domContext);
108
109         if (borderLayout.getChildCount() > 0) {
110             throw new RuntimeException JavaDoc(
111                     "PanelBorder must not have children, only facets allowed!");
112         }
113
114
115         domContext.stepOver();
116         domContext.streamWrite(facesContext, uiComponent);
117     }
118
119     private void renderPanel(FacesContext facesContext, List JavaDoc facets,
120                              Element JavaDoc table, PanelBorder borderLayout,
121                              DOMContext domContext) throws IOException JavaDoc {
122         String JavaDoc order = borderLayout.getLayout();
123         if (order.equalsIgnoreCase(REVERSE_N_S)) {
124             renderSouth(facesContext, domContext, borderLayout, facets, table);
125             renderEastWestCenter(facesContext, domContext, borderLayout, facets,
126                                  table);
127             renderNorth(facesContext, domContext, borderLayout, facets, table);
128
129         } else {
130             renderNorth(facesContext, domContext, borderLayout, facets, table);
131             renderEastWestCenter(facesContext, domContext, borderLayout, facets,
132                                  table);
133             renderSouth(facesContext, domContext, borderLayout, facets, table);
134         }
135     }
136
137     private void renderEastWestCenter(FacesContext facesContext,
138                                       DOMContext domContext,
139                                       PanelBorder borderLayout, List JavaDoc facets,
140                                       Element JavaDoc table) throws IOException JavaDoc {
141         if (borderLayout.getWest() != null ||
142             borderLayout.getCenter() != null ||
143             borderLayout.getEast() != null) {
144             Element JavaDoc tr = domContext.createElement(HTML.TR_ELEM);
145             table.appendChild(tr);
146             String JavaDoc order = borderLayout.getLayout();
147             if (order.equalsIgnoreCase(REVERSE_W_E)) {
148                 renderEast(facesContext, domContext, borderLayout, facets, tr);
149                 renderCenter(facesContext, domContext, borderLayout, facets,
150                              tr);
151                 renderWest(facesContext, domContext, borderLayout, facets, tr);
152             } else {
153                 renderWest(facesContext, domContext, borderLayout, facets, tr);
154                 renderCenter(facesContext, domContext, borderLayout, facets,
155                              tr);
156                 renderEast(facesContext, domContext, borderLayout, facets, tr);
157             }
158
159         }
160     }
161
162     private void renderWest(FacesContext facesContext, DOMContext domContext,
163                             PanelBorder borderLayout, List JavaDoc facets, Element JavaDoc tr)
164             throws IOException JavaDoc {
165         if (facets.contains(PanelBorder.WEST_LAYOUT)) {
166             renderTableCells(facesContext, borderLayout, tr,
167                              PanelBorder.WEST_LAYOUT, domContext);
168         }
169     }
170
171     private void renderCenter(FacesContext facesContext, DOMContext domContext,
172                               PanelBorder borderLayout, List JavaDoc facets, Element JavaDoc tr)
173             throws IOException JavaDoc {
174         if (facets.contains(PanelBorder.CENTER_LAYOUT)) {
175             renderTableCells(facesContext, borderLayout, tr,
176                              PanelBorder.CENTER_LAYOUT, domContext);
177         }
178     }
179
180     private void renderEast(FacesContext facesContext, DOMContext domContext,
181                             PanelBorder borderLayout, List JavaDoc facets, Element JavaDoc tr)
182             throws IOException JavaDoc {
183         if (facets.contains(PanelBorder.EAST_LAYOUT)) {
184             renderTableCells(facesContext, borderLayout, tr,
185                              PanelBorder.EAST_LAYOUT, domContext);
186         }
187     }
188
189
190     private void renderNorth(FacesContext facesContext, DOMContext domContext,
191                              PanelBorder borderLayout, List JavaDoc facets,
192                              Element JavaDoc table) throws IOException JavaDoc {
193         if ((borderLayout.getNorth() != null) &&
194             (facets.contains(PanelBorder.NORTH_LAYOUT))) {
195             Element JavaDoc tr = domContext.createElement(HTML.TR_ELEM);
196             table.appendChild(tr);
197             renderTableCells(facesContext, borderLayout, tr,
198                              PanelBorder.NORTH_LAYOUT, domContext);
199         }
200     }
201
202     private void renderSouth(FacesContext facesContext, DOMContext domContext,
203                              PanelBorder borderLayout, List JavaDoc facets,
204                              Element JavaDoc table) throws IOException JavaDoc {
205         if ((borderLayout.getSouth() != null) &&
206             (facets.contains(PanelBorder.SOUTH_LAYOUT))) {
207             Element JavaDoc tr = domContext.createElement(HTML.TR_ELEM);
208             table.appendChild(tr);
209             renderTableCells(facesContext, borderLayout, tr,
210                              PanelBorder.SOUTH_LAYOUT, domContext);
211         }
212     }
213
214     private Element JavaDoc getTD(FacesContext facesContext, PanelBorder borderLayout,
215                           DOMContext domContext, Element JavaDoc tr)
216             throws IOException JavaDoc {
217         Element JavaDoc td = domContext.createElement(HTML.TD_ELEM);
218         tr.appendChild(td);
219         domContext.setCursorParent(td);
220         return td;
221     }
222
223     private void renderTableCells(FacesContext facesContext,
224                                   PanelBorder borderLayout, Element JavaDoc tr,
225                                   String JavaDoc facet, DOMContext domContext)
226             throws IOException JavaDoc {
227         UIComponent north = borderLayout.getNorth();
228         UIComponent west = borderLayout.getWest();
229         UIComponent east = borderLayout.getEast();
230         UIComponent center = borderLayout.getCenter();
231         UIComponent south = borderLayout.getSouth();
232
233         if (facet.equals(PanelBorder.NORTH_LAYOUT) && north != null) {
234             // Determine how wide the north component should be
235
// Based on if the west, east, and center are found
236
int width = 0;
237
238             if (west != null) {
239                 width++;
240             }
241             if (east != null) {
242                 width++;
243             }
244             if (center != null) {
245                 width++;
246             }
247
248             renderTableCell(facesContext, north,
249                             getTD(facesContext, borderLayout, domContext, tr),
250                             (width == 0) ? 1 : width,
251                             borderLayout.getNorthClass(), null, borderLayout);
252         }
253
254         if (facet.equals(PanelBorder.WEST_LAYOUT) && west != null) {
255             renderTableCell(facesContext, west,
256                             getTD(facesContext, borderLayout, domContext, tr),
257                             1,
258                             borderLayout.getWestClass(), null, borderLayout);
259         }
260
261         if (facet.equals(PanelBorder.EAST_LAYOUT) && east != null) {
262             renderTableCell(facesContext, east,
263                             getTD(facesContext, borderLayout, domContext, tr),
264                             1,
265                             borderLayout.getEastClass(), null, borderLayout);
266         }
267
268         if (facet.equals(PanelBorder.CENTER_LAYOUT) && center != null) {
269             renderTableCell(facesContext, center,
270                             getTD(facesContext, borderLayout, domContext, tr),
271                             1,
272                             borderLayout.getCenterClass(), null, borderLayout);
273         }
274
275         if (facet.equals(PanelBorder.SOUTH_LAYOUT) && south != null) {
276             // Determine how wide the south component should be
277
// Based on if the west, east, and center are found
278
int width = 0;
279
280             if (west != null) {
281                 width++;
282             }
283             if (east != null) {
284                 width++;
285             }
286             if (center != null) {
287                 width++;
288             }
289
290             renderTableCell(facesContext, south,
291                             getTD(facesContext, borderLayout, domContext, tr),
292                             (width == 0) ? 1 : width,
293                             borderLayout.getSouthClass(), null, borderLayout);
294         }
295     }
296
297     private void renderTableCell(FacesContext facesContext,
298                                  UIComponent component,
299                                  Element JavaDoc td, int colspan, String JavaDoc styleClass,
300                                  String JavaDoc style, PanelBorder panelBorder)
301             throws IOException JavaDoc {
302         if (colspan > 0) {
303             td.setAttribute(HTML.COLSPAN_ATTR, Integer.toString(colspan));
304         }
305         else {
306             td.removeAttribute(HTML.COLSPAN_ATTR);
307         }
308         if (styleClass != null && styleClass.length() > 0) {
309             td.setAttribute(HTML.CLASS_ATTR, styleClass);
310         }
311         else {
312             td.removeAttribute(HTML.CLASS_ATTR);
313         }
314         if (style != null && style.length() > 0) {
315             td.setAttribute(HTML.STYLE_ATTR, style);
316         }
317         else {
318             td.removeAttribute(HTML.STYLE_ATTR);
319         }
320
321         DOMContext domContext =
322                 DOMContext.getDOMContext(facesContext, panelBorder);
323         domContext.streamWrite(facesContext, panelBorder,
324                                domContext.getRootNode(), td);
325         CustomComponentUtils.renderChild(facesContext, component);
326     }
327 }
Popular Tags