001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/tags/sakai_2-4-1/samigo-app/src/java/org/sakaiproject/tool/assessment/jsf/renderer/NavigationMapRenderer.java $
003: * $Id: NavigationMapRenderer.java 9268 2006-05-10 21:27:24Z daisyf@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the"License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.assessment.jsf.renderer;
021:
022: import java.io.IOException;
023: import javax.faces.component.UIComponent;
024: import javax.faces.component.UIOutput;
025: import javax.faces.context.FacesContext;
026: import javax.faces.context.ResponseWriter;
027: import javax.faces.render.Renderer;
028: import javax.faces.el.ValueBinding;
029: import java.util.Map;
030: import java.util.Set;
031: import java.util.Iterator;
032:
033: /**
034: * <p>Description: </p>
035: * <p>Render a stylesheet link for the value of our component's
036: * <code>path</code> attribute, prefixed by the context path of this
037: * web application.</p>
038: * <p> Based on example code by Sun Microsystems. </p>
039: * <p>Copyright: Copyright (c) 2004</p>
040: * <p>Organization: Sakai Project</p>
041: * @author Ed Smiley
042: * @version $Id: NavigationMapRenderer.java 9268 2006-05-10 21:27:24Z daisyf@stanford.edu $
043: */
044:
045: public class NavigationMapRenderer extends Renderer {
046:
047: public boolean supportsComponentType(UIComponent component) {
048: return (component instanceof UIOutput);
049: }
050:
051: public void decode(FacesContext context, UIComponent component) {
052: }
053:
054: public void encodeBegin(FacesContext context, UIComponent component)
055: throws IOException {
056: ;
057: }
058:
059: public void encodeChildren(FacesContext context,
060: UIComponent component) throws IOException {
061: ;
062: }
063:
064: /**
065: * <p>Render a relative HTML <code><link></code> element for a
066: * <code>text/css</code> stylesheet at the specified context-relative
067: * path.</p>
068: *
069: * @param context FacesContext for the request we are processing
070: * @param component UIComponent to be rendered
071: *
072: * @throws IOException if an input/output error occurs while rendering
073: * @throws NullPointerException if <code>context</code>
074: * or <code>component</code> is null
075: */
076: public void encodeEnd(FacesContext context, UIComponent component)
077: throws IOException {
078:
079: ResponseWriter writer = context.getResponseWriter();
080: Map map = (Map) get(context, component, "map");
081:
082: if (map == null)
083: return;
084:
085: String separator = (String) get(context, component, "separator");
086: String style = (String) get(context, component, "style");
087: String linkStyle = (String) get(context, component, "linkStyle");
088: if (separator == null)
089: separator = "";
090:
091: String contextPath = context.getExternalContext()
092: .getRequestContextPath();
093:
094: if (style != null && style.length() != 0) {
095: writer.write("<span class=\"" + style + "\">");
096: }
097:
098: Set keySet = map.keySet();
099: Iterator iter = keySet.iterator();
100: String sep = "";
101: while (iter.hasNext()) {
102: writer.write(sep);
103: String key = (String) iter.next();
104: String value = (String) map.get(key);
105: writeLink(writer, component, key, value, contextPath,
106: linkStyle);
107: sep = separator;
108: }
109:
110: if (style != null && style.length() != 0) {
111: writer.write("</span>");
112: }
113:
114: }
115:
116: /**
117: * Write one link
118: * @param writer the writer for output
119: * @param component the component
120: * @param text the text of the link
121: * @param link the context relative url, or script
122: * @param path the context path
123: * @throws IOException
124: */
125: private void writeLink(ResponseWriter writer,
126: UIComponent component, String text, String link,
127: String path, String styleClass) throws IOException {
128: writer.write(" ");
129: writer.startElement("a", component);
130: // if javascript create onclick handler, else regular link
131: if (link.toLowerCase().startsWith("javascript")) {
132: writer.writeAttribute("href", "#", null);
133: writer.writeAttribute("onclick", link, null);
134: } else {
135: writer.writeAttribute("href", path + "/" + link, null);
136: }
137:
138: if (styleClass != null) {
139: writer.writeAttribute("class", styleClass, "styleClass");
140: }
141: writer.writeText(text, null);
142: writer.endElement("a");
143: writer.write(" ");
144: }
145:
146: /**
147: *
148: * @param context FacesContext
149: * @param component UIComponent
150: * @param name String
151: * @return Object
152: */
153: private static Object get(FacesContext context,
154: UIComponent component, String name) {
155: ValueBinding binding = component.getValueBinding(name);
156: if (binding != null) {
157: return binding.getValue(context);
158: } else {
159: return component.getAttributes().get(name);
160: }
161: }
162:
163: }
|