1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 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 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.browser.actions.newActions;
\r
14 import java.util.Collections;
\r
15 import java.util.UUID;
\r
17 import org.simantics.db.Resource;
\r
18 import org.simantics.db.WriteGraph;
\r
19 import org.simantics.db.common.request.WriteRequest;
\r
20 import org.simantics.db.common.utils.ListUtils;
\r
21 import org.simantics.db.common.utils.NameUtils;
\r
22 import org.simantics.db.exception.DatabaseException;
\r
23 import org.simantics.db.layer0.adapter.ActionFactory;
\r
24 import org.simantics.diagram.stubs.G2DResource;
\r
25 import org.simantics.layer0.Layer0;
\r
26 import org.simantics.layer0.utils.direct.GraphUtils;
\r
27 import org.simantics.sysdyn.JFreeChartResource;
\r
28 import org.simantics.ui.SimanticsUI;
\r
31 * Creates a new bar chart to a model
\r
32 * @author Teemu Lempinen
\r
35 public class NewBarChartAction implements ActionFactory{
\r
38 public Runnable create(Object target) {
\r
39 if(!(target instanceof Resource))
\r
41 final Resource model = (Resource)target;
\r
43 return new Runnable() {
\r
47 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
50 public void perform(WriteGraph graph) throws DatabaseException {
\r
51 Layer0 l0 = Layer0.getInstance(graph);
\r
52 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
53 G2DResource g2d = G2DResource.getInstance(graph);
\r
56 Resource jfreechart = GraphUtils.create2(graph, jfree.Chart,
\r
57 l0.HasName, "BarChart" + UUID.randomUUID().toString(),
\r
58 l0.HasLabel, NameUtils.findFreshLabel(graph, "Bar Chart", model),
\r
60 jfree.Chart_visibleBorder, true,
\r
61 jfree.Chart_borderWidth, 3,
\r
62 jfree.Chart_visibleLegend, false
\r
66 graph.claimLiteral(jfreechart, jfree.Chart_borderColor, g2d.Color, new float[] {0,0,0,1});
\r
69 GraphUtils.create2(graph, jfree.TextTitle,
\r
70 l0.HasName, "TextTitle" + UUID.randomUUID().toString(),
\r
71 l0.HasLabel, "Bar Chart Title",
\r
72 jfree.Title_position, jfree.Top,
\r
73 l0.PartOf, jfreechart);
\r
76 Resource domainAxis = GraphUtils.create2(graph, jfree.CategoryAxis,
\r
77 l0.HasName, "CategoryAxis" + UUID.randomUUID().toString());
\r
80 Resource rangeAxis = GraphUtils.create2(graph, jfree.NumberAxis,
\r
81 l0.HasName, "NumberAxis" + UUID.randomUUID().toString());
\r
84 Resource renderer = GraphUtils.create2(graph, jfree.BarRenderer);
\r
87 Resource dataset = GraphUtils.create2(graph, jfree.CategoryDataset,
\r
88 l0.HasName, "CategoryDataset" + UUID.randomUUID().toString(),
\r
89 jfree.Dataset_mapToDomainAxis, domainAxis,
\r
90 jfree.Dataset_mapToRangeAxis, rangeAxis,
\r
91 jfree.Dataset_seriesList, ListUtils.create(graph, Collections.<Resource>emptyList()),
\r
92 jfree.Dataset_renderer, renderer);
\r
95 GraphUtils.create2(graph, jfree.CategoryPlot,
\r
96 l0.HasName, "Category plot" + UUID.randomUUID().toString(),
\r
97 l0.PartOf, jfreechart,
\r
98 jfree.Plot_domainAxis, domainAxis,
\r
99 jfree.Plot_rangeAxis, rangeAxis,
\r
100 jfree.Plot_rangeAxisList, ListUtils.create(graph, Collections.singletonList(rangeAxis)),
\r
101 l0.ConsistsOf, dataset,
\r
102 l0.ConsistsOf, domainAxis,
\r
103 l0.ConsistsOf, rangeAxis);
\r