]> 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
Restructure SLC
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / utils / NodeViewerComparator.java
1 package org.argeo.slc.client.ui.dist.utils;
2
3 import java.math.BigDecimal;
4 import java.util.Calendar;
5 import java.util.List;
6
7 import javax.jcr.Node;
8 import javax.jcr.PropertyType;
9 import javax.jcr.RepositoryException;
10 import javax.jcr.Value;
11 import javax.jcr.ValueFormatException;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.argeo.ArgeoException;
16 import org.argeo.eclipse.ui.GenericTableComparator;
17 import org.eclipse.jface.viewers.Viewer;
18
19 public class NodeViewerComparator extends GenericTableComparator {
20 private final static Log log = LogFactory
21 .getLog(NodeViewerComparator.class);
22
23 protected List<String> propertiesList;
24 protected List<Integer> propertyTypesList;
25 protected Integer propertyType;
26 protected String property;
27
28 public NodeViewerComparator(int defaultColIndex, int defaultDirection,
29 List<String> propertiesList, List<Integer> propertyTypesList) {
30 super(defaultColIndex, defaultDirection);
31 this.propertiesList = propertiesList;
32 this.propertyTypesList = propertyTypesList;
33 this.propertyIndex = defaultColIndex;
34 this.propertyType = propertyTypesList.get(defaultColIndex);
35 this.property = propertiesList.get(defaultColIndex);
36 setColumn(defaultColIndex);
37 }
38
39 @Override
40 public int compare(Viewer viewer, Object e1, Object e2) {
41 int rc = 0;
42 long lc = 0;
43
44 try {
45 Node n1 = (Node) e1;
46 Node n2 = (Node) e2;
47
48 Value v1 = null;
49 Value v2 = null;
50 if (n1.hasProperty(property))
51 v1 = n1.getProperty(property).getValue();
52 if (n2.hasProperty(property))
53 v2 = n2.getProperty(property).getValue();
54
55 if (v2 == null && v1 == null)
56 return 0;
57 else if (v2 == null)
58 return -1;
59 else if (v1 == null)
60 return 1;
61
62 switch (propertyType) {
63 case PropertyType.STRING:
64 rc = v1.getString().compareTo(v2.getString());
65 break;
66 case PropertyType.BOOLEAN:
67 boolean b1 = v1.getBoolean();
68 boolean b2 = v2.getBoolean();
69 if (b1 == b2)
70 rc = 0;
71 else
72 // we assume true is greater than false
73 rc = b1 ? 1 : -1;
74 break;
75 case PropertyType.DATE:
76 Calendar c1 = v1.getDate();
77 Calendar c2 = v2.getDate();
78 if (c1 == null || c2 == null)
79 log.trace("undefined date");
80 lc = c1.getTimeInMillis() - c2.getTimeInMillis();
81 if (lc < Integer.MIN_VALUE)
82 // rc = Integer.MIN_VALUE;
83 rc = -1;
84 else if (lc > Integer.MAX_VALUE)
85 // rc = Integer.MAX_VALUE;
86 rc = 1;
87 else
88 rc = (int) lc;
89 break;
90 case PropertyType.LONG:
91 long l1;
92 long l2;
93 // FIXME sometimes an empty string is set instead of the id
94 try {
95 l1 = v1.getLong();
96 } catch (ValueFormatException ve) {
97 l1 = 0;
98 }
99 try {
100 l2 = v2.getLong();
101 } catch (ValueFormatException ve) {
102 l2 = 0;
103 }
104
105 lc = l1 - l2;
106 if (lc < Integer.MIN_VALUE)
107 // rc = Integer.MIN_VALUE;
108 rc = -1;
109 else if (lc > Integer.MAX_VALUE)
110 // rc = Integer.MAX_VALUE;
111 rc = 1;
112 else
113 rc = (int) lc;
114 break;
115 case PropertyType.DECIMAL:
116 BigDecimal bd1 = v1.getDecimal();
117 BigDecimal bd2 = v2.getDecimal();
118 rc = bd1.compareTo(bd2);
119 break;
120 default:
121 throw new ArgeoException(
122 "Unimplemented comparaison for PropertyType "
123 + propertyType);
124 }
125
126 // If descending order, flip the direction
127 if (direction == DESCENDING) {
128 rc = -rc;
129 }
130
131 } catch (RepositoryException re) {
132 throw new ArgeoException("Unexpected error "
133 + "while comparing nodes", re);
134 }
135 return rc;
136 }
137
138 @Override
139 public void setColumn(int column) {
140 if (column == this.propertyIndex) {
141 // Same column as last sort; toggle the direction
142 direction = 1 - direction;
143 } else {
144 // New column; do a descending sort
145 this.propertyIndex = column;
146 this.propertyType = propertyTypesList.get(column);
147 this.property = propertiesList.get(column);
148 direction = ASCENDING;
149 }
150 }
151 }