From: jsimomaa Date: Thu, 20 Jul 2017 05:10:21 +0000 (+0300) Subject: Copy URI context menu action to Model Browser for development mode X-Git-Tag: v1.31.0~264^2~15 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=6e09484741c188de8a64c668db8d5cb823a12216;p=simantics%2Fplatform.git Copy URI context menu action to Model Browser for development mode The contribution is only visible if -dev argument has been specified, i.e. org.eclipse.core.runtime.Platform.inDevelopmentMode() returns true. It is also visible only when the input object is either a Resource or a Variable. refs #7372 Change-Id: Ie0a798f76a314da30cfd4023bb92f68ececd9f2a --- diff --git a/bundles/org.simantics.browsing.ui.model/adapters.xml b/bundles/org.simantics.browsing.ui.model/adapters.xml index 50b343479..4276f3feb 100644 --- a/bundles/org.simantics.browsing.ui.model/adapters.xml +++ b/bundles/org.simantics.browsing.ui.model/adapters.xml @@ -181,6 +181,14 @@ class="org.simantics.browsing.ui.model.tests.FailTest" constructor="get"> + + + + diff --git a/bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/tests/HasURITest.java b/bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/tests/HasURITest.java new file mode 100644 index 000000000..344362588 --- /dev/null +++ b/bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/tests/HasURITest.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * Copyright (c) 2017 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.tests; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.exception.InvalidVariableException; +import org.simantics.db.layer0.variable.Variable; + +/** + * @author Tuukka Lehtonen + * @since 1.30.0 + */ +public enum HasURITest implements Test { + INSTANCE; + + public static HasURITest get() { + return INSTANCE; + } + + @Override + public boolean isCompatible(Class contentType) { + return contentType.equals(Resource.class) || contentType.equals(Variable.class); + } + + @Override + public boolean test(ReadGraph graph, Object content) throws DatabaseException { + if (content instanceof Resource) { + return graph.getPossibleURI((Resource) content) != null; + } else if (content instanceof Variable) { + try { + Variable v = (Variable) content; + return v.getURI(graph) != null; + } catch (InvalidVariableException e) { + return false; + } + } + return false; + } + +} diff --git a/bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/tests/InDevelopmentModeTest.java b/bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/tests/InDevelopmentModeTest.java new file mode 100644 index 000000000..fa85e256b --- /dev/null +++ b/bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/tests/InDevelopmentModeTest.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright (c) 2017 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.tests; + +import org.eclipse.core.runtime.Platform; +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; + +/** + * @author Tuukka Lehtonen + * @since 1.30.0 + */ +public enum InDevelopmentModeTest implements Test { + INSTANCE; + + public static InDevelopmentModeTest get() { + return INSTANCE; + } + + @Override + public boolean isCompatible(Class contentType) { + return true; + } + + @Override + public boolean test(ReadGraph graph, Object content) throws DatabaseException { + return Platform.inDevelopmentMode(); + } + +} diff --git a/bundles/org.simantics.modeling.ontology/graph/ModelingViewpoint.pgraph b/bundles/org.simantics.modeling.ontology/graph/ModelingViewpoint.pgraph index 0255339ac..86171a31a 100644 --- a/bundles/org.simantics.modeling.ontology/graph/ModelingViewpoint.pgraph +++ b/bundles/org.simantics.modeling.ontology/graph/ModelingViewpoint.pgraph @@ -420,10 +420,23 @@ MOD.Contributions.Help : VP.ActionContribution VP.ActionContribution.HasCategory VP.EditActionCategory VP.ActionContribution.HasNodeType L0.Entity VP.ActionContribution.HasAction ACTIONS.Help - + +MOD.Contributions.CopyURI : VP.ActionContribution + L0.HasLabel "Copy URI" + VP.ActionContribution.HasImage SILK.clipboard + VP.ActionContribution.HasCategory VP.EditActionCategory + VP.ActionContribution.HasNodeType L0.Entity + VP.ActionContribution.HasNodeType MBC.Variable + VP.ActionContribution.HasAction ACTIONS.CopyURI + VP.ActionContribution.IsVisibleIf _ : VP.AndTest + VP.AndTest.HasTest + _ : VP.InDevelopmentModeTest + _ : VP.HasURITest + // Actions MAC VP.BrowseContext.HasActionContribution MOD.Contributions.Help + VP.BrowseContext.HasActionContribution MOD.Contributions.CopyURI VP.BrowseContext.HasActionContribution _ : VP.ActionContribution L0.HasLabel "Migrate" VP.ActionContribution.HasImage SILK.star @@ -659,6 +672,7 @@ ACTIONS.CompilePGraphs : ACT.Action //ACTIONS.MigrateMasterTypical : ACT.Action ACTIONS.RenameDiagramComponents : ACT.Action ACTIONS.Help : ACT.Action +ACTIONS.CopyURI : ACT.Action ACTIONS.NavigateToSubstructure @MOD.sclAction "navigateToSubstructureAction" diff --git a/bundles/org.simantics.modeling/adapters.xml b/bundles/org.simantics.modeling/adapters.xml index 0e3870c35..b74449f82 100644 --- a/bundles/org.simantics.modeling/adapters.xml +++ b/bundles/org.simantics.modeling/adapters.xml @@ -161,6 +161,8 @@ + diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/actions/CopyURI.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/actions/CopyURI.java new file mode 100644 index 000000000..cdb56beea --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/actions/CopyURI.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2017 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.modeling.actions; + +import org.eclipse.swt.dnd.Clipboard; +import org.eclipse.swt.dnd.TextTransfer; +import org.eclipse.swt.dnd.Transfer; +import org.eclipse.swt.widgets.Display; +import org.simantics.Simantics; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.ActionFactory; +import org.simantics.db.layer0.request.PossibleURI; +import org.simantics.db.layer0.request.VariableURI; +import org.simantics.db.layer0.variable.Variable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Jani Simomaa + * @since 1.30.0 + */ +public class CopyURI implements ActionFactory { + + private static final Logger LOGGER = LoggerFactory.getLogger(CopyURI.class); + + @Override + public Runnable create(Object target) { + return () -> { + try { + String uri = getPossibleURI(target); + if (uri != null) { + Clipboard cb = new Clipboard(Display.getCurrent()); + cb.setContents(new Object[] { uri }, new Transfer[] { TextTransfer.getInstance() }); + } + } catch (Exception e) { + LOGGER.error("Could not get URI for input {} to copy to clipboard", target, e); + } + }; + } + + private String getPossibleURI(Object input) throws DatabaseException { + if (input instanceof Resource) { + return Simantics.getSession().syncRequest(new PossibleURI((Resource) input)); + } else if (input instanceof Variable) { + return Simantics.getSession().syncRequest(new VariableURI((Variable) input)); + } + return null; + } + +} diff --git a/bundles/org.simantics.viewpoint.ontology/graph/ViewpointTests.pgraph b/bundles/org.simantics.viewpoint.ontology/graph/ViewpointTests.pgraph index 1eb13238f..4f297e417 100644 --- a/bundles/org.simantics.viewpoint.ontology/graph/ViewpointTests.pgraph +++ b/bundles/org.simantics.viewpoint.ontology/graph/ViewpointTests.pgraph @@ -35,3 +35,9 @@ VP.InstanceOfTest