]> 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
fix grouping issue for workspaces that have the same prefix.
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / utils / NodeViewerComparator.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.slc.client.ui.dist.utils;
17
18 import java.math.BigDecimal;
19 import java.util.Calendar;
20 import java.util.List;
21
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;
27
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;
33
34 public class NodeViewerComparator extends GenericTableComparator {
35 private final static Log log = LogFactory
36 .getLog(NodeViewerComparator.class);
37
38 protected List<String> propertiesList;
39 protected List<Integer> propertyTypesList;
40 protected Integer propertyType;
41 protected String property;
42
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);
52 }
53
54 @Override
55 public int compare(Viewer viewer, Object e1, Object e2) {
56 int rc = 0;
57 long lc = 0;
58
59 try {
60 Node n1 = (Node) e1;
61 Node n2 = (Node) e2;
62
63 Value v1 = null;
64 Value v2 = null;
65 if (n1.hasProperty(property))
66 v1 = n1.getProperty(property).getValue();
67 if (n2.hasProperty(property))
68 v2 = n2.getProperty(property).getValue();
69
70 if (v2 == null && v1 == null)
71 return 0;
72 else if (v2 == null)
73 return -1;
74 else if (v1 == null)
75 return 1;
76
77 switch (propertyType) {
78 case PropertyType.STRING:
79 rc = v1.getString().compareTo(v2.getString());
80 break;
81 case PropertyType.BOOLEAN:
82 boolean b1 = v1.getBoolean();
83 boolean b2 = v2.getBoolean();
84 if (b1 == b2)
85 rc = 0;
86 else
87 // we assume true is greater than false
88 rc = b1 ? 1 : -1;
89 break;
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;
98 rc = -1;
99 else if (lc > Integer.MAX_VALUE)
100 // rc = Integer.MAX_VALUE;
101 rc = 1;
102 else
103 rc = (int) lc;
104 break;
105 case PropertyType.LONG:
106 long l1;
107 long l2;
108 // FIXME sometimes an empty string is set instead of the id
109 try {
110 l1 = v1.getLong();
111 } catch (ValueFormatException ve) {
112 l1 = 0;
113 }
114 try {
115 l2 = v2.getLong();
116 } catch (ValueFormatException ve) {
117 l2 = 0;
118 }
119
120 lc = l1 - l2;
121 if (lc < Integer.MIN_VALUE)
122 // rc = Integer.MIN_VALUE;
123 rc = -1;
124 else if (lc > Integer.MAX_VALUE)
125 // rc = Integer.MAX_VALUE;
126 rc = 1;
127 else
128 rc = (int) lc;
129 break;
130 case PropertyType.DECIMAL:
131 BigDecimal bd1 = v1.getDecimal();
132 BigDecimal bd2 = v2.getDecimal();
133 rc = bd1.compareTo(bd2);
134 break;
135 default:
136 throw new ArgeoException(
137 "Unimplemented comparaison for PropertyType "
138 + propertyType);
139 }
140
141 // If descending order, flip the direction
142 if (direction == DESCENDING) {
143 rc = -rc;
144 }
145
146 } catch (RepositoryException re) {
147 throw new ArgeoException("Unexpected error "
148 + "while comparing nodes", re);
149 }
150 return rc;
151 }
152
153 @Override
154 public void setColumn(int column) {
155 if (column == this.propertyIndex) {
156 // Same column as last sort; toggle the direction
157 direction = 1 - direction;
158 } else {
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;
164 }
165 }
166 }