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.jfreechart.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.jface.layout.LayoutConstants;
\r
20 import org.eclipse.swt.SWT;
\r
21 import org.eclipse.swt.custom.ScrolledComposite;
\r
22 import org.eclipse.swt.graphics.Point;
\r
23 import org.eclipse.swt.widgets.Composite;
\r
24 import org.eclipse.swt.widgets.Group;
\r
25 import org.eclipse.swt.widgets.Label;
\r
26 import org.eclipse.ui.IWorkbenchSite;
\r
27 import org.simantics.browsing.ui.swt.widgets.Button;
\r
28 import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory;
\r
29 import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier;
\r
30 import org.simantics.browsing.ui.swt.widgets.TrackedCombo;
\r
31 import org.simantics.browsing.ui.swt.widgets.TrackedText;
\r
32 import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl;
\r
33 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;
\r
34 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
\r
35 import org.simantics.db.ReadGraph;
\r
36 import org.simantics.db.Resource;
\r
37 import org.simantics.db.WriteGraph;
\r
38 import org.simantics.db.common.request.PossibleObjectWithType;
\r
39 import org.simantics.db.exception.DatabaseException;
\r
40 import org.simantics.db.management.ISessionContext;
\r
41 import org.simantics.jfreechart.chart.properties.BooleanPropertyFactory;
\r
42 import org.simantics.jfreechart.chart.properties.BooleanSelectionListener;
\r
43 import org.simantics.jfreechart.chart.properties.DoubleValidator;
\r
44 import org.simantics.jfreechart.chart.properties.JFreeChartPropertyColorProvider;
\r
45 import org.simantics.jfreechart.chart.properties.LabelPropertyTabContributor;
\r
46 import org.simantics.jfreechart.chart.properties.TitleFactory;
\r
47 import org.simantics.jfreechart.chart.properties.TitleModifier;
\r
48 import org.simantics.layer0.Layer0;
\r
49 import org.simantics.layer0.utils.direct.GraphUtils;
\r
50 import org.simantics.modeling.ui.chart.property.DoublePropertyFactory;
\r
51 import org.simantics.modeling.ui.chart.property.DoublePropertyModifier;
\r
52 import org.simantics.sysdyn.JFreeChartResource;
\r
56 * General properties of a bar chart
\r
57 * @author Teemu Lempinen
\r
60 public class BarGeneralPropertiesTab extends LabelPropertyTabContributor {
\r
62 private ScrolledComposite sc;
\r
63 private Composite composite;
\r
64 private Button hgrid, htitle, hlegend;
\r
65 private TrackedText name, title, time;
\r
66 private TrackedCombo type;
\r
67 private TrackedCombo orientation;
\r
71 public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {
\r
72 // Scrolled composite containing all of the properties in this tab
\r
73 sc = new ScrolledComposite(body, SWT.NONE | SWT.H_SCROLL | SWT.V_SCROLL);
\r
74 GridDataFactory.fillDefaults().grab(true, true).applyTo(sc);
\r
75 GridLayoutFactory.fillDefaults().applyTo(sc);
\r
76 sc.setExpandHorizontal(true);
\r
77 sc.setExpandVertical(true);
\r
79 composite = new Composite(sc, SWT.NONE);
\r
80 GridLayoutFactory.fillDefaults().numColumns(3).margins(3, 3).applyTo(composite);
\r
82 // General properties
\r
83 Group general = new Group(composite, SWT.NONE);
\r
84 GridDataFactory.fillDefaults().grab(true, false).applyTo(general);
\r
85 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(4).applyTo(general);
\r
86 general.setText("General");
\r
88 // first column: labels
\r
89 Composite labelColumn1 = new Composite(general, SWT.NONE);
\r
90 GridDataFactory.fillDefaults().grab(false, true).applyTo(labelColumn1);
\r
91 GridLayoutFactory.fillDefaults().applyTo(labelColumn1);
\r
93 // second column: name and title
\r
94 Composite propertyColumn1 = new Composite(general, SWT.NONE);
\r
95 GridDataFactory.fillDefaults().grab(true, true).applyTo(propertyColumn1);
\r
96 GridLayoutFactory.fillDefaults().spacing(0, LayoutConstants.getSpacing().y).applyTo(propertyColumn1);
\r
98 // third column: labels
\r
99 Composite labelColumn2 = new Composite(general, SWT.NONE);
\r
100 GridDataFactory.fillDefaults().grab(false, true).applyTo(labelColumn2);
\r
101 GridLayoutFactory.fillDefaults().spacing(0, LayoutConstants.getSpacing().y).applyTo(labelColumn2);
\r
103 // fourth column: type and time
\r
104 Composite propertyColumn2 = new Composite(general, SWT.NONE);
\r
105 GridDataFactory.fillDefaults().grab(false, true).applyTo(propertyColumn2);
\r
106 GridLayoutFactory.fillDefaults().applyTo(propertyColumn2);
\r
109 Label label = new Label(labelColumn1, SWT.NONE);
\r
110 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(label);
\r
111 label.setText("Name:");
\r
113 name = new org.simantics.browsing.ui.swt.widgets.TrackedText(propertyColumn1, support, SWT.BORDER);
\r
114 GridDataFactory.fillDefaults().grab(true, false).applyTo(name.getWidget());
\r
115 name.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel));
\r
116 name.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel));
\r
117 name.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));
\r
120 // label = new Label(labelColumn2, SWT.NONE);
\r
121 // GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(label);
\r
122 // label.setText("Type:");
\r
124 // type = new TrackedCombo(propertyColumn2, support, SWT.BORDER | SWT.READ_ONLY);
\r
125 // type.addModifyListener(new TypeModifyListener());
\r
126 // type.setItemFactory(new TypeItemFactory());
\r
127 // type.setSelectionFactory(new TypeSelectionFactory());
\r
128 // GridDataFactory.fillDefaults().applyTo(type.getWidget());
\r
129 label = new Label(labelColumn2, SWT.NONE);
\r
130 label = new Label(propertyColumn2, SWT.NONE);
\r
132 // Title (Which is different than name)
\r
133 label = new Label(labelColumn1, SWT.NONE);
\r
134 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(label);
\r
135 label.setText("Title:");
\r
137 title = new org.simantics.browsing.ui.swt.widgets.TrackedText(propertyColumn1, support, SWT.BORDER);
\r
138 GridDataFactory.fillDefaults().grab(true, false).applyTo(title.getWidget());
\r
139 title.setTextFactory(new TitleFactory());
\r
140 title.addModifyListener(new TitleModifier());
\r
141 title.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));
\r
144 label = new Label(labelColumn2, SWT.NONE);
\r
145 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(label);
\r
146 label.setText("Time:");
\r
148 time = new org.simantics.browsing.ui.swt.widgets.TrackedText(propertyColumn2, support, SWT.BORDER);
\r
149 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(time.getWidget());
\r
150 time.setTextFactory(new DoublePropertyFactory(JFreeChartResource.URIs.Chart_time));
\r
151 time.addModifyListener(new DoublePropertyModifier(context, JFreeChartResource.URIs.Chart_time));
\r
152 time.setInputValidator(new DoubleValidator(true));
\r
153 time.setColorProvider(new JFreeChartPropertyColorProvider(time.getResourceManager()));
\r
155 Group typeGroup = new Group(composite,SWT.NONE);
\r
156 GridDataFactory.fillDefaults().applyTo(typeGroup);
\r
157 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(typeGroup);
\r
158 typeGroup.setText("Visuals");
\r
160 label = new Label(typeGroup, SWT.NONE);
\r
161 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(label);
\r
162 label.setText("Type:");
\r
164 type = new TrackedCombo(typeGroup, support, SWT.BORDER | SWT.READ_ONLY);
\r
165 type.addModifyListener(new TypeModifyListener());
\r
166 type.setItemFactory(new TypeItemFactory());
\r
167 type.setSelectionFactory(new TypeSelectionFactory());
\r
168 GridDataFactory.fillDefaults().applyTo(type.getWidget());
\r
170 label = new Label(typeGroup, SWT.NONE);
\r
171 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(label);
\r
172 label.setText("Orientation:");
\r
174 orientation = new TrackedCombo(typeGroup, support, SWT.BORDER | SWT.READ_ONLY);
\r
175 orientation.addModifyListener(new OrientationModifyListener());
\r
176 orientation.setItemFactory(new OrientationItemFactory());
\r
177 orientation.setSelectionFactory(new OrientationSelectionFactory());
\r
178 GridDataFactory.fillDefaults().applyTo(type.getWidget());
\r
181 // Group for hide options
\r
182 Group hideGroup = new Group(composite, SWT.NONE);
\r
183 GridDataFactory.fillDefaults().applyTo(hideGroup);
\r
184 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(hideGroup);
\r
185 hideGroup.setText("Hide");
\r
187 hgrid = new Button(hideGroup, support, SWT.CHECK);
\r
188 hgrid.setText("Grid");
\r
189 hgrid.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid, true));
\r
190 hgrid.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid));
\r
191 htitle = new Button(hideGroup, support, SWT.CHECK);
\r
192 htitle.setText("Title");
\r
193 htitle.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible, true));
\r
194 htitle.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible));
\r
195 hlegend = new Button(hideGroup, support, SWT.CHECK);
\r
196 hlegend.setText("Legend");
\r
197 hlegend.setSelectionFactory(new BooleanPropertyFactory(null, JFreeChartResource.URIs.Chart_visibleLegend, true));
\r
198 hlegend.addSelectionListener(new BooleanSelectionListener(context, null, JFreeChartResource.URIs.Chart_visibleLegend));
\r
202 // Resize scrolled composite
\r
203 sc.setContent(composite);
\r
204 Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
\r
205 sc.setMinSize(size);
\r
210 * @author Teemu Lempinen
\r
213 private class TypeSelectionFactory extends ReadFactoryImpl<Resource, String> {
\r
215 public String perform(ReadGraph graph, Resource chart) throws DatabaseException {
\r
216 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
217 Layer0 l0 = Layer0.getInstance(graph);
\r
219 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));
\r
221 Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.CategoryDataset));
\r
223 if(dataset != null) {
\r
224 Resource renderer = graph.syncRequest(new PossibleObjectWithType(dataset, jfree.Dataset_renderer, jfree.Renderer));
\r
226 if(renderer != null && graph.isInstanceOf(renderer, jfree.StackedBarRenderer))
\r
235 * RangeItemFactory finds all inexes of a given enumeration
\r
236 * and adds "Sum" and "All" to the returned indexes
\r
237 * @author Teemu Lempinen
\r
240 private class TypeItemFactory extends ReadFactoryImpl<Resource, Map<String, Object>> {
\r
242 public Map<String, Object> perform(ReadGraph graph, Resource series) throws DatabaseException {
\r
243 LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();
\r
244 result.put("Normal", "Normal");
\r
245 result.put("Stacked", "Stacked");
\r
251 * TypeModifyListener for modifying the type of a bar chart
\r
252 * @author Teemu Lempinen
\r
255 private class TypeModifyListener extends ComboModifyListenerImpl<Resource> {
\r
257 public void applyText(WriteGraph graph, Resource chart, String text) throws DatabaseException {
\r
258 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
259 Layer0 l0 = Layer0.getInstance(graph);
\r
261 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));
\r
265 Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.CategoryDataset));
\r
266 if(dataset == null)
\r
269 graph.deny(dataset, jfree.Dataset_renderer);
\r
272 if(text.equals("Stacked"))
\r
273 renderer = GraphUtils.create2(graph, jfree.StackedBarRenderer);
\r
275 renderer = GraphUtils.create2(graph, jfree.BarRenderer);
\r
277 graph.claim(dataset, jfree.Dataset_renderer, renderer);
\r
283 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
286 private class OrientationSelectionFactory extends ReadFactoryImpl<Resource, String> {
\r
288 public String perform(ReadGraph graph, Resource chart) throws DatabaseException {
\r
289 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
290 Layer0 l0 = Layer0.getInstance(graph);
\r
292 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));
\r
294 Boolean orientation = graph.getPossibleRelatedValue(plot, jfree.Plot_orientation);
\r
295 if (orientation != null) {
\r
297 return "Horizontal";
\r
298 return "Vertical";
\r
307 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
310 private class OrientationItemFactory extends ReadFactoryImpl<Resource, Map<String, Object>> {
\r
312 public Map<String, Object> perform(ReadGraph graph, Resource series) throws DatabaseException {
\r
313 LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();
\r
314 result.put("Vertical", "Vertical");
\r
315 result.put("Horizontal", "Horizontal");
\r
322 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
325 private class OrientationModifyListener extends ComboModifyListenerImpl<Resource> {
\r
327 public void applyText(WriteGraph graph, Resource chart, String text) throws DatabaseException {
\r
328 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
329 Layer0 l0 = Layer0.getInstance(graph);
\r
331 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));
\r
336 if(text.equals("Horizontal"))
\r
337 graph.claimLiteral(plot, jfree.Plot_orientation,true);
\r
339 graph.claimLiteral(plot, jfree.Plot_orientation,false);
\r