]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
92d4f9e05b488d8c61d394e69e74f09369e3fbbe
[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.xyline;\r
13 \r
14 import java.util.List;\r
15 \r
16 import org.eclipse.jface.layout.GridDataFactory;\r
17 import org.eclipse.jface.layout.GridLayoutFactory;\r
18 import org.eclipse.jface.viewers.ISelectionProvider;\r
19 import org.eclipse.jface.viewers.IStructuredSelection;\r
20 import org.eclipse.swt.SWT;\r
21 import org.eclipse.swt.custom.ScrolledComposite;\r
22 import org.eclipse.swt.events.SelectionAdapter;\r
23 import org.eclipse.swt.events.SelectionEvent;\r
24 import org.eclipse.swt.graphics.Point;\r
25 import org.eclipse.swt.widgets.Composite;\r
26 import org.eclipse.swt.widgets.Control;\r
27 import org.eclipse.swt.widgets.Tree;\r
28 import org.eclipse.ui.IWorkbenchSite;\r
29 import org.simantics.browsing.ui.NodeContext;\r
30 import org.simantics.browsing.ui.swt.SingleSelectionInputSource;\r
31 import org.simantics.browsing.ui.swt.widgets.Button;\r
32 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;\r
33 import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl;\r
34 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
35 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;\r
36 import org.simantics.db.ReadGraph;\r
37 import org.simantics.db.Resource;\r
38 import org.simantics.db.WriteGraph;\r
39 import org.simantics.db.common.request.PossibleObjectWithType;\r
40 import org.simantics.db.common.utils.ListUtils;\r
41 import org.simantics.db.exception.DatabaseException;\r
42 import org.simantics.db.layer0.util.RemoverUtil;\r
43 import org.simantics.db.management.ISessionContext;\r
44 import org.simantics.db.request.Read;\r
45 import org.simantics.jfreechart.chart.ChartUtils;\r
46 import org.simantics.jfreechart.chart.properties.AdjustableTab;\r
47 import org.simantics.layer0.Layer0;\r
48 import org.simantics.sysdyn.JFreeChartResource;\r
49 import org.simantics.ui.SimanticsUI;\r
50 import org.simantics.utils.datastructures.ArrayMap;\r
51 import org.simantics.utils.ui.AdaptionUtils;\r
52 \r
53 /**\r
54  * PropertyTab displaying properties of axis and variables of a chart\r
55  *  \r
56  * @author Teemu Lempinen\r
57  * @author Tuomas Miettinen\r
58  *\r
59  */\r
60 public class XYLineAxisAndVariablesTab extends AdjustableTab {\r
61 \r
62     private GraphExplorerComposite explorer;\r
63     private ScrolledComposite propertyContainer;\r
64     private Button addAxis, addVariable, remove;\r
65     private WidgetSupportImpl additionalSupport;\r
66         private Composite composite;\r
67         private Composite buttonComposite;\r
68 \r
69     public XYLineAxisAndVariablesTab() {\r
70         additionalSupport = new WidgetSupportImpl();\r
71     }\r
72 \r
73     /**\r
74      * Updates the content of propertyContainer  \r
75      * @param context\r
76      */\r
77     private void updateSelection(ISessionContext context) {\r
78         ISelectionProvider selectionProvider = (ISelectionProvider)explorer.getAdapter(ISelectionProvider.class);\r
79         IStructuredSelection selection = (IStructuredSelection)selectionProvider.getSelection();\r
80         final Resource resource = AdaptionUtils.adaptToSingle(selection, Resource.class);\r
81         if(resource == null)\r
82             return;\r
83 \r
84         // Get the type of the selected node (axis or series)\r
85         String typeUri = null;\r
86         try {\r
87             typeUri = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
88 \r
89                 @Override\r
90                 public String perform(ReadGraph graph) throws DatabaseException {\r
91                     JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
92                     if(graph.isInstanceOf(resource, jfree.Axis))\r
93                         return graph.getURI(jfree.Axis);\r
94                     else if (graph.isInstanceOf(resource, jfree.Series))\r
95                         return graph.getURI(jfree.Series);\r
96                     return null;\r
97                 }\r
98 \r
99             });\r
100         } catch (DatabaseException e) {\r
101             e.printStackTrace();\r
102         }\r
103 \r
104         // Create a PropertyComposite for the selected node\r
105         if(typeUri != null) {\r
106 \r
107             for(Control child : propertyContainer.getChildren()) {\r
108                 child.dispose();\r
109             }\r
110 \r
111             if(typeUri.equals(JFreeChartResource.URIs.Axis)) {\r
112                 AxisPropertyComposite apc = new AxisPropertyComposite(propertyContainer, context, additionalSupport, SWT.NONE, isVertical());\r
113                 propertyContainer.setContent(apc);\r
114                 Point size = apc.computeSize(SWT.DEFAULT, SWT.DEFAULT);\r
115                 propertyContainer.setMinSize(size);\r
116             } else if(typeUri.equals(JFreeChartResource.URIs.Series)) {\r
117                 SeriesPropertyComposite spc = new SeriesPropertyComposite(propertyContainer, context, additionalSupport, SWT.NONE);\r
118                 propertyContainer.setContent(spc);\r
119                 Point size = spc.computeSize(SWT.DEFAULT, SWT.DEFAULT);\r
120                 propertyContainer.setMinSize(size);\r
121             }\r
122         }\r
123 \r
124         additionalSupport.fireInput(context, selection);\r
125     }\r
126 \r
127     /**\r
128      * SelectionListener for adding a new range axis to a plot\r
129      * @author Teemu Lempinen\r
130      *\r
131      */\r
132     private class NewAxisListener extends SelectionListenerImpl<Resource> {\r
133 \r
134         public NewAxisListener(ISessionContext context) {\r
135             super(context);\r
136         }\r
137 \r
138         @Override\r
139         public void apply(WriteGraph graph, Resource chart) throws DatabaseException {\r
140             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
141             Layer0 l0 = Layer0.getInstance(graph);\r
142             Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.Plot));\r
143             if(plot != null) {\r
144                 Resource rangeAxis = ChartUtils.createNumberRangeAxis(graph, plot);\r
145                 if(rangeAxis != null) {\r
146                     Resource domainAxis = graph.getPossibleObject(plot, jfree.Plot_domainAxis);\r
147                     ChartUtils.createXYDataset(graph, plot, domainAxis, rangeAxis);\r
148                 }\r
149             }\r
150         }\r
151     }\r
152 \r
153 \r
154     /**\r
155      * SelectionListener for adding a new variable to a plot\r
156      * @author Teemu Lempinen\r
157      *\r
158      */\r
159     private class NewVariableListener extends SelectionListenerImpl<Resource> {\r
160 \r
161         public NewVariableListener(ISessionContext context) {\r
162             super(context);\r
163         }\r
164 \r
165         @Override\r
166         public void apply(WriteGraph graph, Resource input) throws DatabaseException {\r
167             NodeContext nc = explorer.getExplorer().getRoot();\r
168             if(nc == null)\r
169                 return;\r
170             \r
171             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
172             Layer0 l0 = Layer0.getInstance(graph);\r
173             \r
174             if(input == null) {\r
175                 Resource chart = AdaptionUtils.adaptToSingle(nc, Resource.class);\r
176                 if(chart == null) return;\r
177                 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.Plot));\r
178                 if(plot == null) return;\r
179                 Resource rangelist = graph.getPossibleObject(plot, jfree.Plot_rangeAxisList);\r
180                 if(rangelist == null) return;\r
181                 List<Resource> list = ListUtils.toList(graph, rangelist);\r
182                 if(list == null || list.isEmpty()) return;\r
183                 input = list.get(0);\r
184             }\r
185             \r
186             Resource dataset;\r
187             if(graph.isInstanceOf(input, jfree.Series)) {\r
188                 // Selected resource is series. Add to same dataset\r
189                 dataset = graph.getPossibleObject(input, l0.PartOf);\r
190             } else {\r
191                 // Selected resource is axis. Find the dataset it is mapped to or create dataset if not created already\r
192                 dataset = graph.getPossibleObject(input, jfree.Dataset_mapToRangeAxis_Inverse);\r
193                 if(dataset == null) {\r
194                     Resource plot = graph.getPossibleObject(input, l0.PartOf);\r
195                     if(plot == null) return;\r
196                     Resource domainAxis = graph.getPossibleObject(plot, jfree.Plot_domainAxis);\r
197                     if(domainAxis == null) return;\r
198                     ChartUtils.createXYDataset(graph, plot, domainAxis, input);\r
199                 }\r
200             }\r
201 \r
202             if(dataset != null) {\r
203                 // Create series with no rvi\r
204                 ChartUtils.createSeries(graph, dataset, null);\r
205             }\r
206         }\r
207     }\r
208 \r
209 \r
210     /**\r
211      * SelectionListener for remove button\r
212      * @author Teemu Lempinen\r
213      *\r
214      */\r
215     private class RemoveListener extends SelectionListenerImpl<Resource> {\r
216 \r
217         public RemoveListener(ISessionContext context) {\r
218             super(context);\r
219         }\r
220 \r
221         /**\r
222          * Removes selected resource from explorer\r
223          */\r
224         @Override\r
225         public void apply(WriteGraph graph, Resource input) throws DatabaseException {\r
226             if(input == null)\r
227                 return; \r
228 \r
229             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
230             Layer0 l0 = Layer0.getInstance(graph);\r
231             Resource list = null;\r
232             if(graph.isInstanceOf(input, jfree.Series)) {\r
233                 // Remove series from dataset and seriesList\r
234                 Resource dataset = graph.getPossibleObject(input, l0.PartOf);\r
235                 if(dataset != null)\r
236                     list = graph.getPossibleObject(dataset, jfree.Dataset_seriesList);\r
237             } else {\r
238                 // Remove associated dataset\r
239                 Resource dataset = graph.getPossibleObject(input, jfree.Dataset_mapToRangeAxis_Inverse);\r
240                 if(dataset != null) {\r
241                     graph.deny(dataset, jfree.Dataset_mapToDomainAxis);\r
242                     graph.deny(dataset, jfree.Dataset_mapToRangeAxis);\r
243                     RemoverUtil.remove(graph, dataset);\r
244                 }\r
245 \r
246                 // Remove axis from plot and rangeAxisList\r
247                 Resource plot = graph.getPossibleObject(input, l0.PartOf);\r
248                 if(plot != null)\r
249                     list = graph.getPossibleObject(plot, jfree.Plot_rangeAxisList);\r
250             }\r
251             if(list != null)\r
252                 ListUtils.removeElement(graph, list, input);\r
253             RemoverUtil.remove(graph, input);\r
254         }\r
255     }\r
256 \r
257 \r
258         @Override\r
259         protected void createAndAddControls(Composite body, IWorkbenchSite site,\r
260                         final ISessionContext context, WidgetSupport support) {\r
261                 composite = new Composite(body, SWT.NONE);\r
262 \r
263         // (Ontology-based) GraphExplorer displaying range axis and variables mapped to those axis\r
264         explorer = new AxisAndVariablesExplorerComposite(ArrayMap.keys(\r
265                 "displaySelectors", "displayFilter").values(false, false), site, composite, support, SWT.FULL_SELECTION | SWT.BORDER | SWT.SINGLE);\r
266         explorer.setBrowseContexts(JFreeChartResource.URIs.ChartAxisAndVariablesBrowseContext);\r
267         explorer.setInputSource(new SingleSelectionInputSource(\r
268                 Resource.class));\r
269         explorer.getExplorer().setAutoExpandLevel(2); // Expand everything in the beginning\r
270         explorer.finish();\r
271 \r
272         ((Tree)explorer.getExplorerControl()).addSelectionListener(new SelectionAdapter() {\r
273             public void widgetSelected(SelectionEvent e) {\r
274                 updateSelection(context);\r
275             }\r
276         });\r
277 \r
278         // Scrolled composite for displaying properties of a selection in explorer\r
279         propertyContainer = new ScrolledComposite(composite, SWT.H_SCROLL | SWT.V_SCROLL);\r
280         propertyContainer.setExpandHorizontal(true);\r
281         propertyContainer.setExpandVertical(true);\r
282 \r
283         // Buttons for adding axis and variables and removing selected items from explorer\r
284         buttonComposite = new Composite(composite, SWT.NONE);\r
285 \r
286         addAxis = new Button(buttonComposite, support, SWT.NONE);\r
287         addAxis.setText("Add axis");\r
288         addAxis.addSelectionListener(new NewAxisListener(context));\r
289 \r
290         addVariable = new Button(buttonComposite, additionalSupport, SWT.NONE);\r
291         addVariable.setText("Add variable");\r
292         addVariable.addSelectionListener(new NewVariableListener(context));\r
293 \r
294         remove = new Button(buttonComposite, additionalSupport, SWT.NONE);\r
295         remove.setText("Remove");\r
296         remove.addSelectionListener(new RemoveListener(context));\r
297         }\r
298 \r
299         @Override\r
300         protected void createControlLayoutVertical() {\r
301         GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);\r
302         GridLayoutFactory.fillDefaults().numColumns(1).margins(3, 3).applyTo(composite);\r
303 \r
304         GridDataFactory.fillDefaults().hint(220, SWT.DEFAULT).grab(false, true).applyTo(explorer);\r
305 \r
306         // Scrolled composite for displaying properties of a selection in explorer\r
307         GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 210).span(1, 1).grab(true, false).applyTo(propertyContainer);\r
308         GridLayoutFactory.fillDefaults().applyTo(propertyContainer);\r
309 \r
310         // Buttons for adding axis and variables and removing selected items from explorer\r
311         GridDataFactory.fillDefaults().applyTo(buttonComposite);\r
312         GridLayoutFactory.fillDefaults().numColumns(3).applyTo(buttonComposite);\r
313         }\r
314 \r
315         @Override\r
316         protected void createControlLayoutHorizontal(boolean wideScreen) {\r
317         GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);\r
318         GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).applyTo(composite);\r
319 \r
320         GridDataFactory.fillDefaults().hint(250, SWT.DEFAULT).grab(false, true).applyTo(explorer);\r
321 \r
322         // Scrolled composite for displaying properties of a selection in explorer\r
323         GridDataFactory.fillDefaults().hint(SWT.DEFAULT, SWT.DEFAULT).span(1, 2).grab(true, true).applyTo(propertyContainer);\r
324         GridLayoutFactory.fillDefaults().applyTo(propertyContainer);\r
325 \r
326         // Buttons for adding axis and variables and removing selected items from explorer\r
327         GridDataFactory.fillDefaults().applyTo(buttonComposite);\r
328         GridLayoutFactory.fillDefaults().numColumns(3).applyTo(buttonComposite);\r
329         }\r
330 }\r