1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011, 2014 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.ChartPropertyOptions;
\r
42 import org.simantics.jfreechart.chart.properties.AdjustableTab;
\r
43 import org.simantics.jfreechart.chart.properties.BooleanPropertyFactory;
\r
44 import org.simantics.jfreechart.chart.properties.BooleanSelectionListener;
\r
45 import org.simantics.jfreechart.chart.properties.DoublePropertyFactory2;
\r
46 import org.simantics.jfreechart.chart.properties.DoublePropertyModifier2;
\r
47 import org.simantics.jfreechart.chart.properties.DoubleValidator;
\r
48 import org.simantics.jfreechart.chart.properties.JFreeChartPropertyColorProvider;
\r
49 import org.simantics.jfreechart.chart.properties.TitleFactory;
\r
50 import org.simantics.jfreechart.chart.properties.TitleModifier;
\r
51 import org.simantics.layer0.Layer0;
\r
52 import org.simantics.layer0.utils.direct.GraphUtils;
\r
53 import org.simantics.modeling.ui.chart.property.DoublePropertyFactory;
\r
54 import org.simantics.modeling.ui.chart.property.DoublePropertyModifier;
\r
55 import org.simantics.sysdyn.JFreeChartResource;
\r
59 * General properties of a bar chart
\r
60 * @author Teemu Lempinen
\r
61 * @author Tuomas Miettinen
\r
64 public class BarGeneralPropertiesTab extends AdjustableTab {
\r
66 private ScrolledComposite sc;
\r
67 private Button hgrid, htitle, hlegend;
\r
68 private TrackedText name, title, time;
\r
69 private TrackedCombo type;
\r
70 private TrackedCombo orientation;
\r
72 private boolean showTime = true;
\r
73 private boolean showFilter = false;
\r
74 private Group general;
\r
75 private Composite labelColumn1;
\r
76 private Composite propertyColumn1;
\r
77 private Composite labelColumn2;
\r
78 private Composite propertyColumn2;
\r
79 private Label labelName;
\r
80 private Label labelTitle;
\r
81 private Label labelTime;
\r
82 private Label labelType;
\r
83 private Group typeGroup;
\r
84 private Label labelOrientation;
\r
85 private Group hideGroup;
\r
86 private Label labelUse;
\r
87 private Label labelPercent;
\r
88 private Group filteringGroup;
\r
89 private Button useFilter;
\r
90 private TrackedText fraction;
\r
93 public BarGeneralPropertiesTab() {
\r
97 public BarGeneralPropertiesTab(int options) {
\r
98 showTime = ((options & ChartPropertyOptions.SHOW_TIME) > 0);
\r
99 showFilter = ((options & ChartPropertyOptions.SHOW_FILTER) > 0);
\r
104 * @author Teemu Lempinen
\r
107 private class TypeSelectionFactory extends ReadFactoryImpl<Resource, String> {
\r
109 public String perform(ReadGraph graph, Resource chart) throws DatabaseException {
\r
110 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
111 Layer0 l0 = Layer0.getInstance(graph);
\r
113 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));
\r
115 Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.CategoryDataset));
\r
117 if(dataset != null) {
\r
118 Resource renderer = graph.syncRequest(new PossibleObjectWithType(dataset, jfree.Dataset_renderer, jfree.Renderer));
\r
120 if(renderer != null && graph.isInstanceOf(renderer, jfree.StackedBarRenderer))
\r
129 * RangeItemFactory finds all inexes of a given enumeration
\r
130 * and adds "Sum" and "All" to the returned indexes
\r
131 * @author Teemu Lempinen
\r
134 private class TypeItemFactory extends ReadFactoryImpl<Resource, Map<String, Object>> {
\r
136 public Map<String, Object> perform(ReadGraph graph, Resource series) throws DatabaseException {
\r
137 LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();
\r
138 result.put("Normal", "Normal");
\r
139 result.put("Stacked", "Stacked");
\r
145 * TypeModifyListener for modifying the type of a bar chart
\r
146 * @author Teemu Lempinen
\r
149 private class TypeModifyListener extends ComboModifyListenerImpl<Resource> {
\r
151 public void applyText(WriteGraph graph, Resource chart, String text) throws DatabaseException {
\r
152 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
153 Layer0 l0 = Layer0.getInstance(graph);
\r
155 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));
\r
159 Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.CategoryDataset));
\r
160 if(dataset == null)
\r
163 graph.deny(dataset, jfree.Dataset_renderer);
\r
166 if(text.equals("Stacked"))
\r
167 renderer = GraphUtils.create2(graph, jfree.StackedBarRenderer);
\r
169 renderer = GraphUtils.create2(graph, jfree.BarRenderer);
\r
171 graph.claim(dataset, jfree.Dataset_renderer, renderer);
\r
177 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
180 private class OrientationSelectionFactory extends ReadFactoryImpl<Resource, String> {
\r
182 public String perform(ReadGraph graph, Resource chart) throws DatabaseException {
\r
183 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
184 Layer0 l0 = Layer0.getInstance(graph);
\r
186 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));
\r
188 Boolean orientation = graph.getPossibleRelatedValue(plot, jfree.Plot_orientation);
\r
189 if (orientation != null) {
\r
191 return "Horizontal";
\r
192 return "Vertical";
\r
201 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
204 private class OrientationItemFactory extends ReadFactoryImpl<Resource, Map<String, Object>> {
\r
206 public Map<String, Object> perform(ReadGraph graph, Resource series) throws DatabaseException {
\r
207 LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();
\r
208 result.put("Vertical", "Vertical");
\r
209 result.put("Horizontal", "Horizontal");
\r
216 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
219 private class OrientationModifyListener extends ComboModifyListenerImpl<Resource> {
\r
221 public void applyText(WriteGraph graph, Resource chart, String text) throws DatabaseException {
\r
222 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
223 Layer0 l0 = Layer0.getInstance(graph);
\r
225 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));
\r
230 if(text.equals("Horizontal"))
\r
231 graph.claimLiteral(plot, jfree.Plot_orientation,true);
\r
233 graph.claimLiteral(plot, jfree.Plot_orientation,false);
\r
240 protected void createAndAddControls(Composite body, IWorkbenchSite site,
\r
241 ISessionContext context, WidgetSupport support) {
\r
242 // Scrolled composite containing all of the properties in this tab
\r
243 sc = new ScrolledComposite(body, SWT.NONE | SWT.H_SCROLL | SWT.V_SCROLL);
\r
244 sc.setExpandHorizontal(true);
\r
245 sc.setExpandVertical(true);
\r
247 composite = new Composite(sc, SWT.NONE);
\r
249 // General properties
\r
250 general = new Group(composite, SWT.NONE);
\r
251 general.setText("General");
\r
253 // first column: labels
\r
254 labelColumn1 = new Composite(general, SWT.NONE);
\r
256 // second column: name and title
\r
257 propertyColumn1 = new Composite(general, SWT.NONE);
\r
259 // third column: labels
\r
260 labelColumn2 = new Composite(general, SWT.NONE);
\r
262 // fourth column: type and time
\r
263 propertyColumn2 = new Composite(general, SWT.NONE);
\r
266 labelName = new Label(labelColumn1, SWT.NONE);
\r
267 labelName.setText("Name:");
\r
269 name = new org.simantics.browsing.ui.swt.widgets.TrackedText(propertyColumn1, support, SWT.BORDER);
\r
270 name.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel));
\r
271 name.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel));
\r
272 name.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));
\r
274 labelTitle = new Label(labelColumn2, SWT.NONE);
\r
275 labelTitle = new Label(propertyColumn2, SWT.NONE);
\r
277 // Title (Which is different than name)
\r
278 labelTitle = new Label(labelColumn1, SWT.NONE);
\r
279 labelTitle.setText("Title:");
\r
281 title = new org.simantics.browsing.ui.swt.widgets.TrackedText(propertyColumn1, support, SWT.BORDER);
\r
282 title.setTextFactory(new TitleFactory());
\r
283 title.addModifyListener(new TitleModifier());
\r
284 title.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));
\r
288 labelTime = new Label(labelColumn2, SWT.NONE);
\r
289 labelTime.setText("Time:");
\r
291 time = new org.simantics.browsing.ui.swt.widgets.TrackedText(propertyColumn2, support, SWT.BORDER);
\r
292 time.setTextFactory(new DoublePropertyFactory(JFreeChartResource.URIs.Chart_time));
\r
293 time.addModifyListener(new DoublePropertyModifier(context, JFreeChartResource.URIs.Chart_time));
\r
294 time.setInputValidator(new DoubleValidator(true));
\r
295 time.setColorProvider(new JFreeChartPropertyColorProvider(time.getResourceManager()));
\r
298 typeGroup = new Group(composite,SWT.NONE);
\r
299 typeGroup.setText("Visuals");
\r
301 labelType = new Label(typeGroup, SWT.NONE);
\r
302 labelType.setText("Type:");
\r
304 type = new TrackedCombo(typeGroup, support, SWT.BORDER | SWT.READ_ONLY);
\r
305 type.addModifyListener(new TypeModifyListener());
\r
306 type.setItemFactory(new TypeItemFactory());
\r
307 type.setSelectionFactory(new TypeSelectionFactory());
\r
309 labelOrientation = new Label(typeGroup, SWT.NONE);
\r
310 labelOrientation.setText("Orientation:");
\r
312 orientation = new TrackedCombo(typeGroup, support, SWT.BORDER | SWT.READ_ONLY);
\r
313 orientation.addModifyListener(new OrientationModifyListener());
\r
314 orientation.setItemFactory(new OrientationItemFactory());
\r
315 orientation.setSelectionFactory(new OrientationSelectionFactory());
\r
318 // Group for hide options
\r
319 hideGroup = new Group(composite, SWT.NONE);
\r
320 hideGroup.setText("Hide");
\r
322 hgrid = new Button(hideGroup, support, SWT.CHECK);
\r
323 hgrid.setText("Grid");
\r
324 hgrid.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid, true));
\r
325 hgrid.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid));
\r
326 htitle = new Button(hideGroup, support, SWT.CHECK);
\r
327 htitle.setText("Title");
\r
328 htitle.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible, true));
\r
329 htitle.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible));
\r
330 hlegend = new Button(hideGroup, support, SWT.CHECK);
\r
331 hlegend.setText("Legend");
\r
332 hlegend.setSelectionFactory(new BooleanPropertyFactory(null, JFreeChartResource.URIs.Chart_visibleLegend, true));
\r
333 hlegend.addSelectionListener(new BooleanSelectionListener(context, null, JFreeChartResource.URIs.Chart_visibleLegend));
\r
336 filteringGroup = new Group(composite, SWT.NONE);
\r
337 filteringGroup.setText("Filter");
\r
338 labelUse = new Label(filteringGroup, SWT.NONE);
\r
339 labelUse.setText("Use:");
\r
340 useFilter = new Button(filteringGroup, support, SWT.CHECK);
\r
341 useFilter.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Filter_used, false));
\r
342 useFilter.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Filter_used));
\r
343 labelPercent = new Label(filteringGroup, SWT.NONE);
\r
344 labelPercent.setText("Percent:");
\r
345 fraction = new TrackedText(filteringGroup, support, SWT.BORDER);
\r
346 fraction.setTextFactory(new DoublePropertyFactory2(JFreeChartResource.URIs.Plot,JFreeChartResource.URIs.Filter_fraction));
\r
347 fraction.addModifyListener(new DoublePropertyModifier2(context, JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Filter_fraction));
\r
348 fraction.setInputValidator(new DoubleValidator(true));
\r
349 fraction.setColorProvider(new JFreeChartPropertyColorProvider(fraction.getResourceManager()));
\r
352 // Resize scrolled composite
\r
353 sc.setContent(composite);
\r
358 protected void createControlLayoutVertical() {
\r
359 // Scrolled composite containing all of the properties in this tab
\r
360 GridDataFactory.fillDefaults().grab(true, true).applyTo(sc);
\r
361 GridLayoutFactory.fillDefaults().applyTo(sc);
\r
364 GridLayoutFactory.fillDefaults().numColumns(1).margins(3, 3).applyTo(composite);
\r
366 GridLayoutFactory.fillDefaults().numColumns(1).margins(3, 3).applyTo(composite);
\r
368 // General properties
\r
369 GridDataFactory.fillDefaults().grab(true, false).applyTo(general);
\r
370 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(general);
\r
372 // first column: labels
\r
373 GridDataFactory.fillDefaults().grab(false, true).applyTo(labelColumn1);
\r
374 GridLayoutFactory.fillDefaults().applyTo(labelColumn1);
\r
376 // second column: name and title
\r
377 GridDataFactory.fillDefaults().grab(true, true).applyTo(propertyColumn1);
\r
378 GridLayoutFactory.fillDefaults().spacing(0, LayoutConstants.getSpacing().y).applyTo(propertyColumn1);
\r
380 // third column: labels
\r
381 GridDataFactory.fillDefaults().grab(false, true).applyTo(labelColumn2);
\r
382 GridLayoutFactory.fillDefaults().spacing(0, LayoutConstants.getSpacing().y).applyTo(labelColumn2);
\r
384 // fourth column: type and time
\r
385 GridDataFactory.fillDefaults().grab(false, true).applyTo(propertyColumn2);
\r
386 GridLayoutFactory.fillDefaults().applyTo(propertyColumn2);
\r
389 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelName);
\r
391 GridDataFactory.fillDefaults().grab(true, false).applyTo(name.getWidget());
\r
393 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelTitle);
\r
395 GridDataFactory.fillDefaults().grab(true, false).applyTo(title.getWidget());
\r
399 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelTime);
\r
401 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(time.getWidget());
\r
404 GridDataFactory.fillDefaults().applyTo(typeGroup);
\r
405 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(typeGroup);
\r
407 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelType);
\r
409 GridDataFactory.fillDefaults().applyTo(type.getWidget());
\r
411 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelOrientation);
\r
413 GridDataFactory.fillDefaults().applyTo(type.getWidget());
\r
415 // Group for hide options
\r
416 GridDataFactory.fillDefaults().applyTo(hideGroup);
\r
417 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(hideGroup);
\r
420 GridDataFactory.fillDefaults().applyTo(filteringGroup);
\r
421 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(filteringGroup);
\r
422 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelUse);
\r
423 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelPercent);
\r
424 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(fraction.getWidget());
\r
427 size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
\r
428 sc.setMinSize(size);
\r
432 protected void createControlLayoutHorizontal(boolean wideScreen) {
\r
433 // Scrolled composite containing all of the properties in this tab
\r
434 GridDataFactory.fillDefaults().grab(true, true).applyTo(sc);
\r
435 GridLayoutFactory.fillDefaults().applyTo(sc);
\r
438 GridLayoutFactory.fillDefaults().numColumns(4).margins(3, 3).applyTo(composite);
\r
440 GridLayoutFactory.fillDefaults().numColumns(3).margins(3, 3).applyTo(composite);
\r
442 // General properties
\r
443 GridDataFactory.fillDefaults().grab(true, false).applyTo(general);
\r
444 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(4).applyTo(general);
\r
446 // first column: labels
\r
447 GridDataFactory.fillDefaults().grab(false, true).applyTo(labelColumn1);
\r
448 GridLayoutFactory.fillDefaults().applyTo(labelColumn1);
\r
450 // second column: name and title
\r
451 GridDataFactory.fillDefaults().grab(true, true).applyTo(propertyColumn1);
\r
452 GridLayoutFactory.fillDefaults().spacing(0, LayoutConstants.getSpacing().y).applyTo(propertyColumn1);
\r
454 // third column: labels
\r
455 GridDataFactory.fillDefaults().grab(false, true).applyTo(labelColumn2);
\r
456 GridLayoutFactory.fillDefaults().spacing(0, LayoutConstants.getSpacing().y).applyTo(labelColumn2);
\r
458 // fourth column: type and time
\r
459 GridDataFactory.fillDefaults().grab(false, true).applyTo(propertyColumn2);
\r
460 GridLayoutFactory.fillDefaults().applyTo(propertyColumn2);
\r
463 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelName);
\r
465 GridDataFactory.fillDefaults().grab(true, false).applyTo(name.getWidget());
\r
467 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelTitle);
\r
469 GridDataFactory.fillDefaults().grab(true, false).applyTo(title.getWidget());
\r
473 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelTime);
\r
475 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(time.getWidget());
\r
478 GridDataFactory.fillDefaults().applyTo(typeGroup);
\r
479 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(typeGroup);
\r
481 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelType);
\r
483 GridDataFactory.fillDefaults().applyTo(type.getWidget());
\r
485 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelOrientation);
\r
487 GridDataFactory.fillDefaults().applyTo(type.getWidget());
\r
489 // Group for hide options
\r
490 GridDataFactory.fillDefaults().applyTo(hideGroup);
\r
491 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(hideGroup);
\r
494 GridDataFactory.fillDefaults().applyTo(filteringGroup);
\r
495 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(filteringGroup);
\r
496 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelUse);
\r
497 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelPercent);
\r
498 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(fraction.getWidget());
\r
501 size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
\r
502 sc.setMinSize(size);
\r