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