/
SortableFilterableModel.java
212 lines (176 loc) · 6.96 KB
/
SortableFilterableModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.el.ELContext;
import javax.faces.context.FacesContext;
import org.apache.commons.collections4.comparators.ComparatorChain;
import org.apache.myfaces.trinidad.model.CollectionModel;
import org.apache.myfaces.trinidad.model.SortCriterion;
public class SortableFilterableModel extends CollectionModel implements Serializable {
private List wrappedData;
private List<Integer> sortedFilteredIndexList;
private Integer baseIndex;
private SortCriterion sortCriterion = null;
public SortableFilterableModel(List wrappedData) {
super();
this.wrappedData = wrappedData;
sortedFilteredIndexList = new ArrayList<Integer>();
for (int i = 0; i < wrappedData.size(); i++) {
sortedFilteredIndexList.add(i);
}
}
public Object getRowKey() {
return isRowAvailable() ? baseIndex : null;
}
public void setRowKey(Object object) {
baseIndex = object == null ? -1 : ((Integer)object);
}
public boolean isRowAvailable() {
return sortedFilteredIndexList.indexOf(baseIndex) != -1;
}
public int getRowCount() {
return sortedFilteredIndexList.size();
}
public Object getRowData() {
return wrappedData.get(baseIndex);
}
public int getRowIndex() {
return sortedFilteredIndexList.indexOf(baseIndex);
}
public void setRowIndex(int i) {
if(i < 0 || i >= sortedFilteredIndexList.size()){
baseIndex = -1;
}else{
baseIndex = sortedFilteredIndexList.get(i);
}
}
public Object getWrappedData() {
return wrappedData;
}
public void setWrappedData(Object object) {
this.wrappedData = (List)object;
}
public List<Integer> getSortedFilteredIndexList() {
return sortedFilteredIndexList;
}
@Override
public boolean isSortable(String property) {
try {
Object data = wrappedData.get(0);
Object propertyValue = evaluateProperty(data, property);
// when the value is null, we don't know if we can sort it.
// by default let's support sorting of null values, and let the user
// turn off sorting if necessary:
return (propertyValue instanceof Comparable) ||
(propertyValue == null);
} catch (RuntimeException e) {
e.printStackTrace();
return false;
}
}
private Object evaluateProperty(Object base, String property) {
ELContext elCtx = FacesContext.getCurrentInstance().getELContext();
//simple property -> resolve value directly
if (!property.contains(".")) {
return elCtx.getELResolver().getValue(elCtx, base, property);
}
int index = property.indexOf('.');
Object newBase =
elCtx.getELResolver().getValue(elCtx, base, property.substring(0,
index));
return evaluateProperty(newBase, property.substring(index + 1));
}
@Override
public List<SortCriterion> getSortCriteria() {
if (sortCriterion == null) {
return Collections.emptyList();
} else {
return Collections.singletonList(sortCriterion);
}
}
@Override
public void setSortCriteria(List<SortCriterion> criteria) {
if ((criteria == null) || (criteria.isEmpty())) {
sortCriterion = null;
// restore unsorted order:
Collections.sort(sortedFilteredIndexList); //returns original order but still same filter
} else {
/*SortCriterion sc = criteria.get(0);
sortCriterion = sc;
if(null != sortCriterion) {
System.out.println("sortCriterion.getProperty()="+sortCriterion.getProperty());
System.out.println("sortCriterion.isAscending()="+sortCriterion.isAscending());
}
_sort(sortCriterion.getProperty(), sortCriterion.isAscending());*/
ComparatorChain chain = new ComparatorChain();
for(SortCriterion sort : criteria) {
SortCriterion sc = sort;
sortCriterion = sc;
if (getRowCount() == 0) {
return;
}
Comparator<Integer> comp = new Comp(sortCriterion.getProperty());
if (!sortCriterion.isAscending()) {
comp = new Inverter<Integer>(comp);
}
chain.addComparator(comp);
}
if (sortedFilteredIndexList!= null && !sortedFilteredIndexList.isEmpty()) {
Collections.sort(sortedFilteredIndexList, chain);
}
}
}
private void _sort(String property, boolean isAscending) {
if (getRowCount() == 0) {
return;
}
if (sortedFilteredIndexList!= null && !sortedFilteredIndexList.isEmpty()) {
Comparator<Integer> comp = new Comp(property);
if (!isAscending)
comp = new Inverter<Integer>(comp);
Collections.sort(sortedFilteredIndexList, comp);
}
}
private final class Comp implements Comparator<Integer> {
public Comp(String property) {
_prop = property;
}
public int compare(Integer x, Integer y) {
Object instance1 = wrappedData.get(x);
Object value1 = evaluateProperty(instance1, _prop);
Object instance2 = wrappedData.get(y);
Object value2 = evaluateProperty(instance2, _prop);
if (value1 == null)
return (value2 == null) ? 0 : -1;
if (value2 == null)
return 1;
if (value1 instanceof Comparable) {
if(value1 instanceof String && value2 instanceof String) {
return String.CASE_INSENSITIVE_ORDER.compare((String)value1, (String)value2);
} else {
return ((Comparable<Object>)value1).compareTo(value2);
}
} else {
// if the object is not a Comparable, then
// the best we can do is string comparison:
return value1.toString().toLowerCase().compareTo(value2.toString().toLowerCase());
}
}
private final String _prop;
}
private static final class Inverter<T> implements Comparator<T> {
public Inverter(Comparator<T> comp) {
_comp = comp;
}
public int compare(T o1, T o2) {
if(o1 instanceof String && o2 instanceof String) {
return String.CASE_INSENSITIVE_ORDER.compare((String)o1, (String)o2);
} else {
return _comp.compare(o2, o1);
}
}
private final Comparator<T> _comp;
}
}