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