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