-package org.simantics.selectionview;\r
-\r
-import java.util.function.Consumer;\r
-\r
-import org.eclipse.jface.layout.GridDataFactory;\r
-import org.eclipse.jface.layout.GridLayoutFactory;\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.eclipse.jface.viewers.ISelectionProvider;\r
-import org.eclipse.jface.viewers.StructuredSelection;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Control;\r
-import org.eclipse.swt.widgets.Event;\r
-import org.eclipse.swt.widgets.Listener;\r
-import org.eclipse.ui.IWorkbenchSite;\r
-import org.simantics.Simantics;\r
-import org.simantics.browsing.ui.common.ErrorLogger;\r
-import org.simantics.browsing.ui.common.views.IFilterArea;\r
-import org.simantics.browsing.ui.common.views.IFilterAreaProvider;\r
-import org.simantics.browsing.ui.swt.PartNameListener;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.AsyncReadGraph;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.VirtualGraph;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.procedure.adapter.DisposableAsyncListener;\r
-import org.simantics.db.common.procedure.adapter.ListenerSupport;\r
-import org.simantics.db.common.request.UniqueRead;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.common.request.WriteResultRequest;\r
-import org.simantics.db.common.utils.Logger;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.exception.ServiceNotFoundException;\r
-import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.db.management.ISessionContext;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.scenegraph.ontology.ScenegraphResources;\r
-import org.simantics.utils.datastructures.Pair;\r
-import org.simantics.utils.ui.ISelectionUtils;\r
-import org.simantics.utils.ui.jface.ActiveSelectionProvider;\r
-\r
-abstract public class ModelledTabContributor implements PropertyTabContributor, ListenerSupport {\r
-\r
- static class InputRead extends UniqueRead<Pair<Variable, Resource>> {\r
- \r
- private Object input;\r
- \r
- InputRead(Object input) {\r
- this.input = input;\r
- }\r
-\r
- @Override\r
- public Pair<Variable, Resource> perform(ReadGraph graph) throws DatabaseException {\r
-\r
- Variable resultVariable = null;\r
- Resource resultResource = null;\r
-\r
- if(input instanceof ISelection) {\r
- Variable var = ISelectionUtils.filterSingleSelection((ISelection)input, Variable.class);\r
- if(var != null) resultVariable = var;\r
- Resource res = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class);\r
- if(res != null) resultResource = res;\r
- } else if (input instanceof Resource) {\r
- resultResource = (Resource)input;\r
- }\r
-\r
- return Pair.make(resultVariable, resultResource);\r
-\r
- }\r
-\r
- };\r
- \r
- static class InputListener extends DisposableAsyncListener<Pair<Variable, Resource>> {\r
- \r
- final Resource runtime;\r
- \r
- public InputListener(Resource runtime) {\r
- this.runtime = runtime;\r
- }\r
-\r
- @Override\r
- public void execute(AsyncReadGraph graph, final Pair<Variable, Resource> result) {\r
- \r
- Simantics.getSession().async(new WriteRequest(Simantics.getSession().getService(VirtualGraph.class)) {\r
-\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
-\r
- ScenegraphResources SG = ScenegraphResources.getInstance(graph);\r
- \r
- if(result.first != null) {\r
- String uri = result.first.getURI(graph);\r
- graph.claimLiteral(runtime, SG.Runtime_HasVariable, uri, Bindings.STRING);\r
- }\r
-\r
- if(result.second != null) {\r
- graph.deny(runtime, SG.Runtime_HasResource);\r
- graph.claim(runtime, SG.Runtime_HasResource, result.second);\r
- }\r
- \r
- }\r
- \r
- }); \r
- \r
- }\r
-\r
- @Override\r
- public void exception(AsyncReadGraph graph, Throwable throwable) {\r
- Logger.defaultLogError(throwable);\r
- }\r
- \r
- }\r
- \r
- protected Resource runtime;\r
- \r
- // For ListenerSupport (supporting DB request listeners)\r
- protected boolean disposed = false;\r
- protected ISelection input = StructuredSelection.EMPTY;\r
- \r
- protected ISelectionProvider selectionProvider = new ActiveSelectionProvider();\r
-\r
- abstract public void createControls(Composite body, IWorkbenchSite site);\r
-\r
- public IFilterArea getFilterArea() {\r
- return null;\r
- }\r
-\r
- public void requestFocus() {\r
- }\r
-\r
- public ISelectionProvider getSelectionProvider() {\r
- return selectionProvider; \r
- }\r
-\r
- public Read<String> getPartNameReadRequest(final ISelection forSelection) {\r
- return new UniqueRead<String>() {\r
-\r
- private String forResource(ReadGraph graph, Resource resource) throws DatabaseException {\r
- Layer0 L0 = Layer0.getInstance(graph);\r
- String name = NameUtils.getSafeName(graph, resource);\r
- Resource type = graph.getPossibleType(resource, L0.Entity);\r
- if(type != null) {\r
- name += " : " + NameUtils.getSafeName(graph, type);\r
- }\r
- return name;\r
- }\r
- \r
- @Override\r
- public String perform(ReadGraph graph) throws DatabaseException {\r
- Resource resource = ISelectionUtils.filterSingleSelection(forSelection, Resource.class);\r
- if(resource != null) return forResource(graph, resource);\r
- Variable variable = ISelectionUtils.filterSingleSelection(forSelection, Variable.class);\r
- if(variable != null) {\r
- Resource represents = variable.getPossibleRepresents(graph);\r
- if(represents != null) return forResource(graph, represents);\r
- }\r
- return "Selection";\r
- }\r
- \r
- };\r
- }\r
-\r
- public void updatePartName(ISelection forSelection, Consumer<String> updateCallback) {\r
- Read<String> read = getPartNameReadRequest(forSelection);\r
- if (read == null) {\r
- updateCallback.accept("Selection");\r
- } else {\r
- Simantics.getSession().asyncRequest(read, new PartNameListener(updateCallback, this));\r
- }\r
- }\r
-\r
- public void updatePartName(Consumer<String> updateCallback) {\r
- updatePartName(input, updateCallback);\r
- }\r
-\r
- protected void dispose() {\r
- this.disposed = true;\r
- }\r
-\r
- @Override\r
- public void exception(Throwable t) {\r
- ErrorLogger.defaultLogError("PropertyTabContributorImpl received unexpected exception.", t);\r
- }\r
-\r
- @Override\r
- public boolean isDisposed() {\r
- return disposed;\r
- }\r
-\r
- public Control createControl(Composite parent, final IWorkbenchSite site) {\r
-\r
- class TabComposite extends Composite {\r
- public TabComposite(Composite parent) {\r
- super(parent, 0);\r
-\r
- GridLayoutFactory.fillDefaults().applyTo(parent);\r
- GridDataFactory.fillDefaults().span(1, 1).grab(true, true).applyTo(this);\r
-\r
- Composite body = this;\r
- GridLayoutFactory.fillDefaults().spacing(0, 0).equalWidth(false).numColumns(1).applyTo(body);\r
-\r
- try {\r
- ModelledTabContributor.this.createControls(body, site);\r
- } catch (Throwable t) {\r
- ErrorLogger.defaultLogError(t);\r
- }\r
- }\r
- }\r
-\r
- final TabComposite tc = new TabComposite(parent);\r
- tc.addListener(SWT.Dispose, new Listener() {\r
- public void handleEvent(Event e) {\r
- ModelledTabContributor.this.dispose();\r
- }\r
- });\r
- \r
- return tc;\r
- \r
- }\r
-\r
- @Override\r
- public IPropertyTab create(Composite parent, IWorkbenchSite site, ISessionContext context, Object input) {\r
- IPropertyTab tab = new Tab(site, parent);\r
- tab.createControl((Composite) tab.getControl(), context);\r
- return tab;\r
- }\r
-\r
- class Tab extends Composite implements IPropertyTab2, IFilterAreaProvider {\r
-\r
- final IWorkbenchSite site;\r
-\r
- private InputListener listener;\r
- \r
- public Tab(IWorkbenchSite site, Composite parent) {\r
- \r
- super(parent, SWT.NONE);\r
- this.site = site;\r
- \r
- try {\r
- runtime = Simantics.getSession().sync(new WriteResultRequest<Resource>(Simantics.getSession().getService(VirtualGraph.class)) {\r
- @Override\r
- public Resource perform(WriteGraph graph) throws DatabaseException {\r
- Layer0 L0 = Layer0.getInstance(graph);\r
- ScenegraphResources SG = ScenegraphResources.getInstance(graph);\r
- Resource runtime = graph.newResource();\r
- graph.claim(runtime, L0.InstanceOf, null, SG.Runtime);\r
- return runtime;\r
- }\r
- });\r
- } catch (ServiceNotFoundException e) {\r
- Logger.defaultLogError(e);\r
- } catch (DatabaseException e) {\r
- Logger.defaultLogError(e);\r
- }\r
- \r
- }\r
-\r
- @Override\r
- public void createControl(Composite parent, ISessionContext context) {\r
- Control c = ModelledTabContributor.this.createControl(parent, site);\r
- c.addListener(SWT.Dispose, new Listener() {\r
- public void handleEvent(Event e) {\r
- if(listener != null) {\r
- listener.dispose();\r
- listener = null;\r
- }\r
- }\r
- });\r
- }\r
-\r
- @Override\r
- public Control getControl() {\r
- return this;\r
- }\r
-\r
- @Override\r
- public boolean isDisposed() {\r
- return super.isDisposed();\r
- }\r
-\r
- @Override\r
- public void requestFocus() {\r
- ModelledTabContributor.this.requestFocus();\r
- }\r
-\r
- @Override\r
- public void setInput(ISessionContext context, ISelection selection, boolean force) {\r
- \r
- ModelledTabContributor.this.input = selection;\r
-\r
- if(listener != null) listener.dispose();\r
- \r
- listener = new InputListener(runtime);\r
- \r
- try {\r
- Simantics.getSession().syncRequest(new InputRead(ModelledTabContributor.this.input), listener);\r
- } catch (DatabaseException e) {\r
- Logger.defaultLogError(e);\r
- }\r
- \r
- }\r
-\r
- @Override\r
- public ISelectionProvider getSelectionProvider() {\r
- return ModelledTabContributor.this.getSelectionProvider();\r
- }\r
-\r
- @Override\r
- public IFilterArea getFilterArea() {\r
- return ModelledTabContributor.this.getFilterArea();\r
- }\r
-\r
- @Override\r
- public void updatePartName(Consumer<String> updateCallback) {\r
- ModelledTabContributor.this.updatePartName(input, updateCallback);\r
- }\r
-\r
- }\r
- \r
-}\r
+package org.simantics.selectionview;
+
+import java.util.function.Consumer;
+
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.layout.GridLayoutFactory;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.ISelectionProvider;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Listener;
+import org.eclipse.ui.IWorkbenchSite;
+import org.simantics.Simantics;
+import org.simantics.browsing.ui.common.ErrorLogger;
+import org.simantics.browsing.ui.common.views.IFilterArea;
+import org.simantics.browsing.ui.common.views.IFilterAreaProvider;
+import org.simantics.browsing.ui.swt.PartNameListener;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.VirtualGraph;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.procedure.adapter.DisposableAsyncListener;
+import org.simantics.db.common.procedure.adapter.ListenerSupport;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.request.WriteResultRequest;
+import org.simantics.db.common.utils.Logger;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.ServiceNotFoundException;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.management.ISessionContext;
+import org.simantics.db.request.Read;
+import org.simantics.layer0.Layer0;
+import org.simantics.scenegraph.ontology.ScenegraphResources;
+import org.simantics.utils.datastructures.Pair;
+import org.simantics.utils.ui.ISelectionUtils;
+import org.simantics.utils.ui.jface.ActiveSelectionProvider;
+
+abstract public class ModelledTabContributor implements PropertyTabContributor, ListenerSupport {
+
+ static class InputRead extends UniqueRead<Pair<Variable, Resource>> {
+
+ private Object input;
+
+ InputRead(Object input) {
+ this.input = input;
+ }
+
+ @Override
+ public Pair<Variable, Resource> perform(ReadGraph graph) throws DatabaseException {
+
+ Variable resultVariable = null;
+ Resource resultResource = null;
+
+ if(input instanceof ISelection) {
+ Variable var = ISelectionUtils.filterSingleSelection((ISelection)input, Variable.class);
+ if(var != null) resultVariable = var;
+ Resource res = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class);
+ if(res != null) resultResource = res;
+ } else if (input instanceof Resource) {
+ resultResource = (Resource)input;
+ }
+
+ return Pair.make(resultVariable, resultResource);
+
+ }
+
+ };
+
+ static class InputListener extends DisposableAsyncListener<Pair<Variable, Resource>> {
+
+ final Resource runtime;
+
+ public InputListener(Resource runtime) {
+ this.runtime = runtime;
+ }
+
+ @Override
+ public void execute(AsyncReadGraph graph, final Pair<Variable, Resource> result) {
+
+ Simantics.getSession().async(new WriteRequest(Simantics.getSession().getService(VirtualGraph.class)) {
+
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+
+ ScenegraphResources SG = ScenegraphResources.getInstance(graph);
+
+ if(result.first != null) {
+ String uri = result.first.getURI(graph);
+ graph.claimLiteral(runtime, SG.Runtime_HasVariable, uri, Bindings.STRING);
+ }
+
+ if(result.second != null) {
+ graph.deny(runtime, SG.Runtime_HasResource);
+ graph.claim(runtime, SG.Runtime_HasResource, result.second);
+ }
+
+ }
+
+ });
+
+ }
+
+ @Override
+ public void exception(AsyncReadGraph graph, Throwable throwable) {
+ Logger.defaultLogError(throwable);
+ }
+
+ }
+
+ protected Resource runtime;
+
+ // For ListenerSupport (supporting DB request listeners)
+ protected boolean disposed = false;
+ protected ISelection input = StructuredSelection.EMPTY;
+
+ protected ISelectionProvider selectionProvider = new ActiveSelectionProvider();
+
+ abstract public void createControls(Composite body, IWorkbenchSite site);
+
+ public IFilterArea getFilterArea() {
+ return null;
+ }
+
+ public void requestFocus() {
+ }
+
+ public ISelectionProvider getSelectionProvider() {
+ return selectionProvider;
+ }
+
+ public Read<String> getPartNameReadRequest(final ISelection forSelection) {
+ return new UniqueRead<String>() {
+
+ private String forResource(ReadGraph graph, Resource resource) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ String name = NameUtils.getSafeName(graph, resource);
+ Resource type = graph.getPossibleType(resource, L0.Entity);
+ if(type != null) {
+ name += " : " + NameUtils.getSafeName(graph, type);
+ }
+ return name;
+ }
+
+ @Override
+ public String perform(ReadGraph graph) throws DatabaseException {
+ Resource resource = ISelectionUtils.filterSingleSelection(forSelection, Resource.class);
+ if(resource != null) return forResource(graph, resource);
+ Variable variable = ISelectionUtils.filterSingleSelection(forSelection, Variable.class);
+ if(variable != null) {
+ Resource represents = variable.getPossibleRepresents(graph);
+ if(represents != null) return forResource(graph, represents);
+ }
+ return "Selection";
+ }
+
+ };
+ }
+
+ public void updatePartName(ISelection forSelection, Consumer<String> updateCallback) {
+ Read<String> read = getPartNameReadRequest(forSelection);
+ if (read == null) {
+ updateCallback.accept("Selection");
+ } else {
+ Simantics.getSession().asyncRequest(read, new PartNameListener(updateCallback, this));
+ }
+ }
+
+ public void updatePartName(Consumer<String> updateCallback) {
+ updatePartName(input, updateCallback);
+ }
+
+ protected void dispose() {
+ this.disposed = true;
+ }
+
+ @Override
+ public void exception(Throwable t) {
+ ErrorLogger.defaultLogError("PropertyTabContributorImpl received unexpected exception.", t);
+ }
+
+ @Override
+ public boolean isDisposed() {
+ return disposed;
+ }
+
+ public Control createControl(Composite parent, final IWorkbenchSite site) {
+
+ class TabComposite extends Composite {
+ public TabComposite(Composite parent) {
+ super(parent, 0);
+
+ GridLayoutFactory.fillDefaults().applyTo(parent);
+ GridDataFactory.fillDefaults().span(1, 1).grab(true, true).applyTo(this);
+
+ Composite body = this;
+ GridLayoutFactory.fillDefaults().spacing(0, 0).equalWidth(false).numColumns(1).applyTo(body);
+
+ try {
+ ModelledTabContributor.this.createControls(body, site);
+ } catch (Throwable t) {
+ ErrorLogger.defaultLogError(t);
+ }
+ }
+ }
+
+ final TabComposite tc = new TabComposite(parent);
+ tc.addListener(SWT.Dispose, new Listener() {
+ public void handleEvent(Event e) {
+ ModelledTabContributor.this.dispose();
+ }
+ });
+
+ return tc;
+
+ }
+
+ @Override
+ public IPropertyTab create(Composite parent, IWorkbenchSite site, ISessionContext context, Object input) {
+ IPropertyTab tab = new Tab(site, parent);
+ tab.createControl((Composite) tab.getControl(), context);
+ return tab;
+ }
+
+ class Tab extends Composite implements IPropertyTab2, IFilterAreaProvider {
+
+ final IWorkbenchSite site;
+
+ private InputListener listener;
+
+ public Tab(IWorkbenchSite site, Composite parent) {
+
+ super(parent, SWT.NONE);
+ this.site = site;
+
+ try {
+ runtime = Simantics.getSession().sync(new WriteResultRequest<Resource>(Simantics.getSession().getService(VirtualGraph.class)) {
+ @Override
+ public Resource perform(WriteGraph graph) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ ScenegraphResources SG = ScenegraphResources.getInstance(graph);
+ Resource runtime = graph.newResource();
+ graph.claim(runtime, L0.InstanceOf, null, SG.Runtime);
+ return runtime;
+ }
+ });
+ } catch (ServiceNotFoundException e) {
+ Logger.defaultLogError(e);
+ } catch (DatabaseException e) {
+ Logger.defaultLogError(e);
+ }
+
+ }
+
+ @Override
+ public void createControl(Composite parent, ISessionContext context) {
+ Control c = ModelledTabContributor.this.createControl(parent, site);
+ c.addListener(SWT.Dispose, new Listener() {
+ public void handleEvent(Event e) {
+ if(listener != null) {
+ listener.dispose();
+ listener = null;
+ }
+ }
+ });
+ }
+
+ @Override
+ public Control getControl() {
+ return this;
+ }
+
+ @Override
+ public boolean isDisposed() {
+ return super.isDisposed();
+ }
+
+ @Override
+ public void requestFocus() {
+ ModelledTabContributor.this.requestFocus();
+ }
+
+ @Override
+ public void setInput(ISessionContext context, ISelection selection, boolean force) {
+
+ ModelledTabContributor.this.input = selection;
+
+ if(listener != null) listener.dispose();
+
+ listener = new InputListener(runtime);
+
+ try {
+ Simantics.getSession().syncRequest(new InputRead(ModelledTabContributor.this.input), listener);
+ } catch (DatabaseException e) {
+ Logger.defaultLogError(e);
+ }
+
+ }
+
+ @Override
+ public ISelectionProvider getSelectionProvider() {
+ return ModelledTabContributor.this.getSelectionProvider();
+ }
+
+ @Override
+ public IFilterArea getFilterArea() {
+ return ModelledTabContributor.this.getFilterArea();
+ }
+
+ @Override
+ public void updatePartName(Consumer<String> updateCallback) {
+ ModelledTabContributor.this.updatePartName(input, updateCallback);
+ }
+
+ }
+
+}