]>
git.argeo.org Git - gpl/argeo-jcr.git/blob - swt/org.argeo.cms.jcr.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
.eclipse
.ui
.EclipseUiException
;
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 * JcrColumnDefinition 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.
46 * @return the comparison
49 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
)
93 else if (lc
> Integer
.MAX_VALUE
)
98 case PropertyType
.LONG
:
101 // TODO Sometimes an empty string is set instead of a long
104 } catch (ValueFormatException ve
) {
109 } catch (ValueFormatException ve
) {
114 if (lc
< Integer
.MIN_VALUE
)
116 else if (lc
> Integer
.MAX_VALUE
)
121 case PropertyType
.DECIMAL
:
122 BigDecimal bd1
= v1
.getDecimal();
123 BigDecimal bd2
= v2
.getDecimal();
124 rc
= bd1
.compareTo(bd2
);
126 case PropertyType
.DOUBLE
:
127 Double d1
= v1
.getDouble();
128 Double d2
= v2
.getDouble();
129 rc
= d1
.compareTo(d2
);
132 throw new EclipseUiException(
133 "Unimplemented comparaison for PropertyType "
136 // If descending order, flip the direction
137 if (direction
== DESCENDING
) {
141 } catch (RepositoryException re
) {
142 throw new EclipseUiException("Unexpected error "
143 + "while comparing nodes", re
);
149 * @param propertyType
150 * Corresponding JCR type
151 * @param propertyName
152 * name of the property to use.
154 public void setColumn(int propertyType
, String propertyName
) {
155 if (this.propertyName
!= null && this.propertyName
.equals(propertyName
)) {
156 // Same column as last sort; toggle the direction
157 direction
= 1 - direction
;
159 // New column; do an ascending sort
160 this.propertyType
= propertyType
;
161 this.propertyName
= propertyName
;
162 direction
= ASCENDING
;
166 // Getters and setters
167 protected String
getPropertyName() {
171 protected void setPropertyName(String propertyName
) {
172 this.propertyName
= propertyName
;
175 protected int getPropertyType() {
179 protected void setPropertyType(int propertyType
) {
180 this.propertyType
= propertyType
;
183 protected int getDirection() {
187 protected void setDirection(int direction
) {
188 this.direction
= direction
;