]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
459d0b327c09d751e424fa794518377757a84ab1
[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 org.eclipse.jface.layout.GridDataFactory;\r
15 import org.eclipse.jface.layout.GridLayoutFactory;\r
16 import org.eclipse.jface.viewers.ISelectionProvider;\r
17 import org.eclipse.jface.viewers.IStructuredSelection;\r
18 import org.eclipse.swt.SWT;\r
19 import org.eclipse.swt.custom.ScrolledComposite;\r
20 import org.eclipse.swt.events.SelectionAdapter;\r
21 import org.eclipse.swt.events.SelectionEvent;\r
22 import org.eclipse.swt.graphics.Point;\r
23 import org.eclipse.swt.widgets.Composite;\r
24 import org.eclipse.swt.widgets.Control;\r
25 import org.eclipse.swt.widgets.Tree;\r
26 import org.eclipse.ui.IWorkbenchSite;\r
27 import org.simantics.browsing.ui.swt.SingleSelectionInputSource;\r
28 import org.simantics.browsing.ui.swt.widgets.Button;\r
29 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;\r
30 import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl;\r
31 import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
32 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
33 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;\r
34 import org.simantics.db.Resource;\r
35 import org.simantics.db.WriteGraph;\r
36 import org.simantics.db.common.request.PossibleObjectWithType;\r
37 import org.simantics.db.common.utils.ListUtils;\r
38 import org.simantics.db.exception.DatabaseException;\r
39 import org.simantics.db.layer0.util.RemoverUtil;\r
40 import org.simantics.db.management.ISessionContext;\r
41 import org.simantics.jfreechart.chart.ChartUtils;\r
42 import org.simantics.jfreechart.chart.properties.LabelPropertyTabContributor;\r
43 import org.simantics.jfreechart.chart.properties.xyline.AxisAndVariablesExplorerComposite;\r
44 import org.simantics.layer0.Layer0;\r
45 import org.simantics.sysdyn.JFreeChartResource;\r
46 import org.simantics.ui.utils.AdaptionUtils;\r
47 import org.simantics.utils.datastructures.ArrayMap;\r
48 \r
49 /**\r
50  * Tab containing the series of a bar chart\r
51  * @author Teemu Lempinen\r
52  *\r
53  */\r
54 public class BarSeriesTab extends LabelPropertyTabContributor implements Widget {\r
55 \r
56     private GraphExplorerComposite explorer;\r
57     private ScrolledComposite propertyContainer;\r
58     private WidgetSupportImpl additionalSupport;\r
59     private Button add, remove;\r
60     private Resource chartResource;\r
61     private BarSeriesPropertyComposite spc;\r
62 \r
63 \r
64     public BarSeriesTab() {\r
65         additionalSupport = new WidgetSupportImpl();\r
66     }\r
67 \r
68     @Override\r
69     public void createControls(Composite body, IWorkbenchSite site, final ISessionContext context, WidgetSupport support) {\r
70         support.register(this);\r
71         Composite composite = new Composite(body, SWT.NONE);\r
72         GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);\r
73         GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).applyTo(composite);\r
74 \r
75         // (Ontology-based) GraphExplorer displaying variables in a bar chart\r
76         explorer = new AxisAndVariablesExplorerComposite(ArrayMap.keys(\r
77                 "displaySelectors", "displayFilter").values(false, false), site, composite, support, SWT.FULL_SELECTION | SWT.BORDER | SWT.SINGLE);\r
78         explorer.setBrowseContexts(JFreeChartResource.URIs.BarSeriesBrowseContext);\r
79         explorer.setInputSource(new SingleSelectionInputSource(\r
80                 Resource.class));\r
81         explorer.getExplorer().setAutoExpandLevel(2); // Expand everything in the beginning\r
82         explorer.finish();\r
83 \r
84         ((Tree)explorer.getExplorerControl()).addSelectionListener(new SelectionAdapter() {\r
85             public void widgetSelected(SelectionEvent e) {\r
86                 updateSelection(context);\r
87             }\r
88         });\r
89         GridDataFactory.fillDefaults().hint(250, SWT.DEFAULT).grab(false, true).applyTo(explorer);\r
90 \r
91         // Scrolled composite for displaying properties of a selection in explorer\r
92         propertyContainer = new ScrolledComposite(composite, SWT.H_SCROLL | SWT.V_SCROLL);\r
93         GridDataFactory.fillDefaults().span(1, 2).grab(true, true).applyTo(propertyContainer);\r
94         GridLayoutFactory.fillDefaults().applyTo(propertyContainer);\r
95         propertyContainer.setExpandHorizontal(true);\r
96         propertyContainer.setExpandVertical(true);\r
97 \r
98         // Buttons for adding and removing variables from a pie plot\r
99         Composite buttonComposite = new Composite(composite, SWT.NONE);\r
100         GridDataFactory.fillDefaults().applyTo(buttonComposite);\r
101         GridLayoutFactory.fillDefaults().numColumns(3).applyTo(buttonComposite);\r
102 \r
103         add = new Button(buttonComposite, additionalSupport, SWT.NONE);\r
104         add.setText("Add");\r
105         add.addSelectionListener(new NewVariableListener(context));\r
106 \r
107         remove = new Button(buttonComposite, additionalSupport, SWT.NONE);\r
108         remove.setText("Remove");\r
109         remove.addSelectionListener(new RemoveListener(context));\r
110     }\r
111 \r
112     /**\r
113      * Updates the content of propertyContainer  \r
114      * @param context\r
115      */\r
116     private void updateSelection(ISessionContext context) {\r
117         ISelectionProvider selectionProvider = (ISelectionProvider)explorer.getAdapter(ISelectionProvider.class);\r
118         IStructuredSelection selection = (IStructuredSelection)selectionProvider.getSelection();\r
119         final Resource resource = AdaptionUtils.adaptToSingle(selection, Resource.class);\r
120         if(resource == null)\r
121             return;\r
122 \r
123         for(Control child : propertyContainer.getChildren()) {\r
124             child.dispose();\r
125         }\r
126         spc = new BarSeriesPropertyComposite(propertyContainer, context, additionalSupport, SWT.NONE);\r
127 \r
128         additionalSupport.fireInput(context, selection);\r
129 \r
130         propertyContainer.setContent(spc);\r
131         Point size = spc.computeSize(SWT.DEFAULT, SWT.DEFAULT);\r
132         propertyContainer.setMinSize(size);\r
133     }\r
134 \r
135     /**\r
136      * SelectionListener for adding a new variable to a plot\r
137      * @author Teemu Lempinen\r
138      *\r
139      */\r
140     private class NewVariableListener extends SelectionListenerImpl<Resource> {\r
141 \r
142         public NewVariableListener(ISessionContext context) {\r
143             super(context);\r
144         }\r
145 \r
146         @Override\r
147         public void apply(WriteGraph graph, Resource input) throws DatabaseException {\r
148             Resource dataset = null;\r
149             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
150             Layer0 l0 = Layer0.getInstance(graph);\r
151 \r
152             if(input == null) {\r
153                 if(chartResource != null) {\r
154                     Resource plot = graph.syncRequest(new PossibleObjectWithType(chartResource, l0.ConsistsOf, jfree.Plot));\r
155                     if(plot != null)\r
156                         dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.Dataset));\r
157                 }\r
158             } else {\r
159                 if(graph.isInstanceOf(input, jfree.Series)) {\r
160                     dataset = graph.getPossibleObject(input, l0.PartOf);\r
161                 }\r
162             }\r
163 \r
164             if(dataset != null) {\r
165                 // Create series with no rvi\r
166                 ChartUtils.createSeries(graph, dataset, null);\r
167             }\r
168         }\r
169     }\r
170 \r
171     /**\r
172      * SelectionListener for remove button\r
173      * @author Teemu Lempinen\r
174      *\r
175      */\r
176     private class RemoveListener extends SelectionListenerImpl<Resource> {\r
177 \r
178         public RemoveListener(ISessionContext context) {\r
179             super(context);\r
180         }\r
181 \r
182         /**\r
183          * Removes selected resource from explorer\r
184          */\r
185         @Override\r
186         public void apply(WriteGraph graph, Resource input) throws DatabaseException {\r
187             if(input == null)\r
188                 return; \r
189 \r
190             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
191             Layer0 l0 = Layer0.getInstance(graph);\r
192             Resource list = null;\r
193             if(graph.isInstanceOf(input, jfree.Series)) {\r
194                 // Remove series from dataset and seriesList\r
195                 Resource dataset = graph.getPossibleObject(input, l0.PartOf);\r
196                 if(dataset != null)\r
197                     list = graph.getPossibleObject(dataset, jfree.Dataset_seriesList);\r
198 \r
199                 if(list != null)\r
200                     ListUtils.removeElement(graph, list, input);\r
201                 RemoverUtil.remove(graph, input);\r
202             }\r
203         }\r
204     }\r
205 \r
206     @Override\r
207     public void setInput(ISessionContext context, Object input) {\r
208         chartResource = AdaptionUtils.adaptToSingle(input, Resource.class);\r
209     }\r
210 \r
211 \r
212 }\r