|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| package org.displaytag.model; |
|
13 |
| |
|
14 |
| import java.util.Comparator; |
|
15 |
| |
|
16 |
| import org.apache.commons.lang.builder.EqualsBuilder; |
|
17 |
| import org.apache.commons.lang.builder.HashCodeBuilder; |
|
18 |
| import org.displaytag.decorator.TableDecorator; |
|
19 |
| import org.displaytag.exception.ObjectLookupException; |
|
20 |
| import org.displaytag.exception.RuntimeLookupException; |
|
21 |
| import org.displaytag.util.LookupUtil; |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| public class RowSorter implements Comparator |
|
30 |
| { |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| private String property; |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| private TableDecorator decorator; |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| private boolean ascending; |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| private int columnIndex; |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| private Comparator comparator; |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
102
| public RowSorter(
|
|
67 |
| int sortedColumnIndex, |
|
68 |
| String beanProperty, |
|
69 |
| TableDecorator tableDecorator, |
|
70 |
| boolean ascendingOrder, |
|
71 |
| Comparator compar) |
|
72 |
| { |
|
73 |
102
| this.columnIndex = sortedColumnIndex;
|
|
74 |
102
| this.property = beanProperty;
|
|
75 |
102
| this.decorator = tableDecorator;
|
|
76 |
102
| this.ascending = ascendingOrder;
|
|
77 |
102
| this.comparator = compar;
|
|
78 |
102
| if (compar == null)
|
|
79 |
| { |
|
80 |
0
| throw new IllegalArgumentException(
|
|
81 |
| "A null comparator has been passed to RowSorter. A comparator instance is required"); |
|
82 |
| } |
|
83 |
| } |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
156
| public final int compare(Object object1, Object object2)
|
|
96 |
| { |
|
97 |
| |
|
98 |
156
| Object obj1 = null;
|
|
99 |
156
| Object obj2 = null;
|
|
100 |
| |
|
101 |
| |
|
102 |
156
| if (this.property == null)
|
|
103 |
| { |
|
104 |
12
| if (object1 instanceof Row)
|
|
105 |
| { |
|
106 |
12
| obj1 = ((Row) object1).getCellList().get(this.columnIndex);
|
|
107 |
| } |
|
108 |
12
| if (object2 instanceof Row)
|
|
109 |
| { |
|
110 |
12
| obj2 = ((Row) object2).getCellList().get(this.columnIndex);
|
|
111 |
| } |
|
112 |
| |
|
113 |
12
| return checkNullsAndCompare(obj1, obj2);
|
|
114 |
| } |
|
115 |
| |
|
116 |
144
| if (object1 instanceof Row)
|
|
117 |
| { |
|
118 |
144
| obj1 = ((Row) object1).getObject();
|
|
119 |
| } |
|
120 |
144
| if (object2 instanceof Row)
|
|
121 |
| { |
|
122 |
144
| obj2 = ((Row) object2).getObject();
|
|
123 |
| } |
|
124 |
| |
|
125 |
144
| try
|
|
126 |
| { |
|
127 |
144
| Object result1;
|
|
128 |
144
| Object result2;
|
|
129 |
| |
|
130 |
| |
|
131 |
144
| if (this.decorator != null && this.decorator.hasGetterFor(this.property))
|
|
132 |
| { |
|
133 |
| |
|
134 |
0
| this.decorator.initRow(obj1, 0, 0);
|
|
135 |
| |
|
136 |
0
| result1 = LookupUtil.getBeanProperty(this.decorator, this.property);
|
|
137 |
| |
|
138 |
| |
|
139 |
0
| this.decorator.initRow(obj2, 0, 0);
|
|
140 |
| |
|
141 |
0
| result2 = LookupUtil.getBeanProperty(this.decorator, this.property);
|
|
142 |
| } |
|
143 |
| else |
|
144 |
| { |
|
145 |
144
| result1 = LookupUtil.getBeanProperty(obj1, this.property);
|
|
146 |
144
| result2 = LookupUtil.getBeanProperty(obj2, this.property);
|
|
147 |
| } |
|
148 |
| |
|
149 |
144
| return checkNullsAndCompare(result1, result2);
|
|
150 |
| } |
|
151 |
| catch (ObjectLookupException e) |
|
152 |
| { |
|
153 |
0
| throw new RuntimeLookupException(getClass(), this.property, e);
|
|
154 |
| } |
|
155 |
| } |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
156
| private int checkNullsAndCompare(Object object1, Object object2)
|
|
164 |
| { |
|
165 |
156
| int returnValue;
|
|
166 |
156
| if (object1 == null && object2 != null)
|
|
167 |
| { |
|
168 |
0
| returnValue = -1;
|
|
169 |
| } |
|
170 |
156
| else if (object1 != null && object2 == null)
|
|
171 |
| { |
|
172 |
0
| returnValue = 1;
|
|
173 |
| } |
|
174 |
156
| else if (object1 == null && object2 == null)
|
|
175 |
| { |
|
176 |
| |
|
177 |
0
| returnValue = 0;
|
|
178 |
| } |
|
179 |
| else |
|
180 |
| { |
|
181 |
156
| returnValue = comparator.compare(object1, object2);
|
|
182 |
| } |
|
183 |
156
| int ascendingInt = this.ascending ? 1 : -1;
|
|
184 |
156
| return ascendingInt * returnValue;
|
|
185 |
| } |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
0
| public final boolean equals(Object object)
|
|
194 |
| { |
|
195 |
0
| if (object instanceof RowSorter)
|
|
196 |
| { |
|
197 |
0
| return new EqualsBuilder().append(this.property, ((RowSorter) object).property).append(
|
|
198 |
| this.columnIndex, |
|
199 |
| ((RowSorter) object).columnIndex).isEquals(); |
|
200 |
| } |
|
201 |
| |
|
202 |
0
| return false;
|
|
203 |
| } |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
0
| public final int hashCode()
|
|
209 |
| { |
|
210 |
0
| return new HashCodeBuilder(31, 33).append(this.property).append(this.columnIndex).toHashCode();
|
|
211 |
| } |
|
212 |
| |
|
213 |
| } |