1 /*******************************************************************************
\r
2 * Copyright (c) 2010, 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.properties;
\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
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
47 * SelectionProcessor for processing selections for property view in system dynamics
\r
49 * @author Teemu Lempinen
\r
52 public class ResourceSelectionProcessor implements SelectionProcessor<Object, ReadGraph> {
\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
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
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
78 tabs.add(new ComparableTabContributor(
\r
79 new ArrayIndexesTab(),
\r
81 independentVariables,
\r
89 Resource r = AdaptionUtils.adaptToSingle(selection, Resource.class);
\r
92 // SharedFunctionsFolder has properties but no adapted resource
\r
93 SharedFunctionsFolder sff = AdaptionUtils.adaptToSingle(selection, SharedFunctionsFolder.class);
\r
95 return Collections.singleton(new ComparableTabContributor(
\r
96 new SharedFunctionLibrariesTab(),
\r
99 "Shared Functions"));
\r
102 return Collections.emptyList();
\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
112 Resource connection = backend.getPossibleObject(r, mr.DiagramConnectionToConnection);
\r
113 if(connection != null)
\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
133 expression = expressionList.get(0);
\r
135 tabs.add(new ComparableTabContributor(
\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
149 tabs.add(new ComparableTabContributor(
\r
150 new ArrayIndexesTab(),
\r
155 tabs.add(new ComparableTabContributor(
\r
156 new VariableInformationTab(),
\r
159 "Additional Information"));
\r
164 if (backend.isInstanceOf(r, sr.Input)) {
\r
165 tabs.add(new ComparableTabContributor(
\r
166 new InputVariableTab(),
\r
171 tabs.add(new ComparableTabContributor(
\r
172 new ArrayIndexesTab(),
\r
177 tabs.add(new ComparableTabContributor(
\r
178 new VariableInformationTab(),
\r
181 "Additional Information"));
\r
186 if (backend.isInstanceOf(r, sr.Enumeration)) {
\r
187 Object s = AdaptionUtils.adaptToSingle(selection, ISelection.class);
\r
190 // give either variable or the actual resource
\r
191 return Collections.singleton(new ComparableTabContributor(
\r
192 new EnumerationTab(),
\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
203 return Collections.singleton(
\r
204 new ComparableTabContributor(
\r
205 new ConfigurationTab(),
\r
208 "Model Properties"));
\r
212 if (backend.isInstanceOf(r, sr.Module)){
\r
213 tabs.add(new ComparableTabContributor(
\r
217 "Module Properties"));
\r
218 tabs.add(new ComparableTabContributor(
\r
219 new ModuleInputTab(),
\r
223 tabs.add(new ComparableTabContributor(
\r
224 new ModuleOutputTab(),
\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
238 "Experiment Properties"));
\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
247 "Experiment Properties"));
\r
250 if (backend.isInstanceOf(r, sr.HistoryDataset))
\r
251 return Collections.singleton(
\r
252 new ComparableTabContributor(
\r
253 new HistoryDataTab(),
\r
256 "History Data Properties"));
\r
258 // Saved simulation result
\r
259 if (backend.isInstanceOf(r, sr.Result))
\r
260 return Collections.singleton(
\r
261 new ComparableTabContributor(
\r
265 "Result Properties"));
\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
275 "Reference Properties"));
\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
283 "Dependency Properties"));
\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
291 return Collections.singleton(
\r
292 new ComparableTabContributor(
\r
293 new ModuleTypeTab(),
\r
296 "Module Type Properties"));
\r
300 if (backend.isInstanceOf(r, sr.SysdynModelicaFunction)) {
\r
301 tabs.add(new ComparableTabContributor(
\r
306 tabs.add(new ComparableTabContributor(
\r
307 new ExternalFilesTab(),
\r
310 "External files"));
\r
314 // Function library
\r
315 if (backend.isInstanceOf(r, sr.SysdynModelicaFunctionLibrary)) {
\r
316 Object s = AdaptionUtils.adaptToSingle(selection, ISelection.class);
\r
319 // give either variable or the actual resource
\r
320 return Collections.singleton(new ComparableTabContributor(
\r
321 new FunctionLibraryTab(),
\r
324 "Function library"));
\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
333 if (backend.isInstanceOf(r, jfree.Chart)) {
\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
339 if(backend.isInstanceOf(plot, jfree.XYPlot)) {
\r
340 tabs.add(new ComparableTabContributor(
\r
341 new XYLineGeneralPropertiesTab(),
\r
345 tabs.add(new ComparableTabContributor(
\r
346 new XYLineAxisAndVariablesTab(),
\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
356 tabs.add(new ComparableTabContributor(
\r
357 new BarSeriesTab(),
\r
361 tabs.add(new ComparableTabContributor(
\r
366 } else if(backend.isInstanceOf(plot, jfree.PiePlot)) {
\r
367 tabs.add(new ComparableTabContributor(
\r
368 new PieGeneralPropertiesTab(),
\r
372 tabs.add(new ComparableTabContributor(
\r
373 new PieSeriesTab(),
\r
379 tabs.add(new ComparableTabContributor(
\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
396 return Collections.emptyList();
\r