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