View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.sortheader;
20  
21  import java.io.IOException;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.context.FacesContext;
25  import javax.faces.context.ResponseWriter;
26  
27  import org.apache.myfaces.component.UserRoleUtils;
28  import org.apache.myfaces.component.html.ext.HtmlDataTable;
29  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
30  import org.apache.myfaces.renderkit.html.ext.HtmlLinkRenderer;
31  
32  /**
33   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
34   * @version $Revision: 659874 $ $Date: 2008-05-24 15:59:15 -0500 (Sat, 24 May 2008) $
35   * $Log: HtmlSortHeaderRenderer.java,v $
36   * Revision 1.6  2004/10/13 11:50:58  matze
37   * renamed packages to org.apache
38   *
39   * Revision 1.5  2004/07/01 21:53:10  mwessendorf
40   * ASF switch
41   *
42   * Revision 1.4  2004/06/04 12:10:35  royalts
43   * added check on isArrow
44   *
45   * Revision 1.3  2004/05/18 14:31:38  manolito
46   * user role support completely moved to components source tree
47   *
48   * Revision 1.2  2004/04/22 09:20:55  manolito
49   * derive from HtmlLinkRendererBase instead of HtmlLinkRenderer
50   *
51   * @JSFRenderer
52   *   renderKitId = "HTML_BASIC" 
53   *   family = "javax.faces.Command"
54   *   type = "org.apache.myfaces.SortHeader"
55   *
56   */
57  public class HtmlSortHeaderRenderer
58          extends HtmlLinkRenderer
59  {
60      private static final String FACET_ASCENDING = "ascending";
61      private static final String FACET_DESCENDING = "descending";
62  
63      //private static final Log log = LogFactory.getLog(HtmlSortHeaderRenderer.class);
64  
65      public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException
66      {
67          RendererUtils.checkParamValidity(facesContext, component, HtmlCommandSortHeader.class);
68          if (UserRoleUtils.isEnabledOnUserRole(component))
69          {
70              HtmlCommandSortHeader sortHeader = (HtmlCommandSortHeader)component;
71              HtmlDataTable dataTable = sortHeader.findParentDataTable();
72              
73              if (sortHeader.getColumnName().equals(dataTable.getSortColumn()))
74              {
75                  UIComponent img = (dataTable.isSortAscending())
76                                    ? sortHeader.getFacet(FACET_ASCENDING)
77                                    : sortHeader.getFacet(FACET_DESCENDING);
78                  // render directional image
79                  if (img != null)
80                  {
81                      RendererUtils.renderChild(facesContext, img);
82                  }
83                  // render directional character
84                  if (sortHeader.isArrow())
85                  {
86                      ResponseWriter writer = facesContext.getResponseWriter();
87                      writer.write((dataTable.isSortAscending()) ? "↑" : "↓");
88                  }
89              }            
90          }
91          super.encodeEnd(facesContext, component);
92      }
93  
94  }