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.model.*;
038: import javax.faces.render.*;
039:
040: /**
041: * The HTML selectOne/radio renderer
042: */
043: class HtmlSelectOneRadioRenderer extends SelectRenderer {
044: public static final Renderer RENDERER = new HtmlSelectOneRadioRenderer();
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: * Decodes the data from the form.
056: */
057: @Override
058: public void decode(FacesContext context, UIComponent component) {
059: String clientId = component.getClientId(context);
060:
061: ExternalContext ext = context.getExternalContext();
062: Map<String, String[]> paramMap = ext
063: .getRequestParameterValuesMap();
064:
065: String[] value = paramMap.get(clientId);
066:
067: if (value != null && value.length > 0)
068: ((EditableValueHolder) component)
069: .setSubmittedValue(value[0]);
070: }
071:
072: /**
073: * Renders the open tag for the text.
074: */
075: @Override
076: public void encodeBegin(FacesContext context, UIComponent component)
077: throws IOException {
078: ResponseWriter out = context.getResponseWriter();
079:
080: String id = component.getId();
081:
082: String accesskey;
083: int border;
084: String dir;
085: boolean disabled;
086: String disabledClass;
087: String enabledClass;
088: String lang;
089: String layout;
090:
091: String onblur;
092: String onchange;
093: String onclick;
094: String ondblclick;
095: String onfocus;
096:
097: String onkeydown;
098: String onkeypress;
099: String onkeyup;
100:
101: String onmousedown;
102: String onmousemove;
103: String onmouseout;
104: String onmouseover;
105: String onmouseup;
106:
107: String onselect;
108:
109: boolean readonly;
110: String style;
111: String styleClass;
112: String tabindex;
113: String title;
114: Object value;
115:
116: if (component instanceof HtmlSelectOneRadio) {
117: HtmlSelectOneRadio htmlComponent = (HtmlSelectOneRadio) component;
118:
119: accesskey = htmlComponent.getAccesskey();
120: border = htmlComponent.getBorder();
121: dir = htmlComponent.getDir();
122: disabled = htmlComponent.isDisabled();
123: disabledClass = htmlComponent.getDisabledClass();
124: enabledClass = htmlComponent.getEnabledClass();
125: lang = htmlComponent.getLang();
126: layout = htmlComponent.getLayout();
127:
128: onblur = htmlComponent.getOnblur();
129: onchange = htmlComponent.getOnchange();
130: onclick = htmlComponent.getOnclick();
131: ondblclick = htmlComponent.getOndblclick();
132: onfocus = htmlComponent.getOnfocus();
133:
134: onkeydown = htmlComponent.getOnkeydown();
135: onkeypress = htmlComponent.getOnkeypress();
136: onkeyup = htmlComponent.getOnkeyup();
137:
138: onmousedown = htmlComponent.getOnmousedown();
139: onmousemove = htmlComponent.getOnmousemove();
140: onmouseout = htmlComponent.getOnmouseout();
141: onmouseover = htmlComponent.getOnmouseover();
142: onmouseup = htmlComponent.getOnmouseup();
143:
144: onselect = htmlComponent.getOnselect();
145:
146: readonly = htmlComponent.isReadonly();
147: style = htmlComponent.getStyle();
148: styleClass = htmlComponent.getStyleClass();
149: tabindex = htmlComponent.getTabindex();
150: title = htmlComponent.getTitle();
151:
152: value = htmlComponent.getValue();
153: } else {
154: Map<String, Object> attrMap = component.getAttributes();
155: Integer iValue;
156:
157: accesskey = (String) attrMap.get("accesskey");
158:
159: iValue = (Integer) attrMap.get("border");
160: border = iValue != null ? iValue : 0;
161: dir = (String) attrMap.get("dir");
162: disabled = Boolean.TRUE.equals(attrMap.get("disabled"));
163: disabledClass = (String) attrMap.get("disabledClass");
164: enabledClass = (String) attrMap.get("enabledClass");
165: lang = (String) attrMap.get("lang");
166: layout = (String) attrMap.get("layout");
167:
168: onblur = (String) attrMap.get("onblur");
169: onchange = (String) attrMap.get("onchange");
170: onclick = (String) attrMap.get("onclick");
171: ondblclick = (String) attrMap.get("ondblclick");
172: onfocus = (String) attrMap.get("onfocus");
173:
174: onkeydown = (String) attrMap.get("onkeydown");
175: onkeypress = (String) attrMap.get("onkeypress");
176: onkeyup = (String) attrMap.get("onkeyup");
177:
178: onmousedown = (String) attrMap.get("onmousedown");
179: onmousemove = (String) attrMap.get("onmousemove");
180: onmouseout = (String) attrMap.get("onmouseout");
181: onmouseover = (String) attrMap.get("onmouseover");
182: onmouseup = (String) attrMap.get("onmouseup");
183:
184: onselect = (String) attrMap.get("onselect");
185:
186: readonly = Boolean.TRUE.equals(attrMap.get("readonly"));
187: style = (String) attrMap.get("style");
188: styleClass = (String) attrMap.get("styleClass");
189: tabindex = (String) attrMap.get("tabindex");
190: title = (String) attrMap.get("title");
191:
192: value = attrMap.get("value");
193: }
194:
195: UIViewRoot viewRoot = context.getViewRoot();
196:
197: out.startElement("table", component);
198:
199: if (border > 0)
200: out.writeAttribute("border", border, "border");
201:
202: if (style != null)
203: out.writeAttribute("style", style, "style");
204:
205: if (styleClass != null)
206: out.writeAttribute("class", styleClass, "class");
207:
208: String clientId = component.getClientId(context);
209:
210: if (disabled)
211: out.writeAttribute("disabled", "disabled", "disabled");
212:
213: out.write("\n");
214:
215: if (!"pageDirection".equals(layout)) {
216: out.startElement("tr", component);
217: out.write("\n");
218: }
219:
220: List<SelectItem> items = accrueSelectItems(component);
221:
222: for (int i = 0; i < items.size(); i++) {
223: SelectItem selectItem = items.get(i);
224:
225: String childId = clientId + ":" + i;
226:
227: if ("pageDirection".equals(layout)) {
228: out.startElement("tr", component);
229: out.write("\n");
230: }
231:
232: out.startElement("td", component);
233: out.startElement("input", component);
234: out.writeAttribute("id", childId, "id");
235: out.writeAttribute("name", clientId, "name");
236: out.writeAttribute("type", "radio", "type");
237:
238: if (selectItem.isDisabled() || disabled)
239: out.writeAttribute("disabled", "disabled", "disabled");
240:
241: if (value instanceof String) {
242: String values = (String) value;
243:
244: if (value.equals(selectItem.getValue())) {
245: out.writeAttribute("checked", "checked", "value");
246: }
247: }
248:
249: if (accesskey != null)
250: out.writeAttribute("accesskey", accesskey, "accesskey");
251:
252: if (dir != null)
253: out.writeAttribute("dir", dir, "dir");
254:
255: if (lang != null)
256: out.writeAttribute("lang", lang, "lang");
257:
258: if (onblur != null)
259: out.writeAttribute("onblur", onblur, "onblur");
260:
261: if (onchange != null)
262: out.writeAttribute("onchange", onchange, "onchange");
263:
264: if (onclick != null)
265: out.writeAttribute("onclick", onclick, "onclick");
266:
267: if (ondblclick != null)
268: out.writeAttribute("ondblclick", ondblclick,
269: "ondblclick");
270:
271: if (onfocus != null)
272: out.writeAttribute("onfocus", onfocus, "onfocus");
273:
274: if (onkeydown != null)
275: out.writeAttribute("onkeydown", onkeydown, "onkeydown");
276:
277: if (onkeypress != null)
278: out.writeAttribute("onkeypress", onkeypress,
279: "onkeypress");
280:
281: if (onkeyup != null)
282: out.writeAttribute("onkeyup", onkeyup, "onkeyup");
283:
284: if (onmousedown != null)
285: out.writeAttribute("onmousedown", onmousedown,
286: "onmousedown");
287:
288: if (onmousemove != null)
289: out.writeAttribute("onmousemove", onmousemove,
290: "onmousemove");
291:
292: if (onmouseout != null)
293: out.writeAttribute("onmouseout", onmouseout,
294: "onmouseout");
295:
296: if (onmouseover != null)
297: out.writeAttribute("onmouseover", onmouseover,
298: "onmouseover");
299:
300: if (onmouseup != null)
301: out.writeAttribute("onmouseup", onmouseup, "onmouseup");
302:
303: if (onselect != null)
304: out.writeAttribute("onselect", onselect, "onselect");
305:
306: if (readonly)
307: out.writeAttribute("readonly", "readonly", "readonly");
308:
309: if (tabindex != null)
310: out.writeAttribute("tabindex", tabindex, "tabindex");
311:
312: if (title != null)
313: out.writeAttribute("title", title, "title");
314:
315: Object itemValue = selectItem.getValue();
316: if (itemValue != null)
317: out.writeAttribute("value", String.valueOf(itemValue),
318: "value");
319:
320: out.endElement("input");
321:
322: out.startElement("label", component);
323: out.writeAttribute("for", childId, "for");
324:
325: if (selectItem.isDisabled() || disabled) {
326: if (disabledClass != null)
327: out.writeAttribute("class", disabledClass,
328: "disabledClass");
329: } else {
330: if (enabledClass != null)
331: out.writeAttribute("class", enabledClass,
332: "enabledClass");
333: }
334:
335: String label = selectItem.getLabel();
336: if (label == null)
337: label = String.valueOf(selectItem.getValue());
338:
339: out.writeText(label, "itemLabel");
340: out.endElement("label");
341:
342: out.endElement("td");
343: out.write("\n");
344:
345: if ("pageDirection".equals(layout)) {
346: out.endElement("tr");
347: out.write("\n");
348: }
349: }
350:
351: if (!"pageDirection".equals(layout)) {
352: out.endElement("tr");
353: out.write("\n");
354: }
355:
356: out.endElement("table");
357: out.write("\n");
358: }
359:
360: /**
361: * Renders the content for the component.
362: */
363: @Override
364: public void encodeChildren(FacesContext context,
365: UIComponent component) throws IOException {
366: }
367:
368: /**
369: * Renders the closing tag for the component.
370: */
371: @Override
372: public void encodeEnd(FacesContext context, UIComponent component)
373: throws IOException {
374: }
375:
376: public String toString() {
377: return "HtmlInputTextRenderer[]";
378: }
379: }
|