From 2ac461cf7791c15bae7f6f078638efca48ebf029 Mon Sep 17 00:00:00 2001 From: luukkainen Date: Thu, 17 Jan 2013 13:58:35 +0000 Subject: [PATCH] Temporary UI for labels with separate rvi / labels. refs #3988 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26634 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../ChartSelectionTabContributor.java | 67 ++++++ .../chart/properties/VariableFactory.java | 29 +++ .../chart/properties/VariableModifier.java | 95 ++++++++ .../bar/BarSeriesPropertyComposite2.java | 112 +++++++++ .../chart/properties/bar/BarSeriesTab2.java | 223 ++++++++++++++++++ .../pie/PieSeriesPropertyComposite2.java | 136 +++++++++++ .../chart/properties/pie/PieSeriesTab2.java | 218 +++++++++++++++++ 7 files changed, 880 insertions(+) create mode 100644 org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableFactory.java create mode 100644 org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableModifier.java create mode 100644 org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/bar/BarSeriesPropertyComposite2.java create mode 100644 org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/bar/BarSeriesTab2.java create mode 100644 org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/pie/PieSeriesPropertyComposite2.java create mode 100644 org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/pie/PieSeriesTab2.java diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/ChartSelectionTabContributor.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/ChartSelectionTabContributor.java index 01a076b9..f4ff81a4 100644 --- a/org.simantics.jfreechart/src/org/simantics/jfreechart/ChartSelectionTabContributor.java +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/ChartSelectionTabContributor.java @@ -11,8 +11,10 @@ import org.simantics.jfreechart.chart.properties.ChartTab; import org.simantics.jfreechart.chart.properties.bar.BarAxisTab; import org.simantics.jfreechart.chart.properties.bar.BarGeneralPropertiesTab; import org.simantics.jfreechart.chart.properties.bar.BarSeriesTab; +import org.simantics.jfreechart.chart.properties.bar.BarSeriesTab2; import org.simantics.jfreechart.chart.properties.pie.PieGeneralPropertiesTab; import org.simantics.jfreechart.chart.properties.pie.PieSeriesTab; +import org.simantics.jfreechart.chart.properties.pie.PieSeriesTab2; import org.simantics.jfreechart.chart.properties.xyline.XYLineAxisAndVariablesTab; import org.simantics.jfreechart.chart.properties.xyline.XYLineGeneralPropertiesTab; import org.simantics.layer0.Layer0; @@ -86,4 +88,69 @@ public class ChartSelectionTabContributor { } return false; } + + public static boolean contibuteTabs2(ReadGraph backend, Resource r, List tabs) throws DatabaseException { + JFreeChartResource jfree = JFreeChartResource.getInstance(backend); + if(backend.isInstanceOf(r, jfree.ChartElement)) { + if(backend.hasStatement(r, jfree.ChartElement_component)) + r = backend.getSingleObject(r, jfree.ChartElement_component); + } + + if (backend.isInstanceOf(r, jfree.Chart)) { + + Collection plots = backend.syncRequest(new ObjectsWithType(r, Layer0.getInstance(backend).ConsistsOf, jfree.Plot)); + if(!plots.isEmpty()) { + Resource plot = plots.iterator().next(); + + if(backend.isInstanceOf(plot, jfree.XYPlot)) { + tabs.add(new ComparableTabContributor( + new XYLineGeneralPropertiesTab(), + 10, + r, + "General")); + tabs.add(new ComparableTabContributor( + new XYLineAxisAndVariablesTab(), + 9, + r, + "Axis and Variables")); + } else if(backend.isInstanceOf(plot, jfree.CategoryPlot)) { + tabs.add(new ComparableTabContributor( + new BarGeneralPropertiesTab(), + 10, + r, + "General")); + tabs.add(new ComparableTabContributor( + new BarSeriesTab2(), + 9, + r, + "Variables")); + tabs.add(new ComparableTabContributor( + new BarAxisTab(), + 8, + r, + "Axis")); + } else if(backend.isInstanceOf(plot, jfree.PiePlot)) { + tabs.add(new ComparableTabContributor( + new PieGeneralPropertiesTab(), + 10, + r, + "General")); + tabs.add(new ComparableTabContributor( + new PieSeriesTab2(), + 9, + r, + "Variables")); + } + + tabs.add(new ComparableTabContributor( + new ChartTab(), + 1, + r, + "Chart")); + + return true; + } + } + return false; + } } diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableFactory.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableFactory.java new file mode 100644 index 00000000..f9947090 --- /dev/null +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableFactory.java @@ -0,0 +1,29 @@ +package org.simantics.jfreechart.chart.properties; + +import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.JFreeChartResource; +import org.simantics.utils.datastructures.BijectionMap; + +public class VariableFactory extends ReadFactoryImpl { + BijectionMap map = new BijectionMap(); + + public VariableFactory(BijectionMap map) { + this.map = map; + } + + @Override + public String perform(ReadGraph graph, Resource input) throws DatabaseException { + String value = graph.getPossibleRelatedValue(input, JFreeChartResource.getInstance(graph).variableRVI); + if (value == null) + return ""; + value = map.getRight(value); + if (value == null) + return ""; + return value; + + } + +} diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableModifier.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableModifier.java new file mode 100644 index 00000000..6434a5ea --- /dev/null +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableModifier.java @@ -0,0 +1,95 @@ +package org.simantics.jfreechart.chart.properties; + +import org.eclipse.jface.bindings.keys.KeyStroke; +import org.eclipse.jface.bindings.keys.ParseException; +import org.eclipse.jface.fieldassist.ContentProposalAdapter; +import org.eclipse.jface.fieldassist.IContentProposal; +import org.eclipse.jface.fieldassist.IContentProposalListener; +import org.eclipse.jface.fieldassist.IContentProposalListener2; +import org.eclipse.jface.fieldassist.TextContentAdapter; +import org.eclipse.swt.widgets.Control; +import org.simantics.browsing.ui.swt.widgets.impl.TextModifyListenerImpl; +import org.simantics.browsing.ui.swt.widgets.impl.TrackedModifyEvent; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.databoard.Bindings; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.JFreeChartResource; + +public class VariableModifier extends TextModifyListenerImpl { + + private boolean active; + private Control control; + + private char[] alphaNumericCharacters = { + 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','å','ä','ö', + 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','Å','Ä','Ö', + '1','2','3','4','5','6','7','8','9','0','.','_'}; + + public VariableModifier(Control control, WidgetSupport support) { + this.control = control; + this.active = true; + + KeyStroke keyStroke = null; + try { + keyStroke = KeyStroke.getInstance("Ctrl+Space"); + } catch (ParseException e1) { + e1.printStackTrace(); + } + + //SimpleContentProposalProvider scpp = new VariableProposalProvider(control, support); + IdLabelProposalProvider scpp = new VariableProposalProvider(control, support); + scpp.setFiltering(true); + + ContentProposalAdapter adapter = new ContentProposalAdapter( + control, new TextContentAdapter(), scpp, keyStroke, alphaNumericCharacters); + adapter.setAutoActivationDelay(0); + adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE); + adapter.addContentProposalListener(new IContentProposalListener2() { + + @Override + public void proposalPopupOpened(ContentProposalAdapter adapter) { + if(VariableModifier.this != null) + VariableModifier.this.deactivate(); + } + + @Override + public void proposalPopupClosed(ContentProposalAdapter adapter) { + if(VariableModifier.this != null) + VariableModifier.this.activate(); + } + }); + + adapter.addContentProposalListener(new IContentProposalListener() { + + + @Override + public void proposalAccepted(IContentProposal proposal) { + if(VariableModifier.this.control != null && !VariableModifier.this.control.isDisposed()) + VariableModifier.this.modifyText(new TrackedModifyEvent(VariableModifier.this.control, proposal.getContent())); + } + }); + + + } + + + @Override + public void applyText(WriteGraph graph, Resource resource, String text) throws DatabaseException { + if(active) { + JFreeChartResource jfree = JFreeChartResource.getInstance(graph); + graph.claimLiteral(resource, jfree.variableRVI, text, Bindings.STRING); + graph.deny(resource, jfree.variableFilter); + } + } + + public void deactivate() { + active = false; + } + + public void activate() { + active = true; + } + +} diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/bar/BarSeriesPropertyComposite2.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/bar/BarSeriesPropertyComposite2.java new file mode 100644 index 00000000..049f701f --- /dev/null +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/bar/BarSeriesPropertyComposite2.java @@ -0,0 +1,112 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.jfreechart.chart.properties.bar; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory; +import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier; +import org.simantics.browsing.ui.swt.widgets.TrackedText; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.db.management.ISessionContext; +import org.simantics.jfreechart.chart.properties.DoubleValidator; +import org.simantics.jfreechart.chart.properties.JFreeChartPropertyColorProvider; +import org.simantics.jfreechart.chart.properties.RVIFactory; +import org.simantics.jfreechart.chart.properties.RVIModifier; +import org.simantics.jfreechart.chart.properties.RangeComposite; +import org.simantics.jfreechart.chart.properties.VariableExistsValidator; +import org.simantics.jfreechart.chart.properties.VariableFactory; +import org.simantics.jfreechart.chart.properties.VariableModifier; +import org.simantics.layer0.Layer0; +import org.simantics.modeling.ui.chart.property.DoublePropertyFactory; +import org.simantics.modeling.ui.chart.property.DoublePropertyModifier; +import org.simantics.sysdyn.JFreeChartResource; +import org.simantics.utils.datastructures.BijectionMap; +import org.simantics.utils.datastructures.Pair; + +/** + * Composite for modifying properties of a series in a bar chart + * @author Teemu Lempinen + * + */ +public class BarSeriesPropertyComposite2 extends Composite { + + private TrackedText variable, label, time; + + public BarSeriesPropertyComposite2(Composite parent, final ISessionContext context, WidgetSupport support, Pair variables, int style) { + super(parent, style); + + GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(this); + + // Variable for the series + Label label = new Label(this, SWT.NONE); + label.setText("Variable:"); + GridDataFactory.fillDefaults().align(SWT.END, SWT.FILL).applyTo(label); + + variable = new TrackedText(this, support, SWT.BORDER); + if (variables.second == null) { + variable.setTextFactory(new RVIFactory()); + variable.addModifyListener(new RVIModifier(variable.getWidget(), support)); + variable.setInputValidator(new VariableExistsValidator(support, variable)); + } else { + // FIXME: using bijectionmap and trackedText looses the variables that have the same label. + BijectionMap map = new BijectionMap(); + for (int i = 0; i < variables.first.length; i++) { + map.map(variables.first[i], variables.second[i]); + } + variable.setTextFactory(new VariableFactory(map)); + variable.addModifyListener(new VariableModifier(variable.getWidget(), support)); + variable.setInputValidator(new VariableExistsValidator(support, variable, false, true)); + } + variable.setColorProvider(new JFreeChartPropertyColorProvider(this.variable.getResourceManager())); + GridDataFactory.fillDefaults().grab(true, false).applyTo(this.variable.getWidget()); + + // Range + label = new Label(this, SWT.NONE); + label.setText("Range:"); + GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label); + + RangeComposite rangeComposite = new RangeComposite(this, context, support, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, false).applyTo(rangeComposite); + + + // Label to be displayed in chart for this series + label = new Label(this, SWT.NONE); + label.setText("Label:"); + GridDataFactory.fillDefaults().align(SWT.END, SWT.FILL).applyTo(label); + + this.label = new TrackedText(this, support, SWT.BORDER); + this.label.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel, "")); + this.label.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel)); + this.label.setColorProvider(new JFreeChartPropertyColorProvider(this.label.getResourceManager())); + GridDataFactory.fillDefaults().grab(true, false).applyTo(this.label.getWidget()); + + // Time + label = new Label(this, SWT.NONE); + GridDataFactory.fillDefaults().align(SWT.END, SWT.FILL).applyTo(label); + label.setText("Time:"); + + Composite composite = new Composite(this, SWT.NONE); + GridDataFactory.fillDefaults().applyTo(composite); + GridLayoutFactory.fillDefaults().applyTo(composite); + + time = new org.simantics.browsing.ui.swt.widgets.TrackedText(composite, support, SWT.BORDER); + time.setTextFactory(new DoublePropertyFactory(JFreeChartResource.URIs.Series_time)); + time.addModifyListener(new DoublePropertyModifier(context, JFreeChartResource.URIs.Series_time)); + time.setInputValidator(new DoubleValidator(true)); + time.setColorProvider(new JFreeChartPropertyColorProvider(time.getResourceManager())); + GridDataFactory.fillDefaults().applyTo(time.getWidget()); + } +} diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/bar/BarSeriesTab2.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/bar/BarSeriesTab2.java new file mode 100644 index 00000000..829238d3 --- /dev/null +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/bar/BarSeriesTab2.java @@ -0,0 +1,223 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.jfreechart.chart.properties.bar; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.jface.viewers.ISelectionProvider; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.ScrolledComposite; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Tree; +import org.eclipse.ui.IWorkbenchSite; +import org.simantics.browsing.ui.swt.SingleSelectionInputSource; +import org.simantics.browsing.ui.swt.widgets.Button; +import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite; +import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl; +import org.simantics.browsing.ui.swt.widgets.impl.Widget; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.PossibleObjectWithType; +import org.simantics.db.common.utils.ListUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.util.RemoverUtil; +import org.simantics.db.management.ISessionContext; +import org.simantics.jfreechart.chart.ChartUtils; +import org.simantics.jfreechart.chart.properties.AllVariablesOfModel; +import org.simantics.jfreechart.chart.properties.LabelPropertyTabContributor; +import org.simantics.jfreechart.chart.properties.xyline.AxisAndVariablesExplorerComposite; +import org.simantics.layer0.Layer0; +import org.simantics.sysdyn.JFreeChartResource; +import org.simantics.ui.utils.AdaptionUtils; +import org.simantics.utils.datastructures.ArrayMap; +import org.simantics.utils.datastructures.Pair; +import org.simantics.utils.ui.ExceptionUtils; + +/** + * Tab containing the series of a bar chart + * @author Teemu Lempinen + * + */ +public class BarSeriesTab2 extends LabelPropertyTabContributor implements Widget { + + private GraphExplorerComposite explorer; + private ScrolledComposite propertyContainer; + private WidgetSupportImpl additionalSupport; + private Button add, remove; + private Resource chartResource; + private BarSeriesPropertyComposite2 spc; + + + public BarSeriesTab2() { + additionalSupport = new WidgetSupportImpl(); + } + + @Override + public void createControls(Composite body, IWorkbenchSite site, final ISessionContext context, WidgetSupport support) { + support.register(this); + Composite composite = new Composite(body, SWT.NONE); + + GridDataFactory.fillDefaults().grab(true, true).applyTo(composite); + GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).applyTo(composite); + + // (Ontology-based) GraphExplorer displaying variables in a bar chart + explorer = new AxisAndVariablesExplorerComposite(ArrayMap.keys( + "displaySelectors", "displayFilter").values(false, false), site, composite, support, SWT.FULL_SELECTION | SWT.BORDER | SWT.SINGLE); + explorer.setBrowseContexts(JFreeChartResource.URIs.BarSeriesBrowseContext); + explorer.setInputSource(new SingleSelectionInputSource( + Resource.class)); + explorer.getExplorer().setAutoExpandLevel(2); // Expand everything in the beginning + explorer.finish(); + + ((Tree)explorer.getExplorerControl()).addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + updateSelection(context); + } + }); + GridDataFactory.fillDefaults().hint(250, SWT.DEFAULT).grab(false, true).applyTo(explorer); + + // Scrolled composite for displaying properties of a selection in explorer + propertyContainer = new ScrolledComposite(composite, SWT.H_SCROLL | SWT.V_SCROLL); + GridDataFactory.fillDefaults().span(1, 2).grab(true, true).applyTo(propertyContainer); + GridLayoutFactory.fillDefaults().applyTo(propertyContainer); + propertyContainer.setExpandHorizontal(true); + propertyContainer.setExpandVertical(true); + + // Buttons for adding and removing variables from a pie plot + Composite buttonComposite = new Composite(composite, SWT.NONE); + GridDataFactory.fillDefaults().applyTo(buttonComposite); + GridLayoutFactory.fillDefaults().numColumns(3).applyTo(buttonComposite); + + add = new Button(buttonComposite, additionalSupport, SWT.NONE); + add.setText("Add"); + add.addSelectionListener(new NewVariableListener(context)); + + remove = new Button(buttonComposite, additionalSupport, SWT.NONE); + remove.setText("Remove"); + remove.addSelectionListener(new RemoveListener(context)); + } + + /** + * Updates the content of propertyContainer + * @param context + */ + private void updateSelection(ISessionContext context) { + ISelectionProvider selectionProvider = (ISelectionProvider)explorer.getAdapter(ISelectionProvider.class); + IStructuredSelection selection = (IStructuredSelection)selectionProvider.getSelection(); + final Resource resource = AdaptionUtils.adaptToSingle(selection, Resource.class); + if(resource == null) + return; + + for(Control child : propertyContainer.getChildren()) { + child.dispose(); + } + + try { + Pair variables = context.getSession().syncRequest(new AllVariablesOfModel(resource)); + + spc = new BarSeriesPropertyComposite2(propertyContainer, context, additionalSupport, variables, SWT.NONE); + + additionalSupport.fireInput(context, selection); + + propertyContainer.setContent(spc); + Point size = spc.computeSize(SWT.DEFAULT, SWT.DEFAULT); + propertyContainer.setMinSize(size); + } catch (DatabaseException e) { + ExceptionUtils.logAndShowError(e); + } + } + + /** + * SelectionListener for adding a new variable to a plot + * @author Teemu Lempinen + * + */ + private class NewVariableListener extends SelectionListenerImpl { + + public NewVariableListener(ISessionContext context) { + super(context); + } + + @Override + public void apply(WriteGraph graph, Resource input) throws DatabaseException { + Resource dataset = null; + JFreeChartResource jfree = JFreeChartResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + + if(input == null) { + if(chartResource != null) { + Resource plot = graph.syncRequest(new PossibleObjectWithType(chartResource, l0.ConsistsOf, jfree.Plot)); + if(plot != null) + dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.Dataset)); + } + } else { + if(graph.isInstanceOf(input, jfree.Series)) { + dataset = graph.getPossibleObject(input, l0.PartOf); + } + } + + if(dataset != null) { + // Create series with no rvi + ChartUtils.createSeries(graph, dataset, null); + } + } + } + + /** + * SelectionListener for remove button + * @author Teemu Lempinen + * + */ + private class RemoveListener extends SelectionListenerImpl { + + public RemoveListener(ISessionContext context) { + super(context); + } + + /** + * Removes selected resource from explorer + */ + @Override + public void apply(WriteGraph graph, Resource input) throws DatabaseException { + if(input == null) + return; + + JFreeChartResource jfree = JFreeChartResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + Resource list = null; + if(graph.isInstanceOf(input, jfree.Series)) { + // Remove series from dataset and seriesList + Resource dataset = graph.getPossibleObject(input, l0.PartOf); + if(dataset != null) + list = graph.getPossibleObject(dataset, jfree.Dataset_seriesList); + + if(list != null) + ListUtils.removeElement(graph, list, input); + RemoverUtil.remove(graph, input); + } + } + } + + @Override + public void setInput(ISessionContext context, Object input) { + chartResource = AdaptionUtils.adaptToSingle(input, Resource.class); + } + + +} diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/pie/PieSeriesPropertyComposite2.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/pie/PieSeriesPropertyComposite2.java new file mode 100644 index 00000000..5011332b --- /dev/null +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/pie/PieSeriesPropertyComposite2.java @@ -0,0 +1,136 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.jfreechart.chart.properties.pie; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.simantics.browsing.ui.swt.widgets.Button; +import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory; +import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier; +import org.simantics.browsing.ui.swt.widgets.TrackedText; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.db.management.ISessionContext; +import org.simantics.jfreechart.chart.properties.BooleanPropertyFactory; +import org.simantics.jfreechart.chart.properties.BooleanSelectionListener; +import org.simantics.jfreechart.chart.properties.ColorPicker; +import org.simantics.jfreechart.chart.properties.DoubleValidator; +import org.simantics.jfreechart.chart.properties.JFreeChartPropertyColorProvider; +import org.simantics.jfreechart.chart.properties.RVIFactory; +import org.simantics.jfreechart.chart.properties.RVIModifier; +import org.simantics.jfreechart.chart.properties.RangeComposite; +import org.simantics.jfreechart.chart.properties.VariableExistsValidator; +import org.simantics.jfreechart.chart.properties.VariableFactory; +import org.simantics.jfreechart.chart.properties.VariableModifier; +import org.simantics.layer0.Layer0; +import org.simantics.modeling.ui.chart.property.DoublePropertyFactory; +import org.simantics.modeling.ui.chart.property.DoublePropertyModifier; +import org.simantics.sysdyn.JFreeChartResource; +import org.simantics.utils.datastructures.BijectionMap; +import org.simantics.utils.datastructures.Pair; + +/** + * Composite containing the properties of a series + * @author Teemu Lempinen + * + */ +public class PieSeriesPropertyComposite2 extends Composite { + + private TrackedText variable, label, time; + + public PieSeriesPropertyComposite2(Composite parent, ISessionContext context, WidgetSupport support, Pair variables,int style) { + super(parent, style); + + GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(this); + + // Variable for the series + Label label = new Label(this, SWT.NONE); + label.setText("Variable:"); + GridDataFactory.fillDefaults().align(SWT.END, SWT.FILL).applyTo(label); + + variable = new TrackedText(this, support, SWT.BORDER); + if (variables.second == null) { + variable.setTextFactory(new RVIFactory()); + variable.addModifyListener(new RVIModifier(variable.getWidget(), support)); + variable.setInputValidator(new VariableExistsValidator(support, variable)); + } else { + // FIXME: using bijectionmap and trackedText looses the variables that have the same label. + BijectionMap map = new BijectionMap(); + for (int i = 0; i < variables.first.length; i++) { + map.map(variables.first[i], variables.second[i]); + } + variable.setTextFactory(new VariableFactory(map)); + variable.addModifyListener(new VariableModifier(variable.getWidget(), support)); + variable.setInputValidator(new VariableExistsValidator(support, variable, false, true)); + } + variable.setColorProvider(new JFreeChartPropertyColorProvider(this.variable.getResourceManager())); + GridDataFactory.fillDefaults().grab(true, false).applyTo(this.variable.getWidget()); + + // Range + label = new Label(this, SWT.NONE); + label.setText("Range:"); + GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label); + + RangeComposite rangeComposite = new RangeComposite(this, context, support, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, false).applyTo(rangeComposite); + + + // Label to be displayed in chart for this series + label = new Label(this, SWT.NONE); + label.setText("Label:"); + GridDataFactory.fillDefaults().align(SWT.END, SWT.FILL).applyTo(label); + + this.label = new TrackedText(this, support, SWT.BORDER); + this.label.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel, "")); + this.label.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasLabel)); + this.label.setColorProvider(new JFreeChartPropertyColorProvider(this.label.getResourceManager())); + GridDataFactory.fillDefaults().grab(true, false).applyTo(this.label.getWidget()); + + // Color + label = new Label(this, SWT.NONE); + label.setText("Color:"); + GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label); + + Composite colorPicker = new ColorPicker(this, context, support, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, false).applyTo(colorPicker); + + // Time + label = new Label(this, SWT.NONE); + GridDataFactory.fillDefaults().align(SWT.END, SWT.FILL).applyTo(label); + label.setText("Time:"); + + Composite composite = new Composite(this, SWT.NONE); + GridDataFactory.fillDefaults().applyTo(composite); + GridLayoutFactory.fillDefaults().applyTo(composite); + + time = new org.simantics.browsing.ui.swt.widgets.TrackedText(composite, support, SWT.BORDER); + time.setTextFactory(new DoublePropertyFactory(JFreeChartResource.URIs.Series_time)); + time.addModifyListener(new DoublePropertyModifier(context, JFreeChartResource.URIs.Series_time)); + time.setInputValidator(new DoubleValidator(true)); + time.setColorProvider(new JFreeChartPropertyColorProvider(time.getResourceManager())); + GridDataFactory.fillDefaults().applyTo(time.getWidget()); + + // Exploded + label = new Label(this, SWT.NONE); + label.setText(""); + GridDataFactory.fillDefaults().align(SWT.END, SWT.FILL).applyTo(label); + + Button exploded = new Button(this, support, SWT.CHECK); + exploded.setText("Exploded"); + exploded.addSelectionListener(new BooleanSelectionListener(context, JFreeChartResource.URIs.Series_exploded)); + exploded.setSelectionFactory(new BooleanPropertyFactory(JFreeChartResource.URIs.Series_exploded)); + GridDataFactory.fillDefaults().applyTo(exploded.getWidget()); + } + +} diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/pie/PieSeriesTab2.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/pie/PieSeriesTab2.java new file mode 100644 index 00000000..e0d9d5f2 --- /dev/null +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/pie/PieSeriesTab2.java @@ -0,0 +1,218 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.jfreechart.chart.properties.pie; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.jface.viewers.ISelectionProvider; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.ScrolledComposite; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Tree; +import org.eclipse.ui.IWorkbenchSite; +import org.simantics.browsing.ui.swt.SingleSelectionInputSource; +import org.simantics.browsing.ui.swt.widgets.Button; +import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite; +import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl; +import org.simantics.browsing.ui.swt.widgets.impl.Widget; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.PossibleObjectWithType; +import org.simantics.db.common.utils.ListUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.util.RemoverUtil; +import org.simantics.db.management.ISessionContext; +import org.simantics.jfreechart.chart.ChartUtils; +import org.simantics.jfreechart.chart.properties.AllVariablesOfModel; +import org.simantics.jfreechart.chart.properties.LabelPropertyTabContributor; +import org.simantics.jfreechart.chart.properties.xyline.AxisAndVariablesExplorerComposite; +import org.simantics.layer0.Layer0; +import org.simantics.sysdyn.JFreeChartResource; +import org.simantics.ui.utils.AdaptionUtils; +import org.simantics.utils.datastructures.ArrayMap; +import org.simantics.utils.datastructures.Pair; +import org.simantics.utils.ui.ExceptionUtils; + +/** + * Tab for modifying series in a pie chart configuration + * @author Teemu Lempinen + * + */ +public class PieSeriesTab2 extends LabelPropertyTabContributor implements Widget { + + private GraphExplorerComposite explorer; + private ScrolledComposite propertyContainer; + private WidgetSupportImpl additionalSupport; + private Button add, remove; + private Resource chartResource; + + public PieSeriesTab2() { + additionalSupport = new WidgetSupportImpl(); + } + + @Override + public void createControls(Composite body, IWorkbenchSite site, final ISessionContext context, WidgetSupport support) { + support.register(this); + Composite composite = new Composite(body, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, true).applyTo(composite); + GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).applyTo(composite); + + // (Ontology-based) GraphExplorer displaying variables of a pie chart + explorer = new AxisAndVariablesExplorerComposite(ArrayMap.keys( + "displaySelectors", "displayFilter").values(false, false), site, composite, support, SWT.FULL_SELECTION | SWT.BORDER | SWT.SINGLE); + explorer.setBrowseContexts(JFreeChartResource.URIs.PieSeriesBrowseContext); + explorer.setInputSource(new SingleSelectionInputSource( + Resource.class)); + explorer.getExplorer().setAutoExpandLevel(2); // Expand everything in the beginning + explorer.finish(); + + ((Tree)explorer.getExplorerControl()).addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + updateSelection(context); + } + }); + GridDataFactory.fillDefaults().hint(250, SWT.DEFAULT).grab(false, true).applyTo(explorer); + + // Scrolled composite for displaying properties of a selection in explorer + propertyContainer = new ScrolledComposite(composite, SWT.H_SCROLL | SWT.V_SCROLL); + GridDataFactory.fillDefaults().span(1, 2).grab(true, true).applyTo(propertyContainer); + GridLayoutFactory.fillDefaults().applyTo(propertyContainer); + propertyContainer.setExpandHorizontal(true); + propertyContainer.setExpandVertical(true); + + + // Buttons for adding and removing variables from a pie plot + Composite buttonComposite = new Composite(composite, SWT.NONE); + GridDataFactory.fillDefaults().applyTo(buttonComposite); + GridLayoutFactory.fillDefaults().numColumns(3).applyTo(buttonComposite); + + add = new Button(buttonComposite, additionalSupport, SWT.NONE); + add.setText("Add"); + add.addSelectionListener(new NewVariableListener(context)); + + remove = new Button(buttonComposite, additionalSupport, SWT.NONE); + remove.setText("Remove"); + remove.addSelectionListener(new RemoveListener(context)); + } + + /** + * Updates the content of propertyContainer + * @param context + */ + private void updateSelection(ISessionContext context) { + ISelectionProvider selectionProvider = (ISelectionProvider)explorer.getAdapter(ISelectionProvider.class); + IStructuredSelection selection = (IStructuredSelection)selectionProvider.getSelection(); + final Resource resource = AdaptionUtils.adaptToSingle(selection, Resource.class); + if(resource == null) + return; + + for(Control child : propertyContainer.getChildren()) { + child.dispose(); + } + + try { + Pair variables = context.getSession().syncRequest(new AllVariablesOfModel(resource)); + PieSeriesPropertyComposite2 spc = new PieSeriesPropertyComposite2(propertyContainer, context, additionalSupport, variables, SWT.NONE); + propertyContainer.setContent(spc); + Point size = spc.computeSize(SWT.DEFAULT, SWT.DEFAULT); + propertyContainer.setMinSize(size); + + additionalSupport.fireInput(context, selection); + } catch (DatabaseException e) { + ExceptionUtils.logAndShowError(e); + } + } + + + /** + * SelectionListener for adding a new variable to a plot + * @author Teemu Lempinen + * + */ + private class NewVariableListener extends SelectionListenerImpl { + + public NewVariableListener(ISessionContext context) { + super(context); + } + + @Override + public void apply(WriteGraph graph, Resource input) throws DatabaseException { + JFreeChartResource jfree = JFreeChartResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + Resource dataset = null; + if(input == null) { + if(chartResource != null) { + Resource plot = graph.syncRequest(new PossibleObjectWithType(chartResource, l0.ConsistsOf, jfree.Plot)); + if(plot != null) + dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.Dataset)); + } + } else { + if(graph.isInstanceOf(input, jfree.Series)) { + dataset = graph.getPossibleObject(input, l0.PartOf); + } + } + if(dataset != null) { + // Create series with no rvi + Resource series = ChartUtils.createSeries(graph, dataset, null); + graph.claimLiteral(series, jfree.Series_exploded, false); + } + } + } + + /** + * SelectionListener for remove button + * @author Teemu Lempinen + * + */ + private class RemoveListener extends SelectionListenerImpl { + + public RemoveListener(ISessionContext context) { + super(context); + } + + /** + * Removes selected resource from explorer + */ + @Override + public void apply(WriteGraph graph, Resource input) throws DatabaseException { + if(input == null) + return; + + JFreeChartResource jfree = JFreeChartResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + Resource list = null; + if(graph.isInstanceOf(input, jfree.Series)) { + // Remove series from dataset and seriesList + Resource dataset = graph.getPossibleObject(input, l0.PartOf); + if(dataset != null) + list = graph.getPossibleObject(dataset, jfree.Dataset_seriesList); + + if(list != null) + ListUtils.removeElement(graph, list, input); + RemoverUtil.remove(graph, input); + } + } + } + + @Override + public void setInput(ISessionContext context, Object input) { + chartResource = AdaptionUtils.adaptToSingle(input, Resource.class); + } + +} -- 2.47.1