View Javadoc
1   package org.argeo.slc.client.ui.model;
2   
3   import javax.jcr.Node;
4   import javax.jcr.Property;
5   import javax.jcr.RepositoryException;
6   
7   import org.argeo.eclipse.ui.TreeParent;
8   import org.argeo.slc.SlcException;
9   import org.argeo.slc.SlcNames;
10  import org.argeo.slc.SlcTypes;
11  import org.eclipse.jface.viewers.Viewer;
12  import org.eclipse.jface.viewers.ViewerComparator;
13  
14  /** Enable specific sorting of the ResultTreeView */
15  public class ResultItemsComparator extends ViewerComparator {
16  
17  	@Override
18  	public int category(Object element) {
19  		if (element instanceof SingleResultNode) {
20  			return 10;
21  
22  		}
23  		// folder always first
24  		return 5;
25  	}
26  
27  	@Override
28  	public int compare(Viewer viewer, Object e1, Object e2) {
29  		int cat1 = category(e1);
30  		int cat2 = category(e2);
31  
32  		if (cat1 != cat2) {
33  			return cat1 - cat2;
34  		}
35  
36  		int result = 0;
37  
38  		if (e1 instanceof TreeParent && ((TreeParent) e1).getParent() == null) {
39  			// preserve predefined order on UI root items
40  			return 0;
41  		}
42  
43  		if (e1 instanceof SingleResultNode && e2 instanceof SingleResultNode) {
44  			Node an = ((SingleResultNode) e1).getNode();
45  			Node bn = ((SingleResultNode) e2).getNode();
46  			try {
47  				// Order is different if we are under my Result or )in the
48  				// rest of the tree structure
49  				if (an.getParent().isNodeType(
50  						SlcTypes.SLC_MY_RESULT_ROOT_FOLDER)
51  						|| an.getParent()
52  								.isNodeType(SlcTypes.SLC_RESULT_FOLDER)) {
53  					result = super.compare(viewer, e1, e2);
54  					// Specific case of two result with same name
55  					if (result == 0) {
56  						result = an
57  								.getProperty(SlcNames.SLC_COMPLETED)
58  								.getDate()
59  								.compareTo(
60  										bn.getProperty(SlcNames.SLC_COMPLETED)
61  												.getDate());
62  					}
63  				} else {
64  					result = an
65  							.getProperty(Property.JCR_CREATED)
66  							.getDate()
67  							.compareTo(
68  									bn.getProperty(Property.JCR_CREATED)
69  											.getDate());
70  					result = result * -1; // last are displayed first
71  				}
72  			} catch (RepositoryException e) {
73  				throw new SlcException("Unable to compare date created", e);
74  			}
75  		} else if (e1 instanceof ParentNodeFolder
76  				&& e2 instanceof ParentNodeFolder) {
77  			try {
78  				Node an = ((ParentNodeFolder) e1).getNode();
79  				// under my Result
80  				if (an.isNodeType(SlcTypes.SLC_MY_RESULT_ROOT_FOLDER)
81  						|| an.isNodeType(SlcTypes.SLC_RESULT_FOLDER)) {
82  					result = super.compare(viewer, e1, e2);
83  				} else {
84  					// only remaining objects for the time being
85  					// NT_UNSTRUCTURED that display all result tree structures
86  					// We want the newest folders first
87  					result = super.compare(viewer, e1, e2) * -1;
88  				}
89  			} catch (RepositoryException e) {
90  				throw new SlcException("Unable to compare date created", e);
91  			}
92  		}
93  		return result;
94  	}
95  }