1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011, 2014 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.AdjustableTab;
\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
57 * @author Tuomas Miettinen
\r
60 public class XYLineAxisAndVariablesTab extends AdjustableTab {
\r
62 private GraphExplorerComposite explorer;
\r
63 private ScrolledComposite propertyContainer;
\r
64 private Button addAxis, addVariable, remove;
\r
65 private WidgetSupportImpl additionalSupport;
\r
66 private Composite composite;
\r
67 private Composite buttonComposite;
\r
69 public XYLineAxisAndVariablesTab() {
\r
70 additionalSupport = new WidgetSupportImpl();
\r
74 * Updates the content of propertyContainer
\r
77 private void updateSelection(ISessionContext context) {
\r
78 ISelectionProvider selectionProvider = (ISelectionProvider)explorer.getAdapter(ISelectionProvider.class);
\r
79 IStructuredSelection selection = (IStructuredSelection)selectionProvider.getSelection();
\r
80 final Resource resource = AdaptionUtils.adaptToSingle(selection, Resource.class);
\r
81 if(resource == null)
\r
84 // Get the type of the selected node (axis or series)
\r
85 String typeUri = null;
\r
87 typeUri = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
90 public String perform(ReadGraph graph) throws DatabaseException {
\r
91 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
92 if(graph.isInstanceOf(resource, jfree.Axis))
\r
93 return graph.getURI(jfree.Axis);
\r
94 else if (graph.isInstanceOf(resource, jfree.Series))
\r
95 return graph.getURI(jfree.Series);
\r
100 } catch (DatabaseException e) {
\r
101 e.printStackTrace();
\r
104 // Create a PropertyComposite for the selected node
\r
105 if(typeUri != null) {
\r
107 for(Control child : propertyContainer.getChildren()) {
\r
111 if(typeUri.equals(JFreeChartResource.URIs.Axis)) {
\r
112 AxisPropertyComposite apc = new AxisPropertyComposite(propertyContainer, context, additionalSupport, SWT.NONE, isVertical());
\r
113 propertyContainer.setContent(apc);
\r
114 Point size = apc.computeSize(SWT.DEFAULT, SWT.DEFAULT);
\r
115 propertyContainer.setMinSize(size);
\r
116 } else if(typeUri.equals(JFreeChartResource.URIs.Series)) {
\r
117 SeriesPropertyComposite spc = new SeriesPropertyComposite(propertyContainer, context, additionalSupport, SWT.NONE);
\r
118 propertyContainer.setContent(spc);
\r
119 Point size = spc.computeSize(SWT.DEFAULT, SWT.DEFAULT);
\r
120 propertyContainer.setMinSize(size);
\r
124 additionalSupport.fireInput(context, selection);
\r
128 * SelectionListener for adding a new range axis to a plot
\r
129 * @author Teemu Lempinen
\r
132 private class NewAxisListener extends SelectionListenerImpl<Resource> {
\r
134 public NewAxisListener(ISessionContext context) {
\r
139 public void apply(WriteGraph graph, Resource chart) throws DatabaseException {
\r
140 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
141 Layer0 l0 = Layer0.getInstance(graph);
\r
142 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.Plot));
\r
144 Resource rangeAxis = ChartUtils.createNumberRangeAxis(graph, plot);
\r
145 if(rangeAxis != null) {
\r
146 Resource domainAxis = graph.getPossibleObject(plot, jfree.Plot_domainAxis);
\r
147 ChartUtils.createXYDataset(graph, plot, domainAxis, rangeAxis);
\r
155 * SelectionListener for adding a new variable to a plot
\r
156 * @author Teemu Lempinen
\r
159 private class NewVariableListener extends SelectionListenerImpl<Resource> {
\r
161 public NewVariableListener(ISessionContext context) {
\r
166 public void apply(WriteGraph graph, Resource input) throws DatabaseException {
\r
167 NodeContext nc = explorer.getExplorer().getRoot();
\r
171 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
172 Layer0 l0 = Layer0.getInstance(graph);
\r
174 if(input == null) {
\r
175 Resource chart = AdaptionUtils.adaptToSingle(nc, Resource.class);
\r
176 if(chart == null) return;
\r
177 Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.Plot));
\r
178 if(plot == null) return;
\r
179 Resource rangelist = graph.getPossibleObject(plot, jfree.Plot_rangeAxisList);
\r
180 if(rangelist == null) return;
\r
181 List<Resource> list = ListUtils.toList(graph, rangelist);
\r
182 if(list == null || list.isEmpty()) return;
\r
183 input = list.get(0);
\r
187 if(graph.isInstanceOf(input, jfree.Series)) {
\r
188 // Selected resource is series. Add to same dataset
\r
189 dataset = graph.getPossibleObject(input, l0.PartOf);
\r
191 // Selected resource is axis. Find the dataset it is mapped to or create dataset if not created already
\r
192 dataset = graph.getPossibleObject(input, jfree.Dataset_mapToRangeAxis_Inverse);
\r
193 if(dataset == null) {
\r
194 Resource plot = graph.getPossibleObject(input, l0.PartOf);
\r
195 if(plot == null) return;
\r
196 Resource domainAxis = graph.getPossibleObject(plot, jfree.Plot_domainAxis);
\r
197 if(domainAxis == null) return;
\r
198 ChartUtils.createXYDataset(graph, plot, domainAxis, input);
\r
202 if(dataset != null) {
\r
203 // Create series with no rvi
\r
204 ChartUtils.createSeries(graph, dataset, null);
\r
211 * SelectionListener for remove button
\r
212 * @author Teemu Lempinen
\r
215 private class RemoveListener extends SelectionListenerImpl<Resource> {
\r
217 public RemoveListener(ISessionContext context) {
\r
222 * Removes selected resource from explorer
\r
225 public void apply(WriteGraph graph, Resource input) throws DatabaseException {
\r
229 JFreeChartResource jfree = JFreeChartResource.getInstance(graph);
\r
230 Layer0 l0 = Layer0.getInstance(graph);
\r
231 Resource list = null;
\r
232 if(graph.isInstanceOf(input, jfree.Series)) {
\r
233 // Remove series from dataset and seriesList
\r
234 Resource dataset = graph.getPossibleObject(input, l0.PartOf);
\r
235 if(dataset != null)
\r
236 list = graph.getPossibleObject(dataset, jfree.Dataset_seriesList);
\r
238 // Remove associated dataset
\r
239 Resource dataset = graph.getPossibleObject(input, jfree.Dataset_mapToRangeAxis_Inverse);
\r
240 if(dataset != null) {
\r
241 graph.deny(dataset, jfree.Dataset_mapToDomainAxis);
\r
242 graph.deny(dataset, jfree.Dataset_mapToRangeAxis);
\r
243 RemoverUtil.remove(graph, dataset);
\r
246 // Remove axis from plot and rangeAxisList
\r
247 Resource plot = graph.getPossibleObject(input, l0.PartOf);
\r
249 list = graph.getPossibleObject(plot, jfree.Plot_rangeAxisList);
\r
252 ListUtils.removeElement(graph, list, input);
\r
253 RemoverUtil.remove(graph, input);
\r
259 protected void createAndAddControls(Composite body, IWorkbenchSite site,
\r
260 final ISessionContext context, WidgetSupport support) {
\r
261 composite = new Composite(body, SWT.NONE);
\r
263 // (Ontology-based) GraphExplorer displaying range axis and variables mapped to those axis
\r
264 explorer = new AxisAndVariablesExplorerComposite(ArrayMap.keys(
\r
265 "displaySelectors", "displayFilter").values(false, false), site, composite, support, SWT.FULL_SELECTION | SWT.BORDER | SWT.SINGLE);
\r
266 explorer.setBrowseContexts(JFreeChartResource.URIs.ChartAxisAndVariablesBrowseContext);
\r
267 explorer.setInputSource(new SingleSelectionInputSource(
\r
269 explorer.getExplorer().setAutoExpandLevel(2); // Expand everything in the beginning
\r
272 ((Tree)explorer.getExplorerControl()).addSelectionListener(new SelectionAdapter() {
\r
273 public void widgetSelected(SelectionEvent e) {
\r
274 updateSelection(context);
\r
278 // Scrolled composite for displaying properties of a selection in explorer
\r
279 propertyContainer = new ScrolledComposite(composite, SWT.H_SCROLL | SWT.V_SCROLL);
\r
280 propertyContainer.setExpandHorizontal(true);
\r
281 propertyContainer.setExpandVertical(true);
\r
283 // Buttons for adding axis and variables and removing selected items from explorer
\r
284 buttonComposite = new Composite(composite, SWT.NONE);
\r
286 addAxis = new Button(buttonComposite, support, SWT.NONE);
\r
287 addAxis.setText("Add axis");
\r
288 addAxis.addSelectionListener(new NewAxisListener(context));
\r
290 addVariable = new Button(buttonComposite, additionalSupport, SWT.NONE);
\r
291 addVariable.setText("Add variable");
\r
292 addVariable.addSelectionListener(new NewVariableListener(context));
\r
294 remove = new Button(buttonComposite, additionalSupport, SWT.NONE);
\r
295 remove.setText("Remove");
\r
296 remove.addSelectionListener(new RemoveListener(context));
\r
300 protected void createControlLayoutVertical() {
\r
301 GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);
\r
302 GridLayoutFactory.fillDefaults().numColumns(1).margins(3, 3).applyTo(composite);
\r
304 GridDataFactory.fillDefaults().hint(220, SWT.DEFAULT).grab(false, true).applyTo(explorer);
\r
306 // Scrolled composite for displaying properties of a selection in explorer
\r
307 GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 210).span(1, 1).grab(true, false).applyTo(propertyContainer);
\r
308 GridLayoutFactory.fillDefaults().applyTo(propertyContainer);
\r
310 // Buttons for adding axis and variables and removing selected items from explorer
\r
311 GridDataFactory.fillDefaults().applyTo(buttonComposite);
\r
312 GridLayoutFactory.fillDefaults().numColumns(3).applyTo(buttonComposite);
\r
316 protected void createControlLayoutHorizontal(boolean wideScreen) {
\r
317 GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);
\r
318 GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).applyTo(composite);
\r
320 GridDataFactory.fillDefaults().hint(250, SWT.DEFAULT).grab(false, true).applyTo(explorer);
\r
322 // Scrolled composite for displaying properties of a selection in explorer
\r
323 GridDataFactory.fillDefaults().hint(SWT.DEFAULT, SWT.DEFAULT).span(1, 2).grab(true, true).applyTo(propertyContainer);
\r
324 GridLayoutFactory.fillDefaults().applyTo(propertyContainer);
\r
326 // Buttons for adding axis and variables and removing selected items from explorer
\r
327 GridDataFactory.fillDefaults().applyTo(buttonComposite);
\r
328 GridLayoutFactory.fillDefaults().numColumns(3).applyTo(buttonComposite);
\r