1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.trend.chart.properties;
\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
52 * PropertyTab displaying properties of axis and variables of a chart
\r
54 * @author Teemu Lempinen
\r
57 public class ChartAxisAndVariablesTab extends LabelPropertyTabContributor {
\r
59 private GraphExplorerComposite explorer;
\r
60 private ScrolledComposite propertyContainer;
\r
61 private Button addAxis, addVariable, remove;
\r
62 private WidgetSupportImpl additionalSupport;
\r
64 public ChartAxisAndVariablesTab() {
\r
65 additionalSupport = new WidgetSupportImpl();
\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
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
80 explorer.getExplorer().setAutoExpandLevel(2); // Expand everything in the beginning
\r
83 ((Tree)explorer.getExplorerControl()).addSelectionListener(new SelectionAdapter() {
\r
84 public void widgetSelected(SelectionEvent e) {
\r
85 updateSelection(context);
\r
88 GridDataFactory.fillDefaults().hint(250, SWT.DEFAULT).grab(false, true).applyTo(explorer);
\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
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
102 addAxis = new Button(buttonComposite, support, SWT.NONE);
\r
103 addAxis.setText("Add axis");
\r
104 addAxis.addSelectionListener(new NewAxisListener(context));
\r
106 addVariable = new Button(buttonComposite, additionalSupport, SWT.NONE);
\r
107 addVariable.setText("Add variable");
\r
108 addVariable.addSelectionListener(new NewVariableListener(context));
\r
110 remove = new Button(buttonComposite, additionalSupport, SWT.NONE);
\r
111 remove.setText("Remove");
\r
112 remove.addSelectionListener(new RemoveListener(context));
\r
116 * Updates the content of propertyContainer
\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
126 // Get the type of the selected node (axis or series)
\r
127 String typeUri = null;
\r
129 typeUri = SimanticsUI.getSession().syncRequest(new Read<String>() {
\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
142 } catch (DatabaseException e) {
\r
143 e.printStackTrace();
\r
146 // Create a PropertyComposite for the selected node
\r
147 if(typeUri != null) {
\r
149 for(Control child : propertyContainer.getChildren()) {
\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
166 additionalSupport.fireInput(context, selection);
\r
170 * SelectionListener for adding a new range axis to a plot
\r
171 * @author Teemu Lempinen
\r
174 private class NewAxisListener extends SelectionListenerImpl<Resource> {
\r
176 public NewAxisListener(ISessionContext context) {
\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
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
197 * SelectionListener for adding a new variable to a plot
\r
198 * @author Teemu Lempinen
\r
201 private class NewVariableListener extends SelectionListenerImpl<Resource> {
\r
203 public NewVariableListener(ISessionContext context) {
\r
208 public void apply(WriteGraph graph, Resource input) throws DatabaseException {
\r
211 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
212 Layer0 l0 = Layer0.getInstance(graph);
\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
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
229 if(dataset != null) {
\r
230 // Create series with no rvi
\r
231 ChartUtils.createSeries(graph, dataset, null);
\r
238 * SelectionListener for remove button
\r
239 * @author Teemu Lempinen
\r
242 private class RemoveListener extends SelectionListenerImpl<Resource> {
\r
244 public RemoveListener(ISessionContext context) {
\r
249 * Removes selected resource from explorer
\r
252 public void apply(WriteGraph graph, Resource input) throws DatabaseException {
\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
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
273 // Remove axis from plot and rangeAxisList
\r
274 Resource plot = graph.getPossibleObject(input, l0.PartOf);
\r
276 list = graph.getPossibleObject(plot, jfree.Plot_rangeAxisList);
\r
279 ListUtils.removeElement(graph, list, input);
\r
280 RemoverUtil.remove(graph, input);
\r