]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views.swt/src/org/simantics/views/swt/ModelledSupport.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.views.swt / src / org / simantics / views / swt / ModelledSupport.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.views.swt;
13
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;
40
41 /**
42  * @author Antti Villberg
43  */
44 public class ModelledSupport extends WidgetSupportImpl implements Disposable {
45
46     private final ModelledView view;
47     private InputListener listener;
48     private boolean disposed = false;
49     private Pair<Variable, Resource> previous = null;
50
51     /**
52      * @param modelledConfigurationComposite
53      */
54     public ModelledSupport(ModelledView view) {
55         this.view = view;
56     }
57
58     @Override
59     public void dispose() {
60         
61         disposed = true;
62         
63         if (listener != null)
64             listener.dispose();
65         
66     }
67
68     class InputListener implements AsyncListener<Pair<Variable, Resource>> {
69
70         private boolean disposed = false;
71
72         @Override
73         public boolean isDisposed() {
74             return this.disposed || ModelledSupport.this.disposed;
75         }
76
77         public void dispose() {
78             this.disposed = true;
79         }
80
81         @Override
82         public String toString() {
83             return "InputListener@ModelledSupport@" + view; 
84         }
85
86         @Override
87         public void execute(AsyncReadGraph graph, final Pair<Variable, Resource> result) {
88
89             if(previous != null && previous.equals(result)) return;
90             previous = result;
91
92             graph.getSession().async(new WriteRequest(Simantics.getSession().getService(VirtualGraph.class)) {
93
94                 @Override
95                 public void perform(WriteGraph graph) throws DatabaseException {
96
97                     ScenegraphResources SG = ScenegraphResources.getInstance(graph);
98
99                     Resource runtime = view.runtime;
100
101                     if(result.first != null) {
102                         String uri = result.first.getURI(graph);
103                         graph.claimLiteral(runtime, SG.Runtime_HasVariable, uri, Bindings.STRING);
104                     } else {
105                         graph.deny(runtime, SG.Runtime_HasVariable);
106                     }
107
108                     if(result.second != null) {
109                         graph.deny(runtime, SG.Runtime_HasResource);
110                         graph.claim(runtime, SG.Runtime_HasResource, result.second);
111                     } else {
112                         graph.deny(runtime, SG.Runtime_HasResource);
113                     }
114
115                     view.fireInput();
116
117                 }
118
119             });
120
121         }
122
123         @Override
124         public void exception(AsyncReadGraph graph, Throwable throwable) {
125             Logger.defaultLogError(throwable);
126         }
127
128     }
129
130     @SuppressWarnings({ "unchecked", "rawtypes" })
131     @Override
132     public void fireInput(final ISessionContext context, final Object input) {
133
134         lastInput = input;
135         lastContext = context;
136
137         if (listener != null)
138             listener.dispose();
139
140         if (context == null)
141             return;
142
143         Session session = context.peekSession();
144         if (session == null)
145             return;
146
147         listener = new InputListener();
148
149         if(view.runtime != null) {
150
151             session.async(new UniqueRead<Pair<Variable, Resource>>() {
152
153                 private Variable extractVariable(ReadGraph graph, ISelection selection) throws DatabaseException {
154
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);
158
159                 }
160
161                 private Resource extractResource(ReadGraph graph, ISelection selection) throws DatabaseException {
162
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);
166
167                 }
168
169                 @Override
170                 public Pair<Variable, Resource> perform(ReadGraph graph) throws DatabaseException {
171
172                     Variable resultVariable = null;
173                     Resource resultResource = null; 
174
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;
182                     }
183
184                     return Pair.make(resultVariable, resultResource);
185
186                 }
187
188             }, listener);
189
190         } else {
191
192             for(Widget widget : widgets) {
193                 widget.setInput(context, input);
194                 for (Listener listener : listeners)
195                     listener.execute(input);
196             }
197
198         }
199     }
200
201 }