]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
c4e730da501d271ae1e0f3ad110257cdd497654d
[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 java.util.Collection;\r
15 import java.util.LinkedHashMap;\r
16 import java.util.Map;\r
17 \r
18 import org.eclipse.jface.layout.GridDataFactory;\r
19 import org.eclipse.jface.layout.GridLayoutFactory;\r
20 import org.eclipse.jface.viewers.StructuredSelection;\r
21 import org.eclipse.swt.SWT;\r
22 import org.eclipse.swt.custom.ScrolledComposite;\r
23 import org.eclipse.swt.graphics.Point;\r
24 import org.eclipse.swt.widgets.Composite;\r
25 import org.eclipse.swt.widgets.Group;\r
26 import org.eclipse.swt.widgets.Label;\r
27 import org.eclipse.ui.IWorkbenchSite;\r
28 import org.simantics.browsing.ui.swt.widgets.Button;\r
29 import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory;\r
30 import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier;\r
31 import org.simantics.browsing.ui.swt.widgets.TrackedCombo;\r
32 import org.simantics.browsing.ui.swt.widgets.TrackedText;\r
33 import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl;\r
34 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;\r
35 import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
36 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
37 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;\r
38 import org.simantics.db.ReadGraph;\r
39 import org.simantics.db.Resource;\r
40 import org.simantics.db.WriteGraph;\r
41 import org.simantics.db.common.request.ObjectsWithType;\r
42 import org.simantics.db.common.request.PossibleObjectWithType;\r
43 import org.simantics.db.common.request.ReadRequest;\r
44 import org.simantics.db.exception.DatabaseException;\r
45 import org.simantics.db.management.ISessionContext;\r
46 import org.simantics.layer0.Layer0;\r
47 import org.simantics.layer0.utils.direct.GraphUtils;\r
48 import org.simantics.modeling.ui.chart.property.DoublePropertyFactory;\r
49 import org.simantics.modeling.ui.chart.property.DoublePropertyModifier;\r
50 import org.simantics.sysdyn.JFreeChartResource;\r
51 import org.simantics.sysdyn.ui.properties.LabelPropertyTabContributor;\r
52 import org.simantics.sysdyn.ui.trend.chart.properties.AxisHidePropertyComposite;\r
53 import org.simantics.sysdyn.ui.trend.chart.properties.BooleanPropertyFactory;\r
54 import org.simantics.sysdyn.ui.trend.chart.properties.BooleanSelectionListener;\r
55 import org.simantics.sysdyn.ui.trend.chart.properties.DoubleValidator;\r
56 import org.simantics.sysdyn.ui.trend.chart.properties.JFreeChartPropertyColorProvider;\r
57 import org.simantics.sysdyn.ui.trend.chart.properties.RVIFactory;\r
58 import org.simantics.sysdyn.ui.trend.chart.properties.RVIModifier;\r
59 import org.simantics.sysdyn.ui.trend.chart.properties.TitleFactory;\r
60 import org.simantics.sysdyn.ui.trend.chart.properties.TitleModifier;\r
61 import org.simantics.ui.utils.AdaptionUtils;\r
62 \r
63 /**\r
64  * PropertyTab displaying general properties and x-axis properties of a chart\r
65  * \r
66  * @author Teemu Lempinen\r
67  *\r
68  */\r
69 public class XYLineGeneralPropertiesTab extends LabelPropertyTabContributor implements Widget {\r
70 \r
71     private ScrolledComposite sc;\r
72     private Composite composite;\r
73     private TrackedText name, title, xlabel, xvariable, xmin, xmax;\r
74     private TrackedCombo type;\r
75     private Button hgrid, htitle, hlegend;\r
76     private WidgetSupportImpl domainAxisSupport = new WidgetSupportImpl();\r
77 \r
78     @Override\r
79     public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {    \r
80         support.register(this);\r
81 \r
82         // Scrolled composite containing all of the properties in this tab\r
83         sc = new ScrolledComposite(body, SWT.NONE | SWT.H_SCROLL | SWT.V_SCROLL);\r
84         GridDataFactory.fillDefaults().grab(true, true).applyTo(sc);\r
85         GridLayoutFactory.fillDefaults().applyTo(sc);\r
86         sc.setExpandHorizontal(true);\r
87         sc.setExpandVertical(true);\r
88 \r
89         composite = new Composite(sc, SWT.NONE);\r
90         GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).applyTo(composite);\r
91 \r
92         // General properties\r
93         Group general = new Group(composite, SWT.NONE);\r
94         GridDataFactory.fillDefaults().grab(true, false).applyTo(general);\r
95         GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(general);\r
96         general.setText("General");\r
97 \r
98         // Name\r
99         Label nameLabel = new Label(general, SWT.NONE);\r
100         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(nameLabel);\r
101         nameLabel.setText("Name:");\r
102         nameLabel.setAlignment(SWT.RIGHT);\r
103 \r
104         Composite c = new Composite(general, SWT.NONE);\r
105         GridDataFactory.fillDefaults().applyTo(c);\r
106         GridLayoutFactory.fillDefaults().numColumns(3).applyTo(c);\r
107 \r
108         name = new org.simantics.browsing.ui.swt.widgets.TrackedText(c, support, SWT.BORDER);\r
109         GridDataFactory.fillDefaults().grab(true, false).applyTo(name.getWidget());\r
110         name.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel));\r
111         name.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel));\r
112         name.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));\r
113 \r
114         // Type\r
115         Label label = new Label(c, SWT.NONE);\r
116         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
117         label.setText("Type:");\r
118 \r
119         type = new TrackedCombo(c, support, SWT.BORDER | SWT.READ_ONLY);\r
120         type.addModifyListener(new TypeModifyListener());\r
121         type.setItemFactory(new TypeItemFactory());\r
122         type.setSelectionFactory(new TypeSelectionFactory());\r
123         GridDataFactory.fillDefaults().applyTo(type.getWidget());\r
124 \r
125         // Title (Which is different than name)\r
126         label = new Label(general, SWT.NONE);\r
127         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
128         label.setText("Title:");\r
129 \r
130         title = new org.simantics.browsing.ui.swt.widgets.TrackedText(general, support, SWT.BORDER);\r
131         GridDataFactory.fillDefaults().grab(true, false).applyTo(title.getWidget());\r
132         title.setTextFactory(new TitleFactory());\r
133         title.addModifyListener(new TitleModifier());\r
134         title.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));\r
135 \r
136         // Group for hide options\r
137         Group hideGroup = new Group(composite, SWT.NONE);\r
138         GridDataFactory.fillDefaults().applyTo(hideGroup);\r
139         GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(hideGroup);\r
140         hideGroup.setText("Hide");\r
141 \r
142         hgrid = new Button(hideGroup, support, SWT.CHECK);\r
143         hgrid.setText("Grid");\r
144         hgrid.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid, true));\r
145         hgrid.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid));\r
146         htitle = new Button(hideGroup, support, SWT.CHECK);\r
147         htitle.setText("Title");\r
148         htitle.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible, true));\r
149         htitle.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible));\r
150         hlegend = new Button(hideGroup, support, SWT.CHECK);\r
151         hlegend.setText("Legend");\r
152         hlegend.setSelectionFactory(new BooleanPropertyFactory(null, JFreeChartResource.URIs.Chart_visibleLegend, true));\r
153         hlegend.addSelectionListener(new BooleanSelectionListener(context, null, JFreeChartResource.URIs.Chart_visibleLegend));\r
154 \r
155 \r
156         // X-Axis properties\r
157         Group xgroup = new Group(composite, SWT.NONE);\r
158         GridDataFactory.fillDefaults().span(2, 1).grab(true, false).applyTo(xgroup);\r
159         GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(3).applyTo(xgroup);\r
160         xgroup.setText("X-axis");\r
161 \r
162         // Variable for x-axis (default: empty == time)\r
163         Label xVariableLabel = new Label(xgroup, SWT.NONE);\r
164         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(xVariableLabel);\r
165         xVariableLabel.setText("Variable:");\r
166 \r
167         xvariable = new TrackedText(xgroup, domainAxisSupport, SWT.BORDER);\r
168         xvariable.setTextFactory(new RVIFactory());\r
169         xvariable.addModifyListener(new RVIModifier(xvariable.getWidget(), domainAxisSupport));\r
170         xvariable.setColorProvider(new JFreeChartPropertyColorProvider(xvariable.getResourceManager()));\r
171         GridDataFactory.fillDefaults().grab(true, false).applyTo(xvariable.getWidget());\r
172         \r
173         Composite axisHide = new AxisHidePropertyComposite(xgroup, context, domainAxisSupport, SWT.NONE);\r
174         GridDataFactory.fillDefaults().span(1, 3).applyTo(axisHide);\r
175 \r
176         // Label for x-axis\r
177         label = new Label(xgroup, SWT.NONE);\r
178         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
179         label.setText("Label:");\r
180 \r
181         xlabel = new TrackedText(xgroup, domainAxisSupport, SWT.BORDER);\r
182         xlabel.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel, ""));\r
183         xlabel.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel));\r
184         xlabel.setColorProvider(new JFreeChartPropertyColorProvider(xlabel.getResourceManager()));\r
185         GridDataFactory.fillDefaults().grab(true, false).applyTo(xlabel.getWidget());\r
186 \r
187         // Min and max values for x-axis\r
188         label = new Label(xgroup, SWT.NONE);\r
189         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
190         label.setText("Min:");\r
191 \r
192         Composite minmax = new Composite(xgroup, SWT.NONE);\r
193         GridDataFactory.fillDefaults().applyTo(minmax);\r
194         GridLayoutFactory.fillDefaults().numColumns(3).applyTo(minmax);\r
195         xmin = new TrackedText(minmax, domainAxisSupport, SWT.BORDER);\r
196         xmin.setColorProvider(new JFreeChartPropertyColorProvider(xmin.getResourceManager()));\r
197         xmin.setTextFactory(new DoublePropertyFactory(JFreeChartResource.URIs.Axis_min));\r
198         xmin.addModifyListener(new DoublePropertyModifier(context, JFreeChartResource.URIs.Axis_min));\r
199         xmin.setInputValidator(new DoubleValidator(true));\r
200 \r
201         label = new Label(minmax, SWT.NONE);\r
202         label.setText("Max:");\r
203         xmax = new TrackedText(minmax, domainAxisSupport, SWT.BORDER);\r
204         xmax.setColorProvider(new JFreeChartPropertyColorProvider(xmax.getResourceManager()));\r
205         xmax.setTextFactory(new DoublePropertyFactory(JFreeChartResource.URIs.Axis_max));\r
206         xmax.addModifyListener(new DoublePropertyModifier(context, JFreeChartResource.URIs.Axis_max));\r
207         xmax.setInputValidator(new DoubleValidator(true));\r
208 \r
209         // Set the same width to both label rows\r
210         composite.layout();\r
211         GridDataFactory.fillDefaults().hint(xVariableLabel.getBounds().width, SWT.DEFAULT).align(SWT.END, SWT.CENTER).applyTo(nameLabel);\r
212         \r
213         sc.setContent(composite);\r
214         Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);\r
215         sc.setMinSize(size);\r
216     }\r
217 \r
218     @Override\r
219     public void setInput(final ISessionContext context, Object input) {\r
220         final Resource chart = AdaptionUtils.adaptToSingle(input, Resource.class);\r
221         if(chart == null)\r
222             return; \r
223 \r
224         context.getSession().asyncRequest(new ReadRequest() {\r
225 \r
226             @Override\r
227             public void run(ReadGraph graph) throws DatabaseException {\r
228                 Layer0 l0 = Layer0.getInstance(graph);\r
229                 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
230                 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.Plot));\r
231                 if(plot == null) return;\r
232                 Resource domainAxis = graph.getPossibleObject(plot, jfree.Plot_domainAxis);\r
233                 if(domainAxis == null) return;\r
234                 domainAxisSupport.fireInput(context, new StructuredSelection(domainAxis));\r
235             }\r
236         });\r
237     }\r
238     \r
239     /**\r
240      * \r
241      * @author Teemu Lempinen\r
242      *\r
243      */\r
244     private class TypeSelectionFactory extends ReadFactoryImpl<Resource, String> {\r
245         @Override\r
246         public String perform(ReadGraph graph, Resource chart) throws DatabaseException {\r
247             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
248             Layer0 l0 = Layer0.getInstance(graph);\r
249 \r
250             Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.XYPlot));\r
251             if(plot != null) {\r
252                 Collection<Resource> datasets = graph.syncRequest(new ObjectsWithType(plot, l0.ConsistsOf, jfree.XYDataset));\r
253                 if(!datasets.isEmpty()) {\r
254                     Resource dataset = datasets.iterator().next();\r
255                     if(dataset != null) {\r
256                         Resource renderer = graph.syncRequest(new PossibleObjectWithType(dataset, jfree.Dataset_renderer, jfree.Renderer));\r
257                         if(renderer != null && graph.isInstanceOf(renderer, jfree.XYAreaRenderer))\r
258                             return "Area";\r
259                     }\r
260                 }\r
261             }\r
262             return "Line";\r
263         }\r
264     }\r
265 \r
266     /**\r
267      * RangeItemFactory finds all inexes of a given enumeration \r
268      * and adds "Sum" and "All" to the returned indexes\r
269      * @author Teemu Lempinen\r
270      *\r
271      */\r
272     private class TypeItemFactory extends ReadFactoryImpl<Resource, Map<String, Object>> {\r
273         @Override\r
274         public Map<String, Object> perform(ReadGraph graph, Resource series) throws DatabaseException {\r
275             LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();\r
276             result.put("Line", "Line");\r
277             result.put("Area", "Area");\r
278 //            result.put("Stacked Area", "Stacked Area");\r
279             return result;\r
280         }\r
281     }\r
282 \r
283     /**\r
284      * TypeModifyListener for modifying the type of a bar chart \r
285      * @author Teemu Lempinen\r
286      *\r
287      */\r
288     private class TypeModifyListener  extends ComboModifyListenerImpl<Resource> {\r
289         @Override\r
290         public void applyText(WriteGraph graph, Resource chart, String text) throws DatabaseException {\r
291             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
292             Layer0 l0 = Layer0.getInstance(graph);\r
293 \r
294             Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.XYPlot));\r
295             if(plot == null)\r
296                 return;\r
297 \r
298             Collection<Resource> datasets = graph.syncRequest(new ObjectsWithType(plot, l0.ConsistsOf, jfree.XYDataset));\r
299             if(datasets == null || datasets.isEmpty())\r
300                 return;\r
301 \r
302             for(Resource dataset : datasets) {\r
303                 graph.deny(dataset, jfree.Dataset_renderer);\r
304 \r
305                 Resource renderer;\r
306                 if(text.equals("Area"))\r
307                     renderer = GraphUtils.create2(graph, jfree.XYAreaRenderer);\r
308                 else\r
309                     renderer = GraphUtils.create2(graph, jfree.XYLineRenderer);\r
310 \r
311                 graph.claim(dataset, jfree.Dataset_renderer, renderer);\r
312             }\r
313         }\r
314     }\r
315 \r
316 }