1 /*******************************************************************************
\r
2 * Copyright (c) 2010, 2011 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
249 // Saved simulation result
\r
250 if (backend.isInstanceOf(r, sr.Result))
\r
251 return Collections.singleton(
\r
252 new ComparableTabContributor(
\r
256 "Result Properties"));
\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
266 "Reference Properties"));
\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
274 "Dependency Properties"));
\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
282 return Collections.singleton(
\r
283 new ComparableTabContributor(
\r
284 new ModuleTypeTab(),
\r
287 "Module Type Properties"));
\r
291 if (backend.isInstanceOf(r, sr.SysdynModelicaFunction)) {
\r
292 tabs.add(new ComparableTabContributor(
\r
297 tabs.add(new ComparableTabContributor(
\r
298 new ExternalFilesTab(),
\r
301 "External files"));
\r
305 // Function library
\r
306 if (backend.isInstanceOf(r, sr.SysdynModelicaFunctionLibrary)) {
\r
307 Object s = AdaptionUtils.adaptToSingle(selection, ISelection.class);
\r
310 // give either variable or the actual resource
\r
311 return Collections.singleton(new ComparableTabContributor(
\r
312 new FunctionLibraryTab(),
\r
315 "Function library"));
\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
324 if (backend.isInstanceOf(r, jfree.Chart)) {
\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
330 if(backend.isInstanceOf(plot, jfree.XYPlot)) {
\r
331 tabs.add(new ComparableTabContributor(
\r
332 new XYLineGeneralPropertiesTab(),
\r
336 tabs.add(new ComparableTabContributor(
\r
337 new XYLineAxisAndVariablesTab(),
\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
347 tabs.add(new ComparableTabContributor(
\r
348 new BarSeriesTab(),
\r
352 tabs.add(new ComparableTabContributor(
\r
357 } else if(backend.isInstanceOf(plot, jfree.PiePlot)) {
\r
358 tabs.add(new ComparableTabContributor(
\r
359 new PieGeneralPropertiesTab(),
\r
363 tabs.add(new ComparableTabContributor(
\r
364 new PieSeriesTab(),
\r
370 tabs.add(new ComparableTabContributor(
\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
387 return Collections.emptyList();
\r