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.jfreechart.chart.properties.xyline;
\r
14 import java.util.List;
\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
54 * PropertyTab displaying properties of axis and variables of a chart
\r
56 * @author Teemu Lempinen
\r
59 public class XYLineAxisAndVariablesTab extends LabelPropertyTabContributor {
\r
61 private GraphExplorerComposite explorer;
\r
62 private ScrolledComposite propertyContainer;
\r
63 private Button addAxis, addVariable, remove;
\r
64 private WidgetSupportImpl additionalSupport;
\r
66 public XYLineAxisAndVariablesTab() {
\r
67 additionalSupport = new WidgetSupportImpl();
\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
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
82 explorer.getExplorer().setAutoExpandLevel(2); // Expand everything in the beginning
\r
85 ((Tree)explorer.getExplorerControl()).addSelectionListener(new SelectionAdapter() {
\r
86 public void widgetSelected(SelectionEvent e) {
\r
87 updateSelection(context);
\r
90 GridDataFactory.fillDefaults().hint(250, SWT.DEFAULT).grab(false, true).applyTo(explorer);
\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
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
104 addAxis = new Button(buttonComposite, support, SWT.NONE);
\r
105 addAxis.setText("Add axis");
\r
106 addAxis.addSelectionListener(new NewAxisListener(context));
\r
108 addVariable = new Button(buttonComposite, additionalSupport, SWT.NONE);
\r
109 addVariable.setText("Add variable");
\r
110 addVariable.addSelectionListener(new NewVariableListener(context));
\r
112 remove = new Button(buttonComposite, additionalSupport, SWT.NONE);
\r
113 remove.setText("Remove");
\r
114 remove.addSelectionListener(new RemoveListener(context));
\r
118 * Updates the content of propertyContainer
\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
128 // Get the type of the selected node (axis or series)
\r
129 String typeUri = null;
\r
131 typeUri = SimanticsUI.getSession().syncRequest(new Read<String>() {
\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
144 } catch (DatabaseException e) {
\r
145 e.printStackTrace();
\r
148 // Create a PropertyComposite for the selected node
\r
149 if(typeUri != null) {
\r
151 for(Control child : propertyContainer.getChildren()) {
\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
168 additionalSupport.fireInput(context, selection);
\r
172 * SelectionListener for adding a new range axis to a plot
\r
173 * @author Teemu Lempinen
\r
176 private class NewAxisListener extends SelectionListenerImpl<Resource> {
\r
178 public NewAxisListener(ISessionContext context) {
\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
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
199 * SelectionListener for adding a new variable to a plot
\r
200 * @author Teemu Lempinen
\r
203 private class NewVariableListener extends SelectionListenerImpl<Resource> {
\r
205 public NewVariableListener(ISessionContext context) {
\r
210 public void apply(WriteGraph graph, Resource input) throws DatabaseException {
\r
211 NodeContext nc = explorer.getExplorer().getRoot();
\r
215 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
216 Layer0 l0 = Layer0.getInstance(graph);
\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
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
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
246 if(dataset != null) {
\r
247 // Create series with no rvi
\r
248 ChartUtils.createSeries(graph, dataset, null);
\r
255 * SelectionListener for remove button
\r
256 * @author Teemu Lempinen
\r
259 private class RemoveListener extends SelectionListenerImpl<Resource> {
\r
261 public RemoveListener(ISessionContext context) {
\r
266 * Removes selected resource from explorer
\r
269 public void apply(WriteGraph graph, Resource input) throws DatabaseException {
\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
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
290 // Remove axis from plot and rangeAxisList
\r
291 Resource plot = graph.getPossibleObject(input, l0.PartOf);
\r
293 list = graph.getPossibleObject(plot, jfree.Plot_rangeAxisList);
\r
296 ListUtils.removeElement(graph, list, input);
\r
297 RemoverUtil.remove(graph, input);
\r