]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/actions/TestContribution.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / actions / TestContribution.java
diff --git a/bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/actions/TestContribution.java b/bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/actions/TestContribution.java
new file mode 100644 (file)
index 0000000..a9e02eb
--- /dev/null
@@ -0,0 +1,74 @@
+/*******************************************************************************\r
+ * Copyright (c) 2015 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
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.model.actions;\r
+\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.model.nodetypes.NodeType;\r
+import org.simantics.browsing.ui.model.nodetypes.NodeTypeMultiMap;\r
+import org.simantics.browsing.ui.model.tests.Test;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.viewpoint.ontology.ViewpointResource;\r
+\r
+/**\r
+ * Encapsulation of a browse context {@link NodeType} and input tester. Consists\r
+ * of a node type, a priority and a possible {@link Test} that returns whether\r
+ * the action related to this test is enabled or not. If there is no test\r
+ * defined, the test is considered to return <code>true</code>.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class TestContribution implements Comparable<TestContribution> {\r
+\r
+    NodeType nodeType;\r
+    Test test;\r
+    double priority;\r
+\r
+    public TestContribution(NodeType nodeType, Test test, double priority) {\r
+        super();\r
+        this.nodeType = nodeType;\r
+        this.test = test;\r
+        this.priority = priority;\r
+    }\r
+\r
+    public static void load(ReadGraph g, Resource r, NodeTypeMultiMap<TestContribution> contributions) throws DatabaseException {\r
+        ViewpointResource VR = ViewpointResource.getInstance(g);\r
+\r
+        NodeType nodeType = g.adapt(g.getSingleObject(r, VR.TestContribution_HasNodeType), NodeType.class);\r
+\r
+        Resource testResource = g.getPossibleObject(r, VR.TestContribution_HasTest);\r
+        Test test = testResource == null ? null : g.adapt(testResource, Test.class);\r
+\r
+        Double mpriority = g.getPossibleRelatedValue(r, VR.DropActionContribution_HasPriority);\r
+        double priority = mpriority == null ? 0.0 : mpriority.doubleValue();\r
+\r
+        contributions.put(nodeType, new TestContribution(nodeType, test, priority));\r
+    }\r
+\r
+    public boolean test(ReadGraph graph, NodeContext context) {\r
+        try {\r
+            return test == null || test.test(graph, context.getConstant(BuiltinKeys.INPUT));\r
+        } catch (DatabaseException e) {\r
+            Logger.defaultLogError(e);\r
+            return false;\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public int compareTo(TestContribution o) {\r
+        return Double.compare(o.priority, priority);\r
+    }\r
+\r
+}\r