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.tree.model;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.apache.commons.beanutils.BeanComparator;
24  import org.apache.commons.collections.comparators.NullComparator;
25  import org.apache.commons.collections.comparators.ReverseComparator;
26  import org.apache.commons.lang.StringUtils;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.extremecomponents.table.model.ParameterRegistry;
30  import org.extremecomponents.table.model.TableModel;
31  import org.extremecomponents.tree.bean.TreeNode;
32  
33  
34  /***
35   * Deal with the sorting.
36   *
37   * @author Jeff Johnston
38   */
39  public class SortHandler extends org.extremecomponents.table.model.SortHandler
40  {
41  	private static Log logger = LogFactory.getLog(SortHandler.class);
42  	private TreeModel model;
43  	private String property;
44  	
45  	public SortHandler(TableModel model)
46  	{
47  		super(model);
48  		this.model = (TreeModel) model;
49  	}
50  
51  	/***
52  	 * Take the sortable property and then sort the Collection.  Always sort
53  	 * by parentAttribute first.
54  	 */
55  	public void sort(List rows)
56  	{
57  		property = getSortableProperty();
58  
59  		if (!StringUtils.isEmpty(property))
60  		{
61  			String sortOrder = model.getRegistry().getParameter(ParameterRegistry.SORT + property);
62  
63  			if (sortOrder.equals(SORT_DEFAULT_)) //set the sort back now
64  			{
65  				return;
66  			}
67  			
68  			List parents = new ArrayList();
69  			for (Iterator iter = rows.iterator(); iter.hasNext();)
70  			{
71  				TreeNode node = (TreeNode) iter.next();
72  				if (node.getParent() == null) parents.add(node);
73  			}
74  			
75  			List output = new ArrayList();
76  			subSort(parents, sortOrder); //First sort the parents
77  			recursiveSort(output, parents, sortOrder);
78  			
79  			output.retainAll(rows); //Only keep the original nodes
80  			rows.clear();
81  			rows.addAll(output);
82  		}
83  	}
84  	
85  	public void recursiveSort(List output, List rows, String sortOrder)
86  	{
87  		for (Iterator iter = rows.iterator(); iter.hasNext();)
88  		{
89  			TreeNode node = (TreeNode) iter.next();
90  			
91  			output.add(node);
92  
93  			if (node.getChildren() != null && node.getChildren().size() > 0)
94  			{
95  				subSort(node.getChildren(), sortOrder);
96  				recursiveSort(output, node.getChildren(), sortOrder);
97  			}
98  		}
99  	}
100 	
101 	public void subSort(List rows, String sortOrder)
102 	{
103 		if (sortOrder.equals(SORT_ASC_))
104 		{
105 			BeanComparator comparator = new BeanComparator(property, new NullComparator());
106 			Collections.sort(rows, comparator);
107 		}
108 		else if (sortOrder.equals(SORT_DESC_))
109 		{
110 			BeanComparator reversedNaturalOrderBeanComparator = new BeanComparator(property, new ReverseComparator(new NullComparator()));
111 			Collections.sort(rows, reversedNaturalOrderBeanComparator);
112 		}
113 	}
114 }