1 /*******************************************************************************
\r
2 * Copyright (c) 2013 Association for Decentralized Information Management in
\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
10 * Semantum Oy - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.handlers.newComponents;
\r
14 import java.awt.Color;
\r
15 import java.util.ArrayList;
\r
16 import java.util.Collections;
\r
17 import java.util.UUID;
\r
19 import org.eclipse.core.commands.AbstractHandler;
\r
20 import org.eclipse.core.commands.ExecutionEvent;
\r
21 import org.eclipse.core.commands.ExecutionException;
\r
22 import org.eclipse.jface.viewers.ISelection;
\r
23 import org.eclipse.ui.handlers.HandlerUtil;
\r
24 import org.simantics.db.Resource;
\r
25 import org.simantics.db.WriteGraph;
\r
26 import org.simantics.db.common.request.WriteRequest;
\r
27 import org.simantics.db.common.utils.ListUtils;
\r
28 import org.simantics.db.common.utils.NameUtils;
\r
29 import org.simantics.db.exception.DatabaseException;
\r
30 import org.simantics.diagram.stubs.G2DResource;
\r
31 import org.simantics.jfreechart.chart.ChartUtils;
\r
32 import org.simantics.layer0.Layer0;
\r
33 import org.simantics.layer0.utils.direct.GraphUtils;
\r
34 import org.simantics.sysdyn.JFreeChartResource;
\r
35 import org.simantics.sysdyn.SysdynResource;
\r
36 import org.simantics.sysdyn.ui.browser.nodes.ChartsFolder;
\r
37 import org.simantics.ui.SimanticsUI;
\r
38 import org.simantics.utils.ui.AdaptionUtils;
\r
40 public class NewSensitivityChartHandler extends AbstractHandler {
\r
42 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
45 ISelection sel = HandlerUtil.getCurrentSelection(event);
\r
47 ChartsFolder node = AdaptionUtils.adaptToSingle(sel, ChartsFolder.class);
\r
51 final Resource model = node.data;
\r
53 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
56 public void perform(WriteGraph g) throws DatabaseException {
\r
57 Layer0 l0 = Layer0.getInstance(g);
\r
58 JFreeChartResource jfree = JFreeChartResource.getInstance(g);
\r
59 G2DResource g2d = G2DResource.getInstance(g);
\r
60 SysdynResource SR = SysdynResource.getInstance(g);
\r
62 Resource jfreechart = GraphUtils.create2(g, jfree.Chart,
\r
63 l0.HasName, "SensitivityChart" + UUID.randomUUID().toString(),
\r
64 l0.HasLabel, NameUtils.findFreshLabel(g, "SensitivityChart", 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
70 GraphUtils.create2(g, jfree.TextTitle,
\r
71 l0.HasName, "TextTitle" + UUID.randomUUID().toString(),
\r
72 l0.HasLabel, "Chart Title",
\r
73 jfree.Title_position, jfree.Top,
\r
74 l0.PartOf, jfreechart);
\r
76 Resource domainAxis = GraphUtils.create2(g, jfree.NumberAxis,
\r
77 l0.HasName, "NumberAxis" + UUID.randomUUID().toString());
\r
78 Resource rangeAxis = GraphUtils.create2(g, jfree.NumberAxis,
\r
79 l0.HasName, "NumberAxis" + UUID.randomUUID().toString(),
\r
82 Resource renderer = GraphUtils.create2(g, jfree.DeviationRenderer);
\r
85 new Color(255, 255, 64),
\r
86 new Color(0, 255, 0),
\r
87 new Color(0, 0, 255),
\r
88 new Color(128, 128, 128),
\r
89 new Color(192, 192, 192)
\r
92 float[] colorContainer = new float[] {0, 0, 0, 1};
\r
94 ArrayList<Resource> confidenceBounds = new ArrayList<Resource>();
\r
95 confidenceBounds.add(GraphUtils.create2(g, SR.Charts_SensitivityDataset_ConfidenceBound,
\r
96 SR.Charts_SensitivityDataset_ConfidenceBound_percent, 25.0
\r
98 g.claimLiteral(confidenceBounds.get(0), SR.Charts_SensitivityDataset_ConfidenceBound_color, g2d.Color, colors[0].getColorComponents(colorContainer));
\r
101 confidenceBounds.add(GraphUtils.create2(g, SR.Charts_SensitivityDataset_ConfidenceBound,
\r
102 SR.Charts_SensitivityDataset_ConfidenceBound_percent, 50.0
\r
104 g.claimLiteral(confidenceBounds.get(1), SR.Charts_SensitivityDataset_ConfidenceBound_color, g2d.Color, colors[1].getColorComponents(colorContainer));
\r
106 confidenceBounds.add(GraphUtils.create2(g, SR.Charts_SensitivityDataset_ConfidenceBound,
\r
107 SR.Charts_SensitivityDataset_ConfidenceBound_percent, 75.0
\r
109 g.claimLiteral(confidenceBounds.get(2), SR.Charts_SensitivityDataset_ConfidenceBound_color, g2d.Color, colors[2].getColorComponents(colorContainer));
\r
111 confidenceBounds.add(GraphUtils.create2(g, SR.Charts_SensitivityDataset_ConfidenceBound,
\r
112 SR.Charts_SensitivityDataset_ConfidenceBound_percent, 100.0
\r
114 g.claimLiteral(confidenceBounds.get(3), SR.Charts_SensitivityDataset_ConfidenceBound_color, g2d.Color, colors[3].getColorComponents(colorContainer));
\r
116 confidenceBounds.add(GraphUtils.create2(g, SR.Charts_SensitivityDataset_ConfidenceBound
\r
118 g.claimLiteral(confidenceBounds.get(4), SR.Charts_SensitivityDataset_ConfidenceBound_color, g2d.Color, colors[4].getColorComponents(colorContainer));
\r
121 Resource dataset = GraphUtils.create2(g, SR.Charts_SensitivityDataset,
\r
122 l0.HasName, "SensitivityDataset" + UUID.randomUUID().toString(),
\r
123 jfree.Dataset_mapToDomainAxis, domainAxis,
\r
124 jfree.Dataset_mapToRangeAxis, rangeAxis,
\r
125 jfree.Dataset_seriesList, ListUtils.create(g, new ArrayList<Resource>()),
\r
126 jfree.Dataset_renderer, renderer,
\r
127 SR.Charts_SensitivityDataset_confidenceBounds, ListUtils.create(g, confidenceBounds)
\r
130 ChartUtils.createSeries(g, dataset, null);
\r
135 GraphUtils.create2(g, SR.Charts_SensitivityPlot,
\r
136 l0.HasName, "SensitivityPlot" + UUID.randomUUID().toString(),
\r
137 l0.PartOf, jfreechart,
\r
138 jfree.Plot_domainAxis, domainAxis,
\r
139 jfree.Plot_rangeAxis, rangeAxis,
\r
140 jfree.Plot_rangeAxisList, ListUtils.create(g, Collections.singletonList(rangeAxis)),
\r
141 l0.ConsistsOf, dataset,
\r
142 l0.ConsistsOf, domainAxis,
\r
143 l0.ConsistsOf, rangeAxis);
\r