public class TestsUIResource {\r
\r
public final Resource BrowseContext;\r
+ public final Resource BrowseContext_STSSuiteSorterRule;\r
public final Resource Contributions;\r
public final Resource Contributions_NewSTSSuite;\r
public final Resource Contributions_NewSTSTest;\r
\r
public static class URIs {\r
public static final String BrowseContext = "http://www.simantics.org/TestsUI-1.0/BrowseContext";\r
+ public static final String BrowseContext_STSSuiteSorterRule = "http://www.simantics.org/TestsUI-1.0/BrowseContext/STSSuiteSorterRule";\r
public static final String Contributions = "http://www.simantics.org/TestsUI-1.0/Contributions";\r
public static final String Contributions_NewSTSSuite = "http://www.simantics.org/TestsUI-1.0/Contributions/NewSTSSuite";\r
public static final String Contributions_NewSTSTest = "http://www.simantics.org/TestsUI-1.0/Contributions/NewSTSTest";\r
\r
public TestsUIResource(ReadGraph graph) {\r
BrowseContext = getResourceOrNull(graph, URIs.BrowseContext);\r
+ BrowseContext_STSSuiteSorterRule = getResourceOrNull(graph, URIs.BrowseContext_STSSuiteSorterRule);\r
Contributions = getResourceOrNull(graph, URIs.Contributions);\r
Contributions_NewSTSSuite = getResourceOrNull(graph, URIs.Contributions_NewSTSSuite);\r
Contributions_NewSTSTest = getResourceOrNull(graph, URIs.Contributions_NewSTSTest);\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.tests.modelled.ui;\r
+\r
+import java.util.Collections;\r
+import java.util.Comparator;\r
+import java.util.List;\r
+\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;\r
+import org.simantics.browsing.ui.model.sorters.Sorter;\r
+import org.simantics.browsing.ui.model.sorters.SorterRule;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ResourceRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.RuntimeDatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.tests.modelled.ontology.TestsResource;\r
+import org.simantics.utils.datastructures.Pair;\r
+import org.simantics.utils.strings.AlphanumComparator;\r
+\r
+public class STSSuiteSorterRule implements SorterRule, Sorter {\r
+\r
+ @Override\r
+ public boolean isCompatible(Class<?> contentType) {\r
+ return contentType.equals(Resource.class);\r
+ }\r
+\r
+ @Override\r
+ public Sorter getSorter(ReadGraph graph, Object content) throws DatabaseException {\r
+ return this;\r
+ }\r
+ \r
+ @Override\r
+ public void sort(ReadGraph graph, BrowseContext context, List<NodeContext> nodes) throws DatabaseException \r
+ {\r
+ try {\r
+ STSTestComparator stc = new STSTestComparator();\r
+ stc.graph = graph;\r
+ Collections.sort(nodes, stc);\r
+ } catch (RuntimeDatabaseException e) {\r
+ if (e.getCause()!=null && e.getCause() instanceof DatabaseException) throw (DatabaseException) e.getCause();\r
+ throw e;\r
+ }\r
+ }\r
+ \r
+ static class STSTestQuery extends ResourceRead<Pair<Integer, String>> {\r
+\r
+ public STSTestQuery(Resource resource) {\r
+ super(resource);\r
+ }\r
+\r
+ @Override\r
+ public Pair<Integer, String> perform(ReadGraph graph) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ TestsResource TESTS = TestsResource.getInstance(graph);\r
+ \r
+ String name = graph.getRelatedValue2(resource, L0.HasName, Bindings.STRING);\r
+ Integer priority = graph.getRelatedValue2(resource, TESTS.STSTest_executionPriority, Bindings.INTEGER);\r
+\r
+ return new Pair<Integer, String>(priority, name);\r
+ }\r
+\r
+ }\r
+ \r
+ static class STSTestComparator implements Comparator<NodeContext> {\r
+ ReadGraph graph;\r
+\r
+ @Override\r
+ public int compare(NodeContext nc1, NodeContext nc2) {\r
+ Resource r1 = (Resource) nc1.getConstant(BuiltinKeys.INPUT);\r
+ Resource r2 = (Resource) nc2.getConstant(BuiltinKeys.INPUT);\r
+ \r
+ try {\r
+ Pair<Integer, String> test1 = graph.sync(new STSTestQuery(r1));\r
+ Pair<Integer, String> test2 = graph.sync(new STSTestQuery(r2));\r
+\r
+ if (test1.first < test2.first)\r
+ return -1;\r
+ else if (test1.first > test2.first)\r
+ return 1;\r
+ else return AlphanumComparator.COMPARATOR.compare(test1.second, test2.second);\r
+ \r
+ } catch (DatabaseException e) {\r
+ throw new RuntimeDatabaseException(e);\r
+ }\r
+ }\r
+ }\r
+}\r