]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
72d6a34d76165b49b7dc34f1fcb22edabb1bd840
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 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.browser.actions.drop;\r
13 \r
14 import java.util.Iterator;\r
15 \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.utils.ui.AdaptionUtils;\r
34 \r
35 /**\r
36  * Action for droppin variables to charts\r
37  * @author Teemu Lempinen\r
38  *\r
39  */\r
40 public class ChartDropAction implements DropActionFactory {\r
41 \r
42     @Override\r
43     public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException {\r
44 \r
45         final Resource targetChart = AdaptionUtils.adaptToSingle(target, Resource.class);\r
46         if(targetChart == null || source == null || !(source instanceof IStructuredSelection))\r
47             return null;\r
48         \r
49         final IStructuredSelection selection = (IStructuredSelection) source;\r
50 \r
51         return new Runnable() {\r
52 \r
53             @Override\r
54             public void run() {\r
55                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
56 \r
57                     @Override\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
62 \r
63                         Iterator<?> iterator = selection.iterator();\r
64                         \r
65                         // Run through all selections and add all IndependentVariables and Inputs to the target chart\r
66                         while(iterator.hasNext()) {\r
67 \r
68                             Variable variable = AdaptionUtils.adaptToSingle(iterator.next(), Variable.class);\r
69                             if(variable == null)\r
70                                 continue;\r
71 \r
72                             Resource represents = (Resource)variable.getRepresents(graph);\r
73                             if(represents == null || \r
74                                     !(graph.isInstanceOf(represents, sr.IndependentVariable) || \r
75                                             graph.isInstanceOf(represents, sr.Input)))\r
76                                 continue;\r
77 \r
78                             Resource plot = graph.syncRequest(new PossibleObjectWithType(targetChart, L0.ConsistsOf, JFREE.Plot));\r
79                             if(plot != null) {\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
86                                 }\r
87                             }\r
88                         }\r
89 \r
90                     }\r
91                 });\r
92             }\r
93         };\r
94     }\r
95 \r
96 \r
97     /**\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
103      */\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
107 \r
108         Resource plot = graph.syncRequest(new SingleObjectWithType(pieChart, l0.ConsistsOf, jfree.Plot));\r
109         if(plot == null)\r
110             return;\r
111 \r
112         Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.Dataset));\r
113 \r
114         if(dataset == null)\r
115             return;\r
116 \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
121     }\r
122 \r
123     /**\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
129      */\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
133 \r
134         Resource plot = graph.syncRequest(new SingleObjectWithType(barChart, l0.ConsistsOf, jfree.Plot));\r
135         if(plot == null)\r
136             return;\r
137 \r
138         Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.Dataset));\r
139 \r
140         if(dataset == null)\r
141             return;\r
142 \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
146     }\r
147 \r
148     /**\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
154      */\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
158 \r
159         Resource plot = graph.syncRequest(new SingleObjectWithType(lineChart, l0.ConsistsOf, jfree.Plot));\r
160         if(plot == null)\r
161             return;\r
162 \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
171         } else {\r
172             rangeAxis = ListUtils.toList(graph, rangeAxisList).get(0);\r
173             dataset = graph.getPossibleObject(rangeAxis, jfree.Dataset_mapToRangeAxis_Inverse);\r
174         }\r
175 \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
179     }\r
180 }\r