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