]>
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
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 public class NodeViewerComparator
extends GenericTableComparator
{
35 private final static Log log
= LogFactory
36 .getLog(NodeViewerComparator
.class);
38 protected List
<String
> propertiesList
;
39 protected List
<Integer
> propertyTypesList
;
40 protected Integer propertyType
;
41 protected String property
;
43 public NodeViewerComparator(int defaultColIndex
, int defaultDirection
,
44 List
<String
> propertiesList
, List
<Integer
> propertyTypesList
) {
45 super(defaultColIndex
, defaultDirection
);
46 this.propertiesList
= propertiesList
;
47 this.propertyTypesList
= propertyTypesList
;
48 this.propertyIndex
= defaultColIndex
;
49 this.propertyType
= propertyTypesList
.get(defaultColIndex
);
50 this.property
= propertiesList
.get(defaultColIndex
);
51 setColumn(defaultColIndex
);
55 public int compare(Viewer viewer
, Object e1
, Object e2
) {
65 if (n1
.hasProperty(property
))
66 v1
= n1
.getProperty(property
).getValue();
67 if (n2
.hasProperty(property
))
68 v2
= n2
.getProperty(property
).getValue();
70 if (v2
== null && v1
== null)
77 switch (propertyType
) {
78 case PropertyType
.STRING
:
79 rc
= v1
.getString().compareTo(v2
.getString());
81 case PropertyType
.BOOLEAN
:
82 boolean b1
= v1
.getBoolean();
83 boolean b2
= v2
.getBoolean();
87 // we assume true is greater than false
90 case PropertyType
.DATE
:
91 Calendar c1
= v1
.getDate();
92 Calendar c2
= v2
.getDate();
93 if (c1
== null || c2
== null)
94 log
.trace("undefined date");
95 lc
= c1
.getTimeInMillis() - c2
.getTimeInMillis();
96 if (lc
< Integer
.MIN_VALUE
)
97 // rc = Integer.MIN_VALUE;
99 else if (lc
> Integer
.MAX_VALUE
)
100 // rc = Integer.MAX_VALUE;
105 case PropertyType
.LONG
:
108 // FIXME sometimes an empty string is set instead of the id
111 } catch (ValueFormatException ve
) {
116 } catch (ValueFormatException ve
) {
121 if (lc
< Integer
.MIN_VALUE
)
122 // rc = Integer.MIN_VALUE;
124 else if (lc
> Integer
.MAX_VALUE
)
125 // rc = Integer.MAX_VALUE;
130 case PropertyType
.DECIMAL
:
131 BigDecimal bd1
= v1
.getDecimal();
132 BigDecimal bd2
= v2
.getDecimal();
133 rc
= bd1
.compareTo(bd2
);
136 throw new ArgeoException(
137 "Unimplemented comparaison for PropertyType "
141 // If descending order, flip the direction
142 if (direction
== DESCENDING
) {
146 } catch (RepositoryException re
) {
147 throw new ArgeoException("Unexpected error "
148 + "while comparing nodes", re
);
154 public void setColumn(int column
) {
155 if (column
== this.propertyIndex
) {
156 // Same column as last sort; toggle the direction
157 direction
= 1 - direction
;
159 // New column; do a descending sort
160 this.propertyIndex
= column
;
161 this.propertyType
= propertyTypesList
.get(column
);
162 this.property
= propertiesList
.get(column
);
163 direction
= ASCENDING
;