]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSSuiteSorterRule.java
Merge "Ensure GetElementClassRequest is not constructed without elementFactory"
[simantics/platform.git] / bundles / org.simantics.tests.modelled.ui / src / org / simantics / tests / modelled / ui / STSSuiteSorterRule.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.tests.modelled.ui;\r
13 \r
14 import java.util.Collections;\r
15 import java.util.Comparator;\r
16 import java.util.List;\r
17 \r
18 import org.simantics.browsing.ui.BuiltinKeys;\r
19 import org.simantics.browsing.ui.NodeContext;\r
20 import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;\r
21 import org.simantics.browsing.ui.model.sorters.Sorter;\r
22 import org.simantics.browsing.ui.model.sorters.SorterRule;\r
23 import org.simantics.databoard.Bindings;\r
24 import org.simantics.db.ReadGraph;\r
25 import org.simantics.db.Resource;\r
26 import org.simantics.db.common.request.ResourceRead;\r
27 import org.simantics.db.exception.DatabaseException;\r
28 import org.simantics.db.exception.RuntimeDatabaseException;\r
29 import org.simantics.layer0.Layer0;\r
30 import org.simantics.tests.modelled.ontology.TestsResource;\r
31 import org.simantics.utils.datastructures.Pair;\r
32 import org.simantics.utils.strings.AlphanumComparator;\r
33 \r
34 public class STSSuiteSorterRule implements SorterRule, Sorter {\r
35 \r
36         @Override\r
37         public boolean isCompatible(Class<?> contentType) {\r
38                 return contentType.equals(Resource.class);\r
39         }\r
40 \r
41         @Override\r
42         public Sorter getSorter(ReadGraph graph, Object content) throws DatabaseException {\r
43                 return this;\r
44         }\r
45         \r
46         @Override\r
47         public void sort(ReadGraph graph, BrowseContext context, List<NodeContext> nodes) throws DatabaseException \r
48         {\r
49                 try {\r
50                         STSTestComparator stc = new STSTestComparator();\r
51                         stc.graph = graph;\r
52                         Collections.sort(nodes, stc);\r
53                 } catch (RuntimeDatabaseException e) {\r
54                         if (e.getCause()!=null && e.getCause() instanceof DatabaseException) throw (DatabaseException) e.getCause();\r
55                         throw e;\r
56                 }\r
57         }\r
58         \r
59         static class STSTestQuery extends ResourceRead<Pair<Integer, String>> {\r
60 \r
61                 public STSTestQuery(Resource resource) {\r
62                         super(resource);\r
63                 }\r
64 \r
65                 @Override\r
66                 public Pair<Integer, String> perform(ReadGraph graph) throws DatabaseException {\r
67                         Layer0 L0 = Layer0.getInstance(graph);\r
68                         TestsResource TESTS = TestsResource.getInstance(graph);\r
69                         \r
70                         String name = graph.getRelatedValue2(resource, L0.HasName, Bindings.STRING);\r
71                 Integer priority = graph.getRelatedValue2(resource, TESTS.STSTest_executionPriority, Bindings.INTEGER);\r
72 \r
73                 return new Pair<Integer, String>(priority, name);\r
74             }\r
75 \r
76         }\r
77         \r
78         static class STSTestComparator implements Comparator<NodeContext> {\r
79                 ReadGraph graph;\r
80 \r
81                 @Override\r
82                 public int compare(NodeContext nc1, NodeContext nc2) {\r
83                         Resource r1 = (Resource) nc1.getConstant(BuiltinKeys.INPUT);\r
84                         Resource r2 = (Resource) nc2.getConstant(BuiltinKeys.INPUT);\r
85                         \r
86                         try {\r
87                                 Pair<Integer, String> test1 = graph.sync(new STSTestQuery(r1));\r
88                                 Pair<Integer, String> test2 = graph.sync(new STSTestQuery(r2));\r
89 \r
90                 if (test1.first < test2.first)\r
91                     return -1;\r
92                 else if (test1.first > test2.first)\r
93                     return 1;\r
94                 else return AlphanumComparator.COMPARATOR.compare(test1.second, test2.second);\r
95                 \r
96                         } catch (DatabaseException e) {\r
97                                 throw new RuntimeDatabaseException(e);\r
98                         }\r
99                 }\r
100         }\r
101 }\r