]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
d26e366817b21fdf1960ef101ed46b5bc3863cbf
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 2011 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.sysdyn.ui.properties;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.Collections;\r
17 import java.util.List;\r
18 \r
19 import org.eclipse.jface.viewers.ISelection;\r
20 import org.simantics.db.ReadGraph;\r
21 import org.simantics.db.Resource;\r
22 import org.simantics.db.common.request.ObjectsWithType;\r
23 import org.simantics.db.common.utils.OrderedSetUtils;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;\r
26 import org.simantics.db.exception.ServiceException;\r
27 import org.simantics.diagram.stubs.DiagramResource;\r
28 import org.simantics.layer0.Layer0;\r
29 import org.simantics.modeling.ModelingResources;\r
30 import org.simantics.selectionview.ComparableTabContributor;\r
31 import org.simantics.selectionview.SelectionProcessor;\r
32 import org.simantics.simulation.ontology.SimulationResource;\r
33 import org.simantics.sysdyn.JFreeChartResource;\r
34 import org.simantics.sysdyn.SysdynResource;\r
35 import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionsFolder;\r
36 import org.simantics.sysdyn.ui.trend.chart.properties.ChartTab;\r
37 import org.simantics.sysdyn.ui.trend.chart.properties.bar.BarAxisTab;\r
38 import org.simantics.sysdyn.ui.trend.chart.properties.bar.BarGeneralPropertiesTab;\r
39 import org.simantics.sysdyn.ui.trend.chart.properties.bar.BarSeriesTab;\r
40 import org.simantics.sysdyn.ui.trend.chart.properties.pie.PieGeneralPropertiesTab;\r
41 import org.simantics.sysdyn.ui.trend.chart.properties.pie.PieSeriesTab;\r
42 import org.simantics.sysdyn.ui.trend.chart.properties.xyline.XYLineAxisAndVariablesTab;\r
43 import org.simantics.sysdyn.ui.trend.chart.properties.xyline.XYLineGeneralPropertiesTab;\r
44 import org.simantics.ui.utils.AdaptionUtils;\r
45 \r
46 /**\r
47  * SelectionProcessor for processing selections for property view in system dynamics\r
48  * \r
49  * @author Teemu Lempinen\r
50  *\r
51  */\r
52 public class ResourceSelectionProcessor implements SelectionProcessor<Object, ReadGraph>  {\r
53 \r
54     @Override\r
55     public Collection<?> process(Object selection, ReadGraph backend) {\r
56         Collection<ComparableTabContributor> tabs = new ArrayList<ComparableTabContributor>();\r
57         SysdynResource sr = SysdynResource.getInstance(backend);\r
58         DiagramResource dr = DiagramResource.getInstance(backend);\r
59         ModelingResources mr = ModelingResources.getInstance(backend);\r
60         SimulationResource simu = SimulationResource.getInstance(backend);\r
61         JFreeChartResource jfree = JFreeChartResource.getInstance(backend);\r
62 \r
63         try {\r
64             // Many elements \r
65             if (selection instanceof ArrayList<?> && ((ArrayList<?>) selection).size() > 1) {\r
66                 List<Resource> independentVariables = new ArrayList<Resource>();\r
67                 Resource model = null;\r
68                 for(Object o : (ArrayList<?>)selection) {\r
69                     Resource r = AdaptionUtils.adaptToSingle(o, Resource.class);\r
70                     if(r != null && backend.isInstanceOf(r, sr.IndependentVariable)) {\r
71                         if(model == null)\r
72                             model = backend.getSingleObject(r, Layer0.getInstance(backend).PartOf);\r
73                         if(model.equals( backend.getSingleObject(r, Layer0.getInstance(backend).PartOf)))\r
74                             independentVariables.add(r);\r
75                     }\r
76                 }\r
77 \r
78                 tabs.add(new ComparableTabContributor(\r
79                         new ArrayIndexesTab(),\r
80                         1,\r
81                         independentVariables,\r
82                         "Indexes"));\r
83 \r
84                 return tabs;\r
85             }\r
86 \r
87 \r
88             // Single element\r
89             Resource r = AdaptionUtils.adaptToSingle(selection, Resource.class);\r
90 \r
91             if(r == null) {\r
92                 // SharedFunctionsFolder has properties but no adapted resource\r
93                 SharedFunctionsFolder sff = AdaptionUtils.adaptToSingle(selection, SharedFunctionsFolder.class);\r
94                 if (sff != null) {\r
95                     return Collections.singleton(new ComparableTabContributor(\r
96                             new SharedFunctionLibrariesTab(),\r
97                             2,\r
98                             sff.data,\r
99                             "Shared Functions"));\r
100                 }\r
101 \r
102                 return Collections.emptyList();\r
103             }\r
104 \r
105 \r
106             // if r == diagram element, change it to component\r
107             if (backend.isInstanceOf(r, dr.Element)) {\r
108                 Resource component = backend.getPossibleObject(r, mr.ElementToComponent);\r
109                 if (component != null) {\r
110                     r = component;\r
111                 } else {\r
112                     Resource connection = backend.getPossibleObject(r, mr.DiagramConnectionToConnection);\r
113                     if(connection != null)\r
114                         r = connection;\r
115                 }\r
116             }\r
117 \r
118             // Independent variable\r
119             if (backend.isInstanceOf(r, sr.IndependentVariable)) {\r
120                 Resource activeExpression = backend.getPossibleObject(r, sr.IndependentVariable_activeExpression);\r
121                 Resource expression = null;\r
122                 if(activeExpression != null)\r
123                     // if variable has active expression, display it\r
124                     expression = activeExpression;\r
125                 else if (backend.hasStatement(r, sr.Variable_expressions)){\r
126                     // else display the first expression of the variable\r
127                     Resource expressions = backend.getPossibleObject(r, sr.Variable_expressions);\r
128                     List<Resource> expressionList = OrderedSetUtils.toList(backend, expressions);\r
129                     if(expressionList.isEmpty()) {\r
130                         System.err.println("expressionList is empty for " + r);\r
131                         return Collections.emptyList();\r
132                     }\r
133                     expression = expressionList.get(0);\r
134                 }\r
135                 tabs.add(new ComparableTabContributor(\r
136                         new EquationTab(),\r
137                         3,\r
138                         r,\r
139                         "Equation"));\r
140                 if(expression != null && backend.isInstanceOf(expression, sr.WithLookupExpression)) {\r
141                     // WithLookupExpression has its own extra tab for visual configuration\r
142                     tabs.add(new ComparableTabContributor(\r
143                             new LookupTableTab(),\r
144                             2,\r
145                             expression,\r
146                             "Lookup Table"));\r
147                 }\r
148 \r
149                 tabs.add(new ComparableTabContributor(\r
150                         new ArrayIndexesTab(),\r
151                         1,\r
152                         r,\r
153                         "Indexes"));\r
154 \r
155                 tabs.add(new ComparableTabContributor(\r
156                         new VariableInformationTab(),\r
157                         0,\r
158                         r,\r
159                         "Additional Information"));\r
160                 return tabs;\r
161             }\r
162 \r
163             // Input variable\r
164             if (backend.isInstanceOf(r, sr.Input)) {\r
165                 tabs.add(new ComparableTabContributor(\r
166                         new InputVariableTab(),\r
167                         2,\r
168                         r,\r
169                         "Input"));\r
170 \r
171                 tabs.add(new ComparableTabContributor(\r
172                         new ArrayIndexesTab(),\r
173                         1,\r
174                         r,\r
175                         "Indexes"));\r
176 \r
177                 tabs.add(new ComparableTabContributor(\r
178                         new VariableInformationTab(),\r
179                         0,\r
180                         r,\r
181                         "Additional Information"));\r
182                 return tabs;\r
183             }\r
184 \r
185             // Enumeration\r
186             if (backend.isInstanceOf(r, sr.Enumeration)) {\r
187                 Object s = AdaptionUtils.adaptToSingle(selection, ISelection.class);\r
188                 if(s == null)\r
189                     s = r;\r
190                 // give either variable or the actual resource\r
191                 return Collections.singleton(new ComparableTabContributor(\r
192                         new EnumerationTab(),\r
193                         2,\r
194                         s,\r
195                         "Enumeration"));\r
196             }\r
197 \r
198             // Configuration and model. They both show the properties of the configuration\r
199             if ( backend.isInstanceOf(r, sr.Configuration) || backend.isInstanceOf(r, sr.SysdynModel)) {\r
200                 if(!backend.isInstanceOf(r, sr.SysdynModel))\r
201                     r = backend.getPossibleObject(r, SimulationResource.getInstance(backend).IsConfigurationOf);\r
202                 if (r != null)\r
203                     return Collections.singleton(\r
204                             new ComparableTabContributor(\r
205                                     new ConfigurationTab(),\r
206                                     0,\r
207                                     r,\r
208                                     "Model Properties"));\r
209             }\r
210 \r
211             // Module\r
212             if (backend.isInstanceOf(r, sr.Module)){\r
213                 tabs.add(new ComparableTabContributor(\r
214                         new ModuleTab(),\r
215                         3,\r
216                         r,\r
217                         "Module Properties"));\r
218                 tabs.add(new ComparableTabContributor(\r
219                         new ModuleInputTab(),\r
220                         2,\r
221                         r,\r
222                         "Inputs"));\r
223                 tabs.add(new ComparableTabContributor(\r
224                         new ModuleOutputTab(),\r
225                         1,\r
226                         r,\r
227                         "Outputs"));\r
228                 return tabs;\r
229             }\r
230 \r
231             // Playback experiment\r
232             if (backend.isInstanceOf(r, sr.PlaybackExperiment))\r
233                 return Collections.singleton(\r
234                         new ComparableTabContributor(\r
235                                 new PlaybackExperimentTab(),\r
236                                 0,\r
237                                 r,\r
238                                 "Experiment Properties"));\r
239 \r
240             // Default experiment\r
241             if (backend.isInstanceOf(r, simu.Experiment))\r
242                 return Collections.singleton(\r
243                         new ComparableTabContributor(\r
244                                 new ExperimentTab(),\r
245                                 0,\r
246                                 r,\r
247                                 "Experiment Properties"));\r
248 \r
249             // Saved simulation result\r
250             if (backend.isInstanceOf(r, sr.Result))\r
251                 return Collections.singleton(\r
252                         new ComparableTabContributor(\r
253                                 new ResultTab(),\r
254                                 0,\r
255                                 r,\r
256                                 "Result Properties"));\r
257 \r
258             // Dependency\r
259             if (backend.isInstanceOf(r, sr.Dependency))\r
260                 if (backend.hasStatement(r, sr.Dependency_refersTo))\r
261                     return Collections.singleton(\r
262                             new ComparableTabContributor(\r
263                                     new ReferenceDependencyTab(),\r
264                                     0,\r
265                                     r,\r
266                                     "Reference Properties"));\r
267                 else {\r
268                     Resource diaConnection = backend.getPossibleObject(r, ModelingResources.getInstance(backend).ConnectionToDiagramConnection);\r
269                     return Collections.singleton(\r
270                             new ComparableTabContributor(\r
271                                     new DependencyTab(),\r
272                                     0,\r
273                                     diaConnection,\r
274                                     "Dependency Properties"));\r
275                 }\r
276 \r
277             // Module symbol. Modules in modules-folder are actually symbol resources\r
278             if (backend.isInheritedFrom(r, sr.ModuleSymbol)) {\r
279                 // Find the component resource\r
280                 r =  backend.getPossibleObject(r, mr.SymbolToComponentType);\r
281                 if(r != null)\r
282                     return Collections.singleton(\r
283                             new ComparableTabContributor(\r
284                                     new ModuleTypeTab(),\r
285                                     0,\r
286                                     r,\r
287                                     "Module Type Properties"));\r
288             }\r
289 \r
290             // Function\r
291             if (backend.isInstanceOf(r, sr.SysdynModelicaFunction)) {\r
292                 tabs.add(new ComparableTabContributor(\r
293                         new FunctionTab(),\r
294                         2,\r
295                         r,\r
296                         "Function"));\r
297                 tabs.add(new ComparableTabContributor(\r
298                         new ExternalFilesTab(),\r
299                         1,\r
300                         r,\r
301                         "External files"));\r
302                 return tabs;\r
303             }\r
304 \r
305             // Function library\r
306             if (backend.isInstanceOf(r, sr.SysdynModelicaFunctionLibrary)) {\r
307                 Object s = AdaptionUtils.adaptToSingle(selection, ISelection.class);\r
308                 if(s == null)\r
309                     s = r;\r
310                 // give either variable or the actual resource\r
311                 return Collections.singleton(new ComparableTabContributor(\r
312                         new FunctionLibraryTab(),\r
313                         2,\r
314                         s,\r
315                         "Function library"));\r
316             }\r
317 \r
318             // Chart Element\r
319             if(backend.isInstanceOf(r, jfree.ChartElement)) {\r
320                 if(backend.hasStatement(r, jfree.ChartElement_component))\r
321                     r = backend.getSingleObject(r, jfree.ChartElement_component);\r
322             }\r
323             // Chart\r
324             if (backend.isInstanceOf(r, jfree.Chart)) {\r
325 \r
326                 Collection<Resource> plots = backend.syncRequest(new ObjectsWithType(r, Layer0.getInstance(backend).ConsistsOf, jfree.Plot));\r
327                 if(!plots.isEmpty()) {\r
328                     Resource plot = plots.iterator().next();\r
329 \r
330                     if(backend.isInstanceOf(plot, jfree.XYPlot)) {\r
331                         tabs.add(new ComparableTabContributor(\r
332                                 new XYLineGeneralPropertiesTab(),\r
333                                 10,\r
334                                 r,\r
335                                 "General"));\r
336                         tabs.add(new ComparableTabContributor(\r
337                                 new XYLineAxisAndVariablesTab(),\r
338                                 9,\r
339                                 r,\r
340                                 "Axis and Variables"));\r
341                     } else if(backend.isInstanceOf(plot, jfree.CategoryPlot)) {\r
342                         tabs.add(new ComparableTabContributor(\r
343                                 new BarGeneralPropertiesTab(),\r
344                                 10,\r
345                                 r,\r
346                                 "General"));\r
347                         tabs.add(new ComparableTabContributor(\r
348                                 new BarSeriesTab(),\r
349                                 9,\r
350                                 r,\r
351                                 "Variables"));\r
352                         tabs.add(new ComparableTabContributor(\r
353                                 new BarAxisTab(),\r
354                                 8,\r
355                                 r,\r
356                                 "Axis"));                        \r
357                     } else if(backend.isInstanceOf(plot, jfree.PiePlot)) {\r
358                         tabs.add(new ComparableTabContributor(\r
359                                 new PieGeneralPropertiesTab(),\r
360                                 10,\r
361                                 r,\r
362                                 "General"));\r
363                         tabs.add(new ComparableTabContributor(\r
364                                 new PieSeriesTab(),\r
365                                 9,\r
366                                 r,\r
367                                 "Variables"));\r
368                     }\r
369                     \r
370                     tabs.add(new ComparableTabContributor(\r
371                             new ChartTab(),\r
372                             1,\r
373                             r,\r
374                             "Chart"));\r
375                     return tabs;\r
376 \r
377                 } \r
378             }\r
379 \r
380         } catch (ServiceException e) {\r
381             e.printStackTrace();\r
382         } catch (ManyObjectsForFunctionalRelationException e) {\r
383             e.printStackTrace();\r
384         } catch (DatabaseException e) {\r
385             e.printStackTrace();\r
386         }\r
387         return Collections.emptyList();\r
388     }\r
389 }\r