001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/tags/sakai_2-4-1/samigo-app/src/java/org/sakaiproject/tool/assessment/jsf/renderer/AlphaIndexRenderer.java $
003: * $Id: AlphaIndexRenderer.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:
029: /**
030: * <p>Description: </p>
031: * <p>Render an alphabetical index. Makes any letter that exists in the
032: * initials parameter a link.</p>
033: * <p>Copyright: Copyright (c) 2004</p>
034: * <p>Organization: Sakai Project</p>
035: * @author Ed Smiley
036: * @version $Id: AlphaIndexRenderer.java 9268 2006-05-10 21:27:24Z daisyf@stanford.edu $
037: */
038:
039: public class AlphaIndexRenderer extends Renderer {
040:
041: public boolean supportsComponentType(UIComponent component) {
042: return (component instanceof UIOutput);
043: }
044:
045: public void decode(FacesContext context, UIComponent component) {
046: context.getViewRoot().setTransient(true); /* where "context" is of type "FaceContext" */
047: }
048:
049: public void encodeBegin(FacesContext context, UIComponent component)
050: throws IOException {
051: ;
052: }
053:
054: public void encodeChildren(FacesContext context,
055: UIComponent component) throws IOException {
056: ;
057: }
058:
059: /**
060: * <p>Render an alphabetical index. Makes any letter that exists in the
061: * initials parameter a link. Any other is simply displayed. </p>
062: * <p>Example: <br />
063: * | <a href='#A'><b>A</b></a> | <b>B</b>...
064: *
065: * @param context FacesContext for the request we are processing
066: * @param component UIComponent to be rendered
067: *
068: * @throws IOException if an input/output error occurs while rendering
069: * @throws NullPointerException if <code>context</code>
070: * or <code>component</code> is null
071: */
072: public void encodeEnd(FacesContext context, UIComponent component)
073: throws IOException {
074:
075: if ((context == null) || (component == null)) {
076: throw new NullPointerException();
077: }
078:
079: ResponseWriter writer = context.getResponseWriter();
080: String initials = (String) component.getAttributes().get(
081: "initials");
082:
083: if (initials == null)
084: initials = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
085:
086: // this provides a semi-acceptable default
087:
088: initials = initials.toUpperCase();
089:
090: writer.write("\n"); // this makes the HTML a little cleaner
091:
092: // loop through the index for each letter
093: for (char c = 'A'; c < 'Z' + 1; c++) {
094: // the student's last name starts with this letter, make this a link
095: if (initialExists(c, initials)) {
096: writer.write("|<a href='#" + c + "'><b>" + c
097: + "</b></a>");
098: } else // the intial list DOES NOT contain this letter
099: {
100: writer.write("|<b>" + c + "</b>");
101: }
102: }
103:
104: writer.write("\n"); // this makes the HTML a little cleaner
105: }
106:
107: /**
108: * Is the character c in the initials string?
109: * @param c
110: * @param initials
111: * @return true if it is
112: */
113: private boolean initialExists(char c, String initials) {
114: return initials.indexOf("" + c) > -1;
115: }
116:
117: }
|