001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or
005: * without modification, are permitted provided that the following
006: * conditions are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above
012: * copyright notice, this list of conditions and the following
013: * disclaimer in the documentation and/or other materials
014: * provided with the distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
025: * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
026: * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
027: * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
028: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
029: * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
030: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
031: * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
032: * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
033: *
034: * You acknowledge that this software is not designed, licensed or
035: * intended for use in the design, construction, operation or
036: * maintenance of any nuclear facility.
037: */
038:
039: package org.apache.cocoon.faces.samples.components.renderkit;
040:
041: import org.apache.cocoon.faces.samples.components.components.MapComponent;
042:
043: import javax.faces.component.UIComponent;
044: import javax.faces.context.FacesContext;
045: import javax.faces.context.ResponseWriter;
046:
047: import java.io.IOException;
048:
049: /**
050: * <p>Renderer for {@link MapComponent} in an HTML environment.</p>
051: */
052:
053: public class MapRenderer extends BaseRenderer {
054:
055: // -------------------------------------------------------- Renderer Methods
056:
057: /**
058: * <p>Decode the incoming request parameters to determine which
059: * hotspot (if any) has been selected.</p>
060: *
061: * @param context <code>FacesContext</code>for the current request
062: * @param component <code>UIComponent</code> to be decoded
063: */
064: public void decode(FacesContext context, UIComponent component) {
065:
066: if ((context == null) || (component == null)) {
067: throw new NullPointerException();
068: }
069: MapComponent map = (MapComponent) component;
070:
071: String key = getName(context, map);
072: String value = (String) context.getExternalContext()
073: .getRequestParameterMap().get(key);
074: if (value != null) {
075: map.setCurrent(value);
076: }
077:
078: }
079:
080: /**
081: * <p>Encode the beginning of this component.</p>
082: *
083: * @param context <code>FacesContext</code>for the current request
084: * @param component <code>UIComponent</code> to be decoded
085: */
086: public void encodeBegin(FacesContext context, UIComponent component)
087: throws IOException {
088:
089: if ((context == null) || (component == null)) {
090: throw new NullPointerException();
091: }
092: MapComponent map = (MapComponent) component;
093: ResponseWriter writer = context.getResponseWriter();
094:
095: writer.startElement("map", map);
096: writer.writeAttribute("name", map.getId(), "id");
097:
098: }
099:
100: /**
101: * <p>Encode the children of this component.</p>
102: *
103: * @param context <code>FacesContext</code>for the current request
104: * @param component <code>UIComponent</code> to be decoded
105: */
106: public void encodeChildren(FacesContext context,
107: UIComponent component) throws IOException {
108:
109: if ((context == null) || (component == null)) {
110: throw new NullPointerException();
111: }
112:
113: }
114:
115: /**
116: * <p>Encode the ending of this component.</p>
117: *
118: * @param context <code>FacesContext</code>for the current request
119: * @param component <code>UIComponent</code> to be decoded
120: */
121: public void encodeEnd(FacesContext context, UIComponent component)
122: throws IOException {
123:
124: if ((context == null) || (component == null)) {
125: throw new NullPointerException();
126: }
127: MapComponent map = (MapComponent) component;
128: ResponseWriter writer = context.getResponseWriter();
129:
130: writer.startElement("input", map);
131: writer.writeAttribute("type", "hidden", null);
132: writer
133: .writeAttribute("name", getName(context, map),
134: "clientId");
135: writer.endElement("input");
136: writer.endElement("map");
137:
138: }
139:
140: // --------------------------------------------------------- Private Methods
141:
142: /**
143: * <p>Return the calculated name for the hidden input field.</p>
144: *
145: * @param context Context for the current request
146: * @param component Component we are rendering
147: */
148: private String getName(FacesContext context, UIComponent component) {
149: return (component.getId() + "_current");
150: }
151: }
|