1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 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.browser.actions.drop;
\r
14 import java.util.Iterator;
\r
16 import org.eclipse.jface.viewers.IStructuredSelection;
\r
17 import org.simantics.db.ReadGraph;
\r
18 import org.simantics.db.Resource;
\r
19 import org.simantics.db.WriteGraph;
\r
20 import org.simantics.db.common.request.PossibleObjectWithType;
\r
21 import org.simantics.db.common.request.SingleObjectWithType;
\r
22 import org.simantics.db.common.request.WriteRequest;
\r
23 import org.simantics.db.common.utils.ListUtils;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.simantics.db.layer0.adapter.DropActionFactory;
\r
26 import org.simantics.db.layer0.variable.Variable;
\r
27 import org.simantics.db.layer0.variable.Variables;
\r
28 import org.simantics.jfreechart.chart.ChartUtils;
\r
29 import org.simantics.layer0.Layer0;
\r
30 import org.simantics.sysdyn.JFreeChartResource;
\r
31 import org.simantics.sysdyn.SysdynResource;
\r
32 import org.simantics.ui.SimanticsUI;
\r
33 import org.simantics.ui.utils.AdaptionUtils;
\r
36 * Action for droppin variables to charts
\r
37 * @author Teemu Lempinen
\r
40 public class ChartDropAction implements DropActionFactory {
\r
43 public Runnable create(ReadGraph g, Object target, Object source) throws DatabaseException {
\r
45 final Resource targetChart = AdaptionUtils.adaptToSingle(target, Resource.class);
\r
46 if(targetChart == null || source == null || !(source instanceof IStructuredSelection))
\r
49 final IStructuredSelection selection = (IStructuredSelection) source;
\r
51 return new Runnable() {
\r
55 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
58 public void perform(WriteGraph graph) throws DatabaseException {
\r
59 SysdynResource sr = SysdynResource.getInstance(graph);
\r
60 Layer0 L0 = Layer0.getInstance(graph);
\r
61 JFreeChartResource JFREE = JFreeChartResource.getInstance(graph);
\r
63 Iterator<?> iterator = selection.iterator();
\r
65 // Run through all selections and add all IndependentVariables and Inputs to the target chart
\r
66 while(iterator.hasNext()) {
\r
68 Variable variable = AdaptionUtils.adaptToSingle(iterator.next(), Variable.class);
\r
69 if(variable == null)
\r
72 Resource represents = (Resource)variable.getPropertyValue(graph, Variables.REPRESENTS);
\r
73 if(represents == null ||
\r
74 !(graph.isInstanceOf(represents, sr.IndependentVariable) ||
\r
75 graph.isInstanceOf(represents, sr.Input)))
\r
78 Resource plot = graph.syncRequest(new PossibleObjectWithType(targetChart, L0.ConsistsOf, JFREE.Plot));
\r
80 if(graph.isInstanceOf(plot, JFREE.XYPlot)) {
\r
81 dropToLineChart(graph, targetChart, variable);
\r
82 } else if(graph.isInstanceOf(plot, JFREE.CategoryPlot)) {
\r
83 dropToBarChart(graph, targetChart, variable);
\r
84 } else if(graph.isInstanceOf(plot, JFREE.PiePlot)) {
\r
85 dropToPieChart(graph, targetChart, variable);
\r
98 * Drop variable to a pie chart
\r
99 * @param graph ReadGraph
\r
100 * @param pieChart Pie chart resource
\r
101 * @param variable Dropped variable
\r
102 * @throws DatabaseException
\r
104 private void dropToPieChart(WriteGraph graph, Resource pieChart, Variable variable) throws DatabaseException {
\r
105 Layer0 l0 = Layer0.getInstance(graph);
\r
106 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
108 Resource plot = graph.syncRequest(new SingleObjectWithType(pieChart, l0.ConsistsOf, jfree.Plot));
\r
112 Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.Dataset));
\r
114 if(dataset == null)
\r
117 // Create the series and attach it to the dataset
\r
118 String rvi = Variables.getRVI(graph, variable);
\r
119 Resource series = ChartUtils.createSeries(graph, dataset, rvi);
\r
120 graph.claimLiteral(series, jfree.Series_exploded, false);
\r
124 * Drop variable to a bar chart
\r
125 * @param graph ReadGraph
\r
126 * @param barChart Bar chart resource
\r
127 * @param variable Dropped variable
\r
128 * @throws DatabaseException
\r
130 private void dropToBarChart(WriteGraph graph, Resource barChart, Variable variable) throws DatabaseException {
\r
131 Layer0 l0 = Layer0.getInstance(graph);
\r
132 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
134 Resource plot = graph.syncRequest(new SingleObjectWithType(barChart, l0.ConsistsOf, jfree.Plot));
\r
138 Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.Dataset));
\r
140 if(dataset == null)
\r
143 // Create the series and attach it to the dataset
\r
144 String rvi = Variables.getRVI(graph, variable);
\r
145 ChartUtils.createSeries(graph, dataset, rvi);
\r
149 * Drop variable to a line chart
\r
150 * @param graph ReadGraph
\r
151 * @param lineChart Line chart resource
\r
152 * @param variable Dropped variable
\r
153 * @throws DatabaseException
\r
155 private void dropToLineChart(WriteGraph graph, Resource lineChart, Variable variable) throws DatabaseException {
\r
156 Layer0 l0 = Layer0.getInstance(graph);
\r
157 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
159 Resource plot = graph.syncRequest(new SingleObjectWithType(lineChart, l0.ConsistsOf, jfree.Plot));
\r
163 Resource rangeAxis = null;
\r
164 Resource dataset = null;
\r
165 Resource rangeAxisList = graph.getPossibleObject(plot, jfree.Plot_rangeAxisList);
\r
166 if(rangeAxisList == null || ListUtils.toList(graph, rangeAxisList).isEmpty()) {
\r
167 // No range axis -> Create a new one
\r
168 rangeAxis = ChartUtils.createNumberRangeAxis(graph, plot);
\r
169 Resource domainAxis = graph.getPossibleObject(plot, jfree.Plot_domainAxis);
\r
170 dataset = ChartUtils.createXYDataset(graph, plot, domainAxis, rangeAxis);
\r
172 rangeAxis = ListUtils.toList(graph, rangeAxisList).get(0);
\r
173 dataset = graph.getPossibleObject(rangeAxis, jfree.Dataset_mapToRangeAxis_Inverse);
\r
176 // Create the series and attach it to the dataset
\r
177 String rvi = Variables.getRVI(graph, variable);
\r
178 ChartUtils.createSeries(graph, dataset, rvi);
\r