]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
ad0ca64538c7d0474a3b0504239a7024c40d925e
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011, 2014 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.pie;\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.AdjustableTab;\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.utils.datastructures.ArrayMap;\r
47 import org.simantics.utils.ui.AdaptionUtils;\r
48 \r
49 /**\r
50  * Tab for modifying series in a pie chart configuration\r
51  * @author Teemu Lempinen\r
52  * @author Tuomas Miettinen\r
53  *\r
54  */\r
55 public class PieSeriesTab extends AdjustableTab implements Widget {\r
56 \r
57     private GraphExplorerComposite explorer;\r
58     private ScrolledComposite propertyContainer;\r
59     private WidgetSupportImpl additionalSupport;\r
60     private Button add, remove;\r
61     private Resource chartResource;\r
62     private int options;\r
63         private Composite composite;\r
64         private Composite buttonComposite;\r
65     \r
66     public PieSeriesTab(int options) {\r
67         additionalSupport = new WidgetSupportImpl();\r
68         this.options = options;\r
69     }\r
70 \r
71     /**\r
72      * Updates the content of propertyContainer  \r
73      * @param context\r
74      */\r
75     private void updateSelection(ISessionContext context) {\r
76         ISelectionProvider selectionProvider = (ISelectionProvider)explorer.getAdapter(ISelectionProvider.class);\r
77         IStructuredSelection selection = (IStructuredSelection)selectionProvider.getSelection();\r
78         final Resource resource = AdaptionUtils.adaptToSingle(selection, Resource.class);\r
79         if(resource == null)\r
80             return;\r
81 \r
82         for(Control child : propertyContainer.getChildren()) {\r
83             child.dispose();\r
84         }\r
85 \r
86         PieSeriesPropertyComposite spc = new PieSeriesPropertyComposite(propertyContainer, context, additionalSupport, options,SWT.NONE);\r
87         propertyContainer.setContent(spc);\r
88         Point size = spc.computeSize(SWT.DEFAULT, SWT.DEFAULT);\r
89         propertyContainer.setMinSize(size);\r
90 \r
91         additionalSupport.fireInput(context, selection);\r
92     }\r
93 \r
94 \r
95     /**\r
96      * SelectionListener for adding a new variable to a plot\r
97      * @author Teemu Lempinen\r
98      *\r
99      */\r
100     private class NewVariableListener extends SelectionListenerImpl<Resource> {\r
101 \r
102         public NewVariableListener(ISessionContext context) {\r
103             super(context);\r
104         }\r
105 \r
106         @Override\r
107         public void apply(WriteGraph graph, Resource input) throws DatabaseException {\r
108             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
109             Layer0 l0 = Layer0.getInstance(graph);\r
110             Resource dataset = null;\r
111             if(input == null) {\r
112                 if(chartResource != null) {\r
113                     Resource plot = graph.syncRequest(new PossibleObjectWithType(chartResource, l0.ConsistsOf, jfree.Plot));\r
114                     if(plot != null)\r
115                         dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.Dataset));\r
116                 }\r
117             } else {\r
118                 if(graph.isInstanceOf(input, jfree.Series)) {\r
119                     dataset = graph.getPossibleObject(input, l0.PartOf);\r
120                 }\r
121             }\r
122             if(dataset != null) {\r
123                 // Create series with no rvi\r
124                 Resource series = ChartUtils.createSeries(graph, dataset, null);\r
125                 graph.claimLiteral(series, jfree.Series_exploded, false);\r
126             }\r
127         }\r
128     }\r
129 \r
130     /**\r
131      * SelectionListener for remove button\r
132      * @author Teemu Lempinen\r
133      *\r
134      */\r
135     private class RemoveListener extends SelectionListenerImpl<Resource> {\r
136 \r
137         public RemoveListener(ISessionContext context) {\r
138             super(context);\r
139         }\r
140 \r
141         /**\r
142          * Removes selected resource from explorer\r
143          */\r
144         @Override\r
145         public void apply(WriteGraph graph, Resource input) throws DatabaseException {\r
146             if(input == null)\r
147                 return; \r
148 \r
149             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
150             Layer0 l0 = Layer0.getInstance(graph);\r
151             Resource list = null;\r
152             if(graph.isInstanceOf(input, jfree.Series)) {\r
153                 // Remove series from dataset and seriesList\r
154                 Resource dataset = graph.getPossibleObject(input, l0.PartOf);\r
155                 if(dataset != null)\r
156                     list = graph.getPossibleObject(dataset, jfree.Dataset_seriesList);\r
157 \r
158                 if(list != null)\r
159                     ListUtils.removeElement(graph, list, input);\r
160                 RemoverUtil.remove(graph, input);\r
161             }\r
162         }\r
163     }\r
164     \r
165     @Override\r
166     public void setInput(ISessionContext context, Object input) {\r
167         chartResource = AdaptionUtils.adaptToSingle(input, Resource.class);\r
168     }\r
169 \r
170         @Override\r
171         protected void createAndAddControls(Composite body, IWorkbenchSite site,\r
172                         final ISessionContext context, WidgetSupport support) {\r
173                 support.register(this);\r
174         composite = new Composite(body, SWT.NONE);\r
175 \r
176         // (Ontology-based) GraphExplorer displaying variables of a pie chart\r
177         explorer = new AxisAndVariablesExplorerComposite(ArrayMap.keys(\r
178                 "displaySelectors", "displayFilter").values(false, false), site, composite, support, SWT.FULL_SELECTION | SWT.BORDER | SWT.SINGLE);\r
179         explorer.setBrowseContexts(JFreeChartResource.URIs.PieSeriesBrowseContext);\r
180         explorer.setInputSource(new SingleSelectionInputSource(\r
181                 Resource.class));\r
182         explorer.getExplorer().setAutoExpandLevel(2); // Expand everything in the beginning\r
183         explorer.finish();\r
184 \r
185         ((Tree)explorer.getExplorerControl()).addSelectionListener(new SelectionAdapter() {\r
186             public void widgetSelected(SelectionEvent e) {\r
187                 updateSelection(context);\r
188             }\r
189         });\r
190 \r
191         // Scrolled composite for displaying properties of a selection in explorer\r
192         propertyContainer = new ScrolledComposite(composite, SWT.H_SCROLL | SWT.V_SCROLL);\r
193         propertyContainer.setExpandHorizontal(true);\r
194         propertyContainer.setExpandVertical(true);\r
195 \r
196 \r
197         // Buttons for adding and removing variables from a pie plot\r
198         buttonComposite = new Composite(composite, SWT.NONE);\r
199 \r
200         add = new Button(buttonComposite, additionalSupport, SWT.NONE);\r
201         add.setText("Add");\r
202         add.addSelectionListener(new NewVariableListener(context));\r
203 \r
204         remove = new Button(buttonComposite, additionalSupport, SWT.NONE);\r
205         remove.setText("Remove");\r
206         remove.addSelectionListener(new RemoveListener(context));\r
207         }\r
208 \r
209         @Override\r
210         protected void createControlLayoutVertical() {\r
211         GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);\r
212         GridLayoutFactory.fillDefaults().numColumns(1).margins(3, 3).applyTo(composite);\r
213 \r
214         GridDataFactory.fillDefaults().hint(150, 200).grab(false, true).applyTo(explorer);\r
215 \r
216         // Scrolled composite for displaying properties of a selection in explorer\r
217         GridDataFactory.fillDefaults().hint(150, 190).span(1, 1).grab(true, false).applyTo(propertyContainer);\r
218         GridLayoutFactory.fillDefaults().applyTo(propertyContainer);\r
219 \r
220         // Buttons for adding and removing variables from a pie plot\r
221         GridDataFactory.fillDefaults().applyTo(buttonComposite);\r
222         GridLayoutFactory.fillDefaults().numColumns(3).applyTo(buttonComposite);\r
223         }\r
224 \r
225         @Override\r
226         protected void createControlLayoutHorizontal(boolean wideScreen) {\r
227         GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);\r
228         GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).applyTo(composite);\r
229 \r
230         GridDataFactory.fillDefaults().hint(250, SWT.DEFAULT).grab(false, true).applyTo(explorer);\r
231 \r
232         // Scrolled composite for displaying properties of a selection in explorer\r
233         GridDataFactory.fillDefaults().span(1, 2).grab(true, true).applyTo(propertyContainer);\r
234         GridLayoutFactory.fillDefaults().applyTo(propertyContainer);\r
235 \r
236         // Buttons for adding and removing variables from a pie plot\r
237         GridDataFactory.fillDefaults().applyTo(buttonComposite);\r
238         GridLayoutFactory.fillDefaults().numColumns(3).applyTo(buttonComposite);\r
239         }\r
240 \r
241 }\r