]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
35f0299ff9004320b2a9121184e6e17e2a0da593
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 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.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             // History data\r
250             if (backend.isInstanceOf(r, sr.HistoryDataset))\r
251                 return Collections.singleton(\r
252                         new ComparableTabContributor(\r
253                                 new HistoryDataTab(),\r
254                                 0,\r
255                                 r,\r
256                                 "History Data Properties"));\r
257             \r
258             // Saved simulation result\r
259             if (backend.isInstanceOf(r, sr.Result))\r
260                 return Collections.singleton(\r
261                         new ComparableTabContributor(\r
262                                 new ResultTab(),\r
263                                 0,\r
264                                 r,\r
265                                 "Result Properties"));\r
266 \r
267             // Dependency\r
268             if (backend.isInstanceOf(r, sr.Dependency))\r
269                 if (backend.hasStatement(r, sr.Dependency_refersTo))\r
270                     return Collections.singleton(\r
271                             new ComparableTabContributor(\r
272                                     new ReferenceDependencyTab(),\r
273                                     0,\r
274                                     r,\r
275                                     "Reference Properties"));\r
276                 else {\r
277                     Resource diaConnection = backend.getPossibleObject(r, ModelingResources.getInstance(backend).ConnectionToDiagramConnection);\r
278                     return Collections.singleton(\r
279                             new ComparableTabContributor(\r
280                                     new DependencyTab(),\r
281                                     0,\r
282                                     diaConnection,\r
283                                     "Dependency Properties"));\r
284                 }\r
285 \r
286             // Module symbol. Modules in modules-folder are actually symbol resources\r
287             if (backend.isInheritedFrom(r, sr.ModuleSymbol)) {\r
288                 // Find the component resource\r
289                 r =  backend.getPossibleObject(r, mr.SymbolToComponentType);\r
290                 if(r != null)\r
291                     return Collections.singleton(\r
292                             new ComparableTabContributor(\r
293                                     new ModuleTypeTab(),\r
294                                     0,\r
295                                     r,\r
296                                     "Module Type Properties"));\r
297             }\r
298 \r
299             // Function\r
300             if (backend.isInstanceOf(r, sr.SysdynModelicaFunction)) {\r
301                 tabs.add(new ComparableTabContributor(\r
302                         new FunctionTab(),\r
303                         2,\r
304                         r,\r
305                         "Function"));\r
306                 tabs.add(new ComparableTabContributor(\r
307                         new ExternalFilesTab(),\r
308                         1,\r
309                         r,\r
310                         "External files"));\r
311                 return tabs;\r
312             }\r
313 \r
314             // Function library\r
315             if (backend.isInstanceOf(r, sr.SysdynModelicaFunctionLibrary)) {\r
316                 Object s = AdaptionUtils.adaptToSingle(selection, ISelection.class);\r
317                 if(s == null)\r
318                     s = r;\r
319                 // give either variable or the actual resource\r
320                 return Collections.singleton(new ComparableTabContributor(\r
321                         new FunctionLibraryTab(),\r
322                         2,\r
323                         s,\r
324                         "Function library"));\r
325             }\r
326 \r
327             // Chart Element\r
328             if(backend.isInstanceOf(r, jfree.ChartElement)) {\r
329                 if(backend.hasStatement(r, jfree.ChartElement_component))\r
330                     r = backend.getSingleObject(r, jfree.ChartElement_component);\r
331             }\r
332             // Chart\r
333             if (backend.isInstanceOf(r, jfree.Chart)) {\r
334 \r
335                 Collection<Resource> plots = backend.syncRequest(new ObjectsWithType(r, Layer0.getInstance(backend).ConsistsOf, jfree.Plot));\r
336                 if(!plots.isEmpty()) {\r
337                     Resource plot = plots.iterator().next();\r
338 \r
339                     if(backend.isInstanceOf(plot, jfree.XYPlot)) {\r
340                         tabs.add(new ComparableTabContributor(\r
341                                 new XYLineGeneralPropertiesTab(),\r
342                                 10,\r
343                                 r,\r
344                                 "General"));\r
345                         tabs.add(new ComparableTabContributor(\r
346                                 new XYLineAxisAndVariablesTab(),\r
347                                 9,\r
348                                 r,\r
349                                 "Axis and Variables"));\r
350                     } else if(backend.isInstanceOf(plot, jfree.CategoryPlot)) {\r
351                         tabs.add(new ComparableTabContributor(\r
352                                 new BarGeneralPropertiesTab(),\r
353                                 10,\r
354                                 r,\r
355                                 "General"));\r
356                         tabs.add(new ComparableTabContributor(\r
357                                 new BarSeriesTab(),\r
358                                 9,\r
359                                 r,\r
360                                 "Variables"));\r
361                         tabs.add(new ComparableTabContributor(\r
362                                 new BarAxisTab(),\r
363                                 8,\r
364                                 r,\r
365                                 "Axis"));                        \r
366                     } else if(backend.isInstanceOf(plot, jfree.PiePlot)) {\r
367                         tabs.add(new ComparableTabContributor(\r
368                                 new PieGeneralPropertiesTab(),\r
369                                 10,\r
370                                 r,\r
371                                 "General"));\r
372                         tabs.add(new ComparableTabContributor(\r
373                                 new PieSeriesTab(),\r
374                                 9,\r
375                                 r,\r
376                                 "Variables"));\r
377                     }\r
378                     \r
379                     tabs.add(new ComparableTabContributor(\r
380                             new ChartTab(),\r
381                             1,\r
382                             r,\r
383                             "Chart"));\r
384                     return tabs;\r
385 \r
386                 } \r
387             }\r
388 \r
389         } catch (ServiceException e) {\r
390             e.printStackTrace();\r
391         } catch (ManyObjectsForFunctionalRelationException e) {\r
392             e.printStackTrace();\r
393         } catch (DatabaseException e) {\r
394             e.printStackTrace();\r
395         }\r
396         return Collections.emptyList();\r
397     }\r
398 }\r