]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Copy URI context menu action to Model Browser for development mode 31/731/4
authorjsimomaa <jani.simomaa@gmail.com>
Thu, 20 Jul 2017 05:10:21 +0000 (08:10 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 1 Aug 2017 11:40:24 +0000 (14:40 +0300)
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

bundles/org.simantics.browsing.ui.model/adapters.xml
bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/tests/HasURITest.java [new file with mode: 0644]
bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/tests/InDevelopmentModeTest.java [new file with mode: 0644]
bundles/org.simantics.modeling.ontology/graph/ModelingViewpoint.pgraph
bundles/org.simantics.modeling/adapters.xml
bundles/org.simantics.modeling/src/org/simantics/modeling/actions/CopyURI.java [new file with mode: 0644]
bundles/org.simantics.viewpoint.ontology/graph/ViewpointTests.pgraph

index 50b34347957672a684adf7fb9a5c3c3cc1807964..4276f3febe14de63daf3c8663ab4c7c87a721a17 100644 (file)
             class="org.simantics.browsing.ui.model.tests.FailTest"
             constructor="get">
         </type>
+        <type uri="http://www.simantics.org/Viewpoint-0.0/HasURITest"
+            class="org.simantics.browsing.ui.model.tests.HasURITest"
+            constructor="get">
+        </type>
+        <type uri="http://www.simantics.org/Viewpoint-0.0/InDevelopmentModeTest"
+            class="org.simantics.browsing.ui.model.tests.InDevelopmentModeTest"
+            constructor="get">
+        </type>
     </target>
     
     <target interface="org.simantics.browsing.ui.model.actions.IActionCategory">
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 (file)
index 0000000..3443625
--- /dev/null
@@ -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 (file)
index 0000000..fa85e25
--- /dev/null
@@ -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();
+    }
+
+}
index 0255339ac7bda279053ca6e30b98d744f2362ac2..86171a31a445049705b104c7bfa9b89eb5e42d4c 100644 (file)
@@ -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"
index 0e3870c356673adc721d392164649b8588d89b73..b74449f822ed4ad9eb5084f70f7a4992fe5f2085 100644 (file)
         </type>
                <resource uri="http://www.simantics.org/Modeling-0.0/ModelingActionContext/Actions/Help"
                        class="org.simantics.modeling.actions.Help" />
+        <resource uri="http://www.simantics.org/Modeling-0.0/ModelingActionContext/Actions/CopyURI"
+            class="org.simantics.modeling.actions.CopyURI" />
     </target>
 
        <target interface="org.simantics.db.layer0.adapter.DropActionFactory">
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 (file)
index 0000000..cdb56be
--- /dev/null
@@ -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;
+    }
+
+}
index 1eb13238fad88dc387fa517b4940f066e5ac1ffe..4f297e417e744faf85b3a0f9a590a3fcb8698a6c 100644 (file)
@@ -35,3 +35,9 @@ VP.InstanceOfTest <T VP.Test
 
 VP.FailTest <T VP.Test
     L0.HasDescription "A test that is compatible with all content types and always fails, i.e. returns false."
+
+VP.HasURITest <T VP.Test
+    L0.HasDescription "A test that checks the Resource or Variable given as input has a proper URI."
+
+VP.InDevelopmentModeTest <T VP.Test
+    L0.HasDescription "A test that directly invokes org.eclipse.runtime.core.Platform.inDevelopmentMode()."