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.net.URLEncoder;
034:
035: import javax.faces.component.*;
036: import javax.faces.component.html.*;
037: import javax.faces.context.*;
038: import javax.faces.event.*;
039: import javax.faces.render.*;
040:
041: /**
042: * The HTML command/link renderer
043: */
044: class HtmlCommandLinkRenderer extends BaseRenderer {
045: public static final Renderer RENDERER = new HtmlCommandLinkRenderer();
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 formId = getFormId(context, component);
061: String clientId = component.getClientId(context);
062:
063: String hiddenId = clientId + ":link";
064:
065: ExternalContext ext = context.getExternalContext();
066: Map<String, String> paramMap = ext.getRequestParameterMap();
067:
068: String value = paramMap.get(hiddenId);
069:
070: if (value != null && !"".equals(value)) {
071: ActionEvent event = new ActionEvent(component);
072:
073: component.queueEvent(event);
074: } else {
075: String valueX = clientId + ".x";
076: String valueY = clientId + ".y";
077: }
078: }
079:
080: /**
081: * Renders the open tag for the text.
082: */
083: @Override
084: public void encodeBegin(FacesContext context, UIComponent component)
085: throws IOException {
086: ResponseWriter out = context.getResponseWriter();
087:
088: String id = component.getId();
089:
090: String accesskey;
091: String charset;
092: String coords;
093: String dir;
094: boolean disabled;
095: String hreflang;
096: String lang;
097:
098: String onblur;
099: String onclick;
100: String ondblclick;
101: String onfocus;
102:
103: String onkeydown;
104: String onkeypress;
105: String onkeyup;
106:
107: String onmousedown;
108: String onmousemove;
109: String onmouseout;
110: String onmouseover;
111: String onmouseup;
112:
113: String rel;
114: String rev;
115: String shape;
116: String style;
117: String styleClass;
118: String tabindex;
119: String target;
120: String title;
121: String type;
122:
123: Object value;
124:
125: if (component instanceof HtmlCommandLink) {
126: HtmlCommandLink htmlCommandLink = (HtmlCommandLink) component;
127:
128: accesskey = htmlCommandLink.getAccesskey();
129: charset = htmlCommandLink.getCharset();
130: coords = htmlCommandLink.getCoords();
131: dir = htmlCommandLink.getDir();
132: disabled = htmlCommandLink.isDisabled();
133: hreflang = htmlCommandLink.getHreflang();
134: lang = htmlCommandLink.getLang();
135:
136: onblur = htmlCommandLink.getOnblur();
137: onclick = htmlCommandLink.getOnclick();
138: ondblclick = htmlCommandLink.getOndblclick();
139: onfocus = htmlCommandLink.getOnfocus();
140:
141: onkeydown = htmlCommandLink.getOnkeydown();
142: onkeypress = htmlCommandLink.getOnkeypress();
143: onkeyup = htmlCommandLink.getOnkeyup();
144:
145: onmousedown = htmlCommandLink.getOnmousedown();
146: onmousemove = htmlCommandLink.getOnmousemove();
147: onmouseout = htmlCommandLink.getOnmouseout();
148: onmouseover = htmlCommandLink.getOnmouseover();
149: onmouseup = htmlCommandLink.getOnmouseup();
150:
151: rel = htmlCommandLink.getRel();
152: rev = htmlCommandLink.getRev();
153: shape = htmlCommandLink.getShape();
154: style = htmlCommandLink.getStyle();
155: styleClass = htmlCommandLink.getStyleClass();
156: tabindex = htmlCommandLink.getTabindex();
157: title = htmlCommandLink.getTitle();
158: type = htmlCommandLink.getType();
159:
160: target = htmlCommandLink.getTarget();
161:
162: value = htmlCommandLink.getValue();
163: } else {
164: Map<String, Object> attrMap = component.getAttributes();
165:
166: accesskey = (String) attrMap.get("accesskey");
167: charset = (String) attrMap.get("charset");
168: coords = (String) attrMap.get("coords");
169: dir = (String) attrMap.get("dir");
170: disabled = (Boolean) attrMap.get("disabled");
171: hreflang = (String) attrMap.get("hreflang");
172: lang = (String) attrMap.get("lang");
173:
174: onblur = (String) attrMap.get("onblur");
175: onclick = (String) attrMap.get("onclick");
176: ondblclick = (String) attrMap.get("ondblclick");
177: onfocus = (String) attrMap.get("onfocus");
178:
179: onkeydown = (String) attrMap.get("onkeydown");
180: onkeypress = (String) attrMap.get("onkeypress");
181: onkeyup = (String) attrMap.get("onkeyup");
182:
183: onmousedown = (String) attrMap.get("onmousedown");
184: onmousemove = (String) attrMap.get("onmousemove");
185: onmouseout = (String) attrMap.get("onmouseout");
186: onmouseover = (String) attrMap.get("onmouseover");
187: onmouseup = (String) attrMap.get("onmouseup");
188:
189: rel = (String) attrMap.get("rel");
190: rev = (String) attrMap.get("rev");
191: shape = (String) attrMap.get("shape");
192: style = (String) attrMap.get("style");
193: styleClass = (String) attrMap.get("styleClass");
194: tabindex = (String) attrMap.get("tabindex");
195: title = (String) attrMap.get("title");
196: type = (String) attrMap.get("type");
197:
198: target = (String) attrMap.get("target");
199:
200: value = attrMap.get("value");
201: }
202:
203: String clientId = component.getClientId(context);
204: String formClientId = getFormId(context, component);
205: String hiddenFieldName = clientId + ":link";
206:
207: if (disabled) {
208: out.startElement("span", component);
209:
210: if (target != null)
211: out.writeAttribute("target", target, "target");
212: } else {
213: out.startElement("a", component);
214:
215: out.writeAttribute("href", "#", "href");
216: }
217:
218: //out.writeAttribute("name", component.getClientId(context), "name");
219:
220: if (id != null && !id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
221: out.writeAttribute("id", component.getClientId(context),
222: "id");
223:
224: if (accesskey != null)
225: out.writeAttribute("accesskey", accesskey, "accesskey");
226:
227: if (charset != null)
228: out.writeAttribute("charset", charset, "charset");
229:
230: if (coords != null)
231: out.writeAttribute("coords", coords, "coords");
232:
233: if (dir != null)
234: out.writeAttribute("dir", dir, "dir");
235:
236: if (hreflang != null)
237: out.writeAttribute("hreflang", hreflang, "hreflang");
238:
239: if (lang != null)
240: out.writeAttribute("lang", lang, "lang");
241:
242: if (onblur != null)
243: out.writeAttribute("onblur", onblur, "onblur");
244:
245: if (disabled) {
246: } else {
247: StringBuilder clickJs = new StringBuilder();
248: clickJs.append("document.forms['");
249: clickJs.append(formClientId);
250: clickJs.append("']['");
251: clickJs.append(hiddenFieldName);
252: clickJs.append("'].value='");
253: clickJs.append(clientId);
254: clickJs.append("';");
255:
256: final int childCount = component.getChildCount();
257:
258: if (childCount > 0 && !disabled) {
259: List<UIComponent> children = component.getChildren();
260:
261: for (int i = 0; i < childCount; i++) {
262: UIComponent child = children.get(i);
263:
264: if (child instanceof UIParameter) {
265: UIParameter param = (UIParameter) child;
266: String enc = out.getCharacterEncoding();
267:
268: clickJs.append("document.forms['");
269: clickJs.append(formClientId);
270: clickJs.append("']['");
271:
272: String name = param.getName();
273: String encodedName = URLEncoder.encode(name,
274: enc);
275:
276: HtmlFormRenderer.addCommandLinkParam(context,
277: formClientId, name);
278:
279: clickJs.append(encodedName);
280: clickJs.append("'].value='");
281:
282: String val = toString(context, param, param
283: .getValue());
284: String encodedVal = URLEncoder.encode(val, enc);
285:
286: clickJs.append(encodedVal);
287: clickJs.append("';");
288: }
289: }
290: }
291:
292: if (target != null) {
293: clickJs.append("document.forms['");
294: clickJs.append(formClientId);
295: clickJs.append("'].target='");
296: clickJs.append(target);
297: clickJs.append("';");
298: }
299:
300: clickJs.append("document.forms['");
301: clickJs.append(formClientId);
302: clickJs.append("'].submit();");
303:
304: clickJs.append("return false;");
305:
306: if (onclick != null) {
307: String code = ("var a = function(){" + onclick + "};"
308: + "var b = function() {" + clickJs + "};" + "return a() && b();");
309:
310: out.writeAttribute("onclick", code, "onclick");
311: } else
312: out.writeAttribute("onclick", clickJs.toString(),
313: "onclick");
314: }
315:
316: if (ondblclick != null)
317: out.writeAttribute("ondblclick", ondblclick, "ondblclick");
318:
319: if (onfocus != null)
320: out.writeAttribute("onfocus", onfocus, "onfocus");
321:
322: if (onkeydown != null)
323: out.writeAttribute("onkeydown", onkeydown, "onkeydown");
324:
325: if (onkeypress != null)
326: out.writeAttribute("onkeypress", onkeypress, "onkeypress");
327:
328: if (onkeyup != null)
329: out.writeAttribute("onkeyup", onkeyup, "onkeyup");
330:
331: if (onmousedown != null)
332: out.writeAttribute("onmousedown", onmousedown,
333: "onmousedown");
334:
335: if (onmousemove != null)
336: out.writeAttribute("onmousemove", onmousemove,
337: "onmousemove");
338:
339: if (onmouseout != null)
340: out.writeAttribute("onmouseout", onmouseout, "onmouseout");
341:
342: if (onmouseover != null)
343: out.writeAttribute("onmouseover", onmouseover,
344: "onmouseover");
345:
346: if (onmouseup != null)
347: out.writeAttribute("onmouseup", onmouseup, "onmouseup");
348:
349: if (rel != null)
350: out.writeAttribute("rel", rel, "rel");
351:
352: if (rev != null)
353: out.writeAttribute("rev", rev, "rev");
354:
355: if (shape != null)
356: out.writeAttribute("shape", shape, "shape");
357:
358: if (style != null)
359: out.writeAttribute("style", style, "style");
360:
361: if (styleClass != null)
362: out.writeAttribute("class", styleClass, "class");
363:
364: if (tabindex != null)
365: out.writeAttribute("tabindex", tabindex, "tabindex");
366:
367: if (title != null)
368: out.writeAttribute("title", title, "title");
369:
370: if (type != null)
371: out.writeAttribute("type", type, "type");
372:
373: if (value != null)
374: out.writeText(toString(context, component, value), "value");
375:
376: final int childCount = component.getChildCount();
377:
378: if (childCount > 0) {
379: List<UIComponent> children = component.getChildren();
380:
381: for (int i = 0; i < childCount; i++) {
382: UIComponent child = children.get(i);
383:
384: if (child instanceof UIParameter) {
385: } else if (child.isRendered()) {
386: child.encodeBegin(context);
387: child.encodeChildren(context);
388: child.encodeEnd(context);
389: }
390: }
391: }
392:
393: if (disabled)
394: out.endElement("span");
395: else
396: out.endElement("a");
397:
398: out.startElement("input", component);
399: out.writeAttribute("type", "hidden", "type");
400: out.writeAttribute("name", hiddenFieldName, "name");
401: out.endElement("input");
402: }
403:
404: private String getFormId(FacesContext context, UIComponent component) {
405: for (UIComponent ptr = component.getParent(); ptr != null; ptr = ptr
406: .getParent()) {
407: if (ptr instanceof UIForm) {
408: return ptr.getClientId(context);
409: }
410: }
411:
412: return "";
413: }
414:
415: /**
416: * Renders the content for the component.
417: */
418: @Override
419: public void encodeChildren(FacesContext context,
420: UIComponent component) throws IOException {
421: }
422:
423: /**
424: * Renders the closing tag for the component.
425: */
426: @Override
427: public void encodeEnd(FacesContext context, UIComponent component)
428: throws IOException {
429: }
430:
431: public String toString() {
432: return "HtmlInputTextRenderer[]";
433: }
434: }
|