001: /**********************************************************************************
002: *
003: * $Id: HtmlSortHeaderRenderer.java 21937 2007-02-27 00:10:58Z jholtzman@berkeley.edu $
004: *
005: ***********************************************************************************
006: *
007: * Copyright (c) 2005 The Regents of the University of California, The MIT Corporation
008: *
009: * Licensed under the Educational Community License, Version 1.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.opensource.org/licenses/ecl1.php
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: *
021: **********************************************************************************/package org.sakaiproject.tool.gradebook.jsf;
022:
023: import java.io.IOException;
024:
025: import javax.faces.component.UIComponent;
026: import javax.faces.component.html.HtmlGraphicImage;
027: import javax.faces.context.FacesContext;
028: import javax.faces.context.ResponseWriter;
029:
030: import org.apache.commons.lang.StringUtils;
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.apache.myfaces.component.UserRoleUtils;
034: import org.apache.myfaces.component.html.ext.HtmlDataTable;
035: import org.apache.myfaces.custom.sortheader.HtmlCommandSortHeader;
036: import org.apache.myfaces.shared_impl.renderkit.html.HTML;
037: import org.apache.myfaces.shared_impl.renderkit.html.HtmlLinkRendererBase;
038:
039: /**
040: * Based on org.apache.myfaces.custom.sortheader.HtmlSortHeaderRenderer.
041: * Modified to better distinguish the current sort column and to use an image file
042: * to indicate direction.
043: *
044: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman </a>
045: */
046: public class HtmlSortHeaderRenderer extends HtmlLinkRendererBase {
047: private static final Log log = LogFactory
048: .getLog(HtmlSortHeaderRenderer.class);
049: public final static String CURRENT_SORT_STYLE = "currentSort";
050: public final static String NOT_CURRENT_SORT_STYLE = "notCurrentSort";
051:
052: public void encodeBegin(FacesContext facesContext,
053: UIComponent component) throws IOException {
054: // If this is a currently sorted sort header, always give it the "currentSort" CSS style class
055: if (component instanceof HtmlCommandSortHeader) {
056: HtmlCommandSortHeader sortHeader = (HtmlCommandSortHeader) component;
057: String styleClass = StringUtils.trimToNull(getStyleClass(
058: facesContext, component));
059: String newStyleClass;
060: String unStyleClass;
061: if (sortHeader.findParentDataTable().getSortColumn()
062: .equals(sortHeader.getColumnName())) {
063: newStyleClass = CURRENT_SORT_STYLE;
064: unStyleClass = NOT_CURRENT_SORT_STYLE;
065: } else {
066: newStyleClass = NOT_CURRENT_SORT_STYLE;
067: unStyleClass = CURRENT_SORT_STYLE;
068: }
069: if (StringUtils.indexOf(styleClass, newStyleClass) == -1) {
070: if (StringUtils.indexOf(styleClass, unStyleClass) != -1) {
071: styleClass = StringUtils.replace(styleClass,
072: unStyleClass, newStyleClass);
073: } else if (styleClass != null) {
074: styleClass = (new StringBuffer(styleClass)).append(
075: ' ').append(newStyleClass).toString();
076: } else {
077: styleClass = newStyleClass;
078: }
079: sortHeader.setStyleClass(styleClass);
080: }
081: }
082: super .encodeBegin(facesContext, component); //check for NP
083: }
084:
085: public void encodeEnd(FacesContext facesContext,
086: UIComponent component) throws IOException {
087: if (log.isDebugEnabled())
088: log.debug("encodeEnd rendering " + component);
089: if (!UserRoleUtils.isEnabledOnUserRole(component)) {
090: super .encodeEnd(facesContext, component);
091: } else {
092: HtmlCommandSortHeader sortHeader = (HtmlCommandSortHeader) component;
093: HtmlDataTable dataTable = sortHeader.findParentDataTable();
094:
095: if (sortHeader.isArrow()
096: && sortHeader.getColumnName().equals(
097: dataTable.getSortColumn())) {
098: ResponseWriter writer = facesContext
099: .getResponseWriter();
100:
101: writer.write(HTML.NBSP_ENTITY);
102:
103: HtmlGraphicImage image = new HtmlGraphicImage();
104: String path = facesContext.getExternalContext()
105: .getRequestContextPath();
106: if (dataTable.isSortAscending()) {
107: image.setValue(path + "/images/sortascending.gif");
108: } else {
109: image.setValue(path + "/images/sortdescending.gif");
110: }
111:
112: writer.startElement(HTML.IMG_ELEM, image);
113: writer.writeURIAttribute("src", image.getValue(), null);
114: writer.endElement(HTML.IMG_ELEM);
115: }
116: super.encodeEnd(facesContext, component);
117: }
118: }
119: }
|