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