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