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