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: import java.util.logging.*;
034:
035: import javax.faces.*;
036: import javax.faces.application.*;
037: import javax.faces.component.*;
038: import javax.faces.component.html.*;
039: import javax.faces.context.*;
040: import javax.faces.event.*;
041: import javax.faces.render.*;
042:
043: /**
044: * The HTML command/button renderer
045: */
046: class HtmlCommandButtonRenderer extends Renderer {
047: private static final Logger log = Logger
048: .getLogger(HtmlCommandButtonRenderer.class.getName());
049:
050: public static final Renderer RENDERER = new HtmlCommandButtonRenderer();
051:
052: /**
053: * True if the renderer is responsible for rendering the children.
054: */
055: @Override
056: public boolean getRendersChildren() {
057: return true;
058: }
059:
060: /**
061: * Decodes the data from the form.
062: */
063: @Override
064: public void decode(FacesContext context, UIComponent component) {
065: String clientId = component.getClientId(context);
066:
067: ExternalContext ext = context.getExternalContext();
068: Map<String, String> paramMap = ext.getRequestParameterMap();
069:
070: String value = paramMap.get(clientId);
071:
072: if (value != null) {
073: String type;
074:
075: if (component instanceof HtmlCommandButton) {
076: HtmlCommandButton htmlComp = (HtmlCommandButton) component;
077:
078: type = htmlComp.getType();
079: } else {
080: Map<String, Object> attrMap = component.getAttributes();
081:
082: type = (String) attrMap.get("type");
083: }
084:
085: if ("reset".equals(type))
086: return;
087:
088: if (log.isLoggable(Level.FINE))
089: log.fine(component + " action " + type);
090:
091: ActionEvent event = new ActionEvent(component);
092:
093: component.queueEvent(event);
094: } else {
095: String valueX = paramMap.get(clientId + ".x");
096: String valueY = paramMap.get(clientId + ".y");
097:
098: if (valueX != null || valueY != null) {
099: if (log.isLoggable(Level.FINE))
100: log.fine(component + " action [" + valueX + ","
101: + valueY + "]");
102:
103: ActionEvent event = new ActionEvent(component);
104:
105: component.queueEvent(event);
106: }
107: }
108: }
109:
110: /**
111: * Renders the open tag for the text.
112: */
113: @Override
114: public void encodeBegin(FacesContext context, UIComponent component)
115: throws IOException {
116: ResponseWriter out = context.getResponseWriter();
117:
118: String id = component.getId();
119:
120: String accesskey;
121: String alt;
122: String dir;
123: boolean disabled;
124: String image;
125: String lang;
126:
127: String onblur;
128: String onchange;
129: String onclick;
130: String ondblclick;
131: String onfocus;
132:
133: String onkeydown;
134: String onkeypress;
135: String onkeyup;
136:
137: String onmousedown;
138: String onmousemove;
139: String onmouseout;
140: String onmouseover;
141: String onmouseup;
142:
143: String onselect;
144:
145: boolean readonly;
146: String style;
147: String styleClass;
148: String tabindex;
149: String target;
150: String title;
151: String type;
152: Object value;
153:
154: if (component instanceof HtmlCommandButton) {
155: HtmlCommandButton htmlCommandButton = (HtmlCommandButton) component;
156:
157: accesskey = htmlCommandButton.getAccesskey();
158: alt = htmlCommandButton.getAlt();
159: dir = htmlCommandButton.getDir();
160: disabled = htmlCommandButton.isDisabled();
161: image = htmlCommandButton.getImage();
162: lang = htmlCommandButton.getLang();
163:
164: onblur = htmlCommandButton.getOnblur();
165: onchange = htmlCommandButton.getOnchange();
166: onclick = htmlCommandButton.getOnclick();
167: ondblclick = htmlCommandButton.getOndblclick();
168: onfocus = htmlCommandButton.getOnfocus();
169:
170: onkeydown = htmlCommandButton.getOnkeydown();
171: onkeypress = htmlCommandButton.getOnkeypress();
172: onkeyup = htmlCommandButton.getOnkeyup();
173:
174: onmousedown = htmlCommandButton.getOnmousedown();
175: onmousemove = htmlCommandButton.getOnmousemove();
176: onmouseout = htmlCommandButton.getOnmouseout();
177: onmouseover = htmlCommandButton.getOnmouseover();
178: onmouseup = htmlCommandButton.getOnmouseup();
179:
180: onselect = htmlCommandButton.getOnselect();
181:
182: readonly = htmlCommandButton.isReadonly();
183: style = htmlCommandButton.getStyle();
184: styleClass = htmlCommandButton.getStyleClass();
185: tabindex = htmlCommandButton.getTabindex();
186: title = htmlCommandButton.getTitle();
187: type = htmlCommandButton.getType();
188:
189: value = htmlCommandButton.getValue();
190: } else {
191: Map<String, Object> attrMap = component.getAttributes();
192:
193: accesskey = (String) attrMap.get("accesskey");
194: alt = (String) attrMap.get("alt");
195: dir = (String) attrMap.get("dir");
196: disabled = (Boolean) attrMap.get("disabled");
197: image = (String) attrMap.get("image");
198: lang = (String) attrMap.get("lang");
199:
200: onblur = (String) attrMap.get("onblur");
201: onchange = (String) attrMap.get("onchange");
202: onclick = (String) attrMap.get("onclick");
203: ondblclick = (String) attrMap.get("ondblclick");
204: onfocus = (String) attrMap.get("onfocus");
205:
206: onkeydown = (String) attrMap.get("onkeydown");
207: onkeypress = (String) attrMap.get("onkeypress");
208: onkeyup = (String) attrMap.get("onkeyup");
209:
210: onmousedown = (String) attrMap.get("onmousedown");
211: onmousemove = (String) attrMap.get("onmousemove");
212: onmouseout = (String) attrMap.get("onmouseout");
213: onmouseover = (String) attrMap.get("onmouseover");
214: onmouseup = (String) attrMap.get("onmouseup");
215:
216: onselect = (String) attrMap.get("onselect");
217:
218: readonly = (Boolean) attrMap.get("readonly");
219: style = (String) attrMap.get("style");
220: styleClass = (String) attrMap.get("styleClass");
221: tabindex = (String) attrMap.get("tabindex");
222: title = (String) attrMap.get("title");
223: type = (String) attrMap.get("type");
224:
225: value = attrMap.get("value");
226: }
227:
228: out.startElement("input", component);
229: if (image != null && !"".equals(image)) {
230: out.writeAttribute("type", "image", "type");
231:
232: ViewHandler view = context.getApplication()
233: .getViewHandler();
234:
235: String src = view.getResourceURL(context, image);
236:
237: ExternalContext extContext = context.getExternalContext();
238:
239: out.writeAttribute("src", extContext.encodeActionURL(src),
240: "src");
241: } else {
242: if ("reset".equals(type))
243: out.writeAttribute("type", "reset", "type");
244: else
245: out.writeAttribute("type", "submit", "type");
246:
247: if (value != null)
248: out.writeAttribute("value", String.valueOf(value),
249: "value");
250: }
251:
252: out.writeAttribute("name", component.getClientId(context),
253: "name");
254:
255: if (id != null && !id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
256: out.writeAttribute("id", component.getClientId(context),
257: "id");
258:
259: if (accesskey != null)
260: out.writeAttribute("accesskey", accesskey, "accesskey");
261:
262: if (alt != null)
263: out.writeAttribute("alt", alt, "alt");
264:
265: if (dir != null)
266: out.writeAttribute("dir", dir, "dir");
267:
268: if (disabled)
269: out.writeAttribute("disabled", "disabled", "disabled");
270:
271: if (lang != null)
272: out.writeAttribute("lang", lang, "lang");
273:
274: if (onblur != null)
275: out.writeAttribute("onblur", onblur, "onblur");
276:
277: if (onchange != null)
278: out.writeAttribute("onchange", onchange, "onchange");
279:
280: if (onclick != null)
281: out.writeAttribute("onclick", onclick, "onclick");
282:
283: if (ondblclick != null)
284: out.writeAttribute("ondblclick", ondblclick, "ondblclick");
285:
286: if (onfocus != null)
287: out.writeAttribute("onfocus", onfocus, "onfocus");
288:
289: if (onkeydown != null)
290: out.writeAttribute("onkeydown", onkeydown, "onkeydown");
291:
292: if (onkeypress != null)
293: out.writeAttribute("onkeypress", onkeypress, "onkeypress");
294:
295: if (onkeyup != null)
296: out.writeAttribute("onkeyup", onkeyup, "onkeyup");
297:
298: if (onmousedown != null)
299: out.writeAttribute("onmousedown", onmousedown,
300: "onmousedown");
301:
302: if (onmousemove != null)
303: out.writeAttribute("onmousemove", onmousemove,
304: "onmousemove");
305:
306: if (onmouseout != null)
307: out.writeAttribute("onmouseout", onmouseout, "onmouseout");
308:
309: if (onmouseover != null)
310: out.writeAttribute("onmouseover", onmouseover,
311: "onmouseover");
312:
313: if (onmouseup != null)
314: out.writeAttribute("onmouseup", onmouseup, "onmouseup");
315:
316: if (onselect != null)
317: out.writeAttribute("onselect", onselect, "onselect");
318:
319: if (readonly)
320: out.writeAttribute("readonly", "readonly", "readonly");
321:
322: if (style != null)
323: out.writeAttribute("style", style, "style");
324:
325: if (styleClass != null)
326: out.writeAttribute("class", styleClass, "class");
327:
328: if (tabindex != null)
329: out.writeAttribute("tabindex", tabindex, "tabindex");
330:
331: if (title != null)
332: out.writeAttribute("title", title, "title");
333:
334: out.endElement("input");
335: }
336:
337: /**
338: * Renders the content for the component.
339: */
340: @Override
341: public void encodeChildren(FacesContext context,
342: UIComponent component) throws IOException {
343: }
344:
345: /**
346: * Renders the closing tag for the component.
347: */
348: @Override
349: public void encodeEnd(FacesContext context, UIComponent component)
350: throws IOException {
351: }
352:
353: public String toString() {
354: return "HtmlInputTextRenderer[]";
355: }
356: }
|