From 6a6726b1ee9c2e78971d26b50f15b867cc7c5a02 Mon Sep 17 00:00:00 2001 From: Miro Richard Eklund Date: Tue, 10 Jul 2018 17:42:14 +0300 Subject: [PATCH] Add workbenchselection json fetcher to SCL interface Requires some small refactoring changes to AdaptableHintContext and classes using it, which is why so many files were affected. gitlab #41 Change-Id: I35bc8abe9e1eca8ff34ee503925645ee14d41664 --- .../ui/common}/AdaptableHintContext.java | 2 +- .../StandardWorkbenchSelectionElement.java | 152 ++++++++++++++++++ .../ui/nattable/NatTableGraphExplorer.java | 2 +- .../ui/nattable/RowSelectionItem.java | 2 +- .../browsing/ui/swt/GraphExplorerFactory.java | 1 + .../browsing/ui/swt/GraphExplorerImpl.java | 1 + .../browsing/ui/swt/GraphExplorerImpl2.java | 1 + .../swt/widgets/GraphExplorerComposite.java | 2 +- .../desktop/ui/internal/ExportModel.java | 2 +- .../ui/internal/ExportSharedLibrary.java | 2 +- .../DiagramViewerSelectionProvider.java | 2 +- .../scl/Simantics/WorkbenchSelection.scl | 8 +- 12 files changed, 168 insertions(+), 9 deletions(-) rename bundles/{org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt => org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common}/AdaptableHintContext.java (99%) create mode 100644 bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/StandardWorkbenchSelectionElement.java diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/AdaptableHintContext.java b/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/AdaptableHintContext.java similarity index 99% rename from bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/AdaptableHintContext.java rename to bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/AdaptableHintContext.java index 3b6d45b82..bd7aa2790 100644 --- a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/AdaptableHintContext.java +++ b/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/AdaptableHintContext.java @@ -9,7 +9,7 @@ * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ -package org.simantics.browsing.ui.swt; +package org.simantics.browsing.ui.common; import java.util.Arrays; import java.util.HashMap; diff --git a/bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/StandardWorkbenchSelectionElement.java b/bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/StandardWorkbenchSelectionElement.java new file mode 100644 index 000000000..8d8628c00 --- /dev/null +++ b/bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/StandardWorkbenchSelectionElement.java @@ -0,0 +1,152 @@ +/******************************************************************************* + * Copyright (c) 2007, 2012 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: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.browsing.ui.model; + +import org.eclipse.core.runtime.IAdaptable; +import org.simantics.browsing.ui.BuiltinKeys; +import org.simantics.browsing.ui.GraphExplorer; +import org.simantics.browsing.ui.NodeContext; +import org.simantics.browsing.ui.common.AdaptableHintContext; +import org.simantics.browsing.ui.model.nodetypes.NodeType; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.request.PossibleVariable; +import org.simantics.db.layer0.request.PossibleVariableRepresents; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.ui.selection.AnyResource; +import org.simantics.ui.selection.AnyVariable; +import org.simantics.ui.selection.ExplorerColumnContentType; +import org.simantics.ui.selection.ExplorerInputContentType; +import org.simantics.ui.selection.WorkbenchSelectionContentType; +import org.simantics.ui.selection.WorkbenchSelectionElement; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class StandardWorkbenchSelectionElement extends AdaptableHintContext { + + private static final Logger LOGGER = LoggerFactory.getLogger(StandardWorkbenchSelectionElement.class); + + public static WorkbenchSelectionElement nodeContextToWorkbenchSelectionElement (NodeContext context) { + return (new StandardWorkbenchSelectionElement(context)); + } + + final public WorkbenchSelectionElement wse; + final public Object content; + final public Resource resource; + final public Variable variable; + final public Object input; + + private WorkbenchSelectionElement extractWse(Object content) { + if(content instanceof NodeContext) { + NodeContext context = (NodeContext)content; + Object input = context.getConstant(NodeType.TYPE); + if(input instanceof NodeType) + return ((NodeType)input).getWorkbenchSelectionElement(context); + } + return null; + } + + private Resource extractResource(Object content) { + if(content instanceof NodeContext) { + NodeContext context = (NodeContext)content; + Object input = context.getConstant(BuiltinKeys.INPUT); + if(input instanceof Resource) return (Resource)input; + if(input instanceof IAdaptable) { + Resource var = (Resource)((IAdaptable)input).getAdapter(Resource.class); + if(var != null) return var; + } + } + return null; + } + + private Variable extractVariable(Object content) { + if(content instanceof NodeContext) { + NodeContext context = (NodeContext)content; + Object input = context.getConstant(BuiltinKeys.INPUT); + if(input instanceof Variable) return (Variable)input; + if(input instanceof IAdaptable) { + Variable var = (Variable)((IAdaptable)input).getAdapter(Variable.class); + if(var != null) return var; + } + } + return null; + } + + private Object extractInput(Object content) { + if(content instanceof NodeContext) { + NodeContext context = (NodeContext)content; + return context.getConstant(BuiltinKeys.INPUT); + } + return null; + } + + public StandardWorkbenchSelectionElement(Object content) { + super(new Key[0]); + this.content = content; + this.wse = extractWse(content); + this.resource = extractResource(content); + this.variable = extractVariable(content); + this.input = extractInput(content); + } + + @SuppressWarnings("unchecked") + @Override + public T getContent(WorkbenchSelectionContentType contentType) { + if (wse != null) { + T result = wse.getContent(contentType); + if (result != null) + return result; + } + + if (contentType instanceof AnyResource) { + if (resource != null) + return (T) resource; + if (variable == null) + return null; + try { + return (T) ((AnyResource) contentType).processor.syncRequest(new PossibleVariableRepresents(variable)); + } catch (DatabaseException e) { + LOGGER.error("Unexpected error occurred while resolving Resource from Variable " + variable, e); + } + } + else if (contentType instanceof AnyVariable) { + if (variable != null) + return (T) variable; + if (resource == null) + return null; + try { + return (T) ((AnyVariable) contentType).processor.syncRequest(new PossibleVariable(resource)); + } catch (DatabaseException e) { + LOGGER.error("Unexpected error occurred while resolving Variable from Resource " + resource, e); + } + } else if (contentType instanceof ExplorerInputContentType) { + return (T) input; + } + return null; + } + + @SuppressWarnings("rawtypes") + @Override + public Object getAdapter(Class adapter) { + if(WorkbenchSelectionElement.class == adapter) { + return wse; + } + if(NodeContext.class == adapter) { + if(content instanceof NodeContext) + return (NodeContext)content; + else + return null; + } + return super.getAdapter(adapter); + } + +} \ No newline at end of file diff --git a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/NatTableGraphExplorer.java b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/NatTableGraphExplorer.java index 6765f65ea..be6a73a0c 100644 --- a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/NatTableGraphExplorer.java +++ b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/NatTableGraphExplorer.java @@ -141,6 +141,7 @@ import org.simantics.browsing.ui.PrimitiveQueryUpdater; import org.simantics.browsing.ui.SelectionDataResolver; import org.simantics.browsing.ui.SelectionFilter; import org.simantics.browsing.ui.StatePersistor; +import org.simantics.browsing.ui.common.AdaptableHintContext; import org.simantics.browsing.ui.common.ColumnKeys; import org.simantics.browsing.ui.common.ErrorLogger; import org.simantics.browsing.ui.common.NodeContextBuilder; @@ -178,7 +179,6 @@ import org.simantics.browsing.ui.content.Labeler.EnumerationModifier; import org.simantics.browsing.ui.content.Labeler.Modifier; import org.simantics.browsing.ui.nattable.override.DefaultTreeLayerConfiguration2; import org.simantics.browsing.ui.swt.Activator; -import org.simantics.browsing.ui.swt.AdaptableHintContext; import org.simantics.browsing.ui.swt.DefaultImageDecoratorsProcessor; import org.simantics.browsing.ui.swt.DefaultIsExpandedProcessor; import org.simantics.browsing.ui.swt.DefaultLabelDecoratorsProcessor; diff --git a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/RowSelectionItem.java b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/RowSelectionItem.java index 3d943a092..319a7fbea 100644 --- a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/RowSelectionItem.java +++ b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/RowSelectionItem.java @@ -1,6 +1,6 @@ package org.simantics.browsing.ui.nattable; -import org.simantics.browsing.ui.swt.AdaptableHintContext; +import org.simantics.browsing.ui.common.AdaptableHintContext; import org.simantics.db.layer0.SelectionHints; public class RowSelectionItem extends AdaptableHintContext { diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerFactory.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerFactory.java index b99962bf0..3312430dd 100644 --- a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerFactory.java +++ b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerFactory.java @@ -25,6 +25,7 @@ import org.simantics.browsing.ui.GraphExplorer; import org.simantics.browsing.ui.NodeContext; import org.simantics.browsing.ui.SelectionDataResolver; import org.simantics.browsing.ui.SelectionFilter; +import org.simantics.browsing.ui.common.AdaptableHintContext; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.request.PossibleTypedParent; diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerImpl.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerImpl.java index 0e45f86ca..3cf5f7478 100644 --- a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerImpl.java +++ b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerImpl.java @@ -114,6 +114,7 @@ import org.simantics.browsing.ui.PrimitiveQueryProcessor; import org.simantics.browsing.ui.SelectionDataResolver; import org.simantics.browsing.ui.SelectionFilter; import org.simantics.browsing.ui.StatePersistor; +import org.simantics.browsing.ui.common.AdaptableHintContext; import org.simantics.browsing.ui.common.ColumnKeys; import org.simantics.browsing.ui.common.ErrorLogger; import org.simantics.browsing.ui.common.NodeContextBuilder; diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerImpl2.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerImpl2.java index 928bed03f..af0fa25a3 100644 --- a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerImpl2.java +++ b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerImpl2.java @@ -123,6 +123,7 @@ import org.simantics.browsing.ui.PrimitiveQueryUpdater; import org.simantics.browsing.ui.SelectionDataResolver; import org.simantics.browsing.ui.SelectionFilter; import org.simantics.browsing.ui.StatePersistor; +import org.simantics.browsing.ui.common.AdaptableHintContext; import org.simantics.browsing.ui.common.ColumnKeys; import org.simantics.browsing.ui.common.ErrorLogger; import org.simantics.browsing.ui.common.NodeContextBuilder; diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/GraphExplorerComposite.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/GraphExplorerComposite.java index f583d7e7f..0ee36b365 100644 --- a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/GraphExplorerComposite.java +++ b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/GraphExplorerComposite.java @@ -57,6 +57,7 @@ import org.simantics.browsing.ui.GraphExplorer; import org.simantics.browsing.ui.GraphExplorer.TransientExplorerState; import org.simantics.browsing.ui.NodeContext; import org.simantics.browsing.ui.StatePersistor; +import org.simantics.browsing.ui.common.AdaptableHintContext; import org.simantics.browsing.ui.common.ColumnKeys; import org.simantics.browsing.ui.common.EvaluatorData; import org.simantics.browsing.ui.common.EvaluatorDataImpl; @@ -82,7 +83,6 @@ import org.simantics.browsing.ui.graph.impl.SessionContextInputSource; import org.simantics.browsing.ui.model.browsecontexts.BrowseContext; import org.simantics.browsing.ui.model.nodetypes.NodeType; import org.simantics.browsing.ui.swt.Activator; -import org.simantics.browsing.ui.swt.AdaptableHintContext; import org.simantics.browsing.ui.swt.ComparatorSelector; import org.simantics.browsing.ui.swt.ContextMenuInitializer; import org.simantics.browsing.ui.swt.DefaultExplorerSelectionListener; diff --git a/bundles/org.simantics.desktop.ui/src/org/simantics/desktop/ui/internal/ExportModel.java b/bundles/org.simantics.desktop.ui/src/org/simantics/desktop/ui/internal/ExportModel.java index a214029d5..81bcc0357 100644 --- a/bundles/org.simantics.desktop.ui/src/org/simantics/desktop/ui/internal/ExportModel.java +++ b/bundles/org.simantics.desktop.ui/src/org/simantics/desktop/ui/internal/ExportModel.java @@ -20,7 +20,7 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.swt.widgets.Display; import org.simantics.Simantics; -import org.simantics.browsing.ui.swt.AdaptableHintContext; +import org.simantics.browsing.ui.common.AdaptableHintContext; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.request.UniqueRead; diff --git a/bundles/org.simantics.desktop.ui/src/org/simantics/desktop/ui/internal/ExportSharedLibrary.java b/bundles/org.simantics.desktop.ui/src/org/simantics/desktop/ui/internal/ExportSharedLibrary.java index 7bd7b3dff..96f7da830 100644 --- a/bundles/org.simantics.desktop.ui/src/org/simantics/desktop/ui/internal/ExportSharedLibrary.java +++ b/bundles/org.simantics.desktop.ui/src/org/simantics/desktop/ui/internal/ExportSharedLibrary.java @@ -20,7 +20,7 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.swt.widgets.Display; import org.simantics.Simantics; -import org.simantics.browsing.ui.swt.AdaptableHintContext; +import org.simantics.browsing.ui.common.AdaptableHintContext; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.request.UniqueRead; diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramViewerSelectionProvider.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramViewerSelectionProvider.java index 154d8d4c7..f1493e4cc 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramViewerSelectionProvider.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramViewerSelectionProvider.java @@ -6,7 +6,7 @@ import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.ui.IWorkbenchPartSite; import org.simantics.Simantics; -import org.simantics.browsing.ui.swt.AdaptableHintContext; +import org.simantics.browsing.ui.common.AdaptableHintContext; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.request.ResourceRead; diff --git a/bundles/org.simantics.modeling/scl/Simantics/WorkbenchSelection.scl b/bundles/org.simantics.modeling/scl/Simantics/WorkbenchSelection.scl index 60ab7797b..fc060e083 100644 --- a/bundles/org.simantics.modeling/scl/Simantics/WorkbenchSelection.scl +++ b/bundles/org.simantics.modeling/scl/Simantics/WorkbenchSelection.scl @@ -1,5 +1,5 @@ import "Simantics/Variables" - +import "Simantics/Testing/BrowseContext" importJava "org.simantics.ui.selection.WorkbenchSelectionElement" where data WorkbenchSelectionElement @@ -9,4 +9,8 @@ importJava "org.simantics.ui.selection.WorkbenchSelectionUtils" where possibleWSEVariable :: WorkbenchSelectionElement -> Maybe Variable @JavaName "getPossibleResourceSCL" possibleWSEResource :: WorkbenchSelectionElement -> Maybe Resource - \ No newline at end of file + @JavaName "getPossibleJSON" + possibleWSEJson :: WorkbenchSelectionElement -> Maybe String + +importJava "org.simantics.browsing.ui.model.StandardWorkbenchSelectionElement" where + nodeContextToWorkbenchSelectionElement :: NodeContext -> WorkbenchSelectionElement \ No newline at end of file -- 2.47.1