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.component.*;
035: import javax.faces.component.html.*;
036: import javax.faces.context.*;
037: import javax.faces.render.*;
038: import javax.faces.model.SelectItemGroup;
039: import javax.faces.model.SelectItem;
040:
041: /**
042: * The HTML selectMany/checkbox renderer
043: */
044: class HtmlSelectManyCheckboxRenderer extends SelectRenderer {
045: public static final Renderer RENDERER = new HtmlSelectManyCheckboxRenderer();
046:
047: /**
048: * True if the renderer is responsible for rendering the children.
049: */
050: @Override
051: public boolean getRendersChildren() {
052: return true;
053: }
054:
055: /**
056: * Decodes the data from the form.
057: */
058: @Override
059: public void decode(FacesContext context, UIComponent component) {
060: String clientId = component.getClientId(context);
061:
062: ExternalContext ext = context.getExternalContext();
063: Map<String, String[]> paramMap = ext
064: .getRequestParameterValuesMap();
065:
066: String[] value = paramMap.get(clientId);
067:
068: if (value != null)
069: ((EditableValueHolder) component).setSubmittedValue(value);
070: }
071:
072: /**
073: * Renders the open tag for the text.
074: */
075: @Override
076: public void encodeBegin(FacesContext context,
077: final UIComponent component) throws IOException {
078: final ResponseWriter out = context.getResponseWriter();
079:
080: final int border;
081: final boolean disabled;
082: final String layout;
083:
084: final String style;
085: final String styleClass;
086:
087: if (component instanceof HtmlSelectManyCheckbox) {
088: HtmlSelectManyCheckbox htmlComponent = (HtmlSelectManyCheckbox) component;
089:
090: border = htmlComponent.getBorder();
091: disabled = htmlComponent.isDisabled();
092: layout = htmlComponent.getLayout();
093:
094: style = htmlComponent.getStyle();
095: styleClass = htmlComponent.getStyleClass();
096:
097: } else {
098: Map<String, Object> attrMap = component.getAttributes();
099:
100: border = (Integer) attrMap.get("border");
101: disabled = (Boolean) attrMap.get("disabled");
102: layout = (String) attrMap.get("layout");
103:
104: style = (String) attrMap.get("style");
105: styleClass = (String) attrMap.get("styleClass");
106:
107: }
108:
109: out.startElement("table", component);
110:
111: if (border > 0)
112: out.writeAttribute("border", border, "border");
113:
114: if (style != null)
115: out.writeAttribute("style", style, "style");
116:
117: if (styleClass != null)
118: out.writeAttribute("class", styleClass, "class");
119:
120: if (disabled)
121: out.writeAttribute("disabled", "disabled", "disabled");
122:
123: out.write("\n");
124:
125: if (!"pageDirection".equals(layout)) {
126: out.startElement("tr", component);
127: out.write("\n");
128: }
129: }
130:
131: /**
132: * Renders the content for the component.
133: */
134: @Override
135: public void encodeChildren(FacesContext context,
136: UIComponent component) throws IOException {
137: final ResponseWriter out = context.getResponseWriter();
138:
139: String id = component.getId();
140:
141: final String accesskey;
142: final int border;
143: final String dir;
144: final boolean disabled;
145: final String disabledClass;
146: final String enabledClass;
147: final String lang;
148: final String layout;
149:
150: final String onblur;
151: final String onchange;
152: final String onclick;
153: final String ondblclick;
154: final String onfocus;
155:
156: final String onkeydown;
157: final String onkeypress;
158: final String onkeyup;
159:
160: final String onmousedown;
161: final String onmousemove;
162: final String onmouseout;
163: final String onmouseover;
164: final String onmouseup;
165:
166: final String onselect;
167:
168: final boolean readonly;
169: final String style;
170: final String styleClass;
171: final String tabindex;
172: final String title;
173: final Object value;
174:
175: if (component instanceof HtmlSelectManyCheckbox) {
176: HtmlSelectManyCheckbox htmlComponent = (HtmlSelectManyCheckbox) component;
177:
178: accesskey = htmlComponent.getAccesskey();
179: border = htmlComponent.getBorder();
180: dir = htmlComponent.getDir();
181: disabled = htmlComponent.isDisabled();
182: disabledClass = htmlComponent.getDisabledClass();
183: enabledClass = htmlComponent.getEnabledClass();
184: lang = htmlComponent.getLang();
185: layout = htmlComponent.getLayout();
186:
187: onblur = htmlComponent.getOnblur();
188: onchange = htmlComponent.getOnchange();
189: onclick = htmlComponent.getOnclick();
190: ondblclick = htmlComponent.getOndblclick();
191: onfocus = htmlComponent.getOnfocus();
192:
193: onkeydown = htmlComponent.getOnkeydown();
194: onkeypress = htmlComponent.getOnkeypress();
195: onkeyup = htmlComponent.getOnkeyup();
196:
197: onmousedown = htmlComponent.getOnmousedown();
198: onmousemove = htmlComponent.getOnmousemove();
199: onmouseout = htmlComponent.getOnmouseout();
200: onmouseover = htmlComponent.getOnmouseover();
201: onmouseup = htmlComponent.getOnmouseup();
202:
203: onselect = htmlComponent.getOnselect();
204:
205: readonly = htmlComponent.isReadonly();
206: style = htmlComponent.getStyle();
207: styleClass = htmlComponent.getStyleClass();
208: tabindex = htmlComponent.getTabindex();
209: title = htmlComponent.getTitle();
210:
211: value = htmlComponent.getValue();
212: } else {
213: Map<String, Object> attrMap = component.getAttributes();
214:
215: accesskey = (String) attrMap.get("accesskey");
216: border = (Integer) attrMap.get("border");
217: dir = (String) attrMap.get("dir");
218: disabled = (Boolean) attrMap.get("disabled");
219: disabledClass = (String) attrMap.get("disabledClass");
220: enabledClass = (String) attrMap.get("enabledClass");
221: lang = (String) attrMap.get("lang");
222: layout = (String) attrMap.get("layout");
223:
224: onblur = (String) attrMap.get("onblur");
225: onchange = (String) attrMap.get("onchange");
226: onclick = (String) attrMap.get("onclick");
227: ondblclick = (String) attrMap.get("ondblclick");
228: onfocus = (String) attrMap.get("onfocus");
229:
230: onkeydown = (String) attrMap.get("onkeydown");
231: onkeypress = (String) attrMap.get("onkeypress");
232: onkeyup = (String) attrMap.get("onkeyup");
233:
234: onmousedown = (String) attrMap.get("onmousedown");
235: onmousemove = (String) attrMap.get("onmousemove");
236: onmouseout = (String) attrMap.get("onmouseout");
237: onmouseover = (String) attrMap.get("onmouseover");
238: onmouseup = (String) attrMap.get("onmouseup");
239:
240: onselect = (String) attrMap.get("onselect");
241:
242: readonly = (Boolean) attrMap.get("readonly");
243: style = (String) attrMap.get("style");
244: styleClass = (String) attrMap.get("styleClass");
245: tabindex = (String) attrMap.get("tabindex");
246: title = (String) attrMap.get("title");
247:
248: value = attrMap.get("value");
249: }
250:
251: final String clientId = component.getClientId(context);
252:
253: List<SelectItem> list = super .accrueSelectItems(component);
254: int counter = 0;
255: for (int i = 0; i < list.size(); i++) {
256: SelectItem selectItem = list.get(i);
257: if (selectItem instanceof SelectItemGroup) {
258:
259: if ("pageDirection".equals(layout)) {
260: out.startElement("tr", component);
261: out.write("\n");
262: }
263:
264: out.startElement("td", component);
265:
266: out.write("\n");
267:
268: out.startElement("table", component);
269:
270: if (border > 0)
271: out.writeAttribute("border", border, "border");
272:
273: if (style != null)
274: out.writeAttribute("style", style, "style");
275:
276: if (styleClass != null)
277: out.writeAttribute("class", styleClass, "class");
278:
279: if (disabled)
280: out.writeAttribute("disabled", "disabled",
281: "disabled");
282:
283: out.write("\n");
284:
285: if (!"pageDirection".equals(layout)) {
286: out.startElement("tr", component);
287: out.write("\n");
288: }
289:
290: SelectItem[] items = ((SelectItemGroup) selectItem)
291: .getSelectItems();
292:
293: for (int j = 0; j < items.length; j++) {
294:
295: SelectItem item = items[j];
296:
297: encodeChild(out, component, value, item, clientId,
298: layout, accesskey, disabled, dir, lang,
299: onblur, onchange, onclick, ondblclick,
300: onfocus, onkeydown, onkeypress, onkeyup,
301: onmousedown, onmousemove, onmouseout,
302: onmouseover, onmouseup, onselect, readonly,
303: tabindex, title, disabledClass,
304: enabledClass, counter++);
305:
306: }
307:
308: if (!"pageDirection".equals(layout)) {
309: out.endElement("tr");
310: out.write("\n");
311: }
312:
313: out.endElement("table");
314: out.write("\n");
315:
316: out.endElement("td");
317: out.write("\n");
318:
319: if ("pageDirection".equals(layout)) {
320: out.endElement("tr");
321: out.write("\n");
322: }
323:
324: } else {
325: encodeChild(out, component, value, selectItem,
326: clientId, layout, accesskey, disabled, dir,
327: lang, onblur, onchange, onclick, ondblclick,
328: onfocus, onkeydown, onkeypress, onkeyup,
329: onmousedown, onmousemove, onmouseout,
330: onmouseover, onmouseup, onselect, readonly,
331: tabindex, title, disabledClass, enabledClass,
332: counter++);
333: }
334: }
335: }
336:
337: /**
338: * Renders the closing tag for the component.
339: */
340: @Override
341: public void encodeEnd(FacesContext context, UIComponent component)
342: throws IOException {
343: final ResponseWriter out = context.getResponseWriter();
344:
345: final String layout;
346:
347: if (component instanceof HtmlSelectManyCheckbox) {
348: layout = ((HtmlSelectManyCheckbox) component).getLayout();
349: } else {
350: layout = (String) component.getAttributes().get("layout");
351: }
352: if (!"pageDirection".equals(layout)) {
353: out.endElement("tr");
354: out.write("\n");
355: }
356:
357: out.endElement("table");
358: out.write("\n");
359: }
360:
361: private void encodeChild(ResponseWriter out, UIComponent component,
362: Object value, SelectItem selectItem, String clientId,
363: String layout, String accesskey, boolean disabled,
364: String dir, String lang, String onblur, String onchange,
365: String onclick, String ondblclick, String onfocus,
366: String onkeydown, String onkeypress, String onkeyup,
367: String onmousedown, String onmousemove, String onmouseout,
368: String onmouseover, String onmouseup, String onselect,
369: boolean readonly, String tabindex, String title,
370: String disabledClass, String enabledClass, int counter)
371: throws IOException {
372: String childId = clientId + ":" + counter;
373:
374: if ("pageDirection".equals(layout)) {
375: out.startElement("tr", component);
376: out.write("\n");
377: }
378:
379: out.startElement("td", component);
380:
381: out.startElement("input", component);
382: out.writeAttribute("id", childId, "id");
383: out.writeAttribute("name", clientId, "name");
384: out.writeAttribute("type", "checkbox", "type");
385:
386: if (selectItem.isDisabled() || disabled)
387: out.writeAttribute("disabled", "disabled", "disabled");
388:
389: if (value instanceof Object[]) {
390: Object[] values = (Object[]) value;
391:
392: for (int j = 0; j < values.length; j++) {
393: if (values[j].equals(selectItem.getValue())) {
394: out.writeAttribute("checked", "checked", "value");
395: break;
396: }
397: }
398:
399: }
400:
401: if (accesskey != null)
402: out.writeAttribute("accesskey", accesskey, "accesskey");
403:
404: if (dir != null)
405: out.writeAttribute("dir", dir, "dir");
406:
407: if (lang != null)
408: out.writeAttribute("lang", lang, "lang");
409:
410: if (onblur != null)
411: out.writeAttribute("onblur", onblur, "onblur");
412:
413: if (onchange != null)
414: out.writeAttribute("onchange", onchange, "onchange");
415:
416: if (onclick != null)
417: out.writeAttribute("onclick", onclick, "onclick");
418:
419: if (ondblclick != null)
420: out.writeAttribute("ondblclick", ondblclick, "ondblclick");
421:
422: if (onfocus != null)
423: out.writeAttribute("onfocus", onfocus, "onfocus");
424:
425: if (onkeydown != null)
426: out.writeAttribute("onkeydown", onkeydown, "onkeydown");
427:
428: if (onkeypress != null)
429: out.writeAttribute("onkeypress", onkeypress, "onkeypress");
430:
431: if (onkeyup != null)
432: out.writeAttribute("onkeyup", onkeyup, "onkeyup");
433:
434: if (onmousedown != null)
435: out.writeAttribute("onmousedown", onmousedown,
436: "onmousedown");
437:
438: if (onmousemove != null)
439: out.writeAttribute("onmousemove", onmousemove,
440: "onmousemove");
441:
442: if (onmouseout != null)
443: out.writeAttribute("onmouseout", onmouseout, "onmouseout");
444:
445: if (onmouseover != null)
446: out.writeAttribute("onmouseover", onmouseover,
447: "onmouseover");
448:
449: if (onmouseup != null)
450: out.writeAttribute("onmouseup", onmouseup, "onmouseup");
451:
452: if (onselect != null)
453: out.writeAttribute("onselect", onselect, "onselect");
454:
455: if (readonly)
456: out.writeAttribute("readonly", "readonly", "readonly");
457:
458: if (tabindex != null)
459: out.writeAttribute("tabindex", tabindex, "tabindex");
460:
461: if (title != null)
462: out.writeAttribute("title", title, "title");
463:
464: Object itemValue = selectItem.getValue();
465: if (itemValue != null)
466: out.writeAttribute("value", String.valueOf(itemValue),
467: "value");
468:
469: out.endElement("input");
470:
471: if (selectItem.getLabel() != null) {
472: out.startElement("label", component);
473: out.writeAttribute("for", childId, "for");
474:
475: if (selectItem.isDisabled() || disabled) {
476: if (disabledClass != null)
477: out.writeAttribute("class", disabledClass,
478: "disabledClass");
479: } else {
480: if (enabledClass != null)
481: out.writeAttribute("class", enabledClass,
482: "enabledClass");
483: }
484:
485: out.writeText(selectItem.getLabel(), "itemLabel");
486:
487: out.endElement("label");
488: }
489:
490: out.endElement("td");
491: out.write("\n");
492:
493: if ("pageDirection".equals(layout)) {
494: out.endElement("tr");
495: out.write("\n");
496: }
497:
498: }
499:
500: public String toString() {
501: return "HtmlInputTextRenderer []";
502: }
503: }
|