001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.databinding.datagrid.api.sort;
020:
021: import java.util.List;
022: import org.apache.beehive.netui.databinding.datagrid.api.sort.SortDirection;
023:
024: /**
025: * <p>
026: * The SortModel class groups a set of {@link Sort} objects. This class also provides a set of methods
027: * for interacting with a group of {@link Sort} objects.
028: * </p>
029: * <p>
030: * The list of {@link Sort} objects in a SortModel are ordered. The first sort in group is known as the
031: * "primary" sort and subsequent Sorts are "secondary". The interpretation of using Sorts into
032: * to order a data set is left to implementations of sort algoritms. For example, one possible
033: * sort implementation could order the sorts such that the secondary sort occurs inside the
034: * data ordered by the primary sort and so on.
035: * </p>
036: * <p>
037: * In addition to accessing the Sort objects, the SortModel provides a the ability to plug a simple state
038: * machine that controls how to change a sort direction when cycling through the set of sort directions.
039: * For example, when using a data grid to sort columns of data, a column may start off unsorted,
040: * change to {@link SortDirection#ASCENDING}, to {@link SortDirection#DESCENDING}, and finally back to
041: * {@link SortDirection#NONE}. The {@link SortStrategy} allows this strategy to be changed so that the
042: * sorts can change from {@link SortDirection#NONE} to {@link SortDirection#DESCENDING}, to
043: * {@link SortDirection#ASCENDING}, and finally back to {@link SortDirection#NONE}.
044: * </p>
045: */
046: public class SortModel implements java.io.Serializable {
047:
048: /* todo: consider exposing a Map getSortMap() that would be JSP 2.0 EL bindable */
049: /* todo: expose a convenience method to change the direction of a sort */
050:
051: private SortStrategy _sortStrategy = null;
052: private List/*<Sort>*/_sorts = null;
053:
054: /**
055: * Construct the SortModel with a {@link List} of sorts.
056: * @param sorts the sorts
057: */
058: public SortModel(List/*<Sort>*/sorts) {
059: _sorts = sorts;
060: }
061:
062: /**
063: * Get the {@link SortStrategy} used to cycle these {@link Sort} objects through various
064: * {@link SortDirection}s.
065: * @return the {@link SortStrategy}
066: */
067: public SortStrategy getSortStrategy() {
068: return _sortStrategy;
069: }
070:
071: /**
072: * Set the {@link SortStrategy}.
073: * @param sortStrategy the new {@link SortStrategy}
074: */
075: public void setSortStrategy(SortStrategy sortStrategy) {
076: _sortStrategy = sortStrategy;
077: }
078:
079: public List/*<Sort>*/getSorts() {
080: if (_sorts == null)
081: return null;
082: else
083: return _sorts;
084: }
085:
086: /**
087: * Check to see if a sort expression is the first {@link Sort} in the {@link SortModel}. If the
088: * first {@link Sort} in the SortModel has a {@link Sort#getSortExpression()} that matches the
089: * <code>sortExpression</code> parameter, the method returns <code>true</code>. Otherwise, it
090: * returns <code>false</code>.
091: *
092: * @param sortExpression the sort expression to use when checking the SortModel's first {@link Sort}
093: * @return <code>true</code> if the first {@link Sort} has a sortExpression that matches the
094: * sortExpression parameter. <code>false</code> otherwise.
095: */
096: public boolean isPrimarySort(String sortExpression) {
097: if (sortExpression == null)
098: return false;
099:
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(
104: sortExpression)) {
105: return true;
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 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
126: return true;
127: }
128:
129: /**
130: * Get the {@link SortDirection} for a {@link Sort} given a sort expression.
131: *
132: * @param sortExpression the sort expression used to locate a {@link Sort} object.
133: * @return a {@link Sort}'s {@link SortDirection} if one is found whose <code>sortExpression</code>
134: * property matches the given <code>sortExpression</code>. <code>null</code> otherwise.
135: */
136: public SortDirection getSortDirection(String sortExpression) {
137: Sort term = findSort(sortExpression);
138: return term == null ? SortDirection.NONE : term.getDirection();
139: }
140:
141: /**
142: * Lookup a {@link Sort} object whose {@link Sort#getSortExpression()} matches
143: * the given <code>sortExpression</code>.
144: *
145: * @param sortExpression the sortExpression used to locate a {@link Sort}
146: * @return a {@link Sort} if one is found whose <code>sortExpression</code> property matches
147: * the given <code>sortExpression</code>. <code>null</code> otherwise.
148: */
149: public Sort lookupSort(String sortExpression) {
150: return findSort(sortExpression);
151: }
152:
153: private final Sort findSort(String sortExpression) {
154: if (_sorts == null)
155: return null;
156:
157: for (int i = 0; i < _sorts.size(); i++) {
158: assert _sorts.get(i) instanceof Sort;
159: Sort sort = (Sort) _sorts.get(i);
160: if (sort.getSortExpression().equals(sortExpression))
161: return sort;
162: }
163:
164: return null;
165: }
166: }
|