001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsf.html;
030:
031: import java.io.*;
032: import java.util.*;
033:
034: import javax.faces.*;
035: import javax.faces.component.*;
036: import javax.faces.component.html.*;
037: import javax.faces.context.*;
038: import javax.faces.render.*;
039:
040: /**
041: * The HTML panel/grid renderer
042: */
043: class HtmlPanelGridRenderer extends Renderer {
044: public static final Renderer RENDERER = new HtmlPanelGridRenderer();
045:
046: /**
047: * True if the renderer is responsible for rendering the children.
048: */
049: @Override
050: public boolean getRendersChildren() {
051: return true;
052: }
053:
054: /**
055: * Renders the open tag for the text.
056: */
057: @Override
058: public void encodeBegin(FacesContext context, UIComponent component)
059: throws IOException {
060: ResponseWriter out = context.getResponseWriter();
061:
062: String bgcolor = null;
063: int border = -1;
064: String captionClass = null;
065: String captionStyle = null;
066: String cellpadding = null;
067: String cellspacing = null;
068: int columns;
069: String columnClasses = null;
070: String dir = null;
071: String frame = null;
072: String headerClass = null;
073: String lang = null;
074: String onclick = null;
075: String ondblclick = null;
076: String onkeydown = null;
077: String onkeypress = null;
078: String onkeyup = null;
079: String onmousedown = null;
080: String onmousemove = null;
081: String onmouseout = null;
082: String onmouseover = null;
083: String onmouseup = null;
084: String rules = null;
085: String style = null;
086: String styleClass = null;
087: String summary = null;
088: String title = null;
089: String width = null;
090:
091: String id = component.getId();
092:
093: if (component instanceof HtmlPanelGrid) {
094: HtmlPanelGrid html = (HtmlPanelGrid) component;
095:
096: bgcolor = html.getBgcolor();
097: border = html.getBorder();
098: captionClass = html.getCaptionClass();
099: captionStyle = html.getCaptionStyle();
100: cellpadding = html.getCellpadding();
101: cellspacing = html.getCellspacing();
102: columns = html.getColumns();
103: columnClasses = html.getColumnClasses();
104: dir = html.getDir();
105: frame = html.getFrame();
106: headerClass = html.getHeaderClass();
107: lang = html.getLang();
108: onclick = html.getOnclick();
109: ondblclick = html.getOndblclick();
110: onkeydown = html.getOnkeydown();
111: onkeypress = html.getOnkeypress();
112: onkeyup = html.getOnkeyup();
113: onmousedown = html.getOnmousedown();
114: onmousemove = html.getOnmousemove();
115: onmouseout = html.getOnmouseout();
116: onmouseover = html.getOnmouseover();
117: onmouseup = html.getOnmouseup();
118: rules = html.getRules();
119: style = html.getStyle();
120: styleClass = html.getStyleClass();
121: summary = html.getSummary();
122: title = html.getTitle();
123: width = html.getWidth();
124: } else {
125: Map<String, Object> attrMap = component.getAttributes();
126: Integer iValue;
127:
128: bgcolor = (String) attrMap.get("bgcolor");
129: captionClass = (String) attrMap.get("captionClass");
130: captionStyle = (String) attrMap.get("captionStyle");
131: iValue = (Integer) attrMap.get("columns");
132: columns = iValue != null ? iValue : 0;
133: headerClass = (String) attrMap.get("headerClass");
134: style = (String) attrMap.get("style");
135: styleClass = (String) attrMap.get("styleClass");
136: }
137:
138: String[] columnClassArray = null;
139:
140: if (columnClasses != null) {
141: columnClassArray = columnClasses.split("[ \t,]+");
142:
143: if (columnClassArray.length == 0)
144: columnClassArray = null;
145: }
146:
147: out.startElement("table", component);
148:
149: if (id != null && !id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
150: out.writeAttribute("id", component.getClientId(context),
151: "id");
152:
153: if (bgcolor != null)
154: out.writeAttribute("bgcolor", bgcolor, "bgcolor");
155:
156: if (border >= 0)
157: out.writeAttribute("border", border, "border");
158:
159: if (cellpadding != null)
160: out.writeAttribute("cellpadding", cellpadding,
161: "cellpadding");
162:
163: if (cellspacing != null)
164: out.writeAttribute("cellspacing", cellspacing,
165: "cellspacing");
166:
167: if (dir != null)
168: out.writeAttribute("dir", dir, "dir");
169:
170: if (frame != null)
171: out.writeAttribute("frame", frame, "frame");
172:
173: if (lang != null)
174: out.writeAttribute("lang", lang, "lang");
175:
176: if (onclick != null)
177: out.writeAttribute("onclick", onclick, "onclick");
178:
179: if (ondblclick != null)
180: out.writeAttribute("ondblclick", ondblclick, "ondblclick");
181:
182: if (onkeydown != null)
183: out.writeAttribute("onkeydown", onkeydown, "onkeydown");
184:
185: if (onkeypress != null)
186: out.writeAttribute("onkeypress", onkeypress, "onkeypress");
187:
188: if (onkeyup != null)
189: out.writeAttribute("onkeyup", onkeyup, "onkeyup");
190:
191: if (onmousedown != null)
192: out.writeAttribute("onmousedown", onmousedown,
193: "onmousedown");
194:
195: if (onmousemove != null)
196: out.writeAttribute("onmousemove", onmousemove,
197: "onmousemove");
198:
199: if (onmouseout != null)
200: out.writeAttribute("onmouseout", onmouseout, "onmouseout");
201:
202: if (onmouseover != null)
203: out.writeAttribute("onmouseover", onmouseover,
204: "onmouseover");
205:
206: if (onmouseup != null)
207: out.writeAttribute("onmouseup", onmouseup, "onmouseup");
208:
209: if (rules != null)
210: out.writeAttribute("rules", rules, "rules");
211:
212: if (style != null)
213: out.writeAttribute("style", style, "style");
214:
215: if (styleClass != null)
216: out.writeAttribute("class", styleClass, "styleClass");
217:
218: if (summary != null)
219: out.writeAttribute("summary", summary, "summary");
220:
221: if (title != null)
222: out.writeAttribute("title", title, "title");
223:
224: if (width != null)
225: out.writeAttribute("width", width, "width");
226:
227: out.write("\n");
228:
229: UIComponent caption = component.getFacet("caption");
230: if (caption != null && caption.isRendered()) {
231: out.startElement("caption", caption);
232:
233: if (captionClass != null)
234: out.writeAttribute("class", captionClass,
235: "captionClass");
236:
237: if (captionStyle != null)
238: out.writeAttribute("style", captionStyle,
239: "captionStyle");
240:
241: caption.encodeAll(context);
242:
243: out.endElement("caption");
244: }
245:
246: UIComponent header = component.getFacet("header");
247: if (header != null && header.isRendered()) {
248: out.startElement("thead", header);
249:
250: out.startElement("tr", header);
251: out.startElement("th", header);
252:
253: if (headerClass != null)
254: out.writeAttribute("class", headerClass, "headerClass");
255:
256: if (columns > 0)
257: out.writeAttribute("colspan", columns, "columns");
258:
259: out.writeAttribute("scope", "colgroup", "scope");
260:
261: header.encodeAll(context);
262:
263: out.endElement("th");
264: out.endElement("tr");
265:
266: out.endElement("thead");
267: out.write("\n");
268: }
269: }
270:
271: /**
272: * Renders the content for the component.
273: */
274: @Override
275: public void encodeChildren(FacesContext context,
276: UIComponent component) throws IOException {
277: String columnClasses;
278: String rowClasses;
279: int columns = 0;
280:
281: ResponseWriter out = context.getResponseWriter();
282:
283: if (component instanceof HtmlPanelGrid) {
284: HtmlPanelGrid html = (HtmlPanelGrid) component;
285:
286: columnClasses = html.getColumnClasses();
287: rowClasses = html.getRowClasses();
288: columns = html.getColumns();
289: } else {
290: Map<String, Object> attrMap = component.getAttributes();
291:
292: columnClasses = (String) attrMap.get("columnClasses");
293: rowClasses = (String) attrMap.get("rowClasses");
294: Integer iValue = (Integer) attrMap.get("columns");
295: columns = iValue != null ? iValue : 0;
296: }
297:
298: String[] columnClassArray = null;
299:
300: if (columnClasses != null) {
301: columnClassArray = columnClasses.split("[ \t,]+");
302:
303: if (columnClassArray.length == 0)
304: columnClassArray = null;
305: }
306:
307: String[] rowClassArray = null;
308:
309: if (rowClasses != null) {
310: rowClassArray = rowClasses.split("[ \t,]+");
311:
312: if (rowClassArray.length == 0)
313: rowClassArray = null;
314: }
315:
316: int size = component.getChildCount();
317: if (size == 0)
318: return;
319:
320: if (columns < 1)
321: columns = 1;
322:
323: List<UIComponent> children = component.getChildren();
324: int count = 0;
325:
326: out.startElement("tbody", component);
327: out.write("\n");
328:
329: for (int i = 0; i < size; i++) {
330: UIComponent child = children.get(i);
331:
332: if (!child.isRendered())
333: continue;
334:
335: int column = count % columns;
336: int row = count / columns;
337:
338: if (column == 0) {
339: if (count > 0) {
340: out.endElement("tr");
341: out.write("\n");
342: }
343:
344: out.startElement("tr", child);
345:
346: if (rowClassArray != null) {
347: String v = rowClassArray[row % rowClassArray.length];
348:
349: out.writeAttribute("class", v, "rowClasses");
350: }
351: }
352:
353: count++;
354:
355: out.startElement("td", child);
356: if (columnClassArray != null) {
357: String v = columnClassArray[column
358: % columnClassArray.length];
359:
360: out.writeAttribute("class", v, "columnClasses");
361: }
362:
363: if (child instanceof UIColumn) {
364: int subCount = child.getChildCount();
365:
366: for (int j = 0; j < subCount; j++) {
367: UIComponent subChild = child.getChildren().get(j);
368:
369: subChild.encodeAll(context);
370: }
371: } else {
372: child.encodeAll(context);
373: }
374: out.endElement("td");
375: }
376:
377: if (count > 0) {
378: out.endElement("tr");
379: out.write("\n");
380: }
381:
382: out.endElement("tbody");
383: out.write("\n");
384: }
385:
386: /**
387: * Renders the closing tag for the component.
388: */
389: @Override
390: public void encodeEnd(FacesContext context, UIComponent component)
391: throws IOException {
392: String footerClass;
393: int columns;
394:
395: ResponseWriter out = context.getResponseWriter();
396:
397: if (component instanceof HtmlPanelGrid) {
398: HtmlPanelGrid html = (HtmlPanelGrid) component;
399:
400: columns = html.getColumns();
401: footerClass = html.getFooterClass();
402: } else {
403: Map<String, Object> attrMap = component.getAttributes();
404:
405: columns = (Integer) attrMap.get("columns");
406: footerClass = (String) attrMap.get("footerClass");
407: }
408:
409: UIComponent footer = component.getFacet("footer");
410: if (footer != null && footer.isRendered()) {
411: out.startElement("tfoot", footer);
412:
413: out.startElement("tr", footer);
414: out.startElement("td", footer);
415:
416: if (columns > 0)
417: out.writeAttribute("colspan", columns, "columns");
418:
419: if (footerClass != null)
420: out.writeAttribute("class", footerClass, "footerClass");
421:
422: //out.writeAttribute("scope", "colgroup", "scope");
423:
424: footer.encodeAll(context);
425:
426: out.endElement("td");
427: out.endElement("tr");
428:
429: out.endElement("tfoot");
430: out.write("\n");
431: }
432:
433: out.endElement("table");
434: out.write("\n");
435: }
436:
437: public String toString() {
438: return "HtmlPanelGridRenderer[]";
439: }
440: }
|