]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/actions/TestContribution.java
a9e02eb2ea76046738fbd4468da862bb730766ab
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / actions / TestContribution.java
1 /*******************************************************************************\r
2  * Copyright (c) 2015 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  *     Semantum Oy - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.model.actions;\r
13 \r
14 import org.simantics.browsing.ui.BuiltinKeys;\r
15 import org.simantics.browsing.ui.NodeContext;\r
16 import org.simantics.browsing.ui.model.nodetypes.NodeType;\r
17 import org.simantics.browsing.ui.model.nodetypes.NodeTypeMultiMap;\r
18 import org.simantics.browsing.ui.model.tests.Test;\r
19 import org.simantics.db.ReadGraph;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.db.common.utils.Logger;\r
22 import org.simantics.db.exception.DatabaseException;\r
23 import org.simantics.viewpoint.ontology.ViewpointResource;\r
24 \r
25 /**\r
26  * Encapsulation of a browse context {@link NodeType} and input tester. Consists\r
27  * of a node type, a priority and a possible {@link Test} that returns whether\r
28  * the action related to this test is enabled or not. If there is no test\r
29  * defined, the test is considered to return <code>true</code>.\r
30  * \r
31  * @author Tuukka Lehtonen\r
32  */\r
33 public class TestContribution implements Comparable<TestContribution> {\r
34 \r
35     NodeType nodeType;\r
36     Test test;\r
37     double priority;\r
38 \r
39     public TestContribution(NodeType nodeType, Test test, double priority) {\r
40         super();\r
41         this.nodeType = nodeType;\r
42         this.test = test;\r
43         this.priority = priority;\r
44     }\r
45 \r
46     public static void load(ReadGraph g, Resource r, NodeTypeMultiMap<TestContribution> contributions) throws DatabaseException {\r
47         ViewpointResource VR = ViewpointResource.getInstance(g);\r
48 \r
49         NodeType nodeType = g.adapt(g.getSingleObject(r, VR.TestContribution_HasNodeType), NodeType.class);\r
50 \r
51         Resource testResource = g.getPossibleObject(r, VR.TestContribution_HasTest);\r
52         Test test = testResource == null ? null : g.adapt(testResource, Test.class);\r
53 \r
54         Double mpriority = g.getPossibleRelatedValue(r, VR.DropActionContribution_HasPriority);\r
55         double priority = mpriority == null ? 0.0 : mpriority.doubleValue();\r
56 \r
57         contributions.put(nodeType, new TestContribution(nodeType, test, priority));\r
58     }\r
59 \r
60     public boolean test(ReadGraph graph, NodeContext context) {\r
61         try {\r
62             return test == null || test.test(graph, context.getConstant(BuiltinKeys.INPUT));\r
63         } catch (DatabaseException e) {\r
64             Logger.defaultLogError(e);\r
65             return false;\r
66         }\r
67     }\r
68 \r
69     @Override\r
70     public int compareTo(TestContribution o) {\r
71         return Double.compare(o.priority, priority);\r
72     }\r
73 \r
74 }\r