1 /*******************************************************************************
2 * Copyright (c) 2015 Association for Decentralized Information Management in
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
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.model.actions;
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;
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>.
31 * @author Tuukka Lehtonen
33 public class TestContribution implements Comparable<TestContribution> {
39 public TestContribution(NodeType nodeType, Test test, double priority) {
41 this.nodeType = nodeType;
43 this.priority = priority;
46 public static void load(ReadGraph g, Resource r, NodeTypeMultiMap<TestContribution> contributions) throws DatabaseException {
47 ViewpointResource VR = ViewpointResource.getInstance(g);
49 NodeType nodeType = g.adapt(g.getSingleObject(r, VR.TestContribution_HasNodeType), NodeType.class);
51 Resource testResource = g.getPossibleObject(r, VR.TestContribution_HasTest);
52 Test test = testResource == null ? null : g.adapt(testResource, Test.class);
54 Double mpriority = g.getPossibleRelatedValue(r, VR.DropActionContribution_HasPriority);
55 double priority = mpriority == null ? 0.0 : mpriority.doubleValue();
57 contributions.put(nodeType, new TestContribution(nodeType, test, priority));
60 public boolean test(ReadGraph graph, NodeContext context) {
62 return test == null || test.test(graph, context.getConstant(BuiltinKeys.INPUT));
63 } catch (DatabaseException e) {
64 Logger.defaultLogError(e);
70 public int compareTo(TestContribution o) {
71 return Double.compare(o.priority, priority);