001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/contrib/ufp/usermembership/trunk/tool/src/java/org/sakaiproject/umem/tool/jsf/HtmlSortHeaderRenderer.java $
003: * $Id: HtmlSortHeaderRenderer.java 4381 2007-03-21 11:25:54Z nuno@ufp.pt $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006, 2007 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.umem.tool.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_impl.renderkit.html.HTML;
036: import org.apache.myfaces.shared_impl.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
041: * file to indicate direction.
042: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman </a>
043: */
044: public class HtmlSortHeaderRenderer extends HtmlLinkRendererBase {
045: private static final Log log = LogFactory
046: .getLog(HtmlSortHeaderRenderer.class);
047: public final static String CURRENT_SORT_STYLE = "currentSort";
048: public final static String NOT_CURRENT_SORT_STYLE = "notCurrentSort";
049:
050: public void encodeBegin(FacesContext facesContext,
051: UIComponent component) throws IOException {
052: // If this is a currently sorted sort header, always give it the
053: // "currentSort" CSS style class
054: try {
055: if (component instanceof HtmlCommandSortHeader) {
056: HtmlCommandSortHeader sortHeader = (HtmlCommandSortHeader) component;
057: String styleClass = StringUtils
058: .trimToNull(getStyleClass(facesContext,
059: component));
060: String newStyleClass;
061: String unStyleClass;
062: if (sortHeader.findParentDataTable().getSortColumn()
063: .equals(sortHeader.getColumnName())) {
064: newStyleClass = CURRENT_SORT_STYLE;
065: unStyleClass = NOT_CURRENT_SORT_STYLE;
066: } else {
067: newStyleClass = NOT_CURRENT_SORT_STYLE;
068: unStyleClass = CURRENT_SORT_STYLE;
069: }
070: if (StringUtils.indexOf(styleClass, newStyleClass) == -1) {
071: if (StringUtils.indexOf(styleClass, unStyleClass) != -1) {
072: styleClass = StringUtils.replace(styleClass,
073: unStyleClass, newStyleClass);
074: } else if (styleClass != null) {
075: styleClass = (new StringBuffer(styleClass))
076: .append(' ').append(newStyleClass)
077: .toString();
078: } else {
079: styleClass = newStyleClass;
080: }
081: sortHeader.setStyleClass(styleClass);
082: }
083: }
084: } catch (Exception e) {
085: log.warn("Exception occurred in HtmlSortHeaderRenderer:"
086: + e.getMessage());
087: }
088: super .encodeBegin(facesContext, component); // check for NP
089: }
090:
091: public void encodeEnd(FacesContext facesContext,
092: UIComponent component) throws IOException {
093: try {
094: if (log.isDebugEnabled())
095: log.debug("encodeEnd rendering " + component);
096: if (!UserRoleUtils.isEnabledOnUserRole(component)) {
097: super .encodeEnd(facesContext, component);
098: } else {
099: HtmlCommandSortHeader sortHeader = (HtmlCommandSortHeader) component;
100: HtmlDataTable dataTable = sortHeader
101: .findParentDataTable();
102:
103: if (sortHeader.isArrow()
104: && sortHeader.getColumnName().equals(
105: dataTable.getSortColumn())) {
106: ResponseWriter writer = facesContext
107: .getResponseWriter();
108: writer.write(HTML.NBSP_ENTITY);
109: HtmlGraphicImage image = new HtmlGraphicImage();
110: // String path =
111: // facesContext.getExternalContext().getRequestContextPath();
112: if (dataTable.isSortAscending()) {
113: // image.setValue(path + "/images/sortascending.gif");
114: image
115: .setValue("/library/image/sakai/sortascending.gif");
116: image.setAlt("Sort by title ascending");
117: image.setTitle("Sort by title ascending");
118: } else {
119: // image.setValue(path + "/images/sortdescending.gif");
120: image
121: .setValue("/library/image/sakai/sortdescending.gif");
122: image.setAlt("Sort by title descending");
123: image.setTitle("Sort by title descending");
124: }
125:
126: writer.startElement(HTML.IMG_ELEM, image);
127: writer.writeURIAttribute("src", image.getValue(),
128: null);
129: writer.writeURIAttribute("alt", image.getAlt(),
130: null);
131: writer.writeURIAttribute("title", image.getTitle(),
132: null);
133: writer.endElement(HTML.IMG_ELEM);
134: }
135: super .encodeEnd(facesContext, component);
136: }
137: } catch (Exception e) {
138: log.warn("Exception occurred in HtmlSortHeaderRenderer:"
139: + e.getMessage());
140: }
141: }
142: }
|