]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/NodeViewerComparator.java
1 package org
.argeo
.eclipse
.ui
.jcr
.lists
;
3 import java
.math
.BigDecimal
;
4 import java
.util
.Calendar
;
7 import javax
.jcr
.PropertyType
;
8 import javax
.jcr
.RepositoryException
;
9 import javax
.jcr
.Value
;
10 import javax
.jcr
.ValueFormatException
;
12 import org
.argeo
.ArgeoException
;
13 import org
.eclipse
.jface
.viewers
.Viewer
;
14 import org
.eclipse
.jface
.viewers
.ViewerComparator
;
17 * Base comparator to enable ordering on Table or Tree viewer that display Jcr
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>
28 public class NodeViewerComparator
extends ViewerComparator
{
30 protected String propertyName
;
32 protected int propertyType
;
33 public static final int ASCENDING
= 0, DESCENDING
= 1;
34 protected int direction
= DESCENDING
;
36 public NodeViewerComparator() {
40 * e1 and e2 must both be Jcr nodes.
48 public int compare(Viewer viewer
, Object e1
, Object e2
) {
59 if (n1
.hasProperty(propertyName
))
60 v1
= n1
.getProperty(propertyName
).getValue();
61 if (n2
.hasProperty(propertyName
))
62 v2
= n2
.getProperty(propertyName
).getValue();
64 if (v2
== null && v1
== null)
71 switch (propertyType
) {
72 case PropertyType
.STRING
:
73 rc
= v1
.getString().compareTo(v2
.getString());
75 case PropertyType
.BOOLEAN
:
76 boolean b1
= v1
.getBoolean();
77 boolean b2
= v2
.getBoolean();
81 // we assume true is greater than false
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");
90 lc
= c1
.getTimeInMillis() - c2
.getTimeInMillis();
91 if (lc
< Integer
.MIN_VALUE
)
92 // rc = Integer.MIN_VALUE;
94 else if (lc
> Integer
.MAX_VALUE
)
95 // rc = Integer.MAX_VALUE;
100 case PropertyType
.LONG
:
103 // FIXME sometimes an empty string is set instead of a long
106 } catch (ValueFormatException ve
) {
111 } catch (ValueFormatException ve
) {
116 if (lc
< Integer
.MIN_VALUE
)
118 else if (lc
> Integer
.MAX_VALUE
)
123 case PropertyType
.DECIMAL
:
124 BigDecimal bd1
= v1
.getDecimal();
125 BigDecimal bd2
= v2
.getDecimal();
126 rc
= bd1
.compareTo(bd2
);
128 case PropertyType
.DOUBLE
:
129 Double d1
= v1
.getDouble();
130 Double d2
= v2
.getDouble();
131 rc
= d1
.compareTo(d2
);
134 throw new ArgeoException(
135 "Unimplemented comparaison for PropertyType "
138 // If descending order, flip the direction
139 if (direction
== DESCENDING
) {
143 } catch (RepositoryException re
) {
144 throw new ArgeoException("Unexpected error "
145 + "while comparing nodes", re
);
151 * @param propertyType
152 * Corresponding JCR type
153 * @param propertyName
154 * name of the property to use.
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
;
161 // New column; do an ascending sort
162 this.propertyType
= propertyType
;
163 this.propertyName
= propertyName
;
164 direction
= ASCENDING
;
168 // Getters and setters
169 protected String
getPropertyName() {
173 protected void setPropertyName(String propertyName
) {
174 this.propertyName
= propertyName
;
177 protected int getPropertyType() {
181 protected void setPropertyType(int propertyType
) {
182 this.propertyType
= propertyType
;
185 protected int getDirection() {
189 protected void setDirection(int direction
) {
190 this.direction
= direction
;