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.convert.*;
039: import javax.faces.context.*;
040: import javax.faces.render.*;
041:
042: /**
043: * The HTML selectMany/menu renderer
044: */
045: class HtmlSelectManyMenuRenderer extends SelectRenderer {
046: public static final Renderer RENDERER = new HtmlSelectManyMenuRenderer();
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: * Decodes the data from the form.
058: */
059: @Override
060: public void decode(FacesContext context, UIComponent component) {
061: String clientId = component.getClientId(context);
062:
063: ExternalContext ext = context.getExternalContext();
064: Map<String, String[]> paramMap = ext
065: .getRequestParameterValuesMap();
066:
067: String[] value = paramMap.get(clientId);
068:
069: if (value != null) {
070: ((EditableValueHolder) component).setSubmittedValue(value);
071: }
072: }
073:
074: /**
075: * Renders the open tag for the text.
076: */
077: @Override
078: public void encodeBegin(FacesContext context, UIComponent component)
079: throws IOException {
080: ResponseWriter out = context.getResponseWriter();
081:
082: String id = component.getId();
083:
084: String accesskey;
085: String dir;
086: boolean disabled;
087: String disabledClass;
088: String enabledClass;
089: String lang;
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[] values;
115:
116: if (component instanceof HtmlSelectManyMenu) {
117: HtmlSelectManyMenu htmlComponent = (HtmlSelectManyMenu) component;
118:
119: accesskey = htmlComponent.getAccesskey();
120: dir = htmlComponent.getDir();
121: disabled = htmlComponent.isDisabled();
122: disabledClass = htmlComponent.getDisabledClass();
123: enabledClass = htmlComponent.getEnabledClass();
124: lang = htmlComponent.getLang();
125:
126: onblur = htmlComponent.getOnblur();
127: onchange = htmlComponent.getOnchange();
128: onclick = htmlComponent.getOnclick();
129: ondblclick = htmlComponent.getOndblclick();
130: onfocus = htmlComponent.getOnfocus();
131:
132: onkeydown = htmlComponent.getOnkeydown();
133: onkeypress = htmlComponent.getOnkeypress();
134: onkeyup = htmlComponent.getOnkeyup();
135:
136: onmousedown = htmlComponent.getOnmousedown();
137: onmousemove = htmlComponent.getOnmousemove();
138: onmouseout = htmlComponent.getOnmouseout();
139: onmouseover = htmlComponent.getOnmouseover();
140: onmouseup = htmlComponent.getOnmouseup();
141:
142: onselect = htmlComponent.getOnselect();
143:
144: readonly = htmlComponent.isReadonly();
145: style = htmlComponent.getStyle();
146: styleClass = htmlComponent.getStyleClass();
147: tabindex = htmlComponent.getTabindex();
148: title = htmlComponent.getTitle();
149:
150: values = htmlComponent.getSelectedValues();
151: } else {
152: Map<String, Object> attrMap = component.getAttributes();
153:
154: accesskey = (String) attrMap.get("accesskey");
155: dir = (String) attrMap.get("dir");
156: disabled = (Boolean) attrMap.get("disabled");
157: disabledClass = (String) attrMap.get("disabledClass");
158: enabledClass = (String) attrMap.get("enabledClass");
159: lang = (String) attrMap.get("lang");
160:
161: onblur = (String) attrMap.get("onblur");
162: onchange = (String) attrMap.get("onchange");
163: onclick = (String) attrMap.get("onclick");
164: ondblclick = (String) attrMap.get("ondblclick");
165: onfocus = (String) attrMap.get("onfocus");
166:
167: onkeydown = (String) attrMap.get("onkeydown");
168: onkeypress = (String) attrMap.get("onkeypress");
169: onkeyup = (String) attrMap.get("onkeyup");
170:
171: onmousedown = (String) attrMap.get("onmousedown");
172: onmousemove = (String) attrMap.get("onmousemove");
173: onmouseout = (String) attrMap.get("onmouseout");
174: onmouseover = (String) attrMap.get("onmouseover");
175: onmouseup = (String) attrMap.get("onmouseup");
176:
177: onselect = (String) attrMap.get("onselect");
178:
179: readonly = (Boolean) attrMap.get("readonly");
180: style = (String) attrMap.get("style");
181: styleClass = (String) attrMap.get("styleClass");
182: tabindex = (String) attrMap.get("tabindex");
183: title = (String) attrMap.get("title");
184:
185: values = (Object[]) attrMap.get("selectedValues");
186: }
187:
188: UIViewRoot viewRoot = context.getViewRoot();
189:
190: out.startElement("select", component);
191:
192: if (style != null)
193: out.writeAttribute("style", style, "style");
194:
195: if (styleClass != null)
196: out.writeAttribute("class", styleClass, "class");
197:
198: String clientId = component.getClientId(context);
199: out.writeAttribute("name", clientId, "name");
200:
201: if (id != null && !id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
202: out.writeAttribute("id", clientId, "id");
203:
204: out.writeAttribute("multiple", "multiple", "multiple");
205:
206: if (disabled)
207: out.writeAttribute("disabled", "disabled", "disabled");
208:
209: if (accesskey != null)
210: out.writeAttribute("accesskey", accesskey, "accesskey");
211:
212: if (dir != null)
213: out.writeAttribute("dir", dir, "dir");
214:
215: if (lang != null)
216: out.writeAttribute("lang", lang, "lang");
217:
218: if (onblur != null)
219: out.writeAttribute("onblur", onblur, "onblur");
220:
221: if (onchange != null)
222: out.writeAttribute("onchange", onchange, "onchange");
223:
224: if (onclick != null)
225: out.writeAttribute("onclick", onclick, "onclick");
226:
227: if (ondblclick != null)
228: out.writeAttribute("ondblclick", ondblclick, "ondblclick");
229:
230: if (onfocus != null)
231: out.writeAttribute("onfocus", onfocus, "onfocus");
232:
233: if (onkeydown != null)
234: out.writeAttribute("onkeydown", onkeydown, "onkeydown");
235:
236: if (onkeypress != null)
237: out.writeAttribute("onkeypress", onkeypress, "onkeypress");
238:
239: if (onkeyup != null)
240: out.writeAttribute("onkeyup", onkeyup, "onkeyup");
241:
242: if (onmousedown != null)
243: out.writeAttribute("onmousedown", onmousedown,
244: "onmousedown");
245:
246: if (onmousemove != null)
247: out.writeAttribute("onmousemove", onmousemove,
248: "onmousemove");
249:
250: if (onmouseout != null)
251: out.writeAttribute("onmouseout", onmouseout, "onmouseout");
252:
253: if (onmouseover != null)
254: out.writeAttribute("onmouseover", onmouseover,
255: "onmouseover");
256:
257: if (onmouseup != null)
258: out.writeAttribute("onmouseup", onmouseup, "onmouseup");
259:
260: if (onselect != null)
261: out.writeAttribute("onselect", onselect, "onselect");
262:
263: if (readonly)
264: out.writeAttribute("readonly", "readonly", "readonly");
265:
266: int childCount = component.getChildCount();
267: int size = 0;
268: for (int i = 0; i < childCount; i++) {
269: UIComponent child = component.getChildren().get(i);
270:
271: if (child instanceof UISelectItem) {
272: UISelectItem item = (UISelectItem) child;
273:
274: if (!item.isItemDisabled())
275: size++;
276: }
277: }
278:
279: out.writeAttribute("size", "1", "size");
280:
281: if (tabindex != null)
282: out.writeAttribute("tabindex", tabindex, "tabindex");
283:
284: if (title != null)
285: out.writeAttribute("title", title, "title");
286:
287: out.write("\n");
288:
289: encodeChildren(out, context, component, values, enabledClass,
290: disabledClass);
291:
292: out.endElement("select");
293: out.write("\n");
294: }
295:
296: /**
297: * Renders the content for the component.
298: */
299: @Override
300: public void encodeChildren(FacesContext context,
301: UIComponent component) throws IOException {
302: }
303:
304: /**
305: * Renders the closing tag for the component.
306: */
307: @Override
308: public void encodeEnd(FacesContext context, UIComponent component)
309: throws IOException {
310: }
311:
312: public String toString() {
313: return "HtmlInputTextRenderer[]";
314: }
315: }
|