]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
5982d6dc30f10a06f9fac3ea88674cc4a2ae4eec
[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.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.layer0.Layer0;\r
42 import org.simantics.layer0.utils.direct.GraphUtils;\r
43 import org.simantics.sysdyn.JFreeChartResource;\r
44 import org.simantics.sysdyn.ui.properties.LabelPropertyTabContributor;\r
45 import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyFactory;\r
46 import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyModifier;\r
47 import org.simantics.sysdyn.ui.trend.chart.properties.BooleanPropertyFactory;\r
48 import org.simantics.sysdyn.ui.trend.chart.properties.BooleanSelectionListener;\r
49 import org.simantics.sysdyn.ui.trend.chart.properties.DoubleValidator;\r
50 import org.simantics.sysdyn.ui.trend.chart.properties.JFreeChartPropertyColorProvider;\r
51 import org.simantics.sysdyn.ui.trend.chart.properties.TitleFactory;\r
52 import org.simantics.sysdyn.ui.trend.chart.properties.TitleModifier;\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 \r
68 \r
69     @Override\r
70     public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {    \r
71         // Scrolled composite containing all of the properties in this tab\r
72         sc = new ScrolledComposite(body, SWT.NONE | SWT.H_SCROLL | SWT.V_SCROLL);\r
73         GridDataFactory.fillDefaults().grab(true, true).applyTo(sc);\r
74         GridLayoutFactory.fillDefaults().applyTo(sc);\r
75         sc.setExpandHorizontal(true);\r
76         sc.setExpandVertical(true);\r
77 \r
78         composite = new Composite(sc, SWT.NONE);\r
79         GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).applyTo(composite);\r
80 \r
81         // General properties\r
82         Group general = new Group(composite, SWT.NONE);\r
83         GridDataFactory.fillDefaults().grab(true, false).applyTo(general);\r
84         GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(4).applyTo(general);\r
85         general.setText("General");\r
86 \r
87         // first column: labels\r
88         Composite labelColumn1 = new Composite(general, SWT.NONE);\r
89         GridDataFactory.fillDefaults().grab(false, true).applyTo(labelColumn1);\r
90         GridLayoutFactory.fillDefaults().applyTo(labelColumn1);\r
91         \r
92         // first column: name and title\r
93         Composite propertyColumn1 = new Composite(general, SWT.NONE);\r
94         GridDataFactory.fillDefaults().grab(true, true).applyTo(propertyColumn1);\r
95         GridLayoutFactory.fillDefaults().spacing(0, LayoutConstants.getSpacing().y).applyTo(propertyColumn1);\r
96         \r
97         // first column: labels\r
98         Composite labelColumn2 = new Composite(general, SWT.NONE);\r
99         GridDataFactory.fillDefaults().grab(false, true).applyTo(labelColumn2);\r
100         GridLayoutFactory.fillDefaults().spacing(0, LayoutConstants.getSpacing().y).applyTo(labelColumn2);\r
101         \r
102         // first column: type and time\r
103         Composite propertyColumn2 = new Composite(general, SWT.NONE);\r
104         GridDataFactory.fillDefaults().grab(false, true).applyTo(propertyColumn2);\r
105         GridLayoutFactory.fillDefaults().applyTo(propertyColumn2);\r
106         \r
107         // Name\r
108         Label label = new Label(labelColumn1, SWT.NONE);\r
109         GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(label);\r
110         label.setText("Name:");\r
111 \r
112         name = new org.simantics.browsing.ui.swt.widgets.TrackedText(propertyColumn1, support, SWT.BORDER);\r
113         GridDataFactory.fillDefaults().grab(true, false).applyTo(name.getWidget());\r
114         name.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel));\r
115         name.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel));\r
116         name.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));\r
117 \r
118         // Type\r
119         label = new Label(labelColumn2, SWT.NONE);\r
120         GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(label);\r
121         label.setText("Type:");\r
122 \r
123         type = new TrackedCombo(propertyColumn2, support, SWT.BORDER | SWT.READ_ONLY);\r
124         type.addModifyListener(new TypeModifyListener());\r
125         type.setItemFactory(new TypeItemFactory());\r
126         type.setSelectionFactory(new TypeSelectionFactory());\r
127         GridDataFactory.fillDefaults().applyTo(type.getWidget());\r
128 \r
129         // Title (Which is different than name)\r
130         label = new Label(labelColumn1, SWT.NONE);\r
131         GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(label);\r
132         label.setText("Title:");\r
133 \r
134         title = new org.simantics.browsing.ui.swt.widgets.TrackedText(propertyColumn1, support, SWT.BORDER);\r
135         GridDataFactory.fillDefaults().grab(true, false).applyTo(title.getWidget());\r
136         title.setTextFactory(new TitleFactory());\r
137         title.addModifyListener(new TitleModifier());\r
138         title.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));\r
139 \r
140         // Time\r
141         label = new Label(labelColumn2, SWT.NONE);\r
142         GridDataFactory.fillDefaults().grab(false, true).align(SWT.END, SWT.CENTER).applyTo(label);\r
143         label.setText("Time:");\r
144         \r
145         time = new org.simantics.browsing.ui.swt.widgets.TrackedText(propertyColumn2, support, SWT.BORDER);\r
146         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(time.getWidget());\r
147         time.setTextFactory(new DoublePropertyFactory(JFreeChartResource.URIs.Chart_time));\r
148         time.addModifyListener(new DoublePropertyModifier(context, JFreeChartResource.URIs.Chart_time));\r
149         time.setInputValidator(new DoubleValidator(true));\r
150         time.setColorProvider(new JFreeChartPropertyColorProvider(time.getResourceManager()));\r
151         \r
152         // Group for hide options\r
153         Group hideGroup = new Group(composite, SWT.NONE);\r
154         GridDataFactory.fillDefaults().applyTo(hideGroup);\r
155         GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(hideGroup);\r
156         hideGroup.setText("Hide");\r
157 \r
158         hgrid = new Button(hideGroup, support, SWT.CHECK);\r
159         hgrid.setText("Grid");\r
160         hgrid.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid, true));\r
161         hgrid.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid));\r
162         htitle = new Button(hideGroup, support, SWT.CHECK);\r
163         htitle.setText("Title");\r
164         htitle.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible, true));\r
165         htitle.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible));\r
166         hlegend = new Button(hideGroup, support, SWT.CHECK);\r
167         hlegend.setText("Legend");\r
168         hlegend.setSelectionFactory(new BooleanPropertyFactory(null, JFreeChartResource.URIs.Chart_visibleLegend, true));\r
169         hlegend.addSelectionListener(new BooleanSelectionListener(context, null, JFreeChartResource.URIs.Chart_visibleLegend));\r
170 \r
171         \r
172         \r
173         // Resize scrolled composite\r
174         sc.setContent(composite);\r
175         Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);\r
176         sc.setMinSize(size);\r
177     }\r
178 \r
179     /**\r
180      * \r
181      * @author Teemu Lempinen\r
182      *\r
183      */\r
184     private class TypeSelectionFactory extends ReadFactoryImpl<Resource, String> {\r
185         @Override\r
186         public String perform(ReadGraph graph, Resource chart) throws DatabaseException {\r
187             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
188             Layer0 l0 = Layer0.getInstance(graph);\r
189 \r
190             Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));\r
191             if(plot != null) {\r
192                 Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.CategoryDataset));\r
193 \r
194                 if(dataset != null) {\r
195                     Resource renderer = graph.syncRequest(new PossibleObjectWithType(dataset, jfree.Dataset_renderer, jfree.Renderer));\r
196 \r
197                     if(renderer != null && graph.isInstanceOf(renderer, jfree.StackedBarRenderer))\r
198                         return "Stacked";\r
199                 }\r
200             }\r
201             return "Normal";\r
202         }\r
203     }\r
204 \r
205     /**\r
206      * RangeItemFactory finds all inexes of a given enumeration \r
207      * and adds "Sum" and "All" to the returned indexes\r
208      * @author Teemu Lempinen\r
209      *\r
210      */\r
211     private class TypeItemFactory extends ReadFactoryImpl<Resource, Map<String, Object>> {\r
212         @Override\r
213         public Map<String, Object> perform(ReadGraph graph, Resource series) throws DatabaseException {\r
214             LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();\r
215             result.put("Normal", "Normal");\r
216             result.put("Stacked", "Stacked");\r
217             return result;\r
218         }\r
219     }\r
220 \r
221     /**\r
222      * TypeModifyListener for modifying the type of a bar chart \r
223      * @author Teemu Lempinen\r
224      *\r
225      */\r
226     private class TypeModifyListener  extends ComboModifyListenerImpl<Resource> {\r
227         @Override\r
228         public void applyText(WriteGraph graph, Resource chart, String text) throws DatabaseException {\r
229             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
230             Layer0 l0 = Layer0.getInstance(graph);\r
231 \r
232             Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));\r
233             if(plot == null)\r
234                 return;\r
235 \r
236             Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.CategoryDataset));\r
237             if(dataset == null)\r
238                 return;\r
239 \r
240             graph.deny(dataset, jfree.Dataset_renderer);\r
241 \r
242             Resource renderer;\r
243             if(text.equals("Stacked"))\r
244                 renderer = GraphUtils.create2(graph, jfree.StackedBarRenderer);\r
245             else\r
246                 renderer = GraphUtils.create2(graph, jfree.BarRenderer);\r
247 \r
248             graph.claim(dataset, jfree.Dataset_renderer, renderer);\r
249         }\r
250     }\r
251 \r
252 }\r