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