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.jfreechart.chart.properties.bar;
\r
14 import org.eclipse.jface.layout.GridDataFactory;
\r
15 import org.eclipse.jface.layout.GridLayoutFactory;
\r
16 import org.eclipse.jface.viewers.StructuredSelection;
\r
17 import org.eclipse.swt.SWT;
\r
18 import org.eclipse.swt.custom.ScrolledComposite;
\r
19 import org.eclipse.swt.graphics.Point;
\r
20 import org.eclipse.swt.widgets.Composite;
\r
21 import org.eclipse.swt.widgets.Group;
\r
22 import org.eclipse.swt.widgets.Label;
\r
23 import org.eclipse.swt.widgets.Spinner;
\r
24 import org.eclipse.ui.IWorkbenchSite;
\r
25 import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory;
\r
26 import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier;
\r
27 import org.simantics.browsing.ui.swt.widgets.TrackedText;
\r
28 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;
\r
29 import org.simantics.browsing.ui.swt.widgets.impl.TextModifyListener;
\r
30 import org.simantics.browsing.ui.swt.widgets.impl.TrackedModifyEvent;
\r
31 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
\r
32 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
\r
33 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;
\r
34 import org.simantics.databoard.Bindings;
\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.common.request.ReadRequest;
\r
40 import org.simantics.db.common.request.WriteRequest;
\r
41 import org.simantics.db.exception.DatabaseException;
\r
42 import org.simantics.db.management.ISessionContext;
\r
43 import org.simantics.jfreechart.chart.properties.AxisHidePropertyComposite;
\r
44 import org.simantics.jfreechart.chart.properties.ColorPicker;
\r
45 import org.simantics.jfreechart.chart.properties.DoubleValidator;
\r
46 import org.simantics.jfreechart.chart.properties.JFreeChartPropertyColorProvider;
\r
47 import org.simantics.jfreechart.chart.properties.LabelPropertyTabContributor;
\r
48 import org.simantics.jfreechart.chart.properties.TrackedSpinner;
\r
49 import org.simantics.layer0.Layer0;
\r
50 import org.simantics.modeling.ui.chart.property.DoublePropertyFactory;
\r
51 import org.simantics.modeling.ui.chart.property.DoublePropertyModifier;
\r
52 import org.simantics.sysdyn.JFreeChartResource;
\r
53 import org.simantics.ui.utils.AdaptionUtils;
\r
56 * Tab for bar chart axis properties
\r
57 * @author Teemu Lempinen
\r
60 public class BarAxisTab extends LabelPropertyTabContributor implements Widget {
\r
62 private TrackedSpinner angle;
\r
63 private Integer angleInt = null;
\r
64 private WidgetSupportImpl domainAxisSupport = new WidgetSupportImpl();
\r
65 private WidgetSupportImpl rangeAxisSupport = new WidgetSupportImpl();
\r
66 private TrackedText rangelabel, rangemin, rangemax;
\r
67 private ScrolledComposite sc;
\r
68 private Composite composite;
\r
72 public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {
\r
73 support.register(this);
\r
75 // Scrolled composite containing all of the properties in this tab
\r
76 sc = new ScrolledComposite(body, SWT.NONE | SWT.H_SCROLL | SWT.V_SCROLL);
\r
77 GridDataFactory.fillDefaults().grab(true, true).applyTo(sc);
\r
78 GridLayoutFactory.fillDefaults().margins(3, 3).applyTo(sc);
\r
79 sc.setExpandHorizontal(true);
\r
80 sc.setExpandVertical(true);
\r
82 composite = new Composite(sc, SWT.NONE);
\r
83 GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).applyTo(composite);
\r
85 // Domain Axis properties
\r
86 Group domainGroup = new Group(composite, SWT.NONE);
\r
87 GridDataFactory.fillDefaults().span(2, 1).grab(true, false).applyTo(domainGroup);
\r
88 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(3).applyTo(domainGroup);
\r
89 domainGroup.setText("Domain axis");
\r
92 Label label = new Label(domainGroup, SWT.NONE);
\r
93 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);
\r
94 label.setText("Label:");
\r
96 rangelabel = new TrackedText(domainGroup, domainAxisSupport, SWT.BORDER);
\r
97 rangelabel.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel, ""));
\r
98 rangelabel.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel));
\r
99 rangelabel.setColorProvider(new JFreeChartPropertyColorProvider(rangelabel.getResourceManager()));
\r
100 GridDataFactory.fillDefaults().grab(true, false).applyTo(rangelabel.getWidget());
\r
102 Composite axisHide = new AxisHidePropertyComposite(domainGroup, context, domainAxisSupport, SWT.NONE);
\r
103 GridDataFactory.fillDefaults().span(1, 3).applyTo(axisHide);
\r
105 Label angleLabel = new Label(domainGroup, SWT.NONE);
\r
106 angleLabel.setText("Label angle:");
\r
107 GridDataFactory.fillDefaults().align(SWT.END, SWT.FILL).applyTo(angleLabel);
\r
109 Composite angleComposite = new Composite(domainGroup, SWT.NONE);
\r
110 GridDataFactory.fillDefaults().applyTo(angleComposite);
\r
111 GridLayoutFactory.fillDefaults().applyTo(angleComposite);
\r
112 angle = new TrackedSpinner(angleComposite, domainAxisSupport, SWT.BORDER);
\r
113 angle.setSelectionFactory(new AngleSelectionFactory());
\r
114 angle.addModifyListener(new AngleModifyListener());
\r
115 angle.setMinimum(0);
\r
116 angle.setMaximum(90);
\r
117 angle.getWidget().setIncrement(5);
\r
118 GridDataFactory.fillDefaults().applyTo(angle.getWidget());
\r
121 label = new Label(domainGroup, SWT.NONE);
\r
122 label.setText("Color:");
\r
123 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);
\r
125 Composite colorPicker = new ColorPicker(domainGroup, context, domainAxisSupport, SWT.NONE);
\r
126 GridDataFactory.fillDefaults().grab(true, false).applyTo(colorPicker);
\r
128 domainGroup.layout();
\r
130 // Range Axis properties
\r
131 Group rangeGroup = new Group(composite, SWT.NONE);
\r
132 GridDataFactory.fillDefaults().span(2, 1).grab(true, false).applyTo(rangeGroup);
\r
133 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(3).applyTo(rangeGroup);
\r
134 rangeGroup.setText("Range axis");
\r
136 // Label for range axis
\r
137 label = new Label(rangeGroup, SWT.NONE);
\r
138 label.setText("Label:");
\r
139 label.setAlignment(SWT.RIGHT);
\r
140 GridDataFactory.fillDefaults().hint(angleLabel.getBounds().width, SWT.DEFAULT).align(SWT.END, SWT.CENTER).applyTo(label);
\r
142 rangelabel = new TrackedText(rangeGroup, rangeAxisSupport, SWT.BORDER);
\r
143 rangelabel.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel, ""));
\r
144 rangelabel.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel));
\r
145 rangelabel.setColorProvider(new JFreeChartPropertyColorProvider(rangelabel.getResourceManager()));
\r
146 GridDataFactory.fillDefaults().grab(true, false).applyTo(rangelabel.getWidget());
\r
148 axisHide = new AxisHidePropertyComposite(rangeGroup, context, rangeAxisSupport, SWT.NONE);
\r
149 GridDataFactory.fillDefaults().span(1, 4).applyTo(axisHide);
\r
151 // Min and max values for range axis
\r
152 label = new Label(rangeGroup, SWT.NONE);
\r
153 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);
\r
154 label.setText("Min:");
\r
156 Composite minmax = new Composite(rangeGroup, SWT.NONE);
\r
157 GridDataFactory.fillDefaults().applyTo(minmax);
\r
158 GridLayoutFactory.fillDefaults().numColumns(3).applyTo(minmax);
\r
159 rangemin = new TrackedText(minmax, rangeAxisSupport, SWT.BORDER);
\r
160 rangemin.setColorProvider(new JFreeChartPropertyColorProvider(rangemin.getResourceManager()));
\r
161 rangemin.setTextFactory(new DoublePropertyFactory(JFreeChartResource.URIs.Axis_min));
\r
162 rangemin.addModifyListener(new DoublePropertyModifier(context, JFreeChartResource.URIs.Axis_min));
\r
163 rangemin.setInputValidator(new DoubleValidator(true));
\r
165 label = new Label(minmax, SWT.NONE);
\r
166 label.setText("Max:");
\r
167 rangemax = new TrackedText(minmax, rangeAxisSupport, SWT.BORDER);
\r
168 rangemax.setColorProvider(new JFreeChartPropertyColorProvider(rangemax.getResourceManager()));
\r
169 rangemax.setTextFactory(new DoublePropertyFactory(JFreeChartResource.URIs.Axis_max));
\r
170 rangemax.addModifyListener(new DoublePropertyModifier(context, JFreeChartResource.URIs.Axis_max));
\r
171 rangemax.setInputValidator(new DoubleValidator(true));
\r
174 label = new Label(rangeGroup, SWT.NONE);
\r
175 label.setText("Color:");
\r
176 GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);
\r
178 colorPicker = new ColorPicker(rangeGroup, context, rangeAxisSupport, SWT.NONE);
\r
179 GridDataFactory.fillDefaults().grab(true, false).applyTo(colorPicker);
\r
181 // Resize scrolled composite
\r
182 sc.setContent(composite);
\r
183 Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
\r
184 sc.setMinSize(size);
\r
188 * ModifyListener for the angle {@link TrackedSpinner}
\r
190 * @author Teemu Lempinen
\r
193 private class AngleModifyListener implements TextModifyListener, Widget {
\r
195 private ISessionContext context;
\r
196 private Object lastInput = null;
\r
199 public void modifyText(TrackedModifyEvent e) {
\r
200 if(context == null)
\r
203 // Get the text value from spinner and associated resource (input)
\r
204 Spinner spinner = (Spinner)e.getWidget();
\r
205 final String textValue = spinner.getText();
\r
206 final Object input = lastInput;
\r
209 context.getSession().syncRequest(new WriteRequest() {
\r
212 public void perform(WriteGraph graph) throws DatabaseException {
\r
213 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
214 Resource domainAxis = AdaptionUtils.adaptToSingle(input, Resource.class);
\r
216 // usually reliable, since the spinner does all the checks
\r
217 Double value = Double.parseDouble(textValue);
\r
218 Double oldValue = graph.getPossibleRelatedValue(domainAxis, jfree.Axis_rotateLabelDegrees, Bindings.DOUBLE);
\r
219 if(oldValue == null || !oldValue.equals(value)) {
\r
220 graph.claimLiteral(domainAxis, jfree.Axis_rotateLabelDegrees, value, Bindings.DOUBLE);
\r
221 angleInt = value.intValue();
\r
223 } catch (NumberFormatException e) {
\r
224 graph.claimLiteral(domainAxis, jfree.Axis_rotateLabelDegrees, 0.0, Bindings.DOUBLE);
\r
230 } catch (DatabaseException e1) {
\r
231 e1.printStackTrace();
\r
236 public void setInput(ISessionContext context, Object parameter) {
\r
237 this.context = context;
\r
238 lastInput = parameter;
\r
244 * Class for setting the value for angle {@link TrackedSpinner}
\r
245 * @author Teemu Lempinen
\r
248 private class AngleSelectionFactory extends ReadFactoryImpl<Resource, Integer> {
\r
251 public Integer perform(ReadGraph graph, Resource domainAxis) throws DatabaseException {
\r
252 if(angleInt == null) {
\r
253 Double angle = 0.0;
\r
254 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
255 if(domainAxis != null) {
\r
256 Double value = graph.getPossibleRelatedValue(domainAxis, jfree.Axis_rotateLabelDegrees);
\r
260 return angle.intValue();
\r
269 public void setInput(final ISessionContext context, Object input) {
\r
270 final Resource chart = AdaptionUtils.adaptToSingle(input, Resource.class);
\r
274 context.getSession().asyncRequest(new ReadRequest() {
\r
277 public void run(ReadGraph graph) throws DatabaseException {
\r
278 Layer0 l0 = Layer0.getInstance(graph);
\r
279 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
280 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.Plot));
\r
281 if(plot == null) return;
\r
282 Resource rangeAxis = graph.getPossibleObject(plot, jfree.Plot_rangeAxis);
\r
283 if(rangeAxis == null) return;
\r
284 rangeAxisSupport.fireInput(context, new StructuredSelection(rangeAxis));
\r
286 Resource domainAxis = graph.getPossibleObject(plot, jfree.Plot_domainAxis);
\r
287 if(domainAxis == null) return;
\r
288 domainAxisSupport.fireInput(context, new StructuredSelection(domainAxis));
\r