001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sections/tags/sakai_2-4-1/sections-app-util/src/java/org/sakaiproject/tool/section/jsf/HtmlSortHeaderRenderer.java $
003: * $Id: HtmlSortHeaderRenderer.java 22607 2007-03-14 19:20:08Z jholtzman@berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Regents of the University of California and The Regents of the University of Michigan
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.section.jsf;
021:
022: import java.io.IOException;
023:
024: import javax.faces.component.UIComponent;
025: import javax.faces.component.html.HtmlGraphicImage;
026: import javax.faces.context.FacesContext;
027: import javax.faces.context.ResponseWriter;
028:
029: import org.apache.commons.lang.StringUtils;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.myfaces.component.UserRoleUtils;
033: import org.apache.myfaces.component.html.ext.HtmlDataTable;
034: import org.apache.myfaces.custom.sortheader.HtmlCommandSortHeader;
035: import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
036: import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlLinkRendererBase;
037:
038: /**
039: * Based on org.apache.myfaces.custom.sortheader.HtmlSortHeaderRenderer.
040: * Modified to better distinguish the current sort column and to use an image file
041: * to indicate direction.
042: *
043: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman </a>
044: */
045: public class HtmlSortHeaderRenderer extends HtmlLinkRendererBase {
046: private static final Log log = LogFactory
047: .getLog(HtmlSortHeaderRenderer.class);
048: public final static String CURRENT_SORT_STYLE = "currentSort";
049: public final static String NOT_CURRENT_SORT_STYLE = "notCurrentSort";
050:
051: public void encodeBegin(FacesContext facesContext,
052: UIComponent component) throws IOException {
053: // If this is a currently sorted sort header, always give it the "currentSort" CSS style class
054: if (component instanceof HtmlCommandSortHeader) {
055: HtmlCommandSortHeader sortHeader = (HtmlCommandSortHeader) component;
056: String styleClass = StringUtils.trimToNull(getStyleClass(
057: facesContext, component));
058: String newStyleClass;
059: String unStyleClass;
060: if (sortHeader.findParentDataTable().getSortColumn()
061: .equals(sortHeader.getColumnName())) {
062: newStyleClass = CURRENT_SORT_STYLE;
063: unStyleClass = NOT_CURRENT_SORT_STYLE;
064: } else {
065: newStyleClass = NOT_CURRENT_SORT_STYLE;
066: unStyleClass = CURRENT_SORT_STYLE;
067: }
068: if (StringUtils.indexOf(styleClass, newStyleClass) == -1) {
069: if (StringUtils.indexOf(styleClass, unStyleClass) != -1) {
070: styleClass = StringUtils.replace(styleClass,
071: unStyleClass, newStyleClass);
072: } else if (styleClass != null) {
073: styleClass = (new StringBuffer(styleClass)).append(
074: ' ').append(newStyleClass).toString();
075: } else {
076: styleClass = newStyleClass;
077: }
078: sortHeader.setStyleClass(styleClass);
079: }
080: }
081: super .encodeBegin(facesContext, component); //check for NP
082: }
083:
084: public void encodeEnd(FacesContext facesContext,
085: UIComponent component) throws IOException {
086: if (log.isDebugEnabled())
087: log.debug("encodeEnd rendering " + component);
088: if (!UserRoleUtils.isEnabledOnUserRole(component)) {
089: super .encodeEnd(facesContext, component);
090: } else {
091: HtmlCommandSortHeader sortHeader = (HtmlCommandSortHeader) component;
092: HtmlDataTable dataTable = sortHeader.findParentDataTable();
093:
094: if (sortHeader.isArrow()
095: && sortHeader.getColumnName().equals(
096: dataTable.getSortColumn())) {
097: ResponseWriter writer = facesContext
098: .getResponseWriter();
099:
100: writer.write(HTML.NBSP_ENTITY);
101:
102: HtmlGraphicImage image = new HtmlGraphicImage();
103: String path = facesContext.getExternalContext()
104: .getRequestContextPath();
105: if (dataTable.isSortAscending()) {
106: image.setValue(path + "/images/sortascending.gif");
107: } else {
108: image.setValue(path + "/images/sortdescending.gif");
109: }
110:
111: writer.startElement(HTML.IMG_ELEM, image);
112: writer.writeURIAttribute("src", image.getValue(), null);
113: writer.endElement(HTML.IMG_ELEM);
114: }
115: super.encodeEnd(facesContext, component);
116: }
117: }
118: }
|