]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.ui/src/org/simantics/document/ui/function/All.java
Several Wiki documentation view improvements.
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / function / All.java
index 20b8d3d0e66cb050670b0d701330300b670d9ded..e5f8b439238c9b1709a2f4d13e6d3df38527e831 100644 (file)
@@ -13,8 +13,11 @@ package org.simantics.document.ui.function;
 \r
 import java.util.TreeMap;\r
 \r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
 import org.eclipse.jface.viewers.ISelectionProvider;\r
 import org.eclipse.jface.viewers.StructuredSelection;\r
+import org.eclipse.swt.browser.LocationEvent;\r
 import org.eclipse.swt.graphics.FontData;\r
 import org.eclipse.swt.graphics.Point;\r
 import org.eclipse.swt.graphics.RGB;\r
@@ -30,19 +33,26 @@ import org.simantics.browsing.ui.NodeContext;
 import org.simantics.databoard.Bindings;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.common.request.UnaryRead;\r
 import org.simantics.db.common.request.WriteRequest;\r
 import org.simantics.db.common.utils.Logger;\r
 import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.common.utils.RequestUtil;\r
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.db.layer0.adapter.Instances;\r
 import org.simantics.db.layer0.request.PossibleModel;\r
+import org.simantics.db.layer0.request.PossibleResource;\r
 import org.simantics.db.layer0.util.RemoverUtil;\r
 import org.simantics.db.layer0.variable.RVI;\r
 import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.document.DocumentDialect;\r
 import org.simantics.document.DocumentResource;\r
 import org.simantics.document.DocumentUtils;\r
+import org.simantics.document.ui.Activator;\r
 import org.simantics.document.ui.CSSEditor;\r
 import org.simantics.layer0.Layer0;\r
 import org.simantics.scenegraph.INode;\r
@@ -50,9 +60,12 @@ import org.simantics.scenegraph.loader.ScenegraphLoaderProcess;
 import org.simantics.scenegraph.loader.ScenegraphLoaderUtils;\r
 import org.simantics.scenegraph.loader.ScenegraphLoaderUtils.ScenegraphPropertyReference;\r
 import org.simantics.scl.reflection.annotations.SCLValue;\r
+import org.simantics.scl.runtime.function.Function1;\r
 import org.simantics.scl.runtime.function.FunctionImpl1;\r
 import org.simantics.scl.runtime.function.FunctionImpl3;\r
+import org.simantics.ui.SimanticsUI;\r
 import org.simantics.ui.workbench.ResourceEditorInput2;\r
+import org.simantics.ui.workbench.action.DefaultActions;\r
 import org.simantics.utils.threads.SWTThread;\r
 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
 \r
@@ -546,7 +559,7 @@ public class All {
                        @Override\r
                        void perform(String before, String selected, String after, Point selection) {\r
 \r
-                               textReference.setValue(before + "[[Image:root://Library/image.png|100px]]" + selected + after);\r
+                               textReference.setValue(before + "[[Image:root://Library/image.png|100px]]" + "\r\n" + selected + after);\r
                                \r
                        }\r
                        \r
@@ -683,6 +696,18 @@ public class All {
         \r
     }\r
 \r
+    @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
+    public static Object internalLinkModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
+        return new WikiButtonModifier(graph, context) {\r
+            @Override\r
+            void perform(String before, String selected, String after, Point selection) {\r
+                textReference.setValue(before +\r
+                        "[[Media:root://Documents/Document.pdf|Link to a file within the model]]\r\n" + selected + after);\r
+                \r
+            }\r
+        };\r
+    }\r
+    \r
     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
     public static Object linkModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
 \r
@@ -691,8 +716,8 @@ public class All {
                        @Override\r
                        void perform(String before, String selected, String after, Point selection) {\r
 \r
-                               textReference.setValue(before + "\r\n" +\r
-                                               "[http://www.simantics.org External Website Link]\r\n"  + selected + after);\r
+                               textReference.setValue(before +\r
+                                               "[http://www.simantics.org External Website Link]\r\n" + selected + after);\r
                                \r
                        }\r
                        \r
@@ -815,5 +840,51 @@ public class All {
        return true;\r
        \r
     }\r
-    \r
+\r
+    private static class ResolveURI extends UnaryRead<String, Object> {\r
+        public ResolveURI(String uri) {\r
+            super(uri);\r
+        }\r
+        @Override\r
+        public Object perform(ReadGraph graph) throws DatabaseException {\r
+            Object result = graph.syncRequest(new PossibleResource(parameter));\r
+            if (result == null)\r
+                result = Variables.getPossibleVariable(graph, parameter);\r
+            return result;\r
+        }\r
+    }\r
+\r
+    private static final Function1<Object, Boolean> PERFORM_DEFAULT_ACTION_FOR_URI_RESOURCE = new Function1<Object, Boolean>() {\r
+        @Override\r
+        public Boolean apply(Object p0) {\r
+            LocationEvent le = (LocationEvent) p0;\r
+            if (le.location.startsWith(DocumentDialect.SIMANTICS_INTERNAL_URI_PREFIX)) {\r
+                // This is not a valid URL anyway so deny relocation.\r
+                le.doit = false;\r
+\r
+                // Try to execute default action for the resource or variable\r
+                // that the URI represents.\r
+                String uri = le.location.substring(DocumentDialect.SIMANTICS_INTERNAL_URI_PREFIX.length());\r
+                try {\r
+                    Session s = Simantics.getSession();\r
+                    Object input = RequestUtil.trySyncRequest(s,\r
+                            SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,\r
+                            SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,\r
+                            new ResolveURI(uri));\r
+                    if (input != null) {\r
+                        DefaultActions.asyncPerformDefaultAction(s, input, false, false, false);\r
+                    }\r
+                } catch (DatabaseException | InterruptedException e) {\r
+                    Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to resolve URI to a database resource or variable: " + uri, e));\r
+                }\r
+            }\r
+            return true;\r
+        }\r
+    };\r
+\r
+    @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
+    public static Function1<Object, Boolean> locationChanging(ReadGraph graph, Resource variable, Variable context) throws DatabaseException {\r
+        return PERFORM_DEFAULT_ACTION_FOR_URI_RESOURCE;\r
+    }\r
+\r
 }\r