001: // Copyright 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.grid;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
018: import static org.apache.tapestry.ioc.internal.util.Defense.notNull;
019:
020: import java.util.Collections;
021: import java.util.Comparator;
022: import java.util.List;
023:
024: import org.apache.tapestry.PropertyConduit;
025: import org.apache.tapestry.beaneditor.PropertyModel;
026: import org.apache.tapestry.grid.GridDataSource;
027:
028: public class ListGridDataSource implements GridDataSource {
029: private final List _list;
030:
031: @SuppressWarnings("unchecked")
032: public ListGridDataSource(final List list) {
033: notNull(list, "list");
034:
035: // Copy the list so that we can sort it without distubing the original
036:
037: _list = newList(list);
038: }
039:
040: public int getAvailableRows() {
041: return _list.size();
042: }
043:
044: @SuppressWarnings("unchecked")
045: public void prepare(int startIndex, int endIndex,
046: PropertyModel sortModel, final boolean ascending) {
047: if (sortModel == null)
048: return;
049:
050: final PropertyConduit conduit = sortModel.getConduit();
051:
052: final Comparator valueComparator = new Comparator<Comparable>() {
053: public int compare(Comparable o1, Comparable o2) {
054: // Simplify comparison, and handle case where both are nulls.
055:
056: if (o1 == o2)
057: return 0;
058:
059: if (o2 == null)
060: return 1;
061:
062: if (o1 == null)
063: return -1;
064:
065: return o1.compareTo(o2);
066: }
067: };
068:
069: final Comparator rowComparator = new Comparator() {
070: public int compare(Object row1, Object row2) {
071: Comparable value1 = (Comparable) conduit.get(row1);
072: Comparable value2 = (Comparable) conduit.get(row2);
073:
074: return valueComparator.compare(value1, value2);
075: }
076: };
077:
078: final Comparator reverseComparator = new Comparator() {
079: public int compare(Object o1, Object o2) {
080: int modifier = ascending ? 1 : -1;
081:
082: return modifier * rowComparator.compare(o1, o2);
083: }
084: };
085:
086: // We can freely sort this list because its just a copy.
087:
088: Collections.sort(_list, reverseComparator);
089: }
090:
091: /** Returns the type of the first element in the list, or null if the list is empty. */
092: public Class getRowType() {
093: return _list.isEmpty() ? null : _list.get(0).getClass();
094: }
095:
096: public Object getRowValue(int index) {
097: return _list.get(index);
098: }
099:
100: }
|