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.application.*;
036: import javax.faces.component.*;
037: import javax.faces.component.html.*;
038: import javax.faces.context.*;
039: import javax.faces.convert.*;
040: import javax.faces.render.*;
041:
042: /**
043: * The HTML selectboolen/checkbox renderer
044: */
045: class HtmlBooleanCheckboxRenderer extends Renderer {
046: public static final Renderer RENDERER = new HtmlBooleanCheckboxRenderer();
047:
048: /**
049: * True if the renderer is responsible for rendering the children.
050: */
051: @Override
052: public boolean getRendersChildren() {
053: return true;
054: }
055:
056: /**
057: * Converts from the string value to a boolean value.
058: */
059: public Object getConvertedValue(FacesContext context,
060: UIComponent component, Object submittedValue)
061: throws ConverterException {
062: return Boolean.valueOf((String) submittedValue);
063: }
064:
065: /**
066: * Decodes the data from the form.
067: */
068: @Override
069: public void decode(FacesContext context, UIComponent component) {
070: String clientId = component.getClientId(context);
071:
072: ExternalContext ext = context.getExternalContext();
073: Map<String, String> paramMap = ext.getRequestParameterMap();
074:
075: String value = paramMap.get(clientId);
076:
077: if ("on".equalsIgnoreCase(value)
078: || "true".equalsIgnoreCase(value)
079: || "yes".equalsIgnoreCase(value)) {
080: ((EditableValueHolder) component).setSubmittedValue("true");
081: } else
082: ((EditableValueHolder) component)
083: .setSubmittedValue("false");
084: }
085:
086: /**
087: * Renders the open tag for the text.
088: */
089: @Override
090: public void encodeBegin(FacesContext context, UIComponent component)
091: throws IOException {
092: ResponseWriter out = context.getResponseWriter();
093:
094: String id = component.getId();
095:
096: String accesskey;
097: String dir;
098: boolean disabled;
099: String image;
100: String lang;
101:
102: String onblur;
103: String onchange;
104: String onclick;
105: String ondblclick;
106: String onfocus;
107:
108: String onkeydown;
109: String onkeypress;
110: String onkeyup;
111:
112: String onmousedown;
113: String onmousemove;
114: String onmouseout;
115: String onmouseover;
116: String onmouseup;
117:
118: String onselect;
119:
120: boolean readonly;
121: String style;
122: String styleClass;
123: String tabindex;
124: String title;
125:
126: boolean selected;
127:
128: if (component instanceof HtmlSelectBooleanCheckbox) {
129: HtmlSelectBooleanCheckbox htmlComponent = (HtmlSelectBooleanCheckbox) component;
130:
131: accesskey = htmlComponent.getAccesskey();
132: dir = htmlComponent.getDir();
133: disabled = htmlComponent.isDisabled();
134: lang = htmlComponent.getLang();
135:
136: onblur = htmlComponent.getOnblur();
137: onchange = htmlComponent.getOnchange();
138: onclick = htmlComponent.getOnclick();
139: ondblclick = htmlComponent.getOndblclick();
140: onfocus = htmlComponent.getOnfocus();
141:
142: onkeydown = htmlComponent.getOnkeydown();
143: onkeypress = htmlComponent.getOnkeypress();
144: onkeyup = htmlComponent.getOnkeyup();
145:
146: onmousedown = htmlComponent.getOnmousedown();
147: onmousemove = htmlComponent.getOnmousemove();
148: onmouseout = htmlComponent.getOnmouseout();
149: onmouseover = htmlComponent.getOnmouseover();
150: onmouseup = htmlComponent.getOnmouseup();
151:
152: onselect = htmlComponent.getOnselect();
153:
154: readonly = htmlComponent.isReadonly();
155: style = htmlComponent.getStyle();
156: styleClass = htmlComponent.getStyleClass();
157: tabindex = htmlComponent.getTabindex();
158: title = htmlComponent.getTitle();
159:
160: selected = htmlComponent.isSelected();
161: } else {
162: Map<String, Object> attrMap = component.getAttributes();
163:
164: accesskey = (String) attrMap.get("accesskey");
165: dir = (String) attrMap.get("dir");
166: disabled = Boolean.TRUE.equals(attrMap.get("disabled"));
167: image = (String) attrMap.get("image");
168: lang = (String) attrMap.get("lang");
169:
170: onblur = (String) attrMap.get("onblur");
171: onchange = (String) attrMap.get("onchange");
172: onclick = (String) attrMap.get("onclick");
173: ondblclick = (String) attrMap.get("ondblclick");
174: onfocus = (String) attrMap.get("onfocus");
175:
176: onkeydown = (String) attrMap.get("onkeydown");
177: onkeypress = (String) attrMap.get("onkeypress");
178: onkeyup = (String) attrMap.get("onkeyup");
179:
180: onmousedown = (String) attrMap.get("onmousedown");
181: onmousemove = (String) attrMap.get("onmousemove");
182: onmouseout = (String) attrMap.get("onmouseout");
183: onmouseover = (String) attrMap.get("onmouseover");
184: onmouseup = (String) attrMap.get("onmouseup");
185:
186: onselect = (String) attrMap.get("onselect");
187:
188: readonly = Boolean.TRUE.equals(attrMap.get("readonly"));
189: style = (String) attrMap.get("style");
190: styleClass = (String) attrMap.get("styleClass");
191: tabindex = (String) attrMap.get("tabindex");
192: title = (String) attrMap.get("title");
193:
194: Object selectedValue = attrMap.get("selected");
195:
196: selected = Boolean.TRUE.equals(selectedValue);
197: }
198:
199: out.startElement("input", component);
200:
201: out.writeAttribute("type", "checkbox", "type");
202:
203: out.writeAttribute("name", component.getClientId(context),
204: "name");
205:
206: if (id != null && !id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
207: out.writeAttribute("id", component.getClientId(context),
208: "id");
209:
210: if (accesskey != null)
211: out.writeAttribute("accesskey", accesskey, "accesskey");
212:
213: if (dir != null)
214: out.writeAttribute("dir", dir, "dir");
215:
216: if (disabled)
217: out.writeAttribute("disabled", "disabled", "disabled");
218:
219: if (lang != null)
220: out.writeAttribute("lang", lang, "lang");
221:
222: if (onblur != null)
223: out.writeAttribute("onblur", onblur, "onblur");
224:
225: if (onchange != null)
226: out.writeAttribute("onchange", onchange, "onchange");
227:
228: if (onclick != null)
229: out.writeAttribute("onclick", onclick, "onclick");
230:
231: if (ondblclick != null)
232: out.writeAttribute("ondblclick", ondblclick, "ondblclick");
233:
234: if (onfocus != null)
235: out.writeAttribute("onfocus", onfocus, "onfocus");
236:
237: if (onkeydown != null)
238: out.writeAttribute("onkeydown", onkeydown, "onkeydown");
239:
240: if (onkeypress != null)
241: out.writeAttribute("onkeypress", onkeypress, "onkeypress");
242:
243: if (onkeyup != null)
244: out.writeAttribute("onkeyup", onkeyup, "onkeyup");
245:
246: if (onmousedown != null)
247: out.writeAttribute("onmousedown", onmousedown,
248: "onmousedown");
249:
250: if (onmousemove != null)
251: out.writeAttribute("onmousemove", onmousemove,
252: "onmousemove");
253:
254: if (onmouseout != null)
255: out.writeAttribute("onmouseout", onmouseout, "onmouseout");
256:
257: if (onmouseover != null)
258: out.writeAttribute("onmouseover", onmouseover,
259: "onmouseover");
260:
261: if (onmouseup != null)
262: out.writeAttribute("onmouseup", onmouseup, "onmouseup");
263:
264: if (onselect != null)
265: out.writeAttribute("onselect", onselect, "onselect");
266:
267: if (readonly)
268: out.writeAttribute("readonly", "readonly", "readonly");
269:
270: if (style != null)
271: out.writeAttribute("style", style, "style");
272:
273: if (styleClass != null)
274: out.writeAttribute("class", styleClass, "class");
275:
276: if (tabindex != null)
277: out.writeAttribute("tabindex", tabindex, "tabindex");
278:
279: if (title != null)
280: out.writeAttribute("title", title, "title");
281:
282: if (selected)
283: out.writeAttribute("checked", "checked", "checked");
284:
285: out.endElement("input");
286: }
287:
288: /**
289: * Renders the content for the component.
290: */
291: @Override
292: public void encodeChildren(FacesContext context,
293: UIComponent component) throws IOException {
294: }
295:
296: /**
297: * Renders the closing tag for the component.
298: */
299: @Override
300: public void encodeEnd(FacesContext context, UIComponent component)
301: throws IOException {
302: }
303:
304: public String toString() {
305: return "HtmlInputTextRenderer[]";
306: }
307: }
|