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