001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/help/tags/sakai_2-4-1/help-tool/src/java/org/sakaiproject/jsf/help/ContextSensitiveTreeRender.java $
003: * $Id: ContextSensitiveTreeRender.java 28753 2007-04-12 02:02:54Z ajpoland@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004 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.jsf.help;
021:
022: import java.io.IOException;
023: import java.util.Iterator;
024: import java.util.Set;
025: import java.util.TreeSet;
026:
027: import javax.faces.component.UIComponent;
028: import javax.faces.component.UIData;
029: import javax.faces.context.FacesContext;
030: import javax.faces.context.ResponseWriter;
031: import javax.faces.render.Renderer;
032: import javax.servlet.http.HttpServletRequest;
033:
034: import org.sakaiproject.api.app.help.Category;
035: import org.sakaiproject.api.app.help.Resource;
036: import org.sakaiproject.component.cover.ServerConfigurationService;
037:
038: /**
039: * render response
040: * @version $Id: ContextSensitiveTreeRender.java 28753 2007-04-12 02:02:54Z ajpoland@iupui.edu $
041: */
042: public class ContextSensitiveTreeRender extends Renderer {
043: /**
044: * supports componenet type
045: * @param component
046: * @return true if supported
047: */
048: public boolean supportsComponentType(UIComponent component) {
049: return (component instanceof UIData);
050: }
051:
052: /**
053: * @see javax.faces.render.Renderer#encodeBegin(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
054: */
055: public void encodeBegin(FacesContext context, UIComponent component)
056: throws IOException {
057:
058: String skinRoot = ServerConfigurationService.getString(
059: "skin.repo", "/library/skin");
060: String skin = ServerConfigurationService.getString(
061: "skin.default", "default");
062:
063: String jsLibraryUrl = "../js";
064:
065: ResponseWriter writer = context.getResponseWriter();
066: writer
067: .write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");
068: writer.write("<html>");
069: writer.write("<head>\n");
070: writer.write("<title>Help Index</title>\n");
071: writer.write("<script type=\"text/javascript\" src=\""
072: + jsLibraryUrl + "/csTree.js\"></script>\n");
073: writer.write("<link href=\"" + skinRoot + "/tool_base.css\""
074: + " type=\"text/css\" rel=\"stylesheet\" />\n");
075: writer.write("<link href=\"" + skinRoot + "/" + skin
076: + "/tool.css\""
077: + " type=\"text/css\" rel=\"stylesheet\" />\n");
078: writer
079: .write("<link href=\"../css/csTree.css\" type=\"text/css\" rel=\"stylesheet\" />");
080: //writer.write("<body onload='collapseAll([\"ol\"]); openBookMark();'>");
081: writer.write("</head>\n");
082:
083: writer.write("<body>\n");
084: writer.write("<ol id=\"root\">");
085: UIData data = (UIData) component;
086: Object value = data.getValue();
087: //String helpDocId = (String) component.getAttributes().get("helpDocId");
088: String helpDocId = ((HttpServletRequest) context
089: .getExternalContext().getRequest())
090: .getParameter("help");
091:
092: Set categories = (Set) value;
093:
094: // filter to only include top-level categories
095: for (Iterator i = categories.iterator(); i.hasNext();) {
096: Category c = (Category) i.next();
097: if (c.getParent() != null
098: || "home".equalsIgnoreCase(c.getName())) {
099: i.remove();
100: }
101: }
102:
103: encodeRecursive(writer, categories, helpDocId);
104: writer.write("</ol>");
105:
106: if (helpDocId != null) {
107: writer.write("<script type=\"text/javascript\" src=\""
108: + jsLibraryUrl + "/search.js\"></script>\n");
109: }
110: writer.write("</body></html>");
111: }
112:
113: /**
114: * encode recursively
115: * @param writer
116: * @param categories
117: * @param helpDocId
118: * @throws IOException
119: */
120: private void encodeRecursive(ResponseWriter writer, Set categories,
121: String helpDocId) throws IOException {
122: for (Iterator i = categories.iterator(); i.hasNext();) {
123: Category category = (Category) i.next();
124:
125: Set resources = new TreeSet(category.getResources());
126: String id = category.getName();
127:
128: writer.write("<li class=\"dir\">");
129: writer
130: .write("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td>");
131: writer
132: .write("<img src=\"../image/toc_closed.gif\" alt=\"closed\" /></td>");
133: writer.write("<td><a id=\"" + id + "\" href=\"#"
134: + category.getName()
135: + "\" onclick=\"toggle(this)\">"
136: + category.getName() + "</a></td>");
137: writer.write("</tr></table>");
138:
139: writer.write("<ol class=\"docs\">");
140: Set subCategories = new TreeSet(category.getCategories());
141: encodeRecursive(writer, subCategories, helpDocId);
142: if (resources != null) {
143: for (Iterator j = resources.iterator(); j.hasNext();) {
144: writer.write("<li>");
145: Resource resource = (Resource) j.next();
146:
147: // helpDocId will be a helpDocId (coming from search) or
148: // will be a tool id coming from portal
149: if (helpDocId != null
150: && (helpDocId.equals(resource
151: .getDefaultForTool()) || helpDocId
152: .equals(resource.getDocId()))) {
153: writer
154: .write("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td>");
155: writer
156: .write("<img src=\"../image/topic.gif\" alt=\"topic\"/></td>");
157: writer.write("<td><a id=\"default\""
158: + " href=\"content.hlp?docId="
159: + resource.getDocId()
160: + "\" target = \"content\">"
161: + resource.getName() + "</a></td>");
162: writer.write("</tr></table></li>\n");
163: } else {
164: writer
165: .write("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td>");
166: writer
167: .write("<img src=\"../image/topic.gif\" alt=\"topic\"/></td>");
168: writer.write("<td><a id=\""
169: + resource.getDocId()
170: + "\" href=\"content.hlp?docId="
171: + resource.getDocId()
172: + "\" target = \"content\">"
173: + resource.getName() + "</a></td>");
174: writer.write("</tr></table></li>\n");
175: }
176: }
177: }
178: writer.write("</ol></li>\n");
179: }
180: }
181:
182: /**
183: * contains help doc
184: * @param helpDocId
185: * @param resources
186: * @return true if contained
187: */
188: private boolean containsHelpDoc(String helpDocId, Set resources) {
189: boolean contains = false;
190: if (resources != null) {
191: for (Iterator i = resources.iterator(); i.hasNext();) {
192: Resource resource = (Resource) i.next();
193: if (resource.getDocId().equals(helpDocId)) {
194: contains = true;
195: break;
196: }
197: }
198: }
199: return contains;
200: }
201: }
|