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.xyline;
\r
14 import org.eclipse.jface.layout.GridDataFactory;
\r
15 import org.eclipse.jface.layout.GridLayoutFactory;
\r
16 import org.eclipse.swt.SWT;
\r
17 import org.eclipse.swt.widgets.Composite;
\r
18 import org.eclipse.swt.widgets.Label;
\r
19 import org.eclipse.swt.widgets.Spinner;
\r
20 import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory;
\r
21 import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier;
\r
22 import org.simantics.browsing.ui.swt.widgets.TrackedText;
\r
23 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;
\r
24 import org.simantics.browsing.ui.swt.widgets.impl.TextModifyListener;
\r
25 import org.simantics.browsing.ui.swt.widgets.impl.TrackedModifyEvent;
\r
26 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
\r
27 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
\r
28 import org.simantics.databoard.Bindings;
\r
29 import org.simantics.db.ReadGraph;
\r
30 import org.simantics.db.Resource;
\r
31 import org.simantics.db.WriteGraph;
\r
32 import org.simantics.db.common.request.WriteRequest;
\r
33 import org.simantics.db.exception.DatabaseException;
\r
34 import org.simantics.db.management.ISessionContext;
\r
35 import org.simantics.layer0.Layer0;
\r
36 import org.simantics.sysdyn.JFreeChartResource;
\r
37 import org.simantics.sysdyn.ui.trend.chart.properties.ColorPicker;
\r
38 import org.simantics.sysdyn.ui.trend.chart.properties.JFreeChartPropertyColorProvider;
\r
39 import org.simantics.sysdyn.ui.trend.chart.properties.RVIFactory;
\r
40 import org.simantics.sysdyn.ui.trend.chart.properties.RVIModifier;
\r
41 import org.simantics.sysdyn.ui.trend.chart.properties.RangeComposite;
\r
42 import org.simantics.sysdyn.ui.trend.chart.properties.TrackedSpinner;
\r
43 import org.simantics.ui.utils.AdaptionUtils;
\r
46 * Composite for displaying series properties in {@link XYLineAxisAndVariablesTab}
\r
48 * @author Teemu Lempinen
\r
51 public class SeriesPropertyComposite extends Composite {
\r
53 private TrackedText variable, label;
\r
54 private TrackedSpinner width;
\r
56 public SeriesPropertyComposite(Composite parent, ISessionContext context, WidgetSupport support, int style) {
\r
57 super(parent, style);
\r
59 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(this);
\r
61 // Variable for the series
\r
62 Label label = new Label(this, SWT.NONE);
\r
63 label.setText("Variable:");
\r
64 GridDataFactory.fillDefaults().align(SWT.END, SWT.FILL).applyTo(label);
\r
66 variable = new TrackedText(this, support, SWT.BORDER);
\r
67 variable.setTextFactory(new RVIFactory());
\r
68 variable.addModifyListener(new RVIModifier(variable.getWidget(), support));
\r
69 variable.setColorProvider(new JFreeChartPropertyColorProvider(this.variable.getResourceManager()));
\r
70 GridDataFactory.fillDefaults().grab(true, false).applyTo(this.variable.getWidget());
\r
73 label = new Label(this, SWT.NONE);
\r
74 label.setText("Range:");
\r
75 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);
\r
77 RangeComposite rangeComposite = new RangeComposite(this, context, support, SWT.NONE);
\r
78 GridDataFactory.fillDefaults().grab(true, false).applyTo(rangeComposite);
\r
80 // Label to be displayed in chart for this series
\r
81 label = new Label(this, SWT.NONE);
\r
82 label.setText("Label:");
\r
83 GridDataFactory.fillDefaults().align(SWT.END, SWT.FILL).applyTo(label);
\r
85 this.label = new TrackedText(this, support, SWT.BORDER);
\r
86 this.label.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel, ""));
\r
87 this.label.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel));
\r
88 this.label.setColorProvider(new JFreeChartPropertyColorProvider(this.label.getResourceManager()));
\r
89 GridDataFactory.fillDefaults().grab(true, false).applyTo(this.label.getWidget());
\r
92 label = new Label(this, SWT.NONE);
\r
93 label.setText("Color:");
\r
94 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);
\r
96 Composite colorPicker = new ColorPicker(this, context, support, SWT.NONE);
\r
97 GridDataFactory.fillDefaults().grab(true, false).applyTo(colorPicker);
\r
100 label = new Label(this, SWT.NONE);
\r
101 label.setText("Line width:");
\r
102 GridDataFactory.fillDefaults().align(SWT.END, SWT.FILL).applyTo(label);
\r
104 width = new TrackedSpinner(this, support, SWT.BORDER);
\r
105 width.setSelectionFactory(new WidthSelectionFactory());
\r
106 width.addModifyListener(new WidthModifyListener());
\r
107 width.setMinimum(1);
\r
108 width.setMaximum(10);
\r
114 * ModifyListener for the width {@link TrackedSpinner}
\r
116 * @author Teemu Lempinen
\r
119 private class WidthModifyListener implements TextModifyListener, Widget {
\r
121 private ISessionContext context;
\r
122 private Object lastInput = null;
\r
125 public void modifyText(TrackedModifyEvent e) {
\r
126 if(context == null)
\r
129 // Get the text value from spinner and associated resource (input)
\r
130 Spinner spinner = (Spinner)e.getWidget();
\r
131 final String textValue = spinner.getText();
\r
132 final Object input = lastInput;
\r
135 context.getSession().syncRequest(new WriteRequest() {
\r
138 public void perform(WriteGraph graph) throws DatabaseException {
\r
139 // Apply with (textValue) to the series (input)
\r
140 Resource series = AdaptionUtils.adaptToSingle(input, Resource.class);
\r
141 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
143 // usually reliable, since the spinner does all the checks
\r
144 Integer value = Integer.parseInt(textValue);
\r
145 graph.claimLiteral(series, jfree.Series_lineWidth, value, Bindings.INTEGER);
\r
146 } catch (NumberFormatException e) {
\r
147 e.printStackTrace();
\r
152 } catch (DatabaseException e1) {
\r
153 e1.printStackTrace();
\r
158 public void setInput(ISessionContext context, Object parameter) {
\r
159 this.context = context;
\r
160 lastInput = parameter;
\r
166 * Class for setting the value for width {@link TrackedSpinner}
\r
167 * @author Teemu Lempinen
\r
170 private class WidthSelectionFactory extends ReadFactoryImpl<Resource, Integer> {
\r
173 public Integer perform(ReadGraph graph, Resource axis) throws DatabaseException {
\r
174 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
175 Integer width = graph.getPossibleRelatedValue(axis, jfree.Series_lineWidth);
\r
177 // Default width == 1
\r