--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.views.swt;\r
+\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.Disposable;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.VirtualGraph;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.UniqueRead;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.db.procedure.AsyncListener;\r
+import org.simantics.db.procedure.Listener;\r
+import org.simantics.scenegraph.ontology.ScenegraphResources;\r
+import org.simantics.ui.selection.AnyResource;\r
+import org.simantics.ui.selection.AnyVariable;\r
+import org.simantics.ui.selection.WorkbenchSelectionElement;\r
+import org.simantics.utils.datastructures.Pair;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ */\r
+public class ModelledSupport extends WidgetSupportImpl implements Disposable {\r
+\r
+ private final ModelledView view;\r
+ private InputListener listener;\r
+ private boolean disposed = false;\r
+ private Pair<Variable, Resource> previous = null;\r
+\r
+ /**\r
+ * @param modelledConfigurationComposite\r
+ */\r
+ public ModelledSupport(ModelledView view) {\r
+ this.view = view;\r
+ }\r
+\r
+ @Override\r
+ public void dispose() {\r
+ \r
+ disposed = true;\r
+ \r
+ if (listener != null)\r
+ listener.dispose();\r
+ \r
+ }\r
+\r
+ class InputListener implements AsyncListener<Pair<Variable, Resource>> {\r
+\r
+ private boolean disposed = false;\r
+\r
+ @Override\r
+ public boolean isDisposed() {\r
+ return this.disposed || ModelledSupport.this.disposed;\r
+ }\r
+\r
+ public void dispose() {\r
+ this.disposed = true;\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ return "InputListener@ModelledSupport@" + view; \r
+ }\r
+\r
+ @Override\r
+ public void execute(AsyncReadGraph graph, final Pair<Variable, Resource> result) {\r
+\r
+ if(previous != null && previous.equals(result)) return;\r
+ previous = result;\r
+\r
+ graph.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
+ Resource runtime = view.runtime;\r
+\r
+ if(result.first != null) {\r
+ String uri = result.first.getURI(graph);\r
+ graph.claimLiteral(runtime, SG.Runtime_HasVariable, uri, Bindings.STRING);\r
+ } else {\r
+ graph.deny(runtime, SG.Runtime_HasVariable);\r
+ }\r
+\r
+ if(result.second != null) {\r
+ graph.deny(runtime, SG.Runtime_HasResource);\r
+ graph.claim(runtime, SG.Runtime_HasResource, result.second);\r
+ } else {\r
+ graph.deny(runtime, SG.Runtime_HasResource);\r
+ }\r
+\r
+ view.fireInput();\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
+ @SuppressWarnings({ "unchecked", "rawtypes" })\r
+ @Override\r
+ public void fireInput(final ISessionContext context, final Object input) {\r
+\r
+ lastInput = input;\r
+ lastContext = context;\r
+\r
+ if (listener != null)\r
+ listener.dispose();\r
+\r
+ if (context == null)\r
+ return;\r
+\r
+ Session session = context.peekSession();\r
+ if (session == null)\r
+ return;\r
+\r
+ listener = new InputListener();\r
+\r
+ if(view.runtime != null) {\r
+\r
+ session.async(new UniqueRead<Pair<Variable, Resource>>() {\r
+\r
+ private Variable extractVariable(ReadGraph graph, ISelection selection) throws DatabaseException {\r
+\r
+ WorkbenchSelectionElement single = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);\r
+ if(single != null) return single.getContent(new AnyVariable(graph));\r
+ return ISelectionUtils.filterSingleSelection((ISelection)lastInput, Variable.class);\r
+\r
+ }\r
+\r
+ private Resource extractResource(ReadGraph graph, ISelection selection) throws DatabaseException {\r
+\r
+ WorkbenchSelectionElement single = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);\r
+ if(single != null) return single.getContent(new AnyResource(graph));\r
+ return ISelectionUtils.filterSingleSelection((ISelection)lastInput, Resource.class);\r
+\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(lastInput instanceof ISelection) {\r
+ Variable var = extractVariable(graph, (ISelection)lastInput);\r
+ if(var != null) resultVariable = var;\r
+ Resource res = extractResource(graph, (ISelection)lastInput);\r
+ if(res != null) resultResource = res;\r
+ } else if (lastInput instanceof Resource) {\r
+ resultResource = (Resource)lastInput;\r
+ }\r
+\r
+ return Pair.make(resultVariable, resultResource);\r
+\r
+ }\r
+\r
+ }, listener);\r
+\r
+ } else {\r
+\r
+ for(Widget widget : widgets) {\r
+ widget.setInput(context, input);\r
+ for (Listener listener : listeners)\r
+ listener.execute(input);\r
+ }\r
+\r
+ }\r
+ }\r
+\r
+}\r