1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 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.trend.chart.properties.bar;
\r
14 import java.util.LinkedHashMap;
\r
15 import java.util.Map;
\r
17 import org.eclipse.jface.layout.GridDataFactory;
\r
18 import org.eclipse.jface.layout.GridLayoutFactory;
\r
19 import org.eclipse.swt.SWT;
\r
20 import org.eclipse.swt.custom.ScrolledComposite;
\r
21 import org.eclipse.swt.graphics.Point;
\r
22 import org.eclipse.swt.widgets.Composite;
\r
23 import org.eclipse.swt.widgets.Group;
\r
24 import org.eclipse.swt.widgets.Label;
\r
25 import org.eclipse.ui.IWorkbenchSite;
\r
26 import org.simantics.browsing.ui.swt.widgets.Button;
\r
27 import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory;
\r
28 import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier;
\r
29 import org.simantics.browsing.ui.swt.widgets.TrackedCombo;
\r
30 import org.simantics.browsing.ui.swt.widgets.TrackedText;
\r
31 import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl;
\r
32 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;
\r
33 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
\r
34 import org.simantics.db.ReadGraph;
\r
35 import org.simantics.db.Resource;
\r
36 import org.simantics.db.WriteGraph;
\r
37 import org.simantics.db.common.request.PossibleObjectWithType;
\r
38 import org.simantics.db.exception.DatabaseException;
\r
39 import org.simantics.db.management.ISessionContext;
\r
40 import org.simantics.layer0.Layer0;
\r
41 import org.simantics.layer0.utils.direct.GraphUtils;
\r
42 import org.simantics.sysdyn.JFreeChartResource;
\r
43 import org.simantics.sysdyn.ui.properties.LabelPropertyTabContributor;
\r
44 import org.simantics.sysdyn.ui.trend.chart.properties.BooleanPropertyFactory;
\r
45 import org.simantics.sysdyn.ui.trend.chart.properties.BooleanSelectionListener;
\r
46 import org.simantics.sysdyn.ui.trend.chart.properties.JFreeChartPropertyColorProvider;
\r
47 import org.simantics.sysdyn.ui.trend.chart.properties.TitleFactory;
\r
48 import org.simantics.sysdyn.ui.trend.chart.properties.TitleModifier;
\r
52 * General properties of a bar chart
\r
53 * @author Teemu Lempinen
\r
56 public class BarGeneralPropertiesTab extends LabelPropertyTabContributor {
\r
58 private ScrolledComposite sc;
\r
59 private Composite composite;
\r
60 private Button hgrid, htitle, hlegend;
\r
61 private TrackedText name, title;
\r
62 private TrackedCombo type;
\r
66 public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {
\r
67 // Scrolled composite containing all of the properties in this tab
\r
68 sc = new ScrolledComposite(body, SWT.NONE | SWT.H_SCROLL | SWT.V_SCROLL);
\r
69 GridDataFactory.fillDefaults().grab(true, true).applyTo(sc);
\r
70 GridLayoutFactory.fillDefaults().applyTo(sc);
\r
71 sc.setExpandHorizontal(true);
\r
72 sc.setExpandVertical(true);
\r
74 composite = new Composite(sc, SWT.NONE);
\r
75 GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).applyTo(composite);
\r
77 // General properties
\r
78 Group general = new Group(composite, SWT.NONE);
\r
79 GridDataFactory.fillDefaults().grab(true, false).applyTo(general);
\r
80 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(general);
\r
81 general.setText("General");
\r
84 Label label = new Label(general, SWT.NONE);
\r
85 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);
\r
86 label.setText("Name:");
\r
88 Composite c = new Composite(general, SWT.NONE);
\r
89 GridDataFactory.fillDefaults().applyTo(c);
\r
90 GridLayoutFactory.fillDefaults().numColumns(3).applyTo(c);
\r
92 name = new org.simantics.browsing.ui.swt.widgets.TrackedText(c, support, SWT.BORDER);
\r
93 GridDataFactory.fillDefaults().grab(true, false).applyTo(name.getWidget());
\r
94 name.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel));
\r
95 name.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel));
\r
96 name.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));
\r
99 label = new Label(c, SWT.NONE);
\r
100 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);
\r
101 label.setText("Type:");
\r
103 type = new TrackedCombo(c, support, SWT.BORDER | SWT.READ_ONLY);
\r
104 type.addModifyListener(new TypeModifyListener());
\r
105 type.setItemFactory(new TypeItemFactory());
\r
106 type.setSelectionFactory(new TypeSelectionFactory());
\r
107 GridDataFactory.fillDefaults().applyTo(type.getWidget());
\r
109 // Title (Which is different than name)
\r
110 label = new Label(general, SWT.NONE);
\r
111 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);
\r
112 label.setText("Title:");
\r
114 title = new org.simantics.browsing.ui.swt.widgets.TrackedText(general, support, SWT.BORDER);
\r
115 GridDataFactory.fillDefaults().grab(true, false).applyTo(title.getWidget());
\r
116 title.setTextFactory(new TitleFactory());
\r
117 title.addModifyListener(new TitleModifier());
\r
118 title.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));
\r
120 // Group for hide options
\r
121 Group hideGroup = new Group(composite, SWT.NONE);
\r
122 GridDataFactory.fillDefaults().applyTo(hideGroup);
\r
123 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(hideGroup);
\r
124 hideGroup.setText("Hide");
\r
126 hgrid = new Button(hideGroup, support, SWT.CHECK);
\r
127 hgrid.setText("Grid");
\r
128 hgrid.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid, true));
\r
129 hgrid.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid));
\r
130 htitle = new Button(hideGroup, support, SWT.CHECK);
\r
131 htitle.setText("Title");
\r
132 htitle.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible, true));
\r
133 htitle.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible));
\r
134 hlegend = new Button(hideGroup, support, SWT.CHECK);
\r
135 hlegend.setText("Legend");
\r
136 hlegend.setSelectionFactory(new BooleanPropertyFactory(null, JFreeChartResource.URIs.Chart_visibleLegend, true));
\r
137 hlegend.addSelectionListener(new BooleanSelectionListener(context, null, JFreeChartResource.URIs.Chart_visibleLegend));
\r
139 // Resize scrolled composite
\r
140 sc.setContent(composite);
\r
141 Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
\r
142 sc.setMinSize(size);
\r
147 * @author Teemu Lempinen
\r
150 private class TypeSelectionFactory extends ReadFactoryImpl<Resource, String> {
\r
152 public String perform(ReadGraph graph, Resource chart) throws DatabaseException {
\r
153 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
154 Layer0 l0 = Layer0.getInstance(graph);
\r
156 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));
\r
158 Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.CategoryDataset));
\r
160 if(dataset != null) {
\r
161 Resource renderer = graph.syncRequest(new PossibleObjectWithType(dataset, jfree.Dataset_renderer, jfree.Renderer));
\r
163 if(renderer != null && graph.isInstanceOf(renderer, jfree.StackedBarRenderer))
\r
172 * RangeItemFactory finds all inexes of a given enumeration
\r
173 * and adds "Sum" and "All" to the returned indexes
\r
174 * @author Teemu Lempinen
\r
177 private class TypeItemFactory extends ReadFactoryImpl<Resource, Map<String, Object>> {
\r
179 public Map<String, Object> perform(ReadGraph graph, Resource series) throws DatabaseException {
\r
180 LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();
\r
181 result.put("Normal", "Normal");
\r
182 result.put("Stacked", "Stacked");
\r
188 * TypeModifyListener for modifying the type of a bar chart
\r
189 * @author Teemu Lempinen
\r
192 private class TypeModifyListener extends ComboModifyListenerImpl<Resource> {
\r
194 public void applyText(WriteGraph graph, Resource chart, String text) throws DatabaseException {
\r
195 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
196 Layer0 l0 = Layer0.getInstance(graph);
\r
198 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));
\r
202 Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.CategoryDataset));
\r
203 if(dataset == null)
\r
206 graph.deny(dataset, jfree.Dataset_renderer);
\r
209 if(text.equals("Stacked"))
\r
210 renderer = GraphUtils.create2(graph, jfree.StackedBarRenderer);
\r
212 renderer = GraphUtils.create2(graph, jfree.BarRenderer);
\r
214 graph.claim(dataset, jfree.Dataset_renderer, renderer);
\r