]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
1ae83a6a41261dc740bc846418326bc704837425
[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 pie chart\r
32  * @author Teemu Lempinen\r
33  *\r
34  */\r
35 public class NewPieChartAction 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, "PieChart" + UUID.randomUUID().toString(),\r
58                                 l0.HasLabel, NameUtils.findFreshLabel(graph, "Pie Chart", model),\r
59                                 l0.PartOf, model,\r
60                                 jfree.Chart_visibleBorder, true,\r
61                                 jfree.Chart_borderWidth, 3);\r
62                         graph.claimLiteral(jfreechart, jfree.Chart_borderColor, g2d.Color, new float[] {0,0,0,1});\r
63 \r
64                         // Title\r
65                         GraphUtils.create2(graph, jfree.TextTitle,\r
66                                 l0.HasName, "TextTitle" + UUID.randomUUID().toString(),\r
67                                 l0.HasLabel, "Pie Chart Title",\r
68                                 jfree.Title_position, jfree.Top,\r
69                                 l0.PartOf, jfreechart);\r
70                         \r
71                         // Dataset\r
72                         Resource dataset = GraphUtils.create2(graph, jfree.PieDataset,\r
73                                 l0.HasName, "CategoryDataset" + UUID.randomUUID().toString(),\r
74                                 jfree.Dataset_seriesList, ListUtils.create(graph, Collections.<Resource>emptyList())\r
75                                 );\r
76 \r
77                         // Plot\r
78                         GraphUtils.create2(graph, jfree.PiePlot,\r
79                                 l0.HasName, "PiePlot" + UUID.randomUUID().toString(),\r
80                                 l0.PartOf, jfreechart,\r
81                                 l0.ConsistsOf, dataset\r
82                                 );\r
83                     }\r
84 \r
85                 });\r
86             }\r
87         };\r
88     }\r
89 \r
90 }\r