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