]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
d2ed2907395f07dc9ca5e9b1b68f9f5dcdd14b35
[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 org.eclipse.jface.layout.GridDataFactory;\r
15 import org.eclipse.jface.layout.GridLayoutFactory;\r
16 import org.eclipse.swt.SWT;\r
17 import org.eclipse.swt.custom.ScrolledComposite;\r
18 import org.eclipse.swt.graphics.Point;\r
19 import org.eclipse.swt.widgets.Combo;\r
20 import org.eclipse.swt.widgets.Composite;\r
21 import org.eclipse.swt.widgets.Group;\r
22 import org.eclipse.swt.widgets.Label;\r
23 import org.eclipse.swt.widgets.Spinner;\r
24 import org.eclipse.ui.IWorkbenchSite;\r
25 import org.simantics.browsing.ui.swt.widgets.Button;\r
26 import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory;\r
27 import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier;\r
28 import org.simantics.browsing.ui.swt.widgets.TrackedText;\r
29 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;\r
30 import org.simantics.browsing.ui.swt.widgets.impl.TextModifyListener;\r
31 import org.simantics.browsing.ui.swt.widgets.impl.TrackedModifyEvent;\r
32 import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
33 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
34 import org.simantics.databoard.Bindings;\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.common.request.WriteRequest;\r
40 import org.simantics.db.exception.DatabaseException;\r
41 import org.simantics.db.management.ISessionContext;\r
42 import org.simantics.layer0.Layer0;\r
43 import org.simantics.sysdyn.JFreeChartResource;\r
44 import org.simantics.sysdyn.ui.properties.LabelPropertyTabContributor;\r
45 import org.simantics.sysdyn.ui.trend.chart.properties.BooleanPropertyFactory;\r
46 import org.simantics.sysdyn.ui.trend.chart.properties.BooleanSelectionListener;\r
47 import org.simantics.sysdyn.ui.trend.chart.properties.JFreeChartPropertyColorProvider;\r
48 import org.simantics.sysdyn.ui.trend.chart.properties.TitleFactory;\r
49 import org.simantics.sysdyn.ui.trend.chart.properties.TitleModifier;\r
50 import org.simantics.sysdyn.ui.trend.chart.properties.TrackedSpinner;\r
51 import org.simantics.ui.utils.AdaptionUtils;\r
52 \r
53 /**\r
54  * General properties of a bar chart\r
55  * @author Teemu Lempinen\r
56  *\r
57  */\r
58 public class BarGeneralPropertiesTab extends LabelPropertyTabContributor {\r
59 \r
60     private ScrolledComposite sc;\r
61     private Composite composite;\r
62     private Button hgrid, htitle, hlegend;\r
63     private TrackedText name, title;\r
64     private Combo type;\r
65     private TrackedSpinner angle;\r
66 \r
67     @Override\r
68     public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {    \r
69         // Scrolled composite containing all of the properties in this tab\r
70         sc = new ScrolledComposite(body, SWT.NONE | SWT.H_SCROLL | SWT.V_SCROLL);\r
71         GridDataFactory.fillDefaults().grab(true, true).applyTo(sc);\r
72         GridLayoutFactory.fillDefaults().applyTo(sc);\r
73         sc.setExpandHorizontal(true);\r
74         sc.setExpandVertical(true);\r
75 \r
76         composite = new Composite(sc, SWT.NONE);\r
77         GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).applyTo(composite);\r
78 \r
79         // General properties\r
80         Group general = new Group(composite, SWT.NONE);\r
81         GridDataFactory.fillDefaults().grab(true, false).applyTo(general);\r
82         GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(general);\r
83         general.setText("General");\r
84 \r
85         // Name\r
86         Label label = new Label(general, SWT.NONE);\r
87         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
88         label.setText("Name:");\r
89 \r
90         Composite c = new Composite(general, SWT.NONE);\r
91         GridDataFactory.fillDefaults().applyTo(c);\r
92         GridLayoutFactory.fillDefaults().numColumns(3).applyTo(c);\r
93 \r
94         name = new org.simantics.browsing.ui.swt.widgets.TrackedText(c, support, SWT.BORDER);\r
95         GridDataFactory.fillDefaults().grab(true, false).applyTo(name.getWidget());\r
96         name.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel));\r
97         name.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel));\r
98         name.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));\r
99 \r
100         // Type\r
101         label = new Label(c, SWT.NONE);\r
102         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
103         label.setText("Type:");\r
104 \r
105         type = new Combo(c, SWT.BORDER | SWT.READ_ONLY);\r
106         type.setItems(new String[] {"Normal", "Stacked"});\r
107         type.select(0);\r
108 \r
109         // Title (Which is different than name)\r
110         label = new Label(general, SWT.NONE);\r
111         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
112         label.setText("Title:");\r
113 \r
114         title = new org.simantics.browsing.ui.swt.widgets.TrackedText(general, support, SWT.BORDER);\r
115         GridDataFactory.fillDefaults().grab(true, false).applyTo(title.getWidget());\r
116         title.setTextFactory(new TitleFactory());\r
117         title.addModifyListener(new TitleModifier());\r
118         title.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));\r
119 \r
120         // Group for hide options\r
121         Group hideGroup = new Group(composite, SWT.NONE);\r
122         GridDataFactory.fillDefaults().applyTo(hideGroup);\r
123         GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(hideGroup);\r
124         hideGroup.setText("Hide");\r
125 \r
126         hgrid = new Button(hideGroup, support, SWT.CHECK);\r
127         hgrid.setText("Grid");\r
128         hgrid.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid, true));\r
129         hgrid.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.Plot, JFreeChartResource.URIs.Plot_visibleGrid));\r
130         htitle = new Button(hideGroup, support, SWT.CHECK);\r
131         htitle.setText("Title");\r
132         htitle.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible, true));\r
133         htitle.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.TextTitle, JFreeChartResource.URIs.visible));\r
134         hlegend = new Button(hideGroup, support, SWT.CHECK);\r
135         hlegend.setText("Legend");\r
136         hlegend.setSelectionFactory(new BooleanPropertyFactory(null, JFreeChartResource.URIs.Chart_visibleLegend, true));\r
137         hlegend.addSelectionListener(new BooleanSelectionListener(context, null, JFreeChartResource.URIs.Chart_visibleLegend));\r
138 \r
139         Composite angleComposite = new Composite(composite, SWT.NONE);\r
140         GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(angleComposite);\r
141         GridLayoutFactory.fillDefaults().numColumns(2).applyTo(angleComposite);\r
142         // Line width\r
143         label = new Label(angleComposite, SWT.NONE);\r
144         label.setText("Label angle:");\r
145         GridDataFactory.fillDefaults().align(SWT.END, SWT.FILL).applyTo(label);\r
146 \r
147         angle = new TrackedSpinner(angleComposite, support, SWT.BORDER);\r
148         angle.setSelectionFactory(new AngleSelectionFactory());\r
149         angle.addModifyListener(new AngleModifyListener());\r
150         angle.setMinimum(0);\r
151         angle.setMaximum(90);\r
152         angle.getWidget().setIncrement(5);\r
153         GridDataFactory.fillDefaults().applyTo(angle.getWidget());\r
154 \r
155         sc.setContent(composite);\r
156         Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);\r
157         sc.setMinSize(size);\r
158     }\r
159 \r
160     /**\r
161      * ModifyListener for the angle {@link TrackedSpinner}\r
162      * \r
163      * @author Teemu Lempinen\r
164      *\r
165      */\r
166     private class AngleModifyListener implements TextModifyListener, Widget {\r
167 \r
168         private ISessionContext context;\r
169         private Object lastInput = null;\r
170 \r
171         @Override\r
172         public void modifyText(TrackedModifyEvent e) {\r
173             if(context == null)\r
174                 return;\r
175 \r
176             // Get the text value from spinner and associated resource (input)\r
177             Spinner spinner = (Spinner)e.getWidget();\r
178             final String textValue = spinner.getText();\r
179             final Object input = lastInput;\r
180 \r
181             try {\r
182                 context.getSession().syncRequest(new WriteRequest() {\r
183 \r
184                     @Override\r
185                     public void perform(WriteGraph graph) throws DatabaseException {\r
186                         JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
187                         Layer0 l0 = Layer0.getInstance(graph);\r
188 \r
189                         Resource chart = AdaptionUtils.adaptToSingle(input, Resource.class);\r
190                         Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));\r
191                         Resource domainAxis = graph.getPossibleObject(plot, jfree.Plot_domainAxis);\r
192 \r
193                         try {\r
194                             // usually reliable, since the spinner does all the checks\r
195                             Double value = Double.parseDouble(textValue); \r
196                             graph.claimLiteral(domainAxis, jfree.Axis_rotateLabelDegrees, value, Bindings.DOUBLE);\r
197                         } catch (NumberFormatException e) {\r
198                             graph.claimLiteral(domainAxis, jfree.Axis_rotateLabelDegrees, 0.0, Bindings.DOUBLE);\r
199                         }\r
200                     }\r
201 \r
202                 });\r
203             } catch (DatabaseException e1) {\r
204                 e1.printStackTrace();\r
205             }\r
206         }\r
207 \r
208         @Override\r
209         public void setInput(ISessionContext context, Object parameter) {\r
210             this.context = context;\r
211             lastInput = parameter;\r
212         }\r
213 \r
214     }\r
215 \r
216     /**\r
217      * Class for setting the value for angle {@link TrackedSpinner}\r
218      * @author Teemu Lempinen\r
219      *\r
220      */\r
221     private class AngleSelectionFactory extends ReadFactoryImpl<Resource, Integer>   {\r
222 \r
223         @Override\r
224         public Integer perform(ReadGraph graph, Resource chart) throws DatabaseException {\r
225             Double angle = 0.0;\r
226             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
227             Layer0 l0 = Layer0.getInstance(graph);\r
228             Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));\r
229             if(plot != null) {\r
230                 Resource domainAxis = graph.getPossibleObject(plot, jfree.Plot_domainAxis);\r
231                 if(domainAxis != null) {\r
232                     Double value = graph.getPossibleRelatedValue(domainAxis, jfree.Axis_rotateLabelDegrees);\r
233                     if(value != null)\r
234                         angle = value;\r
235                 }\r
236             }\r
237             return angle.intValue();\r
238 \r
239         }\r
240 \r
241     }\r
242 \r
243 }\r