]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/jcr/lists/NodeViewerComparator.java
All Commons modules building (Eclipse included)
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui / src / org / argeo / eclipse / ui / jcr / lists / NodeViewerComparator.java
1 package org.argeo.eclipse.ui.jcr.lists;
2
3 import java.math.BigDecimal;
4 import java.util.Calendar;
5
6 import javax.jcr.Node;
7 import javax.jcr.PropertyType;
8 import javax.jcr.RepositoryException;
9 import javax.jcr.Value;
10 import javax.jcr.ValueFormatException;
11
12 import org.argeo.ArgeoException;
13 import org.eclipse.jface.viewers.Viewer;
14 import org.eclipse.jface.viewers.ViewerComparator;
15
16 /**
17 * Base comparator to enable ordering on Table or Tree viewer that display Jcr
18 * Nodes.
19 *
20 * Note that the following snippet must be added before setting the comparator
21 * to the corresponding control: <code>
22 * // IMPORTANT: initialize comparator before setting it
23 * ColumnDefinition firstCol = colDefs.get(0);
24 * comparator.setColumn(firstCol.getPropertyType(),
25 * firstCol.getPropertyName());
26 * viewer.setComparator(comparator); </code>
27 */
28 public class NodeViewerComparator extends ViewerComparator {
29
30 protected String propertyName;
31
32 protected int propertyType;
33 public static final int ASCENDING = 0, DESCENDING = 1;
34 protected int direction = DESCENDING;
35
36 public NodeViewerComparator() {
37 }
38
39 /**
40 * e1 and e2 must both be Jcr nodes.
41 *
42 * @param viewer
43 * @param e1
44 * @param e2
45 * @return
46 */
47 @Override
48 public int compare(Viewer viewer, Object e1, Object e2) {
49 int rc = 0;
50 long lc = 0;
51
52 try {
53
54 Node n1 = (Node) e1;
55 Node n2 = (Node) e2;
56
57 Value v1 = null;
58 Value v2 = null;
59 if (n1.hasProperty(propertyName))
60 v1 = n1.getProperty(propertyName).getValue();
61 if (n2.hasProperty(propertyName))
62 v2 = n2.getProperty(propertyName).getValue();
63
64 if (v2 == null && v1 == null)
65 return 0;
66 else if (v2 == null)
67 return -1;
68 else if (v1 == null)
69 return 1;
70
71 switch (propertyType) {
72 case PropertyType.STRING:
73 rc = v1.getString().compareTo(v2.getString());
74 break;
75 case PropertyType.BOOLEAN:
76 boolean b1 = v1.getBoolean();
77 boolean b2 = v2.getBoolean();
78 if (b1 == b2)
79 rc = 0;
80 else
81 // we assume true is greater than false
82 rc = b1 ? 1 : -1;
83 break;
84 case PropertyType.DATE:
85 Calendar c1 = v1.getDate();
86 Calendar c2 = v2.getDate();
87 if (c1 == null || c2 == null)
88 // log.trace("undefined date");
89 ;
90 lc = c1.getTimeInMillis() - c2.getTimeInMillis();
91 if (lc < Integer.MIN_VALUE)
92 // rc = Integer.MIN_VALUE;
93 rc = -1;
94 else if (lc > Integer.MAX_VALUE)
95 // rc = Integer.MAX_VALUE;
96 rc = 1;
97 else
98 rc = (int) lc;
99 break;
100 case PropertyType.LONG:
101 long l1;
102 long l2;
103 // FIXME sometimes an empty string is set instead of a long
104 try {
105 l1 = v1.getLong();
106 } catch (ValueFormatException ve) {
107 l1 = 0;
108 }
109 try {
110 l2 = v2.getLong();
111 } catch (ValueFormatException ve) {
112 l2 = 0;
113 }
114
115 lc = l1 - l2;
116 if (lc < Integer.MIN_VALUE)
117 rc = -1;
118 else if (lc > Integer.MAX_VALUE)
119 rc = 1;
120 else
121 rc = (int) lc;
122 break;
123 case PropertyType.DECIMAL:
124 BigDecimal bd1 = v1.getDecimal();
125 BigDecimal bd2 = v2.getDecimal();
126 rc = bd1.compareTo(bd2);
127 break;
128 case PropertyType.DOUBLE:
129 Double d1 = v1.getDouble();
130 Double d2 = v2.getDouble();
131 rc = d1.compareTo(d2);
132 break;
133 default:
134 throw new ArgeoException(
135 "Unimplemented comparaison for PropertyType "
136 + propertyType);
137 }
138 // If descending order, flip the direction
139 if (direction == DESCENDING) {
140 rc = -rc;
141 }
142
143 } catch (RepositoryException re) {
144 throw new ArgeoException("Unexpected error "
145 + "while comparing nodes", re);
146 }
147 return rc;
148 }
149
150 /**
151 * @param propertyType
152 * Corresponding JCR type
153 * @param propertyName
154 * name of the property to use.
155 */
156 public void setColumn(int propertyType, String propertyName) {
157 if (this.propertyName != null && this.propertyName.equals(propertyName)) {
158 // Same column as last sort; toggle the direction
159 direction = 1 - direction;
160 } else {
161 // New column; do an ascending sort
162 this.propertyType = propertyType;
163 this.propertyName = propertyName;
164 direction = ASCENDING;
165 }
166 }
167
168 // Getters and setters
169 protected String getPropertyName() {
170 return propertyName;
171 }
172
173 protected void setPropertyName(String propertyName) {
174 this.propertyName = propertyName;
175 }
176
177 protected int getPropertyType() {
178 return propertyType;
179 }
180
181 protected void setPropertyType(int propertyType) {
182 this.propertyType = propertyType;
183 }
184
185 protected int getDirection() {
186 return direction;
187 }
188
189 protected void setDirection(int direction) {
190 this.direction = direction;
191 }
192 }