]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
1e3421421dd517bf8d00549c964d9d66b420b6af
[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.db.layer0.util.Layer0Utils;\r
29 import org.simantics.diagram.stubs.G2DResource;\r
30 import org.simantics.layer0.Layer0;\r
31 import org.simantics.layer0.utils.direct.GraphUtils;\r
32 import org.simantics.sysdyn.JFreeChartResource;\r
33 import org.simantics.sysdyn.ui.browser.nodes.ChartsFolder;\r
34 import org.simantics.ui.SimanticsUI;\r
35 import org.simantics.utils.ui.AdaptionUtils;\r
36 \r
37 /**\r
38  * Handler for creating a new Bar Chart\r
39  * @author Teemu Lempinen\r
40  *\r
41  */\r
42 public class NewBarChartHandler extends AbstractHandler {\r
43 \r
44     @Override\r
45     public Object execute(ExecutionEvent event) throws ExecutionException {\r
46         \r
47         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
48 \r
49         ChartsFolder node = AdaptionUtils.adaptToSingle(sel, ChartsFolder.class);\r
50         if (node == null)\r
51             return null;\r
52 \r
53         final Resource model = node.data;        \r
54 \r
55         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
56 \r
57             @Override\r
58             public void perform(WriteGraph g) throws DatabaseException {\r
59                 g.markUndoPoint();\r
60                 Layer0 l0 = Layer0.getInstance(g);\r
61                 JFreeChartResource jfree = JFreeChartResource.getInstance(g);\r
62                 G2DResource g2d = G2DResource.getInstance(g);\r
63 \r
64                 String label = NameUtils.findFreshLabel(g, "Bar Chart", model);\r
65                 \r
66                 Resource jfreechart = GraphUtils.create2(g, jfree.Chart,\r
67                         l0.HasName, "BarChart" + UUID.randomUUID().toString(),\r
68                         l0.HasLabel, label,\r
69                         l0.PartOf, model,\r
70                         jfree.Chart_visibleBorder, true,\r
71                         jfree.Chart_borderWidth, 3,\r
72                         jfree.Chart_visibleLegend, false\r
73                         );\r
74                 \r
75                 g.claimLiteral(jfreechart, jfree.Chart_borderColor, g2d.Color, new float[] {0,0,0,1});\r
76 \r
77                 GraphUtils.create2(g, jfree.TextTitle,\r
78                         l0.HasName, "TextTitle" + UUID.randomUUID().toString(),\r
79                         l0.HasLabel, "Bar Chart Title",\r
80                         jfree.Title_position, jfree.Top,\r
81                         l0.PartOf, jfreechart);\r
82 \r
83                 Resource domainAxis = GraphUtils.create2(g, jfree.CategoryAxis,\r
84                         l0.HasName, "CategoryAxis" + UUID.randomUUID().toString());\r
85                 \r
86                 Resource rangeAxis = GraphUtils.create2(g, jfree.NumberAxis,\r
87                         l0.HasName, "NumberAxis" + UUID.randomUUID().toString());\r
88 \r
89                 Resource renderer = GraphUtils.create2(g, jfree.BarRenderer);\r
90                 \r
91                 Resource dataset = GraphUtils.create2(g, jfree.CategoryDataset,\r
92                         l0.HasName, "CategoryDataset" + UUID.randomUUID().toString(),\r
93                         jfree.Dataset_mapToDomainAxis, domainAxis,\r
94                         jfree.Dataset_mapToRangeAxis, rangeAxis,\r
95                         jfree.Dataset_seriesList, ListUtils.create(g, Collections.<Resource>emptyList()),\r
96                         jfree.Dataset_renderer, renderer);\r
97 \r
98                 GraphUtils.create2(g, jfree.CategoryPlot,\r
99                         l0.HasName, "Category plot" + UUID.randomUUID().toString(),\r
100                         l0.PartOf, jfreechart,\r
101                         jfree.Plot_domainAxis, domainAxis,\r
102                         jfree.Plot_rangeAxis, rangeAxis,\r
103                         jfree.Plot_rangeAxisList, ListUtils.create(g, Collections.singletonList(rangeAxis)),\r
104                         l0.ConsistsOf, dataset,\r
105                         l0.ConsistsOf, domainAxis,\r
106                         l0.ConsistsOf, rangeAxis);\r
107                 Layer0Utils.addCommentMetadata(g, "Created new Bar Chart " +  label + " " + jfreechart);\r
108             }\r
109 \r
110         });\r
111         \r
112         return null;\r
113     }\r
114 \r
115 }\r