]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
9c2e675e98a53dc42b48129c44cc2d3606b5ab9f
[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.ArrayList;\r
15 import java.util.Collections;\r
16 import java.util.UUID;\r
17 \r
18 import org.eclipse.core.commands.AbstractHandler;\r
19 import org.eclipse.core.commands.ExecutionEvent;\r
20 import org.eclipse.core.commands.ExecutionException;\r
21 import org.eclipse.jface.viewers.ISelection;\r
22 import org.eclipse.ui.handlers.HandlerUtil;\r
23 import org.simantics.db.Resource;\r
24 import org.simantics.db.WriteGraph;\r
25 import org.simantics.db.common.request.WriteRequest;\r
26 import org.simantics.db.common.utils.ListUtils;\r
27 import org.simantics.db.common.utils.NameUtils;\r
28 import org.simantics.db.exception.DatabaseException;\r
29 import org.simantics.db.layer0.util.Layer0Utils;\r
30 import org.simantics.diagram.stubs.G2DResource;\r
31 import org.simantics.layer0.Layer0;\r
32 import org.simantics.layer0.utils.direct.GraphUtils;\r
33 import org.simantics.sysdyn.JFreeChartResource;\r
34 import org.simantics.sysdyn.ui.browser.nodes.ChartsFolder;\r
35 import org.simantics.ui.SimanticsUI;\r
36 import org.simantics.utils.ui.AdaptionUtils;\r
37 \r
38 /**\r
39  * Handler for creating a new XYLineChart in model browser\r
40  * @author Teemu Lempinen\r
41  * @author Tuomas Miettinen\r
42  *\r
43  */\r
44 public class NewXYLineChartHandler extends AbstractHandler {\r
45 \r
46     @Override\r
47     public Object execute(ExecutionEvent event) throws ExecutionException {\r
48 \r
49 \r
50         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
51 \r
52         ChartsFolder node = AdaptionUtils.adaptToSingle(sel, ChartsFolder.class);\r
53         if (node == null)\r
54             return null;\r
55 \r
56         final Resource model = node.data;        \r
57 \r
58         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
59 \r
60             @Override\r
61             public void perform(WriteGraph g) throws DatabaseException {\r
62                 g.markUndoPoint();\r
63                 Layer0 l0 = Layer0.getInstance(g);\r
64                 JFreeChartResource jfree = JFreeChartResource.getInstance(g);\r
65                 G2DResource g2d = G2DResource.getInstance(g);\r
66                 \r
67                 String label = NameUtils.findFreshLabel(g, "Chart", model);\r
68                 \r
69                 Resource jfreechart = GraphUtils.create2(g, jfree.Chart,\r
70                         l0.HasName, "Chart" + UUID.randomUUID().toString(),\r
71                         l0.HasLabel, label,\r
72                         l0.PartOf, model,\r
73                         jfree.Chart_visibleBorder, true,\r
74                         jfree.Chart_borderWidth, 3);\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, "Chart Title",\r
80                         jfree.Title_position, jfree.Top,\r
81                         l0.PartOf, jfreechart);\r
82 \r
83                 Resource domainAxis = GraphUtils.create2(g, jfree.NumberAxis,\r
84                         l0.HasName, "NumberAxis" + UUID.randomUUID().toString(),\r
85                         jfree.variableRVI, "/time",\r
86                         l0.HasLabel, "Time");\r
87                 Resource rangeAxis = GraphUtils.create2(g, jfree.NumberAxis,\r
88                         l0.HasName, "NumberAxis" + UUID.randomUUID().toString(),\r
89                         l0.HasLabel, "Y-axis");\r
90                 \r
91                 Resource renderer = GraphUtils.create2(g, jfree.XYLineRenderer);\r
92 \r
93                 Resource dataset = GraphUtils.create2(g, jfree.XYDataset,\r
94                         l0.HasName, "XYDataset" + UUID.randomUUID().toString(),\r
95                         jfree.Dataset_mapToDomainAxis, domainAxis,\r
96                         jfree.Dataset_mapToRangeAxis, rangeAxis,\r
97                         jfree.Dataset_seriesList, ListUtils.create(g, new ArrayList<Resource>()),\r
98                         jfree.Dataset_renderer, renderer);\r
99 \r
100                 GraphUtils.create2(g, jfree.XYPlot,\r
101                         l0.HasName, "XYPlot" + UUID.randomUUID().toString(),\r
102                         l0.PartOf, jfreechart,\r
103                         jfree.Plot_domainAxis, domainAxis,\r
104                         jfree.Plot_rangeAxis, rangeAxis,\r
105                         jfree.Plot_rangeAxisList, ListUtils.create(g, Collections.singletonList(rangeAxis)),\r
106                         l0.ConsistsOf, dataset,\r
107                         l0.ConsistsOf, domainAxis,\r
108                         l0.ConsistsOf, rangeAxis);\r
109                 Layer0Utils.addCommentMetadata(g, "Created new Line Chart " +  label + " " + jfreechart);\r
110             }\r
111 \r
112         });\r
113 \r
114 \r
115 \r
116         return null;\r
117     }\r
118 \r
119 }\r