View Javadoc

1   /*
2    * Copyright 2004 Jeff Johnston
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.extremecomponents.table.model;
17  
18  import java.util.Collections;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Set;
23  
24  import org.apache.commons.beanutils.BeanComparator;
25  import org.apache.commons.collections.comparators.NullComparator;
26  import org.apache.commons.collections.comparators.ReverseComparator;
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  
32  /***
33   * Deal with the sorting.
34   *
35   * @author Jeff Johnston
36   */
37  public class SortHandler
38  {
39  	private static Log logger = LogFactory.getLog(SortHandler.class);
40  	private TableModel model;
41  	
42  	public final static String SORT_ASC_ = "sort_asc";
43  	public final static String SORT_DESC_ = "sort_desc";
44  	public final static String SORT_DEFAULT_ = "sort_default";
45  	public final static String SORT_ASC_IMAGE = "sortAsc";
46  	public final static String SORT_DESC_IMAGE = "sortDesc";
47  	
48  	public SortHandler(TableModel model)
49  	{
50  		this.model = model;
51  	}
52  	
53  	/***
54  	 * Find out if the table is trying to sort.
55  	 */
56  	public boolean canSort(String property)
57  	{
58  		if (!StringUtils.isEmpty(model.getRegistry().getParameter(ParameterRegistry.SORT + property)))
59  		{
60  			return true;
61  		}
62  
63  		return false;
64  	}
65  
66  	/***
67  	 * Find the column that is doing the sorting. Use the property to key off of.
68  	 */
69  	public String getSortableProperty()
70  	{
71  		Map parameters = model.getRegistry().getParameters(ParameterRegistry.SORT) ;
72  
73  		Set set = parameters.keySet();
74  		for (Iterator iter = set.iterator(); iter.hasNext();)
75  		{
76  			String name = (String) iter.next();
77  			String sortParamStr = model.getTableKey() + ParameterRegistry.SORT;
78  			return name.substring(sortParamStr.length());
79  		}
80  		
81  		return null;
82  	}
83  
84  	/***
85  	 * Take the sortable property and then sort the Collection
86  	 */
87  	public void sort(List rows)
88  	{
89  		String property = getSortableProperty();
90  
91  		if (!StringUtils.isEmpty(property))
92  		{
93  			String sortOrder = model.getRegistry().getParameter(ParameterRegistry.SORT + property);
94  
95  			if (sortOrder.equals(SORT_DEFAULT_)) //set the sort back now
96  			{
97  				return;
98  			}
99  
100 			if (sortOrder.equals(SORT_ASC_))
101 			{
102 				BeanComparator comparator = new BeanComparator(property, new NullComparator());
103 				Collections.sort(rows, comparator);
104 			}
105 			else if (sortOrder.equals(SORT_DESC_))
106 			{
107 				BeanComparator reversedNaturalOrderBeanComparator = new BeanComparator(property, new ReverseComparator(new NullComparator()));
108 				Collections.sort(rows, reversedNaturalOrderBeanComparator);
109 			}
110 		}
111 	}
112 }