]>
git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/utils/NodeViewerComparator.java
1 package org
.argeo
.slc
.client
.ui
.dist
.utils
;
3 import java
.math
.BigDecimal
;
4 import java
.util
.Calendar
;
8 import javax
.jcr
.PropertyType
;
9 import javax
.jcr
.RepositoryException
;
10 import javax
.jcr
.Value
;
11 import javax
.jcr
.ValueFormatException
;
13 import org
.apache
.commons
.logging
.Log
;
14 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.argeo
.ArgeoException
;
16 import org
.argeo
.eclipse
.ui
.GenericTableComparator
;
17 import org
.eclipse
.jface
.viewers
.Viewer
;
19 public class NodeViewerComparator
extends GenericTableComparator
{
20 private final static Log log
= LogFactory
21 .getLog(NodeViewerComparator
.class);
23 protected List
<String
> propertiesList
;
24 protected List
<Integer
> propertyTypesList
;
25 protected Integer propertyType
;
26 protected String property
;
28 public NodeViewerComparator(int defaultColIndex
, int defaultDirection
,
29 List
<String
> propertiesList
, List
<Integer
> propertyTypesList
) {
30 super(defaultColIndex
, defaultDirection
);
31 this.propertiesList
= propertiesList
;
32 this.propertyTypesList
= propertyTypesList
;
33 this.propertyIndex
= defaultColIndex
;
34 this.propertyType
= propertyTypesList
.get(defaultColIndex
);
35 this.property
= propertiesList
.get(defaultColIndex
);
36 setColumn(defaultColIndex
);
40 public int compare(Viewer viewer
, Object e1
, Object e2
) {
50 if (n1
.hasProperty(property
))
51 v1
= n1
.getProperty(property
).getValue();
52 if (n2
.hasProperty(property
))
53 v2
= n2
.getProperty(property
).getValue();
55 if (v2
== null && v1
== null)
62 switch (propertyType
) {
63 case PropertyType
.STRING
:
64 rc
= v1
.getString().compareTo(v2
.getString());
66 case PropertyType
.BOOLEAN
:
67 boolean b1
= v1
.getBoolean();
68 boolean b2
= v2
.getBoolean();
72 // we assume true is greater than false
75 case PropertyType
.DATE
:
76 Calendar c1
= v1
.getDate();
77 Calendar c2
= v2
.getDate();
78 if (c1
== null || c2
== null)
79 log
.trace("undefined date");
80 lc
= c1
.getTimeInMillis() - c2
.getTimeInMillis();
81 if (lc
< Integer
.MIN_VALUE
)
82 // rc = Integer.MIN_VALUE;
84 else if (lc
> Integer
.MAX_VALUE
)
85 // rc = Integer.MAX_VALUE;
90 case PropertyType
.LONG
:
93 // FIXME sometimes an empty string is set instead of the id
96 } catch (ValueFormatException ve
) {
101 } catch (ValueFormatException ve
) {
106 if (lc
< Integer
.MIN_VALUE
)
107 // rc = Integer.MIN_VALUE;
109 else if (lc
> Integer
.MAX_VALUE
)
110 // rc = Integer.MAX_VALUE;
115 case PropertyType
.DECIMAL
:
116 BigDecimal bd1
= v1
.getDecimal();
117 BigDecimal bd2
= v2
.getDecimal();
118 rc
= bd1
.compareTo(bd2
);
121 throw new ArgeoException(
122 "Unimplemented comparaison for PropertyType "
126 // If descending order, flip the direction
127 if (direction
== DESCENDING
) {
131 } catch (RepositoryException re
) {
132 throw new ArgeoException("Unexpected error "
133 + "while comparing nodes", re
);
139 public void setColumn(int column
) {
140 if (column
== this.propertyIndex
) {
141 // Same column as last sort; toggle the direction
142 direction
= 1 - direction
;
144 // New column; do a descending sort
145 this.propertyIndex
= column
;
146 this.propertyType
= propertyTypesList
.get(column
);
147 this.property
= propertiesList
.get(column
);
148 direction
= ASCENDING
;