]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
ea64a17dc1601c3d81c8f0a5d2a77ff10bb0a761
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\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
8  *\r
9  * Contributors:\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
13 \r
14 import org.eclipse.jface.dialogs.IInputValidator;\r
15 import org.eclipse.jface.layout.GridDataFactory;\r
16 import org.eclipse.jface.layout.GridLayoutFactory;\r
17 import org.eclipse.jface.viewers.StructuredSelection;\r
18 import org.eclipse.swt.SWT;\r
19 import org.eclipse.swt.custom.ScrolledComposite;\r
20 import org.eclipse.swt.graphics.Point;\r
21 import org.eclipse.swt.widgets.Combo;\r
22 import org.eclipse.swt.widgets.Composite;\r
23 import org.eclipse.swt.widgets.Group;\r
24 import org.eclipse.swt.widgets.Label;\r
25 import org.eclipse.ui.IWorkbenchSite;\r
26 import org.simantics.browsing.ui.swt.widgets.Button;\r
27 import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory;\r
28 import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier;\r
29 import org.simantics.browsing.ui.swt.widgets.TrackedText;\r
30 import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
31 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
32 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;\r
33 import org.simantics.db.ReadGraph;\r
34 import org.simantics.db.Resource;\r
35 import org.simantics.db.common.request.PossibleObjectWithType;\r
36 import org.simantics.db.common.request.ReadRequest;\r
37 import org.simantics.db.exception.DatabaseException;\r
38 import org.simantics.db.management.ISessionContext;\r
39 import org.simantics.layer0.Layer0;\r
40 import org.simantics.modeling.ui.chart.property.DoublePropertyFactory;\r
41 import org.simantics.modeling.ui.chart.property.DoublePropertyModifier;\r
42 import org.simantics.sysdyn.JFreeChartResource;\r
43 import org.simantics.sysdyn.ui.properties.LabelPropertyTabContributor;\r
44 import org.simantics.sysdyn.ui.trend.chart.properties.AxisHidePropertyComposite;\r
45 import org.simantics.sysdyn.ui.trend.chart.properties.BooleanPropertyFactory;\r
46 import org.simantics.sysdyn.ui.trend.chart.properties.BooleanSelectionListener;\r
47 import org.simantics.sysdyn.ui.trend.chart.properties.JFreeChartPropertyColorProvider;\r
48 import org.simantics.sysdyn.ui.trend.chart.properties.RVIFactory;\r
49 import org.simantics.sysdyn.ui.trend.chart.properties.RVIModifier;\r
50 import org.simantics.sysdyn.ui.trend.chart.properties.TitleFactory;\r
51 import org.simantics.sysdyn.ui.trend.chart.properties.TitleModifier;\r
52 import org.simantics.ui.utils.AdaptionUtils;\r
53 \r
54 /**\r
55  * PropertyTab displaying general properties and x-axis properties of a chart\r
56  * \r
57  * @author Teemu Lempinen\r
58  *\r
59  */\r
60 public class XYLineGeneralPropertiesTab extends LabelPropertyTabContributor implements Widget {\r
61 \r
62     private ScrolledComposite sc;\r
63     private Composite composite;\r
64     private TrackedText name, title, xlabel, xvariable, xmin, xmax;\r
65     private Combo type;\r
66     private Button hgrid, htitle, hlegend;\r
67     private WidgetSupportImpl domainAxisSupport = new WidgetSupportImpl();\r
68 \r
69     @Override\r
70     public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {    \r
71         support.register(this);\r
72 \r
73         // Scrolled composite containing all of the properties in this tab\r
74         sc = new ScrolledComposite(body, SWT.NONE | SWT.H_SCROLL | SWT.V_SCROLL);\r
75         GridDataFactory.fillDefaults().grab(true, true).applyTo(sc);\r
76         GridLayoutFactory.fillDefaults().applyTo(sc);\r
77         sc.setExpandHorizontal(true);\r
78         sc.setExpandVertical(true);\r
79 \r
80         composite = new Composite(sc, SWT.NONE);\r
81         GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).applyTo(composite);\r
82 \r
83         // General properties\r
84         Group general = new Group(composite, SWT.NONE);\r
85         GridDataFactory.fillDefaults().grab(true, false).applyTo(general);\r
86         GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(general);\r
87         general.setText("General");\r
88 \r
89         // Name\r
90         Label label = new Label(general, SWT.NONE);\r
91         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
92         label.setText("Name:");\r
93 \r
94         Composite c = new Composite(general, SWT.NONE);\r
95         GridDataFactory.fillDefaults().applyTo(c);\r
96         GridLayoutFactory.fillDefaults().numColumns(3).applyTo(c);\r
97 \r
98         name = new org.simantics.browsing.ui.swt.widgets.TrackedText(c, support, SWT.BORDER);\r
99         GridDataFactory.fillDefaults().grab(true, false).applyTo(name.getWidget());\r
100         name.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel));\r
101         name.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel));\r
102         name.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));\r
103 \r
104         // Type\r
105         label = new Label(c, SWT.NONE);\r
106         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
107         label.setText("Type:");\r
108 \r
109         type = new Combo(c, SWT.BORDER | SWT.READ_ONLY);\r
110         type.setItems(new String[] {"Line", "Fancy", "Area", "3D"});\r
111         type.select(0);\r
112 \r
113         // Title (Which is different than name)\r
114         label = new Label(general, SWT.NONE);\r
115         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
116         label.setText("Title:");\r
117 \r
118         title = new org.simantics.browsing.ui.swt.widgets.TrackedText(general, support, SWT.BORDER);\r
119         GridDataFactory.fillDefaults().grab(true, false).applyTo(title.getWidget());\r
120         title.setTextFactory(new TitleFactory());\r
121         title.addModifyListener(new TitleModifier());\r
122         title.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));\r
123 \r
124         // Group for hide options\r
125         Group hideGroup = new Group(composite, SWT.NONE);\r
126         GridDataFactory.fillDefaults().applyTo(hideGroup);\r
127         GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(hideGroup);\r
128         hideGroup.setText("Hide");\r
129 \r
130         hgrid = new Button(hideGroup, support, SWT.CHECK);\r
131         hgrid.setText("Grid");\r
132         hgrid.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid, true));\r
133         hgrid.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid));\r
134         htitle = new Button(hideGroup, support, SWT.CHECK);\r
135         htitle.setText("Title");\r
136         htitle.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible, true));\r
137         htitle.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible));\r
138         hlegend = new Button(hideGroup, support, SWT.CHECK);\r
139         hlegend.setText("Legend");\r
140         hlegend.setSelectionFactory(new BooleanPropertyFactory(null, JFreeChartResource.URIs.Chart_visibleLegend, true));\r
141         hlegend.addSelectionListener(new BooleanSelectionListener(context, null, JFreeChartResource.URIs.Chart_visibleLegend));\r
142 \r
143 \r
144         // X-Axis properties\r
145         Group xgroup = new Group(composite, SWT.NONE);\r
146         GridDataFactory.fillDefaults().span(2, 1).grab(true, false).applyTo(xgroup);\r
147         GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(3).applyTo(xgroup);\r
148         xgroup.setText("X-axis");\r
149 \r
150         // Variable for x-axis (default: empty == time)\r
151         label = new Label(xgroup, SWT.NONE);\r
152         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
153         label.setText("Variable:");\r
154 \r
155         xvariable = new TrackedText(xgroup, domainAxisSupport, SWT.BORDER);\r
156         xvariable.setTextFactory(new RVIFactory());\r
157         xvariable.addModifyListener(new RVIModifier(xvariable.getWidget(), domainAxisSupport));\r
158         xvariable.setColorProvider(new JFreeChartPropertyColorProvider(xvariable.getResourceManager()));\r
159         GridDataFactory.fillDefaults().grab(true, false).applyTo(xvariable.getWidget());\r
160         \r
161         Composite axisHide = new AxisHidePropertyComposite(xgroup, context, domainAxisSupport, SWT.NONE);\r
162         GridDataFactory.fillDefaults().span(1, 3).applyTo(axisHide);\r
163 \r
164         // Label for x-axis\r
165         label = new Label(xgroup, SWT.NONE);\r
166         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
167         label.setText("Label:");\r
168 \r
169         xlabel = new TrackedText(xgroup, domainAxisSupport, SWT.BORDER);\r
170         xlabel.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel, ""));\r
171         xlabel.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel));\r
172         xlabel.setColorProvider(new JFreeChartPropertyColorProvider(xlabel.getResourceManager()));\r
173         GridDataFactory.fillDefaults().grab(true, false).applyTo(xlabel.getWidget());\r
174 \r
175         // Min and max values for x-axis\r
176         label = new Label(xgroup, SWT.NONE);\r
177         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
178         label.setText("Min:");\r
179 \r
180         Composite minmax = new Composite(xgroup, SWT.NONE);\r
181         GridDataFactory.fillDefaults().applyTo(minmax);\r
182         GridLayoutFactory.fillDefaults().numColumns(3).applyTo(minmax);\r
183         xmin = new TrackedText(minmax, domainAxisSupport, SWT.BORDER);\r
184         xmin.setColorProvider(new JFreeChartPropertyColorProvider(xmin.getResourceManager()));\r
185         xmin.setTextFactory(new DoublePropertyFactory(JFreeChartResource.URIs.Axis_min));\r
186         xmin.addModifyListener(new DoublePropertyModifier(context, JFreeChartResource.URIs.Axis_min));\r
187         xmin.setInputValidator(new DoubleValidator());\r
188 \r
189         label = new Label(minmax, SWT.NONE);\r
190         label.setText("Max:");\r
191         xmax = new TrackedText(minmax, domainAxisSupport, SWT.BORDER);\r
192         xmax.setColorProvider(new JFreeChartPropertyColorProvider(xmax.getResourceManager()));\r
193         xmax.setTextFactory(new DoublePropertyFactory(JFreeChartResource.URIs.Axis_max));\r
194         xmax.addModifyListener(new DoublePropertyModifier(context, JFreeChartResource.URIs.Axis_max));\r
195         xmax.setInputValidator(new DoubleValidator());\r
196 \r
197 \r
198         sc.setContent(composite);\r
199         Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);\r
200         sc.setMinSize(size);\r
201     }\r
202 \r
203     @Override\r
204     public void setInput(final ISessionContext context, Object input) {\r
205         final Resource chart = AdaptionUtils.adaptToSingle(input, Resource.class);\r
206         if(chart == null)\r
207             return; \r
208 \r
209         context.getSession().asyncRequest(new ReadRequest() {\r
210 \r
211             @Override\r
212             public void run(ReadGraph graph) throws DatabaseException {\r
213                 Layer0 l0 = Layer0.getInstance(graph);\r
214                 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
215                 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.Plot));\r
216                 if(plot == null) return;\r
217                 Resource domainAxis = graph.getPossibleObject(plot, jfree.Plot_domainAxis);\r
218                 if(domainAxis == null) return;\r
219                 domainAxisSupport.fireInput(context, new StructuredSelection(domainAxis));\r
220             }\r
221         });\r
222     }\r
223 \r
224     \r
225     /**\r
226      * Validator for validating that an input is double\r
227      * @author Teemu Lempinen\r
228      *\r
229      */\r
230     private class DoubleValidator implements IInputValidator {\r
231         @Override\r
232         public String isValid(String newText) {\r
233             if (newText.trim().isEmpty())\r
234                 return null;\r
235             try {\r
236                 Double.parseDouble(newText);\r
237                 return null;\r
238             } catch (NumberFormatException e) {\r
239                 return e.getMessage();\r
240             }\r
241         }\r
242     }\r
243     \r
244 \r
245 }\r