KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > databinding > datagrid > api > sort > SortModel


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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  * $Header:$
17  */

18 package org.apache.beehive.netui.databinding.datagrid.api.sort;
19
20 import java.util.List JavaDoc;
21 import org.apache.beehive.netui.databinding.datagrid.api.sort.SortDirection;
22
23 /**
24  * <p>
25  * The SortModel class groups a set of {@link Sort} objects. This class also provides a set of methods
26  * for interacting with a group of {@link Sort} objects.
27  * </p>
28  * <p>
29  * The list of {@link Sort} objects in a SortModel are ordered. The first sort in group is known as the
30  * "primary" sort and subsequent Sorts are "secondary". The interpretation of using Sorts into
31  * to order a data set is left to implementations of sort algoritms. For example, one possible
32  * sort implementation could order the sorts such that the secondary sort occurs inside the
33  * data ordered by the primary sort and so on.
34  * </p>
35  * <p>
36  * In addition to accessing the Sort objects, the SortModel provides a the ability to plug a simple state
37  * machine that controls how to change a sort direction when cycling through the set of sort directions.
38  * For example, when using a data grid to sort columns of data, a column may start off unsorted,
39  * change to {@link SortDirection#ASCENDING}, to {@link SortDirection#DESCENDING}, and finally back to
40  * {@link SortDirection#NONE}. The {@link SortStrategy} allows this strategy to be changed so that the
41  * sorts can change from {@link SortDirection#NONE} to {@link SortDirection#DESCENDING}, to
42  * {@link SortDirection#ASCENDING}, and finally back to {@link SortDirection#NONE}.
43  * </p>
44  */

45 public class SortModel
46     implements java.io.Serializable JavaDoc {
47
48     /* todo: consider exposing a Map getSortMap() that would be JSP 2.0 EL bindable */
49     /* todo: expose a convenience method to change the direction of a sort */
50
51     private SortStrategy _sortStrategy = null;
52     private List JavaDoc/*<Sort>*/ _sorts = null;
53
54     /**
55      * Construct the SortModel with a {@link List} of sorts.
56      * @param sorts the sorts
57      */

58     public SortModel(List JavaDoc/*<Sort>*/ sorts) {
59         _sorts = sorts;
60     }
61
62     /**
63      * Get the {@link SortStrategy} used to cycle these {@link Sort} objects through various
64      * {@link SortDirection}s.
65      * @return the {@link SortStrategy}
66      */

67     public SortStrategy getSortStrategy() {
68         return _sortStrategy;
69     }
70
71     /**
72      * Set the {@link SortStrategy}.
73      * @param sortStrategy the new {@link SortStrategy}
74      */

75     public void setSortStrategy(SortStrategy sortStrategy) {
76         _sortStrategy = sortStrategy;
77     }
78
79     public List JavaDoc/*<Sort>*/ getSorts() {
80         if(_sorts == null)
81             return null;
82         else
83             return _sorts;
84     }
85
86     /**
87      * Check to see if a sort expression is the first {@link Sort} in the {@link SortModel}. If the
88      * first {@link Sort} in the SortModel has a {@link Sort#getSortExpression()} that matches the
89      * <code>sortExpression</code> parameter, the method returns <code>true</code>. Otherwise, it
90      * returns <code>false</code>.
91      *
92      * @param sortExpression the sort expression to use when checking the SortModel's first {@link Sort}
93      * @return <code>true</code> if the first {@link Sort} has a sortExpression that matches the
94      * sortExpression parameter. <code>false</code> otherwise.
95      */

96     public boolean isPrimarySort(String JavaDoc sortExpression) {
97         if(sortExpression == null)
98             return false;
99
100         /* optimizing for the case where the sortExpression *is* the primary sort */
101         if(_sorts != null &&
102            _sorts.size() > 0 &&
103            ((Sort)_sorts.get(0)).getSortExpression().equals(sortExpression)) {
104             return true;
105         }
106         else
107             return false;
108     }
109
110     /**
111      * Check to see if the SortModel contains a {@link Sort} whose sort expression matches the given
112      * <code>sortExpression</code>.
113      *
114      * @param sortExpression the sortExpression used to locate a {@link Sort}
115      * @return <code>true</code> if a {@link Sort} is found whose {@link Sort#getSortExpression()} matches
116      * the given <code>sortExpression</code>. <code>false</code> otherwise.
117      */

118     public boolean isSorted(String JavaDoc sortExpression) {
119         if(sortExpression == null)
120             return false;
121
122         Sort term = findSort(sortExpression);
123         if(term == null || term.getDirection() == SortDirection.NONE)
124             return false;
125         else return true;
126     }
127
128     /**
129      * Get the {@link SortDirection} for a {@link Sort} given a sort expression.
130      *
131      * @param sortExpression the sort expression used to locate a {@link Sort} object.
132      * @return a {@link Sort}'s {@link SortDirection} if one is found whose <code>sortExpression</code>
133      * property matches the given <code>sortExpression</code>. <code>null</code> otherwise.
134      */

135     public SortDirection getSortDirection(String JavaDoc sortExpression) {
136         Sort term = findSort(sortExpression);
137         return term == null ? SortDirection.NONE : term.getDirection();
138     }
139
140     /**
141      * Lookup a {@link Sort} object whose {@link Sort#getSortExpression()} matches
142      * the given <code>sortExpression</code>.
143      *
144      * @param sortExpression the sortExpression used to locate a {@link Sort}
145      * @return a {@link Sort} if one is found whose <code>sortExpression</code> property matches
146      * the given <code>sortExpression</code>. <code>null</code> otherwise.
147      */

148     public Sort lookupSort(String JavaDoc sortExpression) {
149         return findSort(sortExpression);
150     }
151
152     private final Sort findSort(String JavaDoc sortExpression) {
153         if(_sorts == null)
154             return null;
155
156         for(int i = 0; i < _sorts.size(); i++) {
157             assert _sorts.get(i) instanceof Sort;
158             Sort sort = (Sort)_sorts.get(i);
159             if(sort.getSortExpression().equals(sortExpression))
160                 return sort;
161         }
162
163         return null;
164     }
165 }
166
Popular Tags