]> 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/ArtifactNamesComparator.java
enhance UI after first feed backs.
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / utils / ArtifactNamesComparator.java
1 package org.argeo.slc.client.ui.dist.utils;
2
3 import org.argeo.eclipse.ui.TreeParent;
4 import org.eclipse.jface.viewers.Viewer;
5 import org.eclipse.jface.viewers.ViewerComparator;
6
7 /**
8 * Enable comparison of two names with form org.argeo.slc-1.2.x
9 */
10
11 public class ArtifactNamesComparator extends ViewerComparator {
12
13 @Override
14 public int category(Object element) {
15 if (element instanceof String) {
16 int lastInd = ((String) element).lastIndexOf('-');
17 if (lastInd > 0)
18 return 10;
19 }
20 // unvalid names always last
21 return 5;
22 }
23
24 @Override
25 public int compare(Viewer viewer, Object e1, Object e2) {
26 int cat1 = category(e1);
27 int cat2 = category(e2);
28
29 if (cat1 != cat2) {
30 return cat1 - cat2;
31 }
32
33 int result = 0;
34
35 String s1, s2;
36
37 if (e1 instanceof TreeParent) {
38 s1 = ((TreeParent) e1).getName();
39 s2 = ((TreeParent) e2).getName();
40 } else {
41 s1 = e1.toString();
42 s2 = e2.toString();
43 }
44
45
46 int i1 = s1.lastIndexOf('-');
47 int i2 = s2.lastIndexOf('-');
48
49
50 // Specific cases, unvalid Strings
51 if (i1 <0)
52 if (i2 <0)
53 return s1.compareTo(s2);
54 else
55 return 1;
56 else
57 if (i2 <0)
58 return -1;
59
60 String aPref = s1.substring(0, s1.lastIndexOf('-'));
61 String aSuf = s1.substring(s1.lastIndexOf('-'));
62
63 String bPref = s2.substring(0, s2.lastIndexOf('-'));
64 String bSuf = s2.substring(s2.lastIndexOf('-'));
65
66 result = aPref.compareTo(bPref);
67 if (result != 0)
68 return result;
69 else
70 return bSuf.compareTo(aSuf);
71
72 }
73 }