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