]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
387f34e60b14cfebf6c4bfb0ca91e6fcd2144c62
[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.swt.SWT;\r
20 import org.eclipse.swt.custom.ScrolledComposite;\r
21 import org.eclipse.swt.graphics.Point;\r
22 import org.eclipse.swt.widgets.Composite;\r
23 import org.eclipse.swt.widgets.Group;\r
24 import org.eclipse.swt.widgets.Label;\r
25 import org.eclipse.ui.IWorkbenchSite;\r
26 import org.simantics.browsing.ui.swt.widgets.Button;\r
27 import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory;\r
28 import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier;\r
29 import org.simantics.browsing.ui.swt.widgets.TrackedCombo;\r
30 import org.simantics.browsing.ui.swt.widgets.TrackedText;\r
31 import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl;\r
32 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;\r
33 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
34 import org.simantics.db.ReadGraph;\r
35 import org.simantics.db.Resource;\r
36 import org.simantics.db.WriteGraph;\r
37 import org.simantics.db.common.request.PossibleObjectWithType;\r
38 import org.simantics.db.exception.DatabaseException;\r
39 import org.simantics.db.management.ISessionContext;\r
40 import org.simantics.layer0.Layer0;\r
41 import org.simantics.layer0.utils.direct.GraphUtils;\r
42 import org.simantics.sysdyn.JFreeChartResource;\r
43 import org.simantics.sysdyn.ui.properties.LabelPropertyTabContributor;\r
44 import org.simantics.sysdyn.ui.trend.chart.properties.BooleanPropertyFactory;\r
45 import org.simantics.sysdyn.ui.trend.chart.properties.BooleanSelectionListener;\r
46 import org.simantics.sysdyn.ui.trend.chart.properties.JFreeChartPropertyColorProvider;\r
47 import org.simantics.sysdyn.ui.trend.chart.properties.TitleFactory;\r
48 import org.simantics.sysdyn.ui.trend.chart.properties.TitleModifier;\r
49 \r
50 \r
51 /**\r
52  * General properties of a bar chart\r
53  * @author Teemu Lempinen\r
54  *\r
55  */\r
56 public class BarGeneralPropertiesTab extends LabelPropertyTabContributor {\r
57 \r
58     private ScrolledComposite sc;\r
59     private Composite composite;\r
60     private Button hgrid, htitle, hlegend;\r
61     private TrackedText name, title;\r
62     private TrackedCombo type;\r
63 \r
64 \r
65     @Override\r
66     public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {    \r
67         // Scrolled composite containing all of the properties in this tab\r
68         sc = new ScrolledComposite(body, SWT.NONE | SWT.H_SCROLL | SWT.V_SCROLL);\r
69         GridDataFactory.fillDefaults().grab(true, true).applyTo(sc);\r
70         GridLayoutFactory.fillDefaults().applyTo(sc);\r
71         sc.setExpandHorizontal(true);\r
72         sc.setExpandVertical(true);\r
73 \r
74         composite = new Composite(sc, SWT.NONE);\r
75         GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).applyTo(composite);\r
76 \r
77         // General properties\r
78         Group general = new Group(composite, SWT.NONE);\r
79         GridDataFactory.fillDefaults().grab(true, false).applyTo(general);\r
80         GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(general);\r
81         general.setText("General");\r
82 \r
83         // Name\r
84         Label label = new Label(general, SWT.NONE);\r
85         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
86         label.setText("Name:");\r
87 \r
88         Composite c = new Composite(general, SWT.NONE);\r
89         GridDataFactory.fillDefaults().applyTo(c);\r
90         GridLayoutFactory.fillDefaults().numColumns(3).applyTo(c);\r
91 \r
92         name = new org.simantics.browsing.ui.swt.widgets.TrackedText(c, support, SWT.BORDER);\r
93         GridDataFactory.fillDefaults().grab(true, false).applyTo(name.getWidget());\r
94         name.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel));\r
95         name.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel));\r
96         name.setColorProvider(new JFreeChartPropertyColorProvider(name.getResourceManager()));\r
97 \r
98         // Type\r
99         label = new Label(c, SWT.NONE);\r
100         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
101         label.setText("Type:");\r
102 \r
103         type = new TrackedCombo(c, support, SWT.BORDER | SWT.READ_ONLY);\r
104         type.addModifyListener(new TypeModifyListener());\r
105         type.setItemFactory(new TypeItemFactory());\r
106         type.setSelectionFactory(new TypeSelectionFactory());\r
107         GridDataFactory.fillDefaults().applyTo(type.getWidget());\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         // Resize scrolled composite\r
140         sc.setContent(composite);\r
141         Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);\r
142         sc.setMinSize(size);\r
143     }\r
144 \r
145     /**\r
146      * \r
147      * @author Teemu Lempinen\r
148      *\r
149      */\r
150     private class TypeSelectionFactory extends ReadFactoryImpl<Resource, String> {\r
151         @Override\r
152         public String perform(ReadGraph graph, Resource chart) throws DatabaseException {\r
153             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
154             Layer0 l0 = Layer0.getInstance(graph);\r
155 \r
156             Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));\r
157             if(plot != null) {\r
158                 Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.CategoryDataset));\r
159 \r
160                 if(dataset != null) {\r
161                     Resource renderer = graph.syncRequest(new PossibleObjectWithType(dataset, jfree.Dataset_renderer, jfree.Renderer));\r
162 \r
163                     if(renderer != null && graph.isInstanceOf(renderer, jfree.StackedBarRenderer))\r
164                         return "Stacked";\r
165                 }\r
166             }\r
167             return "Normal";\r
168         }\r
169     }\r
170 \r
171     /**\r
172      * RangeItemFactory finds all inexes of a given enumeration \r
173      * and adds "Sum" and "All" to the returned indexes\r
174      * @author Teemu Lempinen\r
175      *\r
176      */\r
177     private class TypeItemFactory extends ReadFactoryImpl<Resource, Map<String, Object>> {\r
178         @Override\r
179         public Map<String, Object> perform(ReadGraph graph, Resource series) throws DatabaseException {\r
180             LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();\r
181             result.put("Normal", "Normal");\r
182             result.put("Stacked", "Stacked");\r
183             return result;\r
184         }\r
185     }\r
186 \r
187     /**\r
188      * TypeModifyListener for modifying the type of a bar chart \r
189      * @author Teemu Lempinen\r
190      *\r
191      */\r
192     private class TypeModifyListener  extends ComboModifyListenerImpl<Resource> {\r
193         @Override\r
194         public void applyText(WriteGraph graph, Resource chart, String text) throws DatabaseException {\r
195             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
196             Layer0 l0 = Layer0.getInstance(graph);\r
197 \r
198             Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot));\r
199             if(plot == null)\r
200                 return;\r
201 \r
202             Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.CategoryDataset));\r
203             if(dataset == null)\r
204                 return;\r
205 \r
206             graph.deny(dataset, jfree.Dataset_renderer);\r
207 \r
208             Resource renderer;\r
209             if(text.equals("Stacked"))\r
210                 renderer = GraphUtils.create2(graph, jfree.StackedBarRenderer);\r
211             else\r
212                 renderer = GraphUtils.create2(graph, jfree.BarRenderer);\r
213 \r
214             graph.claim(dataset, jfree.Dataset_renderer, renderer);\r
215         }\r
216     }\r
217 \r
218 }\r