]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/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
{
29 private static final long serialVersionUID
= -7782916140737279027L;
31 protected String propertyName
;
33 protected int propertyType
;
34 public static final int ASCENDING
= 0, DESCENDING
= 1;
35 protected int direction
= DESCENDING
;
37 public NodeViewerComparator() {
41 * e1 and e2 must both be Jcr nodes.
49 public int compare(Viewer viewer
, Object e1
, Object e2
) {
60 if (n1
.hasProperty(propertyName
))
61 v1
= n1
.getProperty(propertyName
).getValue();
62 if (n2
.hasProperty(propertyName
))
63 v2
= n2
.getProperty(propertyName
).getValue();
65 if (v2
== null && v1
== null)
72 switch (propertyType
) {
73 case PropertyType
.STRING
:
74 rc
= v1
.getString().compareTo(v2
.getString());
76 case PropertyType
.BOOLEAN
:
77 boolean b1
= v1
.getBoolean();
78 boolean b2
= v2
.getBoolean();
82 // we assume true is greater than false
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");
91 lc
= c1
.getTimeInMillis() - c2
.getTimeInMillis();
92 if (lc
< Integer
.MIN_VALUE
)
93 // rc = Integer.MIN_VALUE;
95 else if (lc
> Integer
.MAX_VALUE
)
96 // rc = Integer.MAX_VALUE;
101 case PropertyType
.LONG
:
104 // FIXME sometimes an empty string is set instead of a long
107 } catch (ValueFormatException ve
) {
112 } catch (ValueFormatException ve
) {
117 if (lc
< Integer
.MIN_VALUE
)
119 else if (lc
> Integer
.MAX_VALUE
)
124 case PropertyType
.DECIMAL
:
125 BigDecimal bd1
= v1
.getDecimal();
126 BigDecimal bd2
= v2
.getDecimal();
127 rc
= bd1
.compareTo(bd2
);
129 case PropertyType
.DOUBLE
:
130 Double d1
= v1
.getDouble();
131 Double d2
= v2
.getDouble();
132 rc
= d1
.compareTo(d2
);
135 throw new ArgeoException(
136 "Unimplemented comparaison for PropertyType "
139 // If descending order, flip the direction
140 if (direction
== DESCENDING
) {
144 } catch (RepositoryException re
) {
145 throw new ArgeoException("Unexpected error "
146 + "while comparing nodes", re
);
152 * @param propertyType
153 * Corresponding JCR type
154 * @param propertyName
155 * name of the property to use.
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
;
162 // New column; do an ascending sort
163 this.propertyType
= propertyType
;
164 this.propertyName
= propertyName
;
165 direction
= ASCENDING
;
169 // Getters and setters
170 protected String
getPropertyName() {
174 protected void setPropertyName(String propertyName
) {
175 this.propertyName
= propertyName
;
178 protected int getPropertyType() {
182 protected void setPropertyType(int propertyType
) {
183 this.propertyType
= propertyType
;
186 protected int getDirection() {
190 protected void setDirection(int direction
) {
191 this.direction
= direction
;