001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.component.panelborder;
035:
036: import com.icesoft.faces.component.util.CustomComponentUtils;
037: import com.icesoft.faces.context.DOMContext;
038: import com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer;
039: import com.icesoft.faces.renderkit.dom_html_basic.HTML;
040: import com.icesoft.faces.renderkit.dom_html_basic.PassThruAttributeRenderer;
041: import org.w3c.dom.Element;
042:
043: import javax.faces.component.UIComponent;
044: import javax.faces.context.FacesContext;
045: import java.io.IOException;
046: import java.util.List;
047:
048: /**
049: * This PanelBorderRenderer is responsible for rendering PanelBorder
050: * components.
051: */
052: public class PanelBorderRenderer extends DomBasicRenderer {
053: public static final String DEFAULT_LAYOUT = "default";
054: public static final String REVERSE_W_E = "horizontal reverse";
055: public static final String REVERSE_N_S = "vertical reverse";
056: public static final String CENTER_ONLY = "center only";
057: public static final String HIDE_N = "hide north";
058: public static final String HIDE_E = "hide east";
059: public static final String HIDE_S = "hide south";
060: public static final String HIDE_W = "hide west";
061:
062: /* (non-Javadoc)
063: * @see javax.faces.render.Renderer#getRendersChildren()
064: */
065: public boolean getRendersChildren() {
066: return true;
067: }
068:
069: /* (non-Javadoc)
070: * @see javax.faces.render.Renderer#encodeChildren(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
071: */
072: public void encodeChildren(FacesContext facesContext,
073: UIComponent uiComponent) throws IOException {
074: super .encodeChildren(facesContext, uiComponent);
075: }
076:
077: /* (non-Javadoc)
078: * @see com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer#encodeEnd(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
079: */
080: public void encodeEnd(FacesContext facesContext,
081: UIComponent uiComponent) throws IOException {
082: validateParameters(facesContext, uiComponent, PanelBorder.class);
083:
084: DOMContext domContext = DOMContext.attachDOMContext(
085: facesContext, uiComponent);
086: PanelBorder borderLayout = (PanelBorder) uiComponent;
087: List layout = borderLayout.getLayoutAsList();
088: String clientId = uiComponent.getClientId(facesContext);
089: if (!domContext.isInitialized()) {
090: Element table = domContext
091: .createRootElement(HTML.TABLE_ELEM);
092: setRootElementId(facesContext, table, uiComponent);
093: table.setAttribute(HTML.NAME_ATTR, clientId);
094: }
095:
096: Element table = (Element) domContext.getRootNode();
097: PassThruAttributeRenderer.renderAttributes(facesContext,
098: uiComponent, null);
099: table.setAttribute(HTML.CLASS_ATTR, borderLayout
100: .getStyleClass());
101: String style = borderLayout.getStyle();
102: if (style != null && style.length() > 0)
103: table.setAttribute(HTML.STYLE_ATTR, style);
104: else
105: table.removeAttribute(HTML.STYLE_ATTR);
106:
107: DOMContext.removeChildren(table);
108:
109: renderPanel(facesContext, layout, table, borderLayout,
110: domContext);
111:
112: if (borderLayout.getChildCount() > 0) {
113: throw new RuntimeException(
114: "PanelBorder must not have children, only facets allowed!");
115: }
116:
117: domContext.stepOver();
118: domContext.streamWrite(facesContext, uiComponent);
119: }
120:
121: private void renderPanel(FacesContext facesContext, List facets,
122: Element table, PanelBorder borderLayout,
123: DOMContext domContext) throws IOException {
124: String order = borderLayout.getLayout();
125: if (order.equalsIgnoreCase(REVERSE_N_S)) {
126: renderSouth(facesContext, domContext, borderLayout, facets,
127: table);
128: renderEastWestCenter(facesContext, domContext,
129: borderLayout, facets, table);
130: renderNorth(facesContext, domContext, borderLayout, facets,
131: table);
132:
133: } else {
134: renderNorth(facesContext, domContext, borderLayout, facets,
135: table);
136: renderEastWestCenter(facesContext, domContext,
137: borderLayout, facets, table);
138: renderSouth(facesContext, domContext, borderLayout, facets,
139: table);
140: }
141: }
142:
143: private void renderEastWestCenter(FacesContext facesContext,
144: DOMContext domContext, PanelBorder borderLayout,
145: List facets, Element table) throws IOException {
146: if (borderLayout.getWest() != null
147: || borderLayout.getCenter() != null
148: || borderLayout.getEast() != null) {
149: Element tr = domContext.createElement(HTML.TR_ELEM);
150: table.appendChild(tr);
151: String order = borderLayout.getLayout();
152: if (order.equalsIgnoreCase(REVERSE_W_E)) {
153: renderEast(facesContext, domContext, borderLayout,
154: facets, tr);
155: renderCenter(facesContext, domContext, borderLayout,
156: facets, tr);
157: renderWest(facesContext, domContext, borderLayout,
158: facets, tr);
159: } else {
160: renderWest(facesContext, domContext, borderLayout,
161: facets, tr);
162: renderCenter(facesContext, domContext, borderLayout,
163: facets, tr);
164: renderEast(facesContext, domContext, borderLayout,
165: facets, tr);
166: }
167:
168: }
169: }
170:
171: private void renderWest(FacesContext facesContext,
172: DOMContext domContext, PanelBorder borderLayout,
173: List facets, Element tr) throws IOException {
174: if (facets.contains(PanelBorder.WEST_LAYOUT)) {
175: renderTableCells(facesContext, borderLayout, tr,
176: PanelBorder.WEST_LAYOUT, domContext);
177: }
178: }
179:
180: private void renderCenter(FacesContext facesContext,
181: DOMContext domContext, PanelBorder borderLayout,
182: List facets, Element tr) throws IOException {
183: if (facets.contains(PanelBorder.CENTER_LAYOUT)) {
184: renderTableCells(facesContext, borderLayout, tr,
185: PanelBorder.CENTER_LAYOUT, domContext);
186: }
187: }
188:
189: private void renderEast(FacesContext facesContext,
190: DOMContext domContext, PanelBorder borderLayout,
191: List facets, Element tr) throws IOException {
192: if (facets.contains(PanelBorder.EAST_LAYOUT)) {
193: renderTableCells(facesContext, borderLayout, tr,
194: PanelBorder.EAST_LAYOUT, domContext);
195: }
196: }
197:
198: private void renderNorth(FacesContext facesContext,
199: DOMContext domContext, PanelBorder borderLayout,
200: List facets, Element table) throws IOException {
201: if ((borderLayout.getNorth() != null)
202: && (facets.contains(PanelBorder.NORTH_LAYOUT))) {
203: Element tr = domContext.createElement(HTML.TR_ELEM);
204: table.appendChild(tr);
205: renderTableCells(facesContext, borderLayout, tr,
206: PanelBorder.NORTH_LAYOUT, domContext);
207: }
208: }
209:
210: private void renderSouth(FacesContext facesContext,
211: DOMContext domContext, PanelBorder borderLayout,
212: List facets, Element table) throws IOException {
213: if ((borderLayout.getSouth() != null)
214: && (facets.contains(PanelBorder.SOUTH_LAYOUT))) {
215: Element tr = domContext.createElement(HTML.TR_ELEM);
216: table.appendChild(tr);
217: renderTableCells(facesContext, borderLayout, tr,
218: PanelBorder.SOUTH_LAYOUT, domContext);
219: }
220: }
221:
222: private Element getTD(FacesContext facesContext,
223: PanelBorder borderLayout, DOMContext domContext, Element tr)
224: throws IOException {
225: Element td = domContext.createElement(HTML.TD_ELEM);
226: tr.appendChild(td);
227: domContext.setCursorParent(td);
228: return td;
229: }
230:
231: private void renderTableCells(FacesContext facesContext,
232: PanelBorder borderLayout, Element tr, String facet,
233: DOMContext domContext) throws IOException {
234: UIComponent north = borderLayout.getNorth();
235: UIComponent west = borderLayout.getWest();
236: UIComponent east = borderLayout.getEast();
237: UIComponent center = borderLayout.getCenter();
238: UIComponent south = borderLayout.getSouth();
239:
240: if (facet.equals(PanelBorder.NORTH_LAYOUT) && north != null) {
241: // Determine how wide the north component should be
242: // Based on if the west, east, and center are found
243: int width = 0;
244:
245: if (west != null) {
246: width++;
247: }
248: if (east != null) {
249: width++;
250: }
251: if (center != null) {
252: width++;
253: }
254:
255: renderTableCell(facesContext, north, getTD(facesContext,
256: borderLayout, domContext, tr), (width == 0) ? 1
257: : width, borderLayout.getNorthClass(), null,
258: borderLayout);
259: }
260:
261: if (facet.equals(PanelBorder.WEST_LAYOUT) && west != null) {
262: renderTableCell(facesContext, west, getTD(facesContext,
263: borderLayout, domContext, tr), 1, borderLayout
264: .getWestClass(), null, borderLayout);
265: }
266:
267: if (facet.equals(PanelBorder.EAST_LAYOUT) && east != null) {
268: renderTableCell(facesContext, east, getTD(facesContext,
269: borderLayout, domContext, tr), 1, borderLayout
270: .getEastClass(), null, borderLayout);
271: }
272:
273: if (facet.equals(PanelBorder.CENTER_LAYOUT) && center != null) {
274: renderTableCell(facesContext, center, getTD(facesContext,
275: borderLayout, domContext, tr), 1, borderLayout
276: .getCenterClass(), null, borderLayout);
277: }
278:
279: if (facet.equals(PanelBorder.SOUTH_LAYOUT) && south != null) {
280: // Determine how wide the south component should be
281: // Based on if the west, east, and center are found
282: int width = 0;
283:
284: if (west != null) {
285: width++;
286: }
287: if (east != null) {
288: width++;
289: }
290: if (center != null) {
291: width++;
292: }
293:
294: renderTableCell(facesContext, south, getTD(facesContext,
295: borderLayout, domContext, tr), (width == 0) ? 1
296: : width, borderLayout.getSouthClass(), null,
297: borderLayout);
298: }
299: }
300:
301: private void renderTableCell(FacesContext facesContext,
302: UIComponent component, Element td, int colspan,
303: String styleClass, String style, PanelBorder panelBorder)
304: throws IOException {
305: if (colspan > 0) {
306: td.setAttribute(HTML.COLSPAN_ATTR, Integer
307: .toString(colspan));
308: } else {
309: td.removeAttribute(HTML.COLSPAN_ATTR);
310: }
311: if (styleClass != null && styleClass.length() > 0) {
312: td.setAttribute(HTML.CLASS_ATTR, styleClass);
313: } else {
314: td.removeAttribute(HTML.CLASS_ATTR);
315: }
316: if (style != null && style.length() > 0) {
317: td.setAttribute(HTML.STYLE_ATTR, style);
318: } else {
319: td.removeAttribute(HTML.STYLE_ATTR);
320: }
321:
322: DOMContext domContext = DOMContext.getDOMContext(facesContext,
323: panelBorder);
324: domContext.streamWrite(facesContext, panelBorder, domContext
325: .getRootNode(), td);
326: CustomComponentUtils.renderChild(facesContext, component);
327: }
328: }
|