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