]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
2e16bcece73701b68219bf8f8daf26ce3c41614a
[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.newActions;\r
13 \r
14 import java.util.Collections;\r
15 import java.util.UUID;\r
16 \r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.WriteGraph;\r
19 import org.simantics.db.common.request.WriteRequest;\r
20 import org.simantics.db.common.utils.ListUtils;\r
21 import org.simantics.db.common.utils.NameUtils;\r
22 import org.simantics.db.exception.DatabaseException;\r
23 import org.simantics.db.layer0.adapter.ActionFactory;\r
24 import org.simantics.diagram.stubs.G2DResource;\r
25 import org.simantics.layer0.Layer0;\r
26 import org.simantics.layer0.utils.direct.GraphUtils;\r
27 import org.simantics.sysdyn.JFreeChartResource;\r
28 import org.simantics.ui.SimanticsUI;\r
29 \r
30 /**\r
31  * Creates a new bar chart to a model\r
32  * @author Teemu Lempinen\r
33  *\r
34  */\r
35 public class NewBarChartAction implements ActionFactory{\r
36 \r
37     @Override\r
38     public Runnable create(Object target) {\r
39         if(!(target instanceof Resource))\r
40             return null;\r
41         final Resource model = (Resource)target;\r
42 \r
43         return new Runnable() {\r
44             @Override\r
45             public void run() {\r
46 \r
47                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
48 \r
49                     @Override\r
50                     public void perform(WriteGraph graph) throws DatabaseException {\r
51                         Layer0 l0 = Layer0.getInstance(graph);\r
52                         JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
53                         G2DResource g2d = G2DResource.getInstance(graph);\r
54 \r
55                         // Chart\r
56                         Resource jfreechart = GraphUtils.create2(graph, jfree.Chart,\r
57                                 l0.HasName, "BarChart" + UUID.randomUUID().toString(),\r
58                                 l0.HasLabel, NameUtils.findFreshLabel(graph, "Bar Chart", model),\r
59                                 l0.PartOf, model,\r
60                                 jfree.Chart_visibleBorder, true,\r
61                                 jfree.Chart_borderWidth, 3,\r
62                                 jfree.Chart_visibleLegend, false\r
63                                 );\r
64                         \r
65                         // Border color\r
66                         graph.claimLiteral(jfreechart, jfree.Chart_borderColor, g2d.Color, new float[] {0,0,0,1});\r
67 \r
68                         // Title\r
69                         GraphUtils.create2(graph, jfree.TextTitle,\r
70                                 l0.HasName, "TextTitle" + UUID.randomUUID().toString(),\r
71                                 l0.HasLabel, "Bar Chart Title",\r
72                                 jfree.Title_position, jfree.Top,\r
73                                 l0.PartOf, jfreechart);\r
74                         \r
75                         // X-axis\r
76                         Resource domainAxis = GraphUtils.create2(graph, jfree.CategoryAxis,\r
77                                 l0.HasName, "CategoryAxis" + UUID.randomUUID().toString());\r
78                         \r
79                         // Y-axis\r
80                         Resource rangeAxis = GraphUtils.create2(graph, jfree.NumberAxis,\r
81                                 l0.HasName, "NumberAxis" + UUID.randomUUID().toString());\r
82 \r
83                         // Renderer\r
84                         Resource renderer = GraphUtils.create2(graph, jfree.BarRenderer);\r
85                         \r
86                         // Dataset \r
87                         Resource dataset = GraphUtils.create2(graph, jfree.CategoryDataset,\r
88                                 l0.HasName, "CategoryDataset" + UUID.randomUUID().toString(),\r
89                                 jfree.Dataset_mapToDomainAxis, domainAxis,\r
90                                 jfree.Dataset_mapToRangeAxis, rangeAxis,\r
91                                 jfree.Dataset_seriesList, ListUtils.create(graph, Collections.<Resource>emptyList()),\r
92                                 jfree.Dataset_renderer, renderer);\r
93 \r
94                         // Plot\r
95                         GraphUtils.create2(graph, jfree.CategoryPlot,\r
96                                 l0.HasName, "Category plot" + UUID.randomUUID().toString(),\r
97                                 l0.PartOf, jfreechart,\r
98                                 jfree.Plot_domainAxis, domainAxis,\r
99                                 jfree.Plot_rangeAxis, rangeAxis,\r
100                                 jfree.Plot_rangeAxisList, ListUtils.create(graph, Collections.singletonList(rangeAxis)),\r
101                                 l0.ConsistsOf, dataset,\r
102                                 l0.ConsistsOf, domainAxis,\r
103                                 l0.ConsistsOf, rangeAxis);\r
104                     }\r
105 \r
106                 });\r
107             }\r
108         };\r
109     }\r
110 \r
111 }\r