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
.eclipse
.ui
.GenericTableComparator
;
31 import org
.argeo
.slc
.SlcException
;
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 static final long serialVersionUID
= -5966120108210992211L;
38 private final static Log log
= LogFactory
39 .getLog(DistNodeViewerComparator
.class);
41 // Jcr property type goes to 12
42 public final static int NAME_VERSION_TYPE
= 100;
43 public final static int VERSION_TYPE
= 101;
45 protected List
<String
> propertiesList
;
46 protected List
<Integer
> propertyTypesList
;
47 protected Integer propertyType
;
48 protected String property
;
50 private NameVersionComparator nvc
= new NameVersionComparator();
51 private VersionComparator vc
= new VersionComparator();
53 public DistNodeViewerComparator(int defaultColIndex
, int defaultDirection
,
54 List
<String
> propertiesList
, List
<Integer
> propertyTypesList
) {
55 super(defaultColIndex
, defaultDirection
);
56 this.propertiesList
= propertiesList
;
57 this.propertyTypesList
= propertyTypesList
;
58 this.propertyIndex
= defaultColIndex
;
59 this.propertyType
= propertyTypesList
.get(defaultColIndex
);
60 this.property
= propertiesList
.get(defaultColIndex
);
61 setColumn(defaultColIndex
);
65 public int compare(Viewer viewer
, Object e1
, Object e2
) {
75 if (n1
.hasProperty(property
))
76 v1
= n1
.getProperty(property
).getValue();
77 if (n2
.hasProperty(property
))
78 v2
= n2
.getProperty(property
).getValue();
80 if (v2
== null && v1
== null)
87 switch (propertyType
) {
88 case NAME_VERSION_TYPE
:
89 rc
= nvc
.compare(viewer
, v1
.getString(), v2
.getString());
92 rc
= vc
.compare(viewer
, v1
.getString(), v2
.getString());
94 case PropertyType
.STRING
:
95 rc
= v1
.getString().compareTo(v2
.getString());
97 case PropertyType
.BOOLEAN
:
98 boolean b1
= v1
.getBoolean();
99 boolean b2
= v2
.getBoolean();
103 // we assume true is greater than false
106 case PropertyType
.DATE
:
107 Calendar c1
= v1
.getDate();
108 Calendar c2
= v2
.getDate();
109 if (c1
== null || c2
== null)
110 log
.trace("undefined date");
111 lc
= c1
.getTimeInMillis() - c2
.getTimeInMillis();
112 if (lc
< Integer
.MIN_VALUE
)
113 // rc = Integer.MIN_VALUE;
115 else if (lc
> Integer
.MAX_VALUE
)
116 // rc = Integer.MAX_VALUE;
121 case PropertyType
.LONG
:
124 // FIXME sometimes an empty string is set instead of the id
127 } catch (ValueFormatException ve
) {
132 } catch (ValueFormatException ve
) {
137 if (lc
< Integer
.MIN_VALUE
)
138 // rc = Integer.MIN_VALUE;
140 else if (lc
> Integer
.MAX_VALUE
)
141 // rc = Integer.MAX_VALUE;
146 case PropertyType
.DECIMAL
:
147 BigDecimal bd1
= v1
.getDecimal();
148 BigDecimal bd2
= v2
.getDecimal();
149 rc
= bd1
.compareTo(bd2
);
152 throw new SlcException(
153 "Unimplemented comparaison for PropertyType "
157 // If descending order, flip the direction
158 if (direction
== DESCENDING
) {
162 } catch (RepositoryException re
) {
163 throw new SlcException("Unexpected error "
164 + "while comparing nodes", re
);
170 public void setColumn(int column
) {
171 if (column
== this.propertyIndex
) {
172 // Same column as last sort; toggle the direction
173 direction
= 1 - direction
;
175 // New column; do a descending sort
176 this.propertyIndex
= column
;
177 this.propertyType
= propertyTypesList
.get(column
);
178 this.property
= propertiesList
.get(column
);
179 direction
= ASCENDING
;