]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
2f06ce812f6c41d9929a27a512e9f3f5d95aa453
[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.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 XYLineChart in model browser\r
39  * @author Teemu Lempinen\r
40  * @author Tuomas Miettinen\r
41  *\r
42  */\r
43 public class NewXYLineChartHandler extends AbstractHandler {\r
44 \r
45     @Override\r
46     public Object execute(ExecutionEvent event) throws ExecutionException {\r
47 \r
48 \r
49         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
50 \r
51         ChartsFolder node = AdaptionUtils.adaptToSingle(sel, ChartsFolder.class);\r
52         if (node == null)\r
53             return null;\r
54 \r
55         final Resource model = node.data;        \r
56 \r
57         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
58 \r
59             @Override\r
60             public void perform(WriteGraph g) throws DatabaseException {\r
61                 Layer0 l0 = Layer0.getInstance(g);\r
62                 JFreeChartResource jfree = JFreeChartResource.getInstance(g);\r
63                 G2DResource g2d = G2DResource.getInstance(g);\r
64 \r
65                 Resource jfreechart = GraphUtils.create2(g, jfree.Chart,\r
66                         l0.HasName, "Chart" + UUID.randomUUID().toString(),\r
67                         l0.HasLabel, NameUtils.findFreshLabel(g, "Chart", model),\r
68                         l0.PartOf, model,\r
69                         jfree.Chart_visibleBorder, true,\r
70                         jfree.Chart_borderWidth, 3);\r
71                 g.claimLiteral(jfreechart, jfree.Chart_borderColor, g2d.Color, new float[] {0,0,0,1});\r
72 \r
73                 GraphUtils.create2(g, jfree.TextTitle,\r
74                         l0.HasName, "TextTitle" + UUID.randomUUID().toString(),\r
75                         l0.HasLabel, "Chart Title",\r
76                         jfree.Title_position, jfree.Top,\r
77                         l0.PartOf, jfreechart);\r
78 \r
79                 Resource domainAxis = GraphUtils.create2(g, jfree.NumberAxis,\r
80                         l0.HasName, "NumberAxis" + UUID.randomUUID().toString(),\r
81                         jfree.variableRVI, "/time",\r
82                         l0.HasLabel, "Time");\r
83                 Resource rangeAxis = GraphUtils.create2(g, jfree.NumberAxis,\r
84                         l0.HasName, "NumberAxis" + UUID.randomUUID().toString(),\r
85                         l0.HasLabel, "Y-axis");\r
86                 \r
87                 Resource renderer = GraphUtils.create2(g, jfree.XYLineRenderer);\r
88 \r
89                 Resource dataset = GraphUtils.create2(g, jfree.XYDataset,\r
90                         l0.HasName, "XYDataset" + UUID.randomUUID().toString(),\r
91                         jfree.Dataset_mapToDomainAxis, domainAxis,\r
92                         jfree.Dataset_mapToRangeAxis, rangeAxis,\r
93                         jfree.Dataset_seriesList, ListUtils.create(g, new ArrayList<Resource>()),\r
94                         jfree.Dataset_renderer, renderer);\r
95 \r
96                 GraphUtils.create2(g, jfree.XYPlot,\r
97                         l0.HasName, "XYPlot" + UUID.randomUUID().toString(),\r
98                         l0.PartOf, jfreechart,\r
99                         jfree.Plot_domainAxis, domainAxis,\r
100                         jfree.Plot_rangeAxis, rangeAxis,\r
101                         jfree.Plot_rangeAxisList, ListUtils.create(g, Collections.singletonList(rangeAxis)),\r
102                         l0.ConsistsOf, dataset,\r
103                         l0.ConsistsOf, domainAxis,\r
104                         l0.ConsistsOf, rangeAxis);\r
105             }\r
106 \r
107         });\r
108 \r
109 \r
110 \r
111         return null;\r
112     }\r
113 \r
114 }\r