]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
3f17f995c7bbaf5e9457bec76e12c6845988a069
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 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.handlers.newComponents;\r
13 \r
14 import java.util.Collections;\r
15 import java.util.UUID;\r
16 \r
17 import org.eclipse.core.commands.AbstractHandler;\r
18 import org.eclipse.core.commands.ExecutionEvent;\r
19 import org.eclipse.core.commands.ExecutionException;\r
20 import org.eclipse.jface.viewers.ISelection;\r
21 import org.eclipse.ui.handlers.HandlerUtil;\r
22 import org.simantics.db.Resource;\r
23 import org.simantics.db.WriteGraph;\r
24 import org.simantics.db.common.request.WriteRequest;\r
25 import org.simantics.db.common.utils.ListUtils;\r
26 import org.simantics.db.common.utils.NameUtils;\r
27 import org.simantics.db.exception.DatabaseException;\r
28 import org.simantics.diagram.stubs.G2DResource;\r
29 import org.simantics.layer0.Layer0;\r
30 import org.simantics.layer0.utils.direct.GraphUtils;\r
31 import org.simantics.sysdyn.JFreeChartResource;\r
32 import org.simantics.sysdyn.ui.browser.nodes.ChartsFolder;\r
33 import org.simantics.ui.SimanticsUI;\r
34 import org.simantics.utils.ui.AdaptionUtils;\r
35 \r
36 /**\r
37  * Handler for craeting a new Pie Chart\r
38  * @author Teemu Lempinen\r
39  *\r
40  */\r
41 public class NewPieChartHandler extends AbstractHandler {\r
42 \r
43     @Override\r
44     public Object execute(ExecutionEvent event) throws ExecutionException {\r
45         \r
46         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
47 \r
48         ChartsFolder node = AdaptionUtils.adaptToSingle(sel, ChartsFolder.class);\r
49         if (node == null)\r
50             return null;\r
51 \r
52         final Resource model = node.data;        \r
53 \r
54         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
55 \r
56             @Override\r
57             public void perform(WriteGraph g) throws DatabaseException {\r
58                 Layer0 l0 = Layer0.getInstance(g);\r
59                 JFreeChartResource jfree = JFreeChartResource.getInstance(g);\r
60                 G2DResource g2d = G2DResource.getInstance(g);\r
61 \r
62                 Resource jfreechart = GraphUtils.create2(g, jfree.Chart,\r
63                         l0.HasName, "PieChart" + UUID.randomUUID().toString(),\r
64                         l0.HasLabel, NameUtils.findFreshLabel(g, "Pie Chart", model),\r
65                         l0.PartOf, model,\r
66                         jfree.Chart_visibleBorder, true,\r
67                         jfree.Chart_borderWidth, 3);\r
68                 g.claimLiteral(jfreechart, jfree.Chart_borderColor, g2d.Color, new float[] {0,0,0,1});\r
69 \r
70                 GraphUtils.create2(g, jfree.TextTitle,\r
71                         l0.HasName, "TextTitle" + UUID.randomUUID().toString(),\r
72                         l0.HasLabel, "Pie Chart Title",\r
73                         jfree.Title_position, jfree.Top,\r
74                         l0.PartOf, jfreechart);\r
75                 \r
76                 Resource dataset = GraphUtils.create2(g, jfree.PieDataset,\r
77                         l0.HasName, "CategoryDataset" + UUID.randomUUID().toString(),\r
78                         jfree.Dataset_seriesList, ListUtils.create(g, Collections.<Resource>emptyList())\r
79                         );\r
80 \r
81                 GraphUtils.create2(g, jfree.PiePlot,\r
82                         l0.HasName, "PiePlot" + UUID.randomUUID().toString(),\r
83                         l0.PartOf, jfreechart,\r
84                         l0.ConsistsOf, dataset\r
85                         );\r
86             }\r
87 \r
88         });\r
89         \r
90         return null;\r
91     }\r
92 }\r