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