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