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.xyline;
\r
14 import java.util.Collection;
\r
15 import java.util.LinkedHashMap;
\r
16 import java.util.Map;
\r
18 import org.eclipse.jface.layout.GridDataFactory;
\r
19 import org.eclipse.jface.layout.GridLayoutFactory;
\r
20 import org.eclipse.jface.viewers.StructuredSelection;
\r
21 import org.eclipse.swt.SWT;
\r
22 import org.eclipse.swt.custom.ScrolledComposite;
\r
23 import org.eclipse.swt.graphics.Point;
\r
24 import org.eclipse.swt.widgets.Composite;
\r
25 import org.eclipse.swt.widgets.Display;
\r
26 import org.eclipse.swt.widgets.Group;
\r
27 import org.eclipse.swt.widgets.Label;
\r
28 import org.eclipse.ui.IWorkbenchSite;
\r
29 import org.simantics.browsing.ui.swt.widgets.Button;
\r
30 import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory;
\r
31 import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier;
\r
32 import org.simantics.browsing.ui.swt.widgets.TrackedCombo;
\r
33 import org.simantics.browsing.ui.swt.widgets.TrackedText;
\r
34 import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl;
\r
35 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;
\r
36 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
\r
37 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
\r
38 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;
\r
39 import org.simantics.db.ReadGraph;
\r
40 import org.simantics.db.Resource;
\r
41 import org.simantics.db.WriteGraph;
\r
42 import org.simantics.db.common.request.ObjectsWithType;
\r
43 import org.simantics.db.common.request.PossibleObjectWithType;
\r
44 import org.simantics.db.common.request.ReadRequest;
\r
45 import org.simantics.db.exception.DatabaseException;
\r
46 import org.simantics.db.management.ISessionContext;
\r
47 import org.simantics.jfreechart.chart.properties.AdjustableTab;
\r
48 import org.simantics.jfreechart.chart.properties.AxisHidePropertyComposite;
\r
49 import org.simantics.jfreechart.chart.properties.BooleanPropertyFactory;
\r
50 import org.simantics.jfreechart.chart.properties.BooleanSelectionListener;
\r
51 import org.simantics.jfreechart.chart.properties.DoubleValidator;
\r
52 import org.simantics.jfreechart.chart.properties.JFreeChartPropertyColorProvider;
\r
53 import org.simantics.jfreechart.chart.properties.RVIFactory;
\r
54 import org.simantics.jfreechart.chart.properties.RVIModifier;
\r
55 import org.simantics.jfreechart.chart.properties.TitleFactory;
\r
56 import org.simantics.jfreechart.chart.properties.TitleModifier;
\r
57 import org.simantics.jfreechart.chart.properties.VariableExistsValidator;
\r
58 import org.simantics.layer0.Layer0;
\r
59 import org.simantics.layer0.utils.direct.GraphUtils;
\r
60 import org.simantics.modeling.ui.chart.property.DoublePropertyFactory;
\r
61 import org.simantics.modeling.ui.chart.property.DoublePropertyModifier;
\r
62 import org.simantics.sysdyn.JFreeChartResource;
\r
63 import org.simantics.utils.ui.AdaptionUtils;
\r
66 * PropertyTab displaying general properties and x-axis properties of a chart
\r
68 * @author Teemu Lempinen
\r
69 * @author Tuomas Miettinen
\r
72 public class XYLineGeneralPropertiesTab extends AdjustableTab implements Widget {
\r
74 private ScrolledComposite sc;
\r
75 private TrackedText name, title, xlabel, xvariable, xmin, xmax;
\r
76 private TrackedCombo type;
\r
77 private Button hgrid, htitle, hlegend;
\r
78 private WidgetSupportImpl domainAxisSupport = new WidgetSupportImpl();
\r
79 private Group general;
\r
80 private Label nameLabel;
\r
81 private Label labelTitle;
\r
82 private Label labelType;
\r
83 private Group hideGroup;
\r
84 private Group xgroup;
\r
85 private Label xVariableLabel;
\r
86 private Label labelMin;
\r
87 private AxisHidePropertyComposite axisHide;
\r
88 private Label labelLabel;
\r
89 private Label labelMax;
\r
90 private Composite xColumn1;
\r
91 private Composite xColumn2;
\r
94 public void setInput(final ISessionContext context, Object input) {
\r
95 final Resource chart = AdaptionUtils.adaptToSingle(input, Resource.class);
\r
99 context.getSession().asyncRequest(new ReadRequest() {
\r
102 public void run(ReadGraph graph) throws DatabaseException {
\r
103 Layer0 l0 = Layer0.getInstance(graph);
\r
104 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
105 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.Plot));
\r
106 if(plot == null) return;
\r
107 final Resource domainAxis = graph.getPossibleObject(plot, jfree.Plot_domainAxis);
\r
108 if(domainAxis == null) return;
\r
109 Display.getDefault().asyncExec(new Runnable() {
\r
111 public void run() {
\r
112 domainAxisSupport.fireInput(context, new StructuredSelection(domainAxis));
\r
122 * @author Teemu Lempinen
\r
125 private class TypeSelectionFactory extends ReadFactoryImpl<Resource, String> {
\r
127 public String perform(ReadGraph graph, Resource chart) throws DatabaseException {
\r
128 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
129 Layer0 l0 = Layer0.getInstance(graph);
\r
131 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.XYPlot));
\r
133 Collection<Resource> datasets = graph.syncRequest(new ObjectsWithType(plot, l0.ConsistsOf, jfree.XYDataset));
\r
134 if(!datasets.isEmpty()) {
\r
135 Resource dataset = datasets.iterator().next();
\r
136 if(dataset != null) {
\r
137 Resource renderer = graph.syncRequest(new PossibleObjectWithType(dataset, jfree.Dataset_renderer, jfree.Renderer));
\r
138 if(renderer != null && graph.isInstanceOf(renderer, jfree.XYAreaRenderer))
\r
148 * RangeItemFactory finds all inexes of a given enumeration
\r
149 * and adds "Sum" and "All" to the returned indexes
\r
150 * @author Teemu Lempinen
\r
153 private class TypeItemFactory extends ReadFactoryImpl<Resource, Map<String, Object>> {
\r
155 public Map<String, Object> perform(ReadGraph graph, Resource series) throws DatabaseException {
\r
156 LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();
\r
157 result.put("Line", "Line");
\r
158 result.put("Area", "Area");
\r
159 // result.put("Stacked Area", "Stacked Area");
\r
165 * TypeModifyListener for modifying the type of a bar chart
\r
166 * @author Teemu Lempinen
\r
169 private class TypeModifyListener extends ComboModifyListenerImpl<Resource> {
\r
171 public void applyText(WriteGraph graph, Resource chart, String text) throws DatabaseException {
\r
172 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
173 Layer0 l0 = Layer0.getInstance(graph);
\r
175 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.XYPlot));
\r
179 Collection<Resource> datasets = graph.syncRequest(new ObjectsWithType(plot, l0.ConsistsOf, jfree.XYDataset));
\r
180 if(datasets == null || datasets.isEmpty())
\r
183 for(Resource dataset : datasets) {
\r
184 graph.deny(dataset, jfree.Dataset_renderer);
\r
187 if(text.equals("Area"))
\r
188 renderer = GraphUtils.create2(graph, jfree.XYAreaRenderer);
\r
190 renderer = GraphUtils.create2(graph, jfree.XYLineRenderer);
\r
192 graph.claim(dataset, jfree.Dataset_renderer, renderer);
\r
198 protected void createAndAddControls(Composite body, IWorkbenchSite site,
\r
199 ISessionContext context, WidgetSupport support) {
\r
200 support.register(this);
\r
202 // Scrolled composite containing all of the properties in this tab
\r
203 sc = new ScrolledComposite(body, SWT.NONE | SWT.H_SCROLL | SWT.V_SCROLL);
\r
204 sc.setExpandHorizontal(true);
\r
205 sc.setExpandVertical(true);
\r
207 composite = new Composite(sc, SWT.NONE);
\r
209 // General properties
\r
210 general = new Group(composite, SWT.NONE);
\r
211 general.setText("General");
\r
214 nameLabel = new Label(general, SWT.NONE);
\r
215 nameLabel.setText("Name:");
\r
216 nameLabel.setAlignment(SWT.RIGHT);
\r
218 name = new org.simantics.browsing.ui.swt.widgets.TrackedText(general, support, SWT.BORDER);
\r
219 name.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel));
\r
220 name.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel));
\r
221 name.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));
\r
224 labelType = new Label(general, SWT.NONE);
\r
225 labelType.setText("Type:");
\r
227 type = new TrackedCombo(general, support, SWT.BORDER | SWT.READ_ONLY);
\r
228 type.addModifyListener(new TypeModifyListener());
\r
229 type.setItemFactory(new TypeItemFactory());
\r
230 type.setSelectionFactory(new TypeSelectionFactory());
\r
232 // Title (Which is different than name)
\r
233 labelTitle = new Label(general, SWT.NONE);
\r
234 labelTitle.setText("Title:");
\r
236 title = new org.simantics.browsing.ui.swt.widgets.TrackedText(general, support, SWT.BORDER);
\r
237 title.setTextFactory(new TitleFactory());
\r
238 title.addModifyListener(new TitleModifier());
\r
239 title.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));
\r
241 // Group for hide options
\r
242 hideGroup = new Group(composite, SWT.NONE);
\r
243 hideGroup.setText("Hide");
\r
245 hgrid = new Button(hideGroup, support, SWT.CHECK);
\r
246 hgrid.setText("Grid");
\r
247 hgrid.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid, true));
\r
248 hgrid.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid));
\r
249 htitle = new Button(hideGroup, support, SWT.CHECK);
\r
250 htitle.setText("Title");
\r
251 htitle.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible, true));
\r
252 htitle.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible));
\r
253 hlegend = new Button(hideGroup, support, SWT.CHECK);
\r
254 hlegend.setText("Legend");
\r
255 hlegend.setSelectionFactory(new BooleanPropertyFactory(null, JFreeChartResource.URIs.Chart_visibleLegend, true));
\r
256 hlegend.addSelectionListener(new BooleanSelectionListener(context, null, JFreeChartResource.URIs.Chart_visibleLegend));
\r
259 // X-Axis properties
\r
260 xgroup = new Group(composite, SWT.NONE);
\r
261 xgroup.setText("X-axis");
\r
263 xColumn1 = new Composite(xgroup, SWT.NONE);
\r
265 // Variable for x-axis (default: empty == time)
\r
266 xVariableLabel = new Label(xColumn1, SWT.NONE);
\r
267 xVariableLabel.setText("Variable:");
\r
269 xvariable = new TrackedText(xColumn1, domainAxisSupport, SWT.BORDER);
\r
270 xvariable.setTextFactory(new RVIFactory());
\r
271 xvariable.addModifyListener(new RVIModifier(xvariable.getWidget(), domainAxisSupport));
\r
272 xvariable.setColorProvider(new JFreeChartPropertyColorProvider(xvariable.getResourceManager()));
\r
273 xvariable.setInputValidator(new VariableExistsValidator(support, xvariable, true));
\r
275 // Label for x-axis
\r
276 labelLabel = new Label(xColumn1, SWT.NONE);
\r
277 labelLabel.setText("Label:");
\r
279 xlabel = new TrackedText(xColumn1, domainAxisSupport, SWT.BORDER);
\r
280 xlabel.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel, ""));
\r
281 xlabel.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel));
\r
282 xlabel.setColorProvider(new JFreeChartPropertyColorProvider(xlabel.getResourceManager()));
\r
284 xColumn2 = new Composite(xgroup, SWT.NONE);
\r
286 // Min value for x-axis
\r
287 labelMin = new Label(xColumn2, SWT.NONE);
\r
288 labelMin.setText("Min:");
\r
289 labelMin.setAlignment(SWT.RIGHT);
\r
291 xmin = new TrackedText(xColumn2, domainAxisSupport, SWT.BORDER);
\r
292 xmin.setColorProvider(new JFreeChartPropertyColorProvider(xmin.getResourceManager()));
\r
293 xmin.setTextFactory(new DoublePropertyFactory(JFreeChartResource.URIs.Axis_min));
\r
294 xmin.addModifyListener(new DoublePropertyModifier(context, JFreeChartResource.URIs.Axis_min));
\r
295 xmin.setInputValidator(new DoubleValidator(true));
\r
297 // Max value for x-axis
\r
298 labelMax = new Label(xColumn2, SWT.NONE);
\r
299 labelMax.setText("Max:");
\r
301 xmax = new TrackedText(xColumn2, domainAxisSupport, SWT.BORDER);
\r
302 xmax.setColorProvider(new JFreeChartPropertyColorProvider(xmax.getResourceManager()));
\r
303 xmax.setTextFactory(new DoublePropertyFactory(JFreeChartResource.URIs.Axis_max));
\r
304 xmax.addModifyListener(new DoublePropertyModifier(context, JFreeChartResource.URIs.Axis_max));
\r
305 xmax.setInputValidator(new DoubleValidator(true));
\r
307 // Axis hide buttons
\r
308 axisHide = new AxisHidePropertyComposite(composite, context, domainAxisSupport, SWT.NONE);
\r
310 sc.setContent(composite);
\r
314 protected void createControlLayoutVertical() {
\r
315 // Scrolled composite containing all of the properties in this tab
\r
316 GridDataFactory.fillDefaults().grab(true, true).applyTo(sc);
\r
317 GridLayoutFactory.fillDefaults().applyTo(sc);
\r
319 GridLayoutFactory.fillDefaults().numColumns(1).margins(3, 3).applyTo(composite);
\r
321 // General properties
\r
322 GridDataFactory.fillDefaults().grab(true, false).applyTo(general);
\r
323 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(general);
\r
326 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(nameLabel);
\r
328 GridDataFactory.fillDefaults().grab(true, false).applyTo(name.getWidget());
\r
331 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(labelType);
\r
333 GridDataFactory.fillDefaults().applyTo(type.getWidget());
\r
335 // Title (Which is different than name)
\r
336 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(labelTitle);
\r
338 GridDataFactory.fillDefaults().span(1, 1).grab(true, false).applyTo(title.getWidget());
\r
340 // Group for hide options
\r
341 GridDataFactory.fillDefaults().applyTo(hideGroup);
\r
342 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(hideGroup);
\r
344 // X-Axis properties
\r
345 GridDataFactory.fillDefaults().grab(true, false).applyTo(xgroup);
\r
346 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(1).applyTo(xgroup);
\r
348 GridDataFactory.fillDefaults().grab(true, false).applyTo(xColumn1);
\r
349 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(xColumn1);
\r
351 // Variable for x-axis (default: empty == time)
\r
352 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(xVariableLabel);
\r
354 GridDataFactory.fillDefaults().grab(true, false).applyTo(xvariable.getWidget());
\r
356 // Label for x-axis
\r
357 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(labelLabel);
\r
359 GridDataFactory.fillDefaults().grab(true, false).applyTo(xlabel.getWidget());
\r
361 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(xColumn2);
\r
363 // Min value for x-axis
\r
364 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(labelMin);
\r
365 GridDataFactory.fillDefaults().applyTo(xmin.getWidget());
\r
367 // Max value for x-axis
\r
368 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(labelMax);
\r
369 GridDataFactory.fillDefaults().applyTo(xmax.getWidget());
\r
371 // Axis hide buttons
\r
372 GridDataFactory.fillDefaults().applyTo(axisHide);
\r
374 // Set the same width to both label rows
\r
375 composite.layout();
\r
376 GridDataFactory.fillDefaults().hint(xVariableLabel.getBounds().width, SWT.DEFAULT).align(SWT.END, SWT.CENTER).applyTo(nameLabel);
\r
377 GridDataFactory.fillDefaults().hint(xVariableLabel.getBounds().width, SWT.DEFAULT).align(SWT.END, SWT.CENTER).applyTo(labelMin);
\r
379 Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
\r
380 sc.setMinSize(size);
\r
384 protected void createControlLayoutHorizontal(boolean wideScreen) {
\r
385 // Scrolled composite containing all of the properties in this tab
\r
386 GridDataFactory.fillDefaults().grab(true, true).applyTo(sc);
\r
387 GridLayoutFactory.fillDefaults().applyTo(sc);
\r
389 GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).applyTo(composite);
\r
391 // General properties
\r
392 GridDataFactory.fillDefaults().grab(true, false).applyTo(general);
\r
393 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(4).applyTo(general);
\r
396 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(nameLabel);
\r
398 GridDataFactory.fillDefaults().grab(true, false).applyTo(name.getWidget());
\r
401 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(labelType);
\r
403 GridDataFactory.fillDefaults().applyTo(type.getWidget());
\r
405 // Title (Which is different than name)
\r
406 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(labelTitle);
\r
408 GridDataFactory.fillDefaults().span(3, 1).grab(true, false).applyTo(title.getWidget());
\r
410 // Group for hide options
\r
411 GridDataFactory.fillDefaults().applyTo(hideGroup);
\r
412 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(hideGroup);
\r
414 // X-Axis properties
\r
415 GridDataFactory.fillDefaults().grab(true, false).applyTo(xgroup);
\r
416 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(xgroup);
\r
418 GridDataFactory.fillDefaults().grab(true, false).applyTo(xColumn1);
\r
419 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(xColumn1);
\r
421 // Variable for x-axis (default: empty == time)
\r
422 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(xVariableLabel);
\r
424 GridDataFactory.fillDefaults().grab(true, false).applyTo(xvariable.getWidget());
\r
426 // Label for x-axis
\r
427 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(labelLabel);
\r
429 GridDataFactory.fillDefaults().grab(true, false).applyTo(xlabel.getWidget());
\r
431 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(xColumn2);
\r
433 // Min value for x-axis
\r
434 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(labelMin);
\r
435 GridDataFactory.fillDefaults().applyTo(xmin.getWidget());
\r
437 // Max value for x-axis
\r
438 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(labelMax);
\r
439 GridDataFactory.fillDefaults().applyTo(xmax.getWidget());
\r
441 // Axis hide buttons
\r
442 GridDataFactory.fillDefaults().applyTo(axisHide);
\r
444 // Set the same width to both label rows
\r
445 composite.layout();
\r
446 GridDataFactory.fillDefaults().hint(xVariableLabel.getBounds().width, SWT.DEFAULT).align(SWT.END, SWT.CENTER).applyTo(nameLabel);
\r
448 Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
\r
449 sc.setMinSize(size);
\r