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 Composite composite;
\r
68 private Button hgrid, htitle, hlegend;
\r
69 private TrackedText name, title, time;
\r
70 private TrackedCombo type;
\r
71 private TrackedCombo orientation;
\r
73 private boolean showTime = true;
\r
74 private boolean showFilter = false;
\r
75 private Group general;
\r
76 private Composite labelColumn1;
\r
77 private Composite propertyColumn1;
\r
78 private Composite labelColumn2;
\r
79 private Composite propertyColumn2;
\r
80 private Label labelName;
\r
81 private Label labelTitle;
\r
82 private Label labelTime;
\r
83 private Label labelType;
\r
84 private Group typeGroup;
\r
85 private Label labelOrientation;
\r
86 private Group hideGroup;
\r
87 private Label labelUse;
\r
88 private Label labelPercent;
\r
89 private Group filteringGroup;
\r
90 private Button useFilter;
\r
91 private TrackedText fraction;
\r
94 public BarGeneralPropertiesTab() {
\r
98 public BarGeneralPropertiesTab(int options) {
\r
99 showTime = ((options & ChartPropertyOptions.SHOW_TIME) > 0);
\r
100 showFilter = ((options & ChartPropertyOptions.SHOW_FILTER) > 0);
\r
105 * @author Teemu Lempinen
\r
108 private class TypeSelectionFactory extends ReadFactoryImpl<Resource, String> {
\r
110 public String perform(ReadGraph graph, Resource chart) throws DatabaseException {
\r
111 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
112 Layer0 l0 = Layer0.getInstance(graph);
\r
114 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));
\r
116 Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.CategoryDataset));
\r
118 if(dataset != null) {
\r
119 Resource renderer = graph.syncRequest(new PossibleObjectWithType(dataset, jfree.Dataset_renderer, jfree.Renderer));
\r
121 if(renderer != null && graph.isInstanceOf(renderer, jfree.StackedBarRenderer))
\r
130 * RangeItemFactory finds all inexes of a given enumeration
\r
131 * and adds "Sum" and "All" to the returned indexes
\r
132 * @author Teemu Lempinen
\r
135 private class TypeItemFactory extends ReadFactoryImpl<Resource, Map<String, Object>> {
\r
137 public Map<String, Object> perform(ReadGraph graph, Resource series) throws DatabaseException {
\r
138 LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();
\r
139 result.put("Normal", "Normal");
\r
140 result.put("Stacked", "Stacked");
\r
146 * TypeModifyListener for modifying the type of a bar chart
\r
147 * @author Teemu Lempinen
\r
150 private class TypeModifyListener extends ComboModifyListenerImpl<Resource> {
\r
152 public void applyText(WriteGraph graph, Resource chart, String text) 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
160 Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.CategoryDataset));
\r
161 if(dataset == null)
\r
164 graph.deny(dataset, jfree.Dataset_renderer);
\r
167 if(text.equals("Stacked"))
\r
168 renderer = GraphUtils.create2(graph, jfree.StackedBarRenderer);
\r
170 renderer = GraphUtils.create2(graph, jfree.BarRenderer);
\r
172 graph.claim(dataset, jfree.Dataset_renderer, renderer);
\r
178 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
181 private class OrientationSelectionFactory extends ReadFactoryImpl<Resource, String> {
\r
183 public String perform(ReadGraph graph, Resource chart) throws DatabaseException {
\r
184 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
185 Layer0 l0 = Layer0.getInstance(graph);
\r
187 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));
\r
189 Boolean orientation = graph.getPossibleRelatedValue(plot, jfree.Plot_orientation);
\r
190 if (orientation != null) {
\r
192 return "Horizontal";
\r
193 return "Vertical";
\r
202 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
205 private class OrientationItemFactory extends ReadFactoryImpl<Resource, Map<String, Object>> {
\r
207 public Map<String, Object> perform(ReadGraph graph, Resource series) throws DatabaseException {
\r
208 LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();
\r
209 result.put("Vertical", "Vertical");
\r
210 result.put("Horizontal", "Horizontal");
\r
217 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
220 private class OrientationModifyListener extends ComboModifyListenerImpl<Resource> {
\r
222 public void applyText(WriteGraph graph, Resource chart, String text) throws DatabaseException {
\r
223 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
224 Layer0 l0 = Layer0.getInstance(graph);
\r
226 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));
\r
231 if(text.equals("Horizontal"))
\r
232 graph.claimLiteral(plot, jfree.Plot_orientation,true);
\r
234 graph.claimLiteral(plot, jfree.Plot_orientation,false);
\r
241 protected void createAndAddControls(Composite body, IWorkbenchSite site,
\r
242 ISessionContext context, WidgetSupport support) {
\r
243 // Scrolled composite containing all of the properties in this tab
\r
244 sc = new ScrolledComposite(body, SWT.NONE | SWT.H_SCROLL | SWT.V_SCROLL);
\r
245 sc.setExpandHorizontal(true);
\r
246 sc.setExpandVertical(true);
\r
248 composite = new Composite(sc, SWT.NONE);
\r
250 // General properties
\r
251 general = new Group(composite, SWT.NONE);
\r
252 general.setText("General");
\r
254 // first column: labels
\r
255 labelColumn1 = new Composite(general, SWT.NONE);
\r
257 // second column: name and title
\r
258 propertyColumn1 = new Composite(general, SWT.NONE);
\r
260 // third column: labels
\r
261 labelColumn2 = new Composite(general, SWT.NONE);
\r
263 // fourth column: type and time
\r
264 propertyColumn2 = new Composite(general, SWT.NONE);
\r
267 labelName = new Label(labelColumn1, SWT.NONE);
\r
268 labelName.setText("Name:");
\r
270 name = new org.simantics.browsing.ui.swt.widgets.TrackedText(propertyColumn1, support, SWT.BORDER);
\r
271 name.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel));
\r
272 name.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel));
\r
273 name.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));
\r
275 labelTitle = new Label(labelColumn2, SWT.NONE);
\r
276 labelTitle = new Label(propertyColumn2, SWT.NONE);
\r
278 // Title (Which is different than name)
\r
279 labelTitle = new Label(labelColumn1, SWT.NONE);
\r
280 labelTitle.setText("Title:");
\r
282 title = new org.simantics.browsing.ui.swt.widgets.TrackedText(propertyColumn1, support, SWT.BORDER);
\r
283 title.setTextFactory(new TitleFactory());
\r
284 title.addModifyListener(new TitleModifier());
\r
285 title.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));
\r
289 labelTime = new Label(labelColumn2, SWT.NONE);
\r
290 labelTime.setText("Time:");
\r
292 time = new org.simantics.browsing.ui.swt.widgets.TrackedText(propertyColumn2, support, SWT.BORDER);
\r
293 time.setTextFactory(new DoublePropertyFactory(JFreeChartResource.URIs.Chart_time));
\r
294 time.addModifyListener(new DoublePropertyModifier(context, JFreeChartResource.URIs.Chart_time));
\r
295 time.setInputValidator(new DoubleValidator(true));
\r
296 time.setColorProvider(new JFreeChartPropertyColorProvider(time.getResourceManager()));
\r
299 typeGroup = new Group(composite,SWT.NONE);
\r
300 typeGroup.setText("Visuals");
\r
302 labelType = new Label(typeGroup, SWT.NONE);
\r
303 labelType.setText("Type:");
\r
305 type = new TrackedCombo(typeGroup, support, SWT.BORDER | SWT.READ_ONLY);
\r
306 type.addModifyListener(new TypeModifyListener());
\r
307 type.setItemFactory(new TypeItemFactory());
\r
308 type.setSelectionFactory(new TypeSelectionFactory());
\r
310 labelOrientation = new Label(typeGroup, SWT.NONE);
\r
311 labelOrientation.setText("Orientation:");
\r
313 orientation = new TrackedCombo(typeGroup, support, SWT.BORDER | SWT.READ_ONLY);
\r
314 orientation.addModifyListener(new OrientationModifyListener());
\r
315 orientation.setItemFactory(new OrientationItemFactory());
\r
316 orientation.setSelectionFactory(new OrientationSelectionFactory());
\r
319 // Group for hide options
\r
320 hideGroup = new Group(composite, SWT.NONE);
\r
321 hideGroup.setText("Hide");
\r
323 hgrid = new Button(hideGroup, support, SWT.CHECK);
\r
324 hgrid.setText("Grid");
\r
325 hgrid.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid, true));
\r
326 hgrid.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid));
\r
327 htitle = new Button(hideGroup, support, SWT.CHECK);
\r
328 htitle.setText("Title");
\r
329 htitle.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible, true));
\r
330 htitle.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible));
\r
331 hlegend = new Button(hideGroup, support, SWT.CHECK);
\r
332 hlegend.setText("Legend");
\r
333 hlegend.setSelectionFactory(new BooleanPropertyFactory(null, JFreeChartResource.URIs.Chart_visibleLegend, true));
\r
334 hlegend.addSelectionListener(new BooleanSelectionListener(context, null, JFreeChartResource.URIs.Chart_visibleLegend));
\r
337 filteringGroup = new Group(composite, SWT.NONE);
\r
338 filteringGroup.setText("Filter");
\r
339 labelUse = new Label(filteringGroup, SWT.NONE);
\r
340 labelUse.setText("Use:");
\r
341 useFilter = new Button(filteringGroup, support, SWT.CHECK);
\r
342 useFilter.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Filter_used, false));
\r
343 useFilter.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Filter_used));
\r
344 labelPercent = new Label(filteringGroup, SWT.NONE);
\r
345 labelPercent.setText("Percent:");
\r
346 fraction = new TrackedText(filteringGroup, support, SWT.BORDER);
\r
347 fraction.setTextFactory(new DoublePropertyFactory2(JFreeChartResource.URIs.Plot,JFreeChartResource.URIs.Filter_fraction));
\r
348 fraction.addModifyListener(new DoublePropertyModifier2(context, JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Filter_fraction));
\r
349 fraction.setInputValidator(new DoubleValidator(true));
\r
350 fraction.setColorProvider(new JFreeChartPropertyColorProvider(fraction.getResourceManager()));
\r
353 // Resize scrolled composite
\r
354 sc.setContent(composite);
\r
359 protected void createControlLayoutVertical() {
\r
360 // Scrolled composite containing all of the properties in this tab
\r
361 GridDataFactory.fillDefaults().grab(true, true).applyTo(sc);
\r
362 GridLayoutFactory.fillDefaults().applyTo(sc);
\r
365 GridLayoutFactory.fillDefaults().numColumns(1).margins(3, 3).applyTo(composite);
\r
367 GridLayoutFactory.fillDefaults().numColumns(1).margins(3, 3).applyTo(composite);
\r
369 // General properties
\r
370 GridDataFactory.fillDefaults().grab(true, false).applyTo(general);
\r
371 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(general);
\r
373 // first column: labels
\r
374 GridDataFactory.fillDefaults().grab(false, true).applyTo(labelColumn1);
\r
375 GridLayoutFactory.fillDefaults().applyTo(labelColumn1);
\r
377 // second column: name and title
\r
378 GridDataFactory.fillDefaults().grab(true, true).applyTo(propertyColumn1);
\r
379 GridLayoutFactory.fillDefaults().spacing(0, LayoutConstants.getSpacing().y).applyTo(propertyColumn1);
\r
381 // third column: labels
\r
382 GridDataFactory.fillDefaults().grab(false, true).applyTo(labelColumn2);
\r
383 GridLayoutFactory.fillDefaults().spacing(0, LayoutConstants.getSpacing().y).applyTo(labelColumn2);
\r
385 // fourth column: type and time
\r
386 GridDataFactory.fillDefaults().grab(false, true).applyTo(propertyColumn2);
\r
387 GridLayoutFactory.fillDefaults().applyTo(propertyColumn2);
\r
390 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelName);
\r
392 GridDataFactory.fillDefaults().grab(true, false).applyTo(name.getWidget());
\r
394 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelTitle);
\r
396 GridDataFactory.fillDefaults().grab(true, false).applyTo(title.getWidget());
\r
400 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelTime);
\r
402 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(time.getWidget());
\r
405 GridDataFactory.fillDefaults().applyTo(typeGroup);
\r
406 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(typeGroup);
\r
408 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelType);
\r
410 GridDataFactory.fillDefaults().applyTo(type.getWidget());
\r
412 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelOrientation);
\r
414 GridDataFactory.fillDefaults().applyTo(type.getWidget());
\r
416 // Group for hide options
\r
417 GridDataFactory.fillDefaults().applyTo(hideGroup);
\r
418 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(hideGroup);
\r
421 GridDataFactory.fillDefaults().applyTo(filteringGroup);
\r
422 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(filteringGroup);
\r
423 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelUse);
\r
424 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelPercent);
\r
425 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(fraction.getWidget());
\r
428 size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
\r
429 sc.setMinSize(size);
\r
433 protected void createControlLayoutHorizontal(boolean wideScreen) {
\r
434 // Scrolled composite containing all of the properties in this tab
\r
435 GridDataFactory.fillDefaults().grab(true, true).applyTo(sc);
\r
436 GridLayoutFactory.fillDefaults().applyTo(sc);
\r
439 GridLayoutFactory.fillDefaults().numColumns(4).margins(3, 3).applyTo(composite);
\r
441 GridLayoutFactory.fillDefaults().numColumns(3).margins(3, 3).applyTo(composite);
\r
443 // General properties
\r
444 GridDataFactory.fillDefaults().grab(true, false).applyTo(general);
\r
445 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(4).applyTo(general);
\r
447 // first column: labels
\r
448 GridDataFactory.fillDefaults().grab(false, true).applyTo(labelColumn1);
\r
449 GridLayoutFactory.fillDefaults().applyTo(labelColumn1);
\r
451 // second column: name and title
\r
452 GridDataFactory.fillDefaults().grab(true, true).applyTo(propertyColumn1);
\r
453 GridLayoutFactory.fillDefaults().spacing(0, LayoutConstants.getSpacing().y).applyTo(propertyColumn1);
\r
455 // third column: labels
\r
456 GridDataFactory.fillDefaults().grab(false, true).applyTo(labelColumn2);
\r
457 GridLayoutFactory.fillDefaults().spacing(0, LayoutConstants.getSpacing().y).applyTo(labelColumn2);
\r
459 // fourth column: type and time
\r
460 GridDataFactory.fillDefaults().grab(false, true).applyTo(propertyColumn2);
\r
461 GridLayoutFactory.fillDefaults().applyTo(propertyColumn2);
\r
464 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelName);
\r
466 GridDataFactory.fillDefaults().grab(true, false).applyTo(name.getWidget());
\r
468 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelTitle);
\r
470 GridDataFactory.fillDefaults().grab(true, false).applyTo(title.getWidget());
\r
474 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelTime);
\r
476 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(time.getWidget());
\r
479 GridDataFactory.fillDefaults().applyTo(typeGroup);
\r
480 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(typeGroup);
\r
482 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelType);
\r
484 GridDataFactory.fillDefaults().applyTo(type.getWidget());
\r
486 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelOrientation);
\r
488 GridDataFactory.fillDefaults().applyTo(type.getWidget());
\r
490 // Group for hide options
\r
491 GridDataFactory.fillDefaults().applyTo(hideGroup);
\r
492 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(hideGroup);
\r
495 GridDataFactory.fillDefaults().applyTo(filteringGroup);
\r
496 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(filteringGroup);
\r
497 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelUse);
\r
498 GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(labelPercent);
\r
499 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(fraction.getWidget());
\r
502 size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
\r
503 sc.setMinSize(size);
\r