1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.views.swt;
\r
14 import org.eclipse.jface.viewers.ISelection;
\r
15 import org.simantics.Simantics;
\r
16 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
\r
17 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;
\r
18 import org.simantics.databoard.Bindings;
\r
19 import org.simantics.db.AsyncReadGraph;
\r
20 import org.simantics.db.Disposable;
\r
21 import org.simantics.db.ReadGraph;
\r
22 import org.simantics.db.Resource;
\r
23 import org.simantics.db.Session;
\r
24 import org.simantics.db.VirtualGraph;
\r
25 import org.simantics.db.WriteGraph;
\r
26 import org.simantics.db.common.request.UniqueRead;
\r
27 import org.simantics.db.common.request.WriteRequest;
\r
28 import org.simantics.db.common.utils.Logger;
\r
29 import org.simantics.db.exception.DatabaseException;
\r
30 import org.simantics.db.layer0.variable.Variable;
\r
31 import org.simantics.db.management.ISessionContext;
\r
32 import org.simantics.db.procedure.AsyncListener;
\r
33 import org.simantics.db.procedure.Listener;
\r
34 import org.simantics.scenegraph.ontology.ScenegraphResources;
\r
35 import org.simantics.ui.selection.AnyResource;
\r
36 import org.simantics.ui.selection.AnyVariable;
\r
37 import org.simantics.ui.selection.WorkbenchSelectionElement;
\r
38 import org.simantics.utils.datastructures.Pair;
\r
39 import org.simantics.utils.ui.ISelectionUtils;
\r
42 * @author Antti Villberg
\r
44 public class ModelledSupport extends WidgetSupportImpl implements Disposable {
\r
46 private final ModelledView view;
\r
47 private InputListener listener;
\r
48 private boolean disposed = false;
\r
49 private Pair<Variable, Resource> previous = null;
\r
52 * @param modelledConfigurationComposite
\r
54 public ModelledSupport(ModelledView view) {
\r
59 public void dispose() {
\r
63 if (listener != null)
\r
68 class InputListener implements AsyncListener<Pair<Variable, Resource>> {
\r
70 private boolean disposed = false;
\r
73 public boolean isDisposed() {
\r
74 return this.disposed || ModelledSupport.this.disposed;
\r
77 public void dispose() {
\r
78 this.disposed = true;
\r
82 public String toString() {
\r
83 return "InputListener@ModelledSupport@" + view;
\r
87 public void execute(AsyncReadGraph graph, final Pair<Variable, Resource> result) {
\r
89 if(previous != null && previous.equals(result)) return;
\r
92 graph.getSession().async(new WriteRequest(Simantics.getSession().getService(VirtualGraph.class)) {
\r
95 public void perform(WriteGraph graph) throws DatabaseException {
\r
97 ScenegraphResources SG = ScenegraphResources.getInstance(graph);
\r
99 Resource runtime = view.runtime;
\r
101 if(result.first != null) {
\r
102 String uri = result.first.getURI(graph);
\r
103 graph.claimLiteral(runtime, SG.Runtime_HasVariable, uri, Bindings.STRING);
\r
105 graph.deny(runtime, SG.Runtime_HasVariable);
\r
108 if(result.second != null) {
\r
109 graph.deny(runtime, SG.Runtime_HasResource);
\r
110 graph.claim(runtime, SG.Runtime_HasResource, result.second);
\r
112 graph.deny(runtime, SG.Runtime_HasResource);
\r
124 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
125 Logger.defaultLogError(throwable);
\r
130 @SuppressWarnings({ "unchecked", "rawtypes" })
\r
132 public void fireInput(final ISessionContext context, final Object input) {
\r
135 lastContext = context;
\r
137 if (listener != null)
\r
138 listener.dispose();
\r
140 if (context == null)
\r
143 Session session = context.peekSession();
\r
144 if (session == null)
\r
147 listener = new InputListener();
\r
149 if(view.runtime != null) {
\r
151 session.async(new UniqueRead<Pair<Variable, Resource>>() {
\r
153 private Variable extractVariable(ReadGraph graph, ISelection selection) throws DatabaseException {
\r
155 WorkbenchSelectionElement single = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);
\r
156 if(single != null) return single.getContent(new AnyVariable(graph));
\r
157 return ISelectionUtils.filterSingleSelection((ISelection)lastInput, Variable.class);
\r
161 private Resource extractResource(ReadGraph graph, ISelection selection) throws DatabaseException {
\r
163 WorkbenchSelectionElement single = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);
\r
164 if(single != null) return single.getContent(new AnyResource(graph));
\r
165 return ISelectionUtils.filterSingleSelection((ISelection)lastInput, Resource.class);
\r
170 public Pair<Variable, Resource> perform(ReadGraph graph) throws DatabaseException {
\r
172 Variable resultVariable = null;
\r
173 Resource resultResource = null;
\r
175 if(lastInput instanceof ISelection) {
\r
176 Variable var = extractVariable(graph, (ISelection)lastInput);
\r
177 if(var != null) resultVariable = var;
\r
178 Resource res = extractResource(graph, (ISelection)lastInput);
\r
179 if(res != null) resultResource = res;
\r
180 } else if (lastInput instanceof Resource) {
\r
181 resultResource = (Resource)lastInput;
\r
184 return Pair.make(resultVariable, resultResource);
\r
192 for(Widget widget : widgets) {
\r
193 widget.setInput(context, input);
\r
194 for (Listener listener : listeners)
\r
195 listener.execute(input);
\r