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