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;
\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.ui.utils.AdaptionUtils;
\r
40 * Composite for displaying series properties in {@link ChartAxisAndVariablesTab}
\r
42 * @author Teemu Lempinen
\r
45 public class SeriesPropertyComposite extends Composite {
\r
47 private TrackedText variable, label;
\r
48 private TrackedSpinner width;
\r
50 public SeriesPropertyComposite(Composite parent, ISessionContext context, WidgetSupport support, int style) {
\r
51 super(parent, style);
\r
53 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(this);
\r
55 // Variable for the series
\r
56 Label label = new Label(this, SWT.NONE);
\r
57 label.setText("Variable:");
\r
58 GridDataFactory.fillDefaults().align(SWT.END, SWT.FILL).applyTo(label);
\r
60 variable = new TrackedText(this, support, SWT.BORDER);
\r
61 variable.setTextFactory(new RVIFactory());
\r
62 variable.addModifyListener(new RVIModifier(variable.getWidget(), support));
\r
63 variable.setColorProvider(new JFreeChartPropertyColorProvider(this.variable.getResourceManager()));
\r
64 GridDataFactory.fillDefaults().grab(true, false).applyTo(this.variable.getWidget());
\r
66 // Label to be displayed in chart for this series (usually same as the variable
\r
67 label = new Label(this, SWT.NONE);
\r
68 label.setText("Label:");
\r
69 GridDataFactory.fillDefaults().align(SWT.END, SWT.FILL).applyTo(label);
\r
71 this.label = new TrackedText(this, support, SWT.BORDER);
\r
72 this.label.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel, ""));
\r
73 this.label.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel));
\r
74 this.label.setColorProvider(new JFreeChartPropertyColorProvider(this.label.getResourceManager()));
\r
75 GridDataFactory.fillDefaults().grab(true, false).applyTo(this.label.getWidget());
\r
78 label = new Label(this, SWT.NONE);
\r
79 label.setText("Color:");
\r
80 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);
\r
82 Composite colorPicker = new ColorPicker(this, context, support, SWT.NONE);
\r
83 GridDataFactory.fillDefaults().grab(true, false).applyTo(colorPicker);
\r
86 label = new Label(this, SWT.NONE);
\r
87 label.setText("Line width:");
\r
88 GridDataFactory.fillDefaults().align(SWT.END, SWT.FILL).applyTo(label);
\r
90 width = new TrackedSpinner(this, support, SWT.BORDER);
\r
91 width.setSelectionFactory(new WidthSelectionFactory());
\r
92 width.addModifyListener(new WidthModifyListener());
\r
93 width.setMinimum(1);
\r
94 width.setMaximum(10);
\r
100 * ModifyListener for the width {@link TrackedSpinner}
\r
102 * @author Teemu Lempinen
\r
105 private class WidthModifyListener implements TextModifyListener, Widget {
\r
107 private ISessionContext context;
\r
108 private Object lastInput = null;
\r
111 public void modifyText(TrackedModifyEvent e) {
\r
112 if(context == null)
\r
115 // Get the text value from spinner and associated resource (input)
\r
116 Spinner spinner = (Spinner)e.getWidget();
\r
117 final String textValue = spinner.getText();
\r
118 final Object input = lastInput;
\r
121 context.getSession().syncRequest(new WriteRequest() {
\r
124 public void perform(WriteGraph graph) throws DatabaseException {
\r
125 // Apply with (textValue) to the series (input)
\r
126 Resource series = AdaptionUtils.adaptToSingle(input, Resource.class);
\r
127 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
129 // usually reliable, since the spinner does all the checks
\r
130 Integer value = Integer.parseInt(textValue);
\r
131 graph.claimLiteral(series, jfree.Series_lineWidth, value, Bindings.INTEGER);
\r
132 } catch (NumberFormatException e) {
\r
133 e.printStackTrace();
\r
138 } catch (DatabaseException e1) {
\r
139 e1.printStackTrace();
\r
144 public void setInput(ISessionContext context, Object parameter) {
\r
145 this.context = context;
\r
146 lastInput = parameter;
\r
152 * Class for setting the value for width {@link TrackedSpinner}
\r
153 * @author Teemu Lempinen
\r
156 private class WidthSelectionFactory extends ReadFactoryImpl<Resource, Integer> {
\r
159 public Integer perform(ReadGraph graph, Resource axis) throws DatabaseException {
\r
160 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
161 Integer width = graph.getPossibleRelatedValue(axis, jfree.Series_lineWidth);
\r
163 // Default width == 1
\r