2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.client
.ui
.dist
.utils
;
18 import java
.math
.BigDecimal
;
19 import java
.util
.Calendar
;
20 import java
.util
.List
;
22 import javax
.jcr
.Node
;
23 import javax
.jcr
.PropertyType
;
24 import javax
.jcr
.RepositoryException
;
25 import javax
.jcr
.Value
;
26 import javax
.jcr
.ValueFormatException
;
28 import org
.apache
.commons
.logging
.Log
;
29 import org
.apache
.commons
.logging
.LogFactory
;
30 import org
.argeo
.ArgeoException
;
31 import org
.argeo
.eclipse
.ui
.GenericTableComparator
;
32 import org
.eclipse
.jface
.viewers
.Viewer
;
34 /** Add ability to order by name version and version */
35 public class DistNodeViewerComparator
extends GenericTableComparator
{
36 private final static Log log
= LogFactory
37 .getLog(DistNodeViewerComparator
.class);
39 // Jcr property type goes to 12
40 public final static int NAME_VERSION_TYPE
= 100;
41 public final static int VERSION_TYPE
= 101;
43 protected List
<String
> propertiesList
;
44 protected List
<Integer
> propertyTypesList
;
45 protected Integer propertyType
;
46 protected String property
;
48 private NameVersionComparator nvc
= new NameVersionComparator();
49 private VersionComparator vc
= new VersionComparator();
51 public DistNodeViewerComparator(int defaultColIndex
, int defaultDirection
,
52 List
<String
> propertiesList
, List
<Integer
> propertyTypesList
) {
53 super(defaultColIndex
, defaultDirection
);
54 this.propertiesList
= propertiesList
;
55 this.propertyTypesList
= propertyTypesList
;
56 this.propertyIndex
= defaultColIndex
;
57 this.propertyType
= propertyTypesList
.get(defaultColIndex
);
58 this.property
= propertiesList
.get(defaultColIndex
);
59 setColumn(defaultColIndex
);
63 public int compare(Viewer viewer
, Object e1
, Object e2
) {
73 if (n1
.hasProperty(property
))
74 v1
= n1
.getProperty(property
).getValue();
75 if (n2
.hasProperty(property
))
76 v2
= n2
.getProperty(property
).getValue();
78 if (v2
== null && v1
== null)
85 switch (propertyType
) {
86 case NAME_VERSION_TYPE
:
87 rc
= nvc
.compare(viewer
, v1
.getString(), v2
.getString());
90 rc
= vc
.compare(viewer
, v1
.getString(), v2
.getString());
92 case PropertyType
.STRING
:
93 rc
= v1
.getString().compareTo(v2
.getString());
95 case PropertyType
.BOOLEAN
:
96 boolean b1
= v1
.getBoolean();
97 boolean b2
= v2
.getBoolean();
101 // we assume true is greater than false
104 case PropertyType
.DATE
:
105 Calendar c1
= v1
.getDate();
106 Calendar c2
= v2
.getDate();
107 if (c1
== null || c2
== null)
108 log
.trace("undefined date");
109 lc
= c1
.getTimeInMillis() - c2
.getTimeInMillis();
110 if (lc
< Integer
.MIN_VALUE
)
111 // rc = Integer.MIN_VALUE;
113 else if (lc
> Integer
.MAX_VALUE
)
114 // rc = Integer.MAX_VALUE;
119 case PropertyType
.LONG
:
122 // FIXME sometimes an empty string is set instead of the id
125 } catch (ValueFormatException ve
) {
130 } catch (ValueFormatException ve
) {
135 if (lc
< Integer
.MIN_VALUE
)
136 // rc = Integer.MIN_VALUE;
138 else if (lc
> Integer
.MAX_VALUE
)
139 // rc = Integer.MAX_VALUE;
144 case PropertyType
.DECIMAL
:
145 BigDecimal bd1
= v1
.getDecimal();
146 BigDecimal bd2
= v2
.getDecimal();
147 rc
= bd1
.compareTo(bd2
);
150 throw new ArgeoException(
151 "Unimplemented comparaison for PropertyType "
155 // If descending order, flip the direction
156 if (direction
== DESCENDING
) {
160 } catch (RepositoryException re
) {
161 throw new ArgeoException("Unexpected error "
162 + "while comparing nodes", re
);
168 public void setColumn(int column
) {
169 if (column
== this.propertyIndex
) {
170 // Same column as last sort; toggle the direction
171 direction
= 1 - direction
;
173 // New column; do a descending sort
174 this.propertyIndex
= column
;
175 this.propertyType
= propertyTypesList
.get(column
);
176 this.property
= propertiesList
.get(column
);
177 direction
= ASCENDING
;