]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
e75989ba31113cc108cced9acc5d6fc77b7d8cf5
[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.bar;\r
13 \r
14 import java.util.LinkedHashMap;\r
15 import java.util.Map;\r
16 \r
17 import org.eclipse.jface.layout.GridDataFactory;\r
18 import org.eclipse.jface.layout.GridLayoutFactory;\r
19 import org.eclipse.jface.layout.LayoutConstants;\r
20 import org.eclipse.swt.SWT;\r
21 import org.eclipse.swt.custom.ScrolledComposite;\r
22 import org.eclipse.swt.graphics.Point;\r
23 import org.eclipse.swt.widgets.Composite;\r
24 import org.eclipse.swt.widgets.Group;\r
25 import org.eclipse.swt.widgets.Label;\r
26 import org.eclipse.ui.IWorkbenchSite;\r
27 import org.simantics.browsing.ui.swt.widgets.Button;\r
28 import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory;\r
29 import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier;\r
30 import org.simantics.browsing.ui.swt.widgets.TrackedCombo;\r
31 import org.simantics.browsing.ui.swt.widgets.TrackedText;\r
32 import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl;\r
33 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;\r
34 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
35 import org.simantics.db.ReadGraph;\r
36 import org.simantics.db.Resource;\r
37 import org.simantics.db.WriteGraph;\r
38 import org.simantics.db.common.request.PossibleObjectWithType;\r
39 import org.simantics.db.exception.DatabaseException;\r
40 import org.simantics.db.management.ISessionContext;\r
41 import org.simantics.jfreechart.chart.properties.BooleanPropertyFactory;\r
42 import org.simantics.jfreechart.chart.properties.BooleanSelectionListener;\r
43 import org.simantics.jfreechart.chart.properties.DoublePropertyFactory2;\r
44 import org.simantics.jfreechart.chart.properties.DoublePropertyModifier2;\r
45 import org.simantics.jfreechart.chart.properties.DoubleValidator;\r
46 import org.simantics.jfreechart.chart.properties.JFreeChartPropertyColorProvider;\r
47 import org.simantics.jfreechart.chart.properties.LabelPropertyTabContributor;\r
48 import org.simantics.jfreechart.chart.properties.TitleFactory;\r
49 import org.simantics.jfreechart.chart.properties.TitleModifier;\r
50 import org.simantics.layer0.Layer0;\r
51 import org.simantics.layer0.utils.direct.GraphUtils;\r
52 import org.simantics.modeling.ui.chart.property.DoublePropertyFactory;\r
53 import org.simantics.modeling.ui.chart.property.DoublePropertyModifier;\r
54 import org.simantics.sysdyn.JFreeChartResource;\r
55 \r
56 \r
57 /**\r
58  * General properties of a bar chart\r
59  * @author Teemu Lempinen\r
60  *\r
61  */\r
62 public class BarGeneralPropertiesTab extends LabelPropertyTabContributor {\r
63 \r
64     private ScrolledComposite sc;\r
65     private Composite composite;\r
66     private Button hgrid, htitle, hlegend;\r
67     private TrackedText name, title, time;\r
68     private TrackedCombo type;\r
69     private TrackedCombo orientation;\r
70 \r
71 \r
72     @Override\r
73     public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {    \r
74         // Scrolled composite containing all of the properties in this tab\r
75         sc = new ScrolledComposite(body, SWT.NONE | SWT.H_SCROLL | SWT.V_SCROLL);\r
76         GridDataFactory.fillDefaults().grab(true, true).applyTo(sc);\r
77         GridLayoutFactory.fillDefaults().applyTo(sc);\r
78         sc.setExpandHorizontal(true);\r
79         sc.setExpandVertical(true);\r
80 \r
81         composite = new Composite(sc, SWT.NONE);\r
82         GridLayoutFactory.fillDefaults().numColumns(4).margins(3, 3).applyTo(composite);\r
83 \r
84         // General properties\r
85         Group general = new Group(composite, SWT.NONE);\r
86         GridDataFactory.fillDefaults().grab(true, false).applyTo(general);\r
87         GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(4).applyTo(general);\r
88         general.setText("General");\r
89 \r
90         // first column: labels\r
91         Composite labelColumn1 = new Composite(general, SWT.NONE);\r
92         GridDataFactory.fillDefaults().grab(false, true).applyTo(labelColumn1);\r
93         GridLayoutFactory.fillDefaults().applyTo(labelColumn1);\r
94         \r
95         // second column: name and title\r
96         Composite propertyColumn1 = new Composite(general, SWT.NONE);\r
97         GridDataFactory.fillDefaults().grab(true, true).applyTo(propertyColumn1);\r
98         GridLayoutFactory.fillDefaults().spacing(0, LayoutConstants.getSpacing().y).applyTo(propertyColumn1);\r
99         \r
100         // third column: labels\r
101         Composite labelColumn2 = new Composite(general, SWT.NONE);\r
102         GridDataFactory.fillDefaults().grab(false, true).applyTo(labelColumn2);\r
103         GridLayoutFactory.fillDefaults().spacing(0, LayoutConstants.getSpacing().y).applyTo(labelColumn2);\r
104         \r
105         // fourth column: type and time\r
106         Composite propertyColumn2 = new Composite(general, SWT.NONE);\r
107         GridDataFactory.fillDefaults().grab(false, true).applyTo(propertyColumn2);\r
108         GridLayoutFactory.fillDefaults().applyTo(propertyColumn2);\r
109         \r
110         // Name\r
111         Label label = new Label(labelColumn1, SWT.NONE);\r
112         GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(label);\r
113         label.setText("Name:");\r
114 \r
115         name = new org.simantics.browsing.ui.swt.widgets.TrackedText(propertyColumn1, support, SWT.BORDER);\r
116         GridDataFactory.fillDefaults().grab(true, false).applyTo(name.getWidget());\r
117         name.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel));\r
118         name.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel));\r
119         name.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));\r
120 \r
121         // Type\r
122 //        label = new Label(labelColumn2, SWT.NONE);\r
123 //        GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(label);\r
124 //        label.setText("Type:");\r
125 //\r
126 //        type = new TrackedCombo(propertyColumn2, support, SWT.BORDER | SWT.READ_ONLY);\r
127 //        type.addModifyListener(new TypeModifyListener());\r
128 //        type.setItemFactory(new TypeItemFactory());\r
129 //        type.setSelectionFactory(new TypeSelectionFactory());\r
130 //        GridDataFactory.fillDefaults().applyTo(type.getWidget());\r
131         label = new Label(labelColumn2, SWT.NONE);\r
132         label = new Label(propertyColumn2, SWT.NONE);\r
133         \r
134         // Title (Which is different than name)\r
135         label = new Label(labelColumn1, SWT.NONE);\r
136         GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(label);\r
137         label.setText("Title:");\r
138 \r
139         title = new org.simantics.browsing.ui.swt.widgets.TrackedText(propertyColumn1, support, SWT.BORDER);\r
140         GridDataFactory.fillDefaults().grab(true, false).applyTo(title.getWidget());\r
141         title.setTextFactory(new TitleFactory());\r
142         title.addModifyListener(new TitleModifier());\r
143         title.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));\r
144 \r
145         // Time\r
146         label = new Label(labelColumn2, SWT.NONE);\r
147         GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(label);\r
148         label.setText("Time:");\r
149         \r
150         time = new org.simantics.browsing.ui.swt.widgets.TrackedText(propertyColumn2, support, SWT.BORDER);\r
151         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(time.getWidget());\r
152         time.setTextFactory(new DoublePropertyFactory(JFreeChartResource.URIs.Chart_time));\r
153         time.addModifyListener(new DoublePropertyModifier(context, JFreeChartResource.URIs.Chart_time));\r
154         time.setInputValidator(new DoubleValidator(true));\r
155         time.setColorProvider(new JFreeChartPropertyColorProvider(time.getResourceManager()));\r
156         \r
157         Group typeGroup = new Group(composite,SWT.NONE);\r
158         GridDataFactory.fillDefaults().applyTo(typeGroup);\r
159         GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(typeGroup);\r
160         typeGroup.setText("Visuals");\r
161         \r
162         label = new Label(typeGroup, SWT.NONE);\r
163         GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(label);\r
164         label.setText("Type:");\r
165 \r
166         type = new TrackedCombo(typeGroup, support, SWT.BORDER | SWT.READ_ONLY);\r
167         type.addModifyListener(new TypeModifyListener());\r
168         type.setItemFactory(new TypeItemFactory());\r
169         type.setSelectionFactory(new TypeSelectionFactory());\r
170         GridDataFactory.fillDefaults().applyTo(type.getWidget());\r
171         \r
172         label = new Label(typeGroup, SWT.NONE);\r
173         GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(label);\r
174         label.setText("Orientation:");\r
175         \r
176         orientation = new TrackedCombo(typeGroup, support, SWT.BORDER | SWT.READ_ONLY);\r
177         orientation.addModifyListener(new OrientationModifyListener());\r
178         orientation.setItemFactory(new OrientationItemFactory());\r
179         orientation.setSelectionFactory(new OrientationSelectionFactory());\r
180         GridDataFactory.fillDefaults().applyTo(type.getWidget());\r
181 \r
182         \r
183         // Group for hide options\r
184         Group hideGroup = new Group(composite, SWT.NONE);\r
185         GridDataFactory.fillDefaults().applyTo(hideGroup);\r
186         GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(hideGroup);\r
187         hideGroup.setText("Hide");\r
188 \r
189         hgrid = new Button(hideGroup, support, SWT.CHECK);\r
190         hgrid.setText("Grid");\r
191         hgrid.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid, true));\r
192         hgrid.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid));\r
193         htitle = new Button(hideGroup, support, SWT.CHECK);\r
194         htitle.setText("Title");\r
195         htitle.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible, true));\r
196         htitle.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible));\r
197         hlegend = new Button(hideGroup, support, SWT.CHECK);\r
198         hlegend.setText("Legend");\r
199         hlegend.setSelectionFactory(new BooleanPropertyFactory(null, JFreeChartResource.URIs.Chart_visibleLegend, true));\r
200         hlegend.addSelectionListener(new BooleanSelectionListener(context, null, JFreeChartResource.URIs.Chart_visibleLegend));\r
201 \r
202         Group filteringGroup = new Group(composite, SWT.NONE);\r
203         GridDataFactory.fillDefaults().applyTo(filteringGroup);\r
204         GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(filteringGroup);\r
205         hideGroup.setText("Filter");\r
206         label = new Label(filteringGroup, SWT.NONE);\r
207         GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(label);\r
208         label.setText("Use:");\r
209         Button useFilter = new Button(filteringGroup, support, SWT.CHECK);\r
210         useFilter.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Filter_used, false));\r
211         useFilter.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Filter_used));\r
212         label = new Label(filteringGroup, SWT.NONE);\r
213         GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(label);\r
214         label.setText("Percent:");\r
215         TrackedText fraction = new TrackedText(filteringGroup, support, SWT.BORDER);\r
216         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(fraction.getWidget());\r
217         fraction.setTextFactory(new DoublePropertyFactory2(JFreeChartResource.URIs.Plot,JFreeChartResource.URIs.Filter_fraction));\r
218         fraction.addModifyListener(new DoublePropertyModifier2(context, JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Filter_fraction));\r
219         fraction.setInputValidator(new DoubleValidator(true));\r
220         fraction.setColorProvider(new JFreeChartPropertyColorProvider(fraction.getResourceManager()));\r
221         \r
222         // Resize scrolled composite\r
223         sc.setContent(composite);\r
224         Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);\r
225         sc.setMinSize(size);\r
226         \r
227  \r
228     }\r
229 \r
230     /**\r
231      * \r
232      * @author Teemu Lempinen\r
233      *\r
234      */\r
235     private class TypeSelectionFactory extends ReadFactoryImpl<Resource, String> {\r
236         @Override\r
237         public String perform(ReadGraph graph, Resource chart) throws DatabaseException {\r
238             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
239             Layer0 l0 = Layer0.getInstance(graph);\r
240 \r
241             Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));\r
242             if(plot != null) {\r
243                 Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.CategoryDataset));\r
244 \r
245                 if(dataset != null) {\r
246                     Resource renderer = graph.syncRequest(new PossibleObjectWithType(dataset, jfree.Dataset_renderer, jfree.Renderer));\r
247 \r
248                     if(renderer != null && graph.isInstanceOf(renderer, jfree.StackedBarRenderer))\r
249                         return "Stacked";\r
250                 }\r
251             }\r
252             return "Normal";\r
253         }\r
254     }\r
255 \r
256     /**\r
257      * RangeItemFactory finds all inexes of a given enumeration \r
258      * and adds "Sum" and "All" to the returned indexes\r
259      * @author Teemu Lempinen\r
260      *\r
261      */\r
262     private class TypeItemFactory extends ReadFactoryImpl<Resource, Map<String, Object>> {\r
263         @Override\r
264         public Map<String, Object> perform(ReadGraph graph, Resource series) throws DatabaseException {\r
265             LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();\r
266             result.put("Normal", "Normal");\r
267             result.put("Stacked", "Stacked");\r
268             return result;\r
269         }\r
270     }\r
271 \r
272     /**\r
273      * TypeModifyListener for modifying the type of a bar chart \r
274      * @author Teemu Lempinen\r
275      *\r
276      */\r
277     private class TypeModifyListener  extends ComboModifyListenerImpl<Resource> {\r
278         @Override\r
279         public void applyText(WriteGraph graph, Resource chart, String text) throws DatabaseException {\r
280             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
281             Layer0 l0 = Layer0.getInstance(graph);\r
282 \r
283             Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));\r
284             if(plot == null)\r
285                 return;\r
286 \r
287             Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.CategoryDataset));\r
288             if(dataset == null)\r
289                 return;\r
290 \r
291             graph.deny(dataset, jfree.Dataset_renderer);\r
292 \r
293             Resource renderer;\r
294             if(text.equals("Stacked"))\r
295                 renderer = GraphUtils.create2(graph, jfree.StackedBarRenderer);\r
296             else\r
297                 renderer = GraphUtils.create2(graph, jfree.BarRenderer);\r
298 \r
299             graph.claim(dataset, jfree.Dataset_renderer, renderer);\r
300         }\r
301     }\r
302     \r
303     /**\r
304      * \r
305      * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
306      *\r
307      */\r
308     private class OrientationSelectionFactory extends ReadFactoryImpl<Resource, String> {\r
309         @Override\r
310         public String perform(ReadGraph graph, Resource chart) throws DatabaseException {\r
311             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
312             Layer0 l0 = Layer0.getInstance(graph);\r
313 \r
314             Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));\r
315             if(plot != null) {\r
316                 Boolean orientation = graph.getPossibleRelatedValue(plot, jfree.Plot_orientation);\r
317                 if (orientation != null) {\r
318                         if (orientation)\r
319                                 return "Horizontal";\r
320                         return "Vertical";                      \r
321                 }\r
322                             }\r
323             return "Vertical";\r
324         }\r
325     }\r
326 \r
327    /**\r
328     * \r
329     * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
330     *\r
331     */\r
332     private class OrientationItemFactory extends ReadFactoryImpl<Resource, Map<String, Object>> {\r
333         @Override\r
334         public Map<String, Object> perform(ReadGraph graph, Resource series) throws DatabaseException {\r
335             LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();\r
336             result.put("Vertical", "Vertical");\r
337             result.put("Horizontal", "Horizontal");\r
338             return result;\r
339         }\r
340     }\r
341 \r
342     /**\r
343      * \r
344      * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
345      *\r
346      */\r
347     private class OrientationModifyListener  extends ComboModifyListenerImpl<Resource> {\r
348         @Override\r
349         public void applyText(WriteGraph graph, Resource chart, String text) throws DatabaseException {\r
350             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
351             Layer0 l0 = Layer0.getInstance(graph);\r
352 \r
353             Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));\r
354             if(plot == null)\r
355                 return;\r
356 \r
357            \r
358             if(text.equals("Horizontal"))\r
359                 graph.claimLiteral(plot, jfree.Plot_orientation,true);\r
360             else\r
361                 graph.claimLiteral(plot, jfree.Plot_orientation,false);\r
362 \r
363             \r
364         }\r
365     }\r
366 \r
367 }\r