| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
package org.apache.tapestry5.internal.grid; |
| 16 | |
|
| 17 | |
import org.apache.tapestry5.PropertyConduit; |
| 18 | |
import org.apache.tapestry5.grid.ColumnSort; |
| 19 | |
import org.apache.tapestry5.grid.GridDataSource; |
| 20 | |
import org.apache.tapestry5.grid.SortConstraint; |
| 21 | |
import org.apache.tapestry5.ioc.internal.util.CollectionFactory; |
| 22 | |
import org.apache.tapestry5.ioc.internal.util.Defense; |
| 23 | |
|
| 24 | |
import java.util.Collection; |
| 25 | |
import java.util.Collections; |
| 26 | |
import java.util.Comparator; |
| 27 | |
import java.util.List; |
| 28 | |
|
| 29 | |
public class CollectionGridDataSource implements GridDataSource |
| 30 | |
{ |
| 31 | |
private final List list; |
| 32 | |
|
| 33 | |
@SuppressWarnings("unchecked") |
| 34 | |
public CollectionGridDataSource(final Collection collection) |
| 35 | 120 | { |
| 36 | 120 | Defense.notNull(collection, "collection"); |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | 120 | list = CollectionFactory.newList(collection); |
| 41 | 120 | } |
| 42 | |
|
| 43 | |
public int getAvailableRows() |
| 44 | |
{ |
| 45 | 116 | return list.size(); |
| 46 | |
} |
| 47 | |
|
| 48 | |
public void prepare(int startIndex, int endIndex, List<SortConstraint> sortConstraints) |
| 49 | |
{ |
| 50 | 112 | for (SortConstraint constraint : sortConstraints) |
| 51 | |
{ |
| 52 | 34 | final ColumnSort sort = constraint.getColumnSort(); |
| 53 | |
|
| 54 | 34 | if (sort == ColumnSort.UNSORTED) continue; |
| 55 | |
|
| 56 | 34 | final PropertyConduit conduit = constraint.getPropertyModel().getConduit(); |
| 57 | |
|
| 58 | 34 | final Comparator valueComparator = new Comparator<Comparable>() |
| 59 | |
{ |
| 60 | 210126 | public int compare(Comparable o1, Comparable o2) |
| 61 | |
{ |
| 62 | |
|
| 63 | |
|
| 64 | 210092 | if (o1 == o2) return 0; |
| 65 | |
|
| 66 | 210066 | if (o2 == null) return 1; |
| 67 | |
|
| 68 | 209886 | if (o1 == null) return -1; |
| 69 | |
|
| 70 | 209838 | return o1.compareTo(o2); |
| 71 | |
} |
| 72 | |
}; |
| 73 | |
|
| 74 | 34 | final Comparator rowComparator = new Comparator() |
| 75 | |
{ |
| 76 | 34 | public int compare(Object row1, Object row2) |
| 77 | |
{ |
| 78 | 210092 | Comparable value1 = (Comparable) conduit.get(row1); |
| 79 | 210092 | Comparable value2 = (Comparable) conduit.get(row2); |
| 80 | |
|
| 81 | 210092 | return valueComparator.compare(value1, value2); |
| 82 | |
} |
| 83 | |
}; |
| 84 | |
|
| 85 | 34 | final Comparator reverseComparator = new Comparator() |
| 86 | |
{ |
| 87 | 34 | public int compare(Object o1, Object o2) |
| 88 | |
{ |
| 89 | 210092 | int modifier = sort == ColumnSort.ASCENDING ? 1 : -1; |
| 90 | |
|
| 91 | 210092 | return modifier * rowComparator.compare(o1, o2); |
| 92 | |
} |
| 93 | |
}; |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | 34 | Collections.sort(list, reverseComparator); |
| 98 | 34 | } |
| 99 | 112 | } |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public Class getRowType() |
| 105 | |
{ |
| 106 | 102 | return list.isEmpty() ? null : list.get(0).getClass(); |
| 107 | |
} |
| 108 | |
|
| 109 | |
public Object getRowValue(int index) |
| 110 | |
{ |
| 111 | 1826 | return list.get(index); |
| 112 | |
} |
| 113 | |
|
| 114 | |
} |