X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.model%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fmodel%2Factions%2FTestContribution.java;fp=bundles%2Forg.simantics.browsing.ui.model%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fmodel%2Factions%2FTestContribution.java;h=a9e02eb2ea76046738fbd4468da862bb730766ab;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git 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 index 000000000..a9e02eb2e --- /dev/null +++ b/bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/actions/TestContribution.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2015 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.browsing.ui.model.actions; + +import org.simantics.browsing.ui.BuiltinKeys; +import org.simantics.browsing.ui.NodeContext; +import org.simantics.browsing.ui.model.nodetypes.NodeType; +import org.simantics.browsing.ui.model.nodetypes.NodeTypeMultiMap; +import org.simantics.browsing.ui.model.tests.Test; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.utils.Logger; +import org.simantics.db.exception.DatabaseException; +import org.simantics.viewpoint.ontology.ViewpointResource; + +/** + * Encapsulation of a browse context {@link NodeType} and input tester. Consists + * of a node type, a priority and a possible {@link Test} that returns whether + * the action related to this test is enabled or not. If there is no test + * defined, the test is considered to return true. + * + * @author Tuukka Lehtonen + */ +public class TestContribution implements Comparable { + + NodeType nodeType; + Test test; + double priority; + + public TestContribution(NodeType nodeType, Test test, double priority) { + super(); + this.nodeType = nodeType; + this.test = test; + this.priority = priority; + } + + public static void load(ReadGraph g, Resource r, NodeTypeMultiMap contributions) throws DatabaseException { + ViewpointResource VR = ViewpointResource.getInstance(g); + + NodeType nodeType = g.adapt(g.getSingleObject(r, VR.TestContribution_HasNodeType), NodeType.class); + + Resource testResource = g.getPossibleObject(r, VR.TestContribution_HasTest); + Test test = testResource == null ? null : g.adapt(testResource, Test.class); + + Double mpriority = g.getPossibleRelatedValue(r, VR.DropActionContribution_HasPriority); + double priority = mpriority == null ? 0.0 : mpriority.doubleValue(); + + contributions.put(nodeType, new TestContribution(nodeType, test, priority)); + } + + public boolean test(ReadGraph graph, NodeContext context) { + try { + return test == null || test.test(graph, context.getConstant(BuiltinKeys.INPUT)); + } catch (DatabaseException e) { + Logger.defaultLogError(e); + return false; + } + } + + @Override + public int compareTo(TestContribution o) { + return Double.compare(o.priority, priority); + } + +}