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
.eclipse
.ui
.GenericTableComparator
;
16 import org
.argeo
.slc
.SlcException
;
17 import org
.eclipse
.jface
.viewers
.Viewer
;
19 /** Add ability to order by name version and version */
20 public class DistNodeViewerComparator
extends GenericTableComparator
{
21 private static final long serialVersionUID
= -5966120108210992211L;
23 private final static Log log
= LogFactory
24 .getLog(DistNodeViewerComparator
.class);
26 // Jcr property type goes to 12
27 public final static int NAME_VERSION_TYPE
= 100;
28 public final static int VERSION_TYPE
= 101;
30 protected List
<String
> propertiesList
;
31 protected List
<Integer
> propertyTypesList
;
32 protected Integer propertyType
;
33 protected String property
;
35 private NameVersionComparator nvc
= new NameVersionComparator();
36 private VersionComparator vc
= new VersionComparator();
38 public DistNodeViewerComparator(int defaultColIndex
, int defaultDirection
,
39 List
<String
> propertiesList
, List
<Integer
> propertyTypesList
) {
40 super(defaultColIndex
, defaultDirection
);
41 this.propertiesList
= propertiesList
;
42 this.propertyTypesList
= propertyTypesList
;
43 this.propertyIndex
= defaultColIndex
;
44 this.propertyType
= propertyTypesList
.get(defaultColIndex
);
45 this.property
= propertiesList
.get(defaultColIndex
);
46 setColumn(defaultColIndex
);
50 public int compare(Viewer viewer
, Object e1
, Object e2
) {
60 if (n1
.hasProperty(property
))
61 v1
= n1
.getProperty(property
).getValue();
62 if (n2
.hasProperty(property
))
63 v2
= n2
.getProperty(property
).getValue();
65 if (v2
== null && v1
== null)
72 switch (propertyType
) {
73 case NAME_VERSION_TYPE
:
74 rc
= nvc
.compare(viewer
, v1
.getString(), v2
.getString());
77 rc
= vc
.compare(viewer
, v1
.getString(), v2
.getString());
79 case PropertyType
.STRING
:
80 rc
= v1
.getString().compareTo(v2
.getString());
82 case PropertyType
.BOOLEAN
:
83 boolean b1
= v1
.getBoolean();
84 boolean b2
= v2
.getBoolean();
88 // we assume true is greater than false
91 case PropertyType
.DATE
:
92 Calendar c1
= v1
.getDate();
93 Calendar c2
= v2
.getDate();
94 if (c1
== null || c2
== null)
95 log
.trace("undefined date");
96 lc
= c1
.getTimeInMillis() - c2
.getTimeInMillis();
97 if (lc
< Integer
.MIN_VALUE
)
98 // rc = Integer.MIN_VALUE;
100 else if (lc
> Integer
.MAX_VALUE
)
101 // rc = Integer.MAX_VALUE;
106 case PropertyType
.LONG
:
109 // FIXME sometimes an empty string is set instead of the id
112 } catch (ValueFormatException ve
) {
117 } catch (ValueFormatException ve
) {
122 if (lc
< Integer
.MIN_VALUE
)
123 // rc = Integer.MIN_VALUE;
125 else if (lc
> Integer
.MAX_VALUE
)
126 // rc = Integer.MAX_VALUE;
131 case PropertyType
.DECIMAL
:
132 BigDecimal bd1
= v1
.getDecimal();
133 BigDecimal bd2
= v2
.getDecimal();
134 rc
= bd1
.compareTo(bd2
);
137 throw new SlcException(
138 "Unimplemented comparaison for PropertyType "
142 // If descending order, flip the direction
143 if (direction
== DESCENDING
) {
147 } catch (RepositoryException re
) {
148 throw new SlcException("Unexpected error "
149 + "while comparing nodes", re
);
155 public void setColumn(int column
) {
156 if (column
== this.propertyIndex
) {
157 // Same column as last sort; toggle the direction
158 direction
= 1 - direction
;
160 // New column; do a descending sort
161 this.propertyIndex
= column
;
162 this.propertyType
= propertyTypesList
.get(column
);
163 this.property
= propertiesList
.get(column
);
164 direction
= ASCENDING
;