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 javax.faces.component.UIComponent;
22  import javax.faces.event.AbortProcessingException;
23  import javax.faces.event.ActionEvent;
24  import javax.faces.event.FacesEvent;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.myfaces.component.html.ext.HtmlCommandLink;
29  import org.apache.myfaces.component.html.ext.HtmlDataTable;
30  
31  /**
32   * Clickable sort column header. 
33   * 
34   * Must be nested inside an extended data_table tag. This tag is 
35   * derived from the standard command_link tag and has the additional 
36   * attributes columnName and arrow. 
37   * 
38   * Note: In contrast to normal command links, the default for the 
39   * "immediate" attribute is "true". This is desirable as it avoids 
40   * validating all input fields in the enclosing form when the column 
41   * sort order changes. HOWEVER when the table contains input 
42   * components "immediate" must be set to false; otherwise input 
43   * fields will render blank after a sort, or will show their old 
44   * values (ie will not appear to sort though output fields in the 
45   * table will sort) when sort ordering is changed. 
46   * 
47   * Unless otherwise specified, all attributes accept static values or EL expressions.
48   * 
49   * @JSFComponent
50   *   name = "t:commandSortHeader"
51   *   class = "org.apache.myfaces.custom.sortheader.HtmlCommandSortHeader"
52   *   tagClass = "org.apache.myfaces.custom.sortheader.HtmlCommandSortHeaderTag"
53   * @since 1.1.7
54   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
55   * @version $Revision: 691856 $ $Date: 2008-09-03 21:40:30 -0500 (Wed, 03 Sep 2008) $
56   */
57  public abstract class AbstractHtmlCommandSortHeader
58          extends HtmlCommandLink
59  {
60      private static final Log log = LogFactory.getLog(AbstractHtmlCommandSortHeader.class);
61  
62      public static final String COMPONENT_TYPE = "org.apache.myfaces.HtmlCommandSortHeader";
63      public static final String COMPONENT_FAMILY = "javax.faces.Command";
64      public static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.SortHeader";
65  
66      /*
67      public boolean isImmediate()
68      {
69          return true;
70      }
71      */
72  
73      public void broadcast(FacesEvent event) throws AbortProcessingException
74      {
75          if (event instanceof ActionEvent)
76          {
77              HtmlDataTable dataTable = findParentDataTable();
78              if (dataTable == null)
79              {
80                  log.error("CommandSortHeader has no MyFacesHtmlDataTable parent");
81              }
82              else
83              {
84                  String colName = getColumnName();                                
85                  String currentSortColumn = dataTable.getSortColumn();                                
86                  
87                  boolean currentAscending = dataTable.isSortAscending();
88                  
89                  if (colName.equals(currentSortColumn))
90                  {
91                      String propName = getPropertyName();                       
92                      if (propName != null)
93                          dataTable.setSortProperty(getPropertyName());                        
94                      
95                      dataTable.setSortColumn(getColumnName()); 
96                      dataTable.setSortAscending(!currentAscending);
97                  }
98                  else
99                  {
100                     dataTable.setSortProperty(getPropertyName());
101                     dataTable.setSortColumn(getColumnName());
102                     dataTable.setSortAscending(true);
103                 }
104             }
105         }
106         super.broadcast(event);
107     }       
108 
109     public HtmlDataTable findParentDataTable()
110     {
111         UIComponent parent = getParent();
112         while (parent != null)
113         {
114             if (parent instanceof HtmlDataTable)
115             {
116                 return (HtmlDataTable)parent;
117             }
118             parent = parent.getParent();
119         }
120         return null;
121     }
122 
123     /**
124      * The name of this column. This name must uniquely identify this 
125      * column among all other (sortable) columns in the same 
126      * data_table. The sortColumn attribute of the embedding 
127      * data_table reflects the current sort column (see extended 
128      * data_table).
129      * 
130      * @JSFProperty
131      *   required="true"
132      */
133     public abstract String getColumnName();
134 
135     /**
136      * The property name associated with this column. This name must 
137      * be one of the properties of the row object by which the sorting 
138      * should be performed. The sortProperty attribute of the 
139      * embedding data_table reflects the current sort property 
140      * (see extended data_table).
141      * 
142      * @JSFProperty
143      */
144     public abstract String getPropertyName();
145 
146     /**
147      * Indicates whether an arrow, that shows the sort direction 
148      * should be rendered. Default: false
149      * 
150      * @JSFProperty
151      *   defaultValue = "false"
152      */
153     public abstract boolean isArrow();
154     
155 }