From 433093e96b4c307800a8921eff48dff92ed3b718 Mon Sep 17 00:00:00 2001 From: lempinen Date: Thu, 15 Dec 2011 12:05:34 +0000 Subject: [PATCH] Changed updating methods for different chart components to support updating also in diagram elements git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@23570 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/trend/chart/AbstractAxis.java | 9 +- .../sysdyn/ui/trend/chart/AbstractPlot.java | 238 +++++++++++------- .../ui/trend/chart/CategoryDataset.java | 2 +- .../sysdyn/ui/trend/chart/CategoryPlot.java | 47 ++-- .../sysdyn/ui/trend/chart/JFreeChart.java | 134 +++++++--- .../sysdyn/ui/trend/chart/PiePlot.java | 22 +- .../sysdyn/ui/trend/chart/XYDataset.java | 145 ++++++----- .../sysdyn/ui/trend/chart/XYPlot.java | 45 ++-- .../chart/element/ChartElementFactory.java | 35 +-- .../ui/trend/chart/element/ChartNode.java | 9 +- 10 files changed, 416 insertions(+), 270 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/AbstractAxis.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/AbstractAxis.java index 9d04cab3..9f37e5ee 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/AbstractAxis.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/AbstractAxis.java @@ -36,12 +36,19 @@ public abstract class AbstractAxis implements IAxis { protected Double min, max, rotate; /** - * + * Creates a new axis * @param graph ReadGraph * @param axisResource resource of type JFreeChart.NumberAxis */ public AbstractAxis(ReadGraph graph, Resource axisResource) { try { + /* + * Axis is practically always called from a listener, + * so it is safe to always create a new axis every time. + * + * The parent listener takes care that the axis is updated. + * (And the code stays much more readable) + */ Layer0 l0 = Layer0.getInstance(graph); JFreeChartResource jfree = JFreeChartResource.getInstance(graph); label = graph.getPossibleRelatedValue(axisResource, l0.HasLabel); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/AbstractPlot.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/AbstractPlot.java index afd6e255..48e0f693 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/AbstractPlot.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/AbstractPlot.java @@ -23,8 +23,11 @@ import org.simantics.db.Resource; import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.utils.ListUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.procedure.Listener; +import org.simantics.db.request.Read; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.JFreeChartResource; +import org.simantics.ui.SimanticsUI; /** * Abstract plot class for all JFreeChart plots @@ -35,108 +38,175 @@ import org.simantics.sysdyn.JFreeChartResource; public abstract class AbstractPlot implements IPlot { protected Resource resource; - protected ArrayList ranges; - protected ArrayList domains; - protected ArrayList datasets; - protected HashMap rangeMappings; - protected HashMap domainMappings; protected Plot plot; - + protected PlotProperties currentProperties; + private PlotPropertyListener listener; + + public AbstractPlot(ReadGraph graph, Resource resource) { this.resource = resource; - - try { - Layer0 l0 = Layer0.getInstance(graph); - JFreeChartResource jfree = JFreeChartResource.getInstance(graph); + } - HashMap axisMap = new HashMap(); - ranges = new ArrayList(); + @Override + public void dispose() { + if(currentProperties != null) { + for(IAxis axis : currentProperties.ranges) + axis.dispose(); - // Get all range axis - Resource rangeList = graph.getPossibleObject(resource, jfree.Plot_rangeAxisList); - if(rangeList != null) { - for(Resource axisResource : ListUtils.toList(graph, rangeList)) { - IAxis axis = graph.adapt(axisResource, IAxis.class); - if(axis.getAxis() instanceof Axis) { - ranges.add(axis); - axisMap.put(axisResource, axis); - } - } - } - - // Get all domain axis - // There usually is only one domain axis, but this supports also multiple domain axis - domains = new ArrayList(); - for(Resource axisResource : graph.syncRequest(new ObjectsWithType(resource, jfree.Plot_domainAxis, jfree.Axis))) { - IAxis axis = graph.adapt(axisResource, IAxis.class); - if(axis.getAxis() instanceof Axis) { - domains.add(axis); - axisMap.put(axisResource, axis); - } - } + for(IAxis axis : currentProperties.domains) + axis.dispose(); - // Get all datasets and map them to axis - datasets = new ArrayList(); - rangeMappings = new HashMap(); - domainMappings = new HashMap(); - for(Resource datasetResource : graph.syncRequest(new ObjectsWithType(resource, l0.ConsistsOf, jfree.Dataset))) { - IDataset dataset = graph.adapt(datasetResource, IDataset.class); - if(dataset != null) { - datasets.add(dataset); - Resource axisResource = graph.getPossibleObject(datasetResource, jfree.Dataset_mapToRangeAxis); - IAxis axis; - if(axisMap.containsKey(axisResource)) { - axis = axisMap.get(axisResource); - rangeMappings.put(dataset, axis); - } - - axisResource = graph.getPossibleObject(datasetResource, jfree.Dataset_mapToDomainAxis); - if(axisMap.containsKey(axisResource)) { - axis = axisMap.get(axisResource); - domainMappings.put(dataset, axis); - } - } - } - - } catch (DatabaseException e) { - e.printStackTrace(); + for(IDataset dataset : currentProperties.datasets) + dataset.dispose(); } - - } - - @Override - public void dispose() { - for(IAxis axis : ranges) - axis.dispose(); - - for(IAxis axis : domains) - axis.dispose(); - - for(IDataset dataset : datasets) - dataset.dispose(); + if(listener != null) + listener.dispose(); } @Override public Resource getResource() { return resource; } - + protected abstract Plot newPlot(); - protected abstract void setPlotProperties(); - + protected abstract void setPlotProperties(PlotProperties properties); + protected abstract void getOtherProperties(ReadGraph graph, PlotProperties properties) throws DatabaseException; + @Override public Plot getPlot() { if(plot == null) plot = newPlot(); - - SwingUtilities.invokeLater(new Runnable() { - - @Override - public void run() { - setPlotProperties(); - } - }); - + + if(listener == null || listener.isDisposed()) { + listener = new PlotPropertyListener(); + SimanticsUI.getSession().asyncRequest(new Read() { + + @Override + public PlotProperties perform(ReadGraph graph) throws DatabaseException { + + PlotProperties properties = new PlotProperties(); + + Layer0 l0 = Layer0.getInstance(graph); + JFreeChartResource jfree = JFreeChartResource.getInstance(graph); + + HashMap axisMap = new HashMap(); + + // Get all range axis + Resource rangeList = graph.getPossibleObject(resource, jfree.Plot_rangeAxisList); + if(rangeList != null) { + for(Resource axisResource : ListUtils.toList(graph, rangeList)) { + IAxis axis = graph.adapt(axisResource, IAxis.class); + if(axis.getAxis() instanceof Axis) { + properties.ranges.add(axis); + axisMap.put(axisResource, axis); + } + } + } + + // Get all domain axis + // There usually is only one domain axis, but this supports also multiple domain axis + for(Resource axisResource : graph.syncRequest(new ObjectsWithType(resource, jfree.Plot_domainAxis, jfree.Axis))) { + IAxis axis = graph.adapt(axisResource, IAxis.class); + if(axis.getAxis() instanceof Axis) { + properties.domains.add(axis); + axisMap.put(axisResource, axis); + } + } + + // Get all datasets and map them to axis + for(Resource datasetResource : graph.syncRequest(new ObjectsWithType(resource, l0.ConsistsOf, jfree.Dataset))) { + IDataset dataset = graph.adapt(datasetResource, IDataset.class); + if(dataset != null) { + properties.datasets.add(dataset); + Resource axisResource = graph.getPossibleObject(datasetResource, jfree.Dataset_mapToRangeAxis); + IAxis axis; + if(axisMap.containsKey(axisResource)) { + axis = axisMap.get(axisResource); + properties.rangeMappings.put(dataset, axis); + } + + axisResource = graph.getPossibleObject(datasetResource, jfree.Dataset_mapToDomainAxis); + if(axisMap.containsKey(axisResource)) { + axis = axisMap.get(axisResource); + properties.domainMappings.put(dataset, axis); + } + } + } + getOtherProperties(graph, properties); + return properties; + + } + }, listener); + } + return plot; } + + protected class PlotProperties { + public ArrayList ranges; + public ArrayList domains; + public ArrayList datasets; + public HashMap rangeMappings; + public HashMap domainMappings; + public HashMap otherProperties; + + public PlotProperties() { + datasets = new ArrayList(); + rangeMappings = new HashMap(); + domainMappings = new HashMap(); + ranges = new ArrayList(); + domains = new ArrayList(); + otherProperties = new HashMap(); + } + + @Override + public boolean equals(Object other) { + if(!this.getClass().equals(other.getClass())) + return false; + PlotProperties p = (PlotProperties)other; + if(!ranges.equals(p.ranges)) + return false; + if(!domains.equals(p.domains)) + return false; + if(!datasets.equals(p.datasets)) + return false; + if(!rangeMappings.equals(p.rangeMappings)) + return false; + if(!domainMappings.equals(p.domainMappings)) + return false; + if(!otherProperties.equals(p.otherProperties)) + return false; + return true; + } + } + + private class PlotPropertyListener implements Listener { + + private boolean disposed = false; + + public void dispose() { + disposed = true; + } + @Override + public void execute(final PlotProperties result) { + SwingUtilities.invokeLater(new Runnable() { + + @Override + public void run() { + setPlotProperties(result); + } + }); + } + + @Override + public void exception(Throwable t) { + t.printStackTrace(); + } + + @Override + public boolean isDisposed() { + return disposed; + } + + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/CategoryDataset.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/CategoryDataset.java index 1fa12dbd..7be59dcb 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/CategoryDataset.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/CategoryDataset.java @@ -241,7 +241,7 @@ public class CategoryDataset extends AbstractDataset { // Add found series for(int i = 0; i < series.size(); i++) { TempSeries s = series.get(i); - if(renderer instanceof org.jfree.chart.renderer.category.StackedBarRenderer) { + if(renderer instanceof org.jfree.chart.renderer.category.StackedBarRenderer && s.name.contains("[")) { String category = s.name.substring(0, s.name.indexOf('[')); String series = s.name.substring(s.name.indexOf('[')); dataset.addValue(s.value, series, category); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/CategoryPlot.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/CategoryPlot.java index 69d5d791..a680d41c 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/CategoryPlot.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/CategoryPlot.java @@ -27,22 +27,9 @@ import org.simantics.sysdyn.JFreeChartResource; * */ public class CategoryPlot extends AbstractPlot { - - private Boolean visibleGrid; - public CategoryPlot(ReadGraph graph, Resource resource) { super(graph, resource); - - try { - JFreeChartResource jfree = JFreeChartResource.getInstance(graph); - // Visual properties - visibleGrid = graph.getPossibleRelatedValue(resource, jfree.Plot_visibleGrid); - } catch(DatabaseException e) { - e.printStackTrace(); - } - - } @Override @@ -51,7 +38,7 @@ public class CategoryPlot extends AbstractPlot { } @Override - protected void setPlotProperties() { + protected void setPlotProperties(PlotProperties properties) { if(!(plot instanceof org.jfree.chart.plot.CategoryPlot)) return; @@ -59,29 +46,30 @@ public class CategoryPlot extends AbstractPlot { /* Support using multiple axis, but prefer using only one domain and * one range axis */ - for(int i = 0; i < ranges.size(); i++) { - cplot.setRangeAxis(i, (ValueAxis)ranges.get(i).getAxis()); + for(int i = 0; i < properties.ranges.size(); i++) { + cplot.setRangeAxis(i, (ValueAxis)properties.ranges.get(i).getAxis()); } - for(int i = 0; i < domains.size(); i++) { - cplot.setDomainAxis(i, (CategoryAxis)domains.get(i).getAxis()); + for(int i = 0; i < properties.domains.size(); i++) { + cplot.setDomainAxis(i, (CategoryAxis)properties.domains.get(i).getAxis()); } IAxis axis; - for(int i = 0; i < datasets.size(); i++) { - IDataset dataset = datasets.get(i); + for(int i = 0; i < properties.datasets.size(); i++) { + IDataset dataset = properties.datasets.get(i); org.jfree.data.category.CategoryDataset ds = (org.jfree.data.category.CategoryDataset)dataset.getDataset(); cplot.setDataset(i, ds); // System.out.println("setting dataset " + i + ": " + ds); cplot.setRenderer(i, (CategoryItemRenderer)dataset.getRenderer()); - axis = rangeMappings.get(dataset); - if(axis != null && ranges.contains(axis)) - cplot.mapDatasetToRangeAxis(i, ranges.indexOf(axis)); - axis = domainMappings.get(dataset); - if(axis != null && ranges.contains(axis)) - cplot.mapDatasetToDomainAxis(i, domains.indexOf(axis)); + axis = properties.rangeMappings.get(dataset); + if(axis != null && properties.ranges.contains(axis)) + cplot.mapDatasetToRangeAxis(i, properties.ranges.indexOf(axis)); + axis = properties.domainMappings.get(dataset); + if(axis != null && properties.ranges.contains(axis)) + cplot.mapDatasetToDomainAxis(i, properties.domains.indexOf(axis)); } + Boolean visibleGrid = (Boolean)properties.otherProperties.get("visibleGrid"); if(visibleGrid != null) { cplot.setRangeGridlinesVisible(visibleGrid); cplot.setDomainGridlinesVisible(false); @@ -92,4 +80,11 @@ public class CategoryPlot extends AbstractPlot { cplot.setOutlineVisible(false); } + @Override + protected void getOtherProperties(ReadGraph graph, PlotProperties properties) throws DatabaseException { + JFreeChartResource jfree = JFreeChartResource.getInstance(graph); + Boolean visibleGrid = graph.getPossibleRelatedValue(resource, jfree.Plot_visibleGrid); + properties.otherProperties.put("visibleGrid", visibleGrid); + } + } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/JFreeChart.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/JFreeChart.java index e9c36388..8a2f79c0 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/JFreeChart.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/JFreeChart.java @@ -17,6 +17,8 @@ import java.util.Collection; import javax.swing.SwingUtilities; +import org.jfree.chart.title.LegendTitle; +import org.jfree.chart.title.TextTitle; import org.jfree.ui.RectangleInsets; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; @@ -24,9 +26,12 @@ import org.simantics.db.Resource; import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.request.PossibleObjectWithType; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.procedure.Listener; +import org.simantics.db.request.Read; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.JFreeChartResource; -import org.simantics.utils.RunnableWithObject; +import org.simantics.ui.SimanticsUI; +import org.simantics.utils.datastructures.Pair; /** * Class representing a complete JFreeChart.Chart @@ -40,11 +45,9 @@ import org.simantics.utils.RunnableWithObject; public class JFreeChart implements IJFreeChart { private org.jfree.chart.JFreeChart jfreechart; - - private ITitle title; - private Boolean legendVisible; - private IPlot plot; + private ITitle title; + private Resource chartResource; /** * @@ -52,14 +55,12 @@ public class JFreeChart implements IJFreeChart { * @param chartResource Resource of type JFreeChart.Chart */ public JFreeChart(ReadGraph graph, Resource chartResource) { + this.chartResource = chartResource; + try { JFreeChartResource jfree = JFreeChartResource.getInstance(graph); Layer0 l0 = Layer0.getInstance(graph); - Resource titleResource = graph.syncRequest(new PossibleObjectWithType(chartResource, l0.ConsistsOf, jfree.Title)); - title = graph.adapt(titleResource, ITitle.class); - legendVisible = graph.getPossibleRelatedValue(chartResource, jfree.Chart_visibleLegend, Bindings.BOOLEAN); - Collection plotsCollection = graph.syncRequest(new ObjectsWithType(chartResource, l0.ConsistsOf, jfree.Plot)); for(Resource plotResource : plotsCollection) { this.plot = graph.adapt(plotResource, IPlot.class); @@ -70,6 +71,9 @@ public class JFreeChart implements IJFreeChart { } } + + JFreeChartListener listener; + /** * Returns a new chart using the information collected in the constructor */ @@ -78,33 +82,28 @@ public class JFreeChart implements IJFreeChart { if(plot == null) return null; - jfreechart = new org.jfree.chart.JFreeChart(plot.getPlot()); - - SwingUtilities.invokeLater(new RunnableWithObject(plot) { - @Override - public void run() { - if(jfreechart == null) - return; - - jfreechart.setBackgroundPaint(Color.WHITE); - if(jfreechart.getLegend() != null) { - jfreechart.getLegend().setBorder(0, 0, 0, 0); - int size = jfreechart.getLegend().getItemFont().getSize(); - jfreechart.getLegend().setItemFont(new Font("helvetica", Font.PLAIN, size)); - } - - org.jfree.chart.title.TextTitle t = (org.jfree.chart.title.TextTitle)title.getTitle(); - if(t.isVisible()) { - t.setFont(new Font("georgia", Font.BOLD, 13)); - t.setPadding(new RectangleInsets(4, 0, 0, 0)); - jfreechart.setTitle(t); + if(jfreechart == null) + jfreechart = new org.jfree.chart.JFreeChart(plot.getPlot()); + + if(listener == null) { + listener = new JFreeChartListener(); + SimanticsUI.getSession().asyncRequest(new Read>() { + + @Override + public Pair perform(ReadGraph graph) throws DatabaseException { + if(chartResource == null || !graph.hasStatement(chartResource)) + return null; + + JFreeChartResource jfree = JFreeChartResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + + Resource titleResource = graph.syncRequest(new PossibleObjectWithType(chartResource, l0.ConsistsOf, jfree.Title)); + title = graph.adapt(titleResource, ITitle.class); + Boolean legendVisible = graph.getPossibleRelatedValue(chartResource, jfree.Chart_visibleLegend, Bindings.BOOLEAN); + return new Pair(title, legendVisible); } - - if(legendVisible != null && !legendVisible) { - jfreechart.removeLegend(); - } - } - }); + }, listener); + } return jfreechart; } @@ -112,9 +111,72 @@ public class JFreeChart implements IJFreeChart { @Override public void dispose() { // Call dispose to title and plots to disable their possible listeners - title.dispose(); + if(title != null) + title.dispose(); + if(listener != null) + listener.dispose(); if(plot != null) plot.dispose(); } + + private class JFreeChartListener implements Listener> { + + private boolean disposed = false; + private LegendTitle legend; + + public void dispose() { + disposed = true; + } + + @Override + public void execute(final Pair result) { + if(result == null) + return; + + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + if(jfreechart == null) + return; + + jfreechart.setBackgroundPaint(Color.WHITE); + + if(jfreechart.getLegend() != null && !jfreechart.getLegend().equals(legend)) { + legend = jfreechart.getLegend(); + legend.setBorder(0, 0, 0, 0); + int size = legend.getItemFont().getSize(); + legend.setItemFont(new Font("helvetica", Font.PLAIN, size)); + } + + if(Boolean.FALSE.equals(result.second)) { + jfreechart.removeLegend(); + } else if (jfreechart.getLegend() == null && legend != null){ + jfreechart.addLegend(legend); + } + + TextTitle t = (org.jfree.chart.title.TextTitle)result.first.getTitle(); + if(t.isVisible()) { + t.setFont(new Font("georgia", Font.BOLD, 13)); + t.setPadding(new RectangleInsets(4, 0, 0, 0)); + jfreechart.setTitle(t); + } else { + jfreechart.setTitle((TextTitle)null); + } + } + }); + } + + @Override + public void exception(Throwable t) { + t.printStackTrace(); + } + + @Override + public boolean isDisposed() { + return disposed; + } + + } + } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/PiePlot.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/PiePlot.java index 556c8742..126a0184 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/PiePlot.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/PiePlot.java @@ -40,16 +40,8 @@ public class PiePlot extends AbstractPlot { private org.jfree.data.general.PieDataset pieDataset; private DatasetChangeListener listener; - private Boolean labelsVisible; - public PiePlot(ReadGraph graph, Resource resource) { super(graph, resource); - - try { - labelsVisible = graph.getPossibleRelatedValue(resource, JFreeChartResource.getInstance(graph).Plot_visibleLabels, Bindings.BOOLEAN); - } catch (DatabaseException e) { - e.printStackTrace(); - } } /** @@ -84,15 +76,21 @@ public class PiePlot extends AbstractPlot { } @Override - protected void setPlotProperties() { + protected void getOtherProperties(ReadGraph graph, PlotProperties properties) throws DatabaseException { + Boolean labelsVisible = graph.getPossibleRelatedValue(resource, JFreeChartResource.getInstance(graph).Plot_visibleLabels, Bindings.BOOLEAN); + properties.otherProperties.put("labelsVisible", labelsVisible); + } + + @Override + protected void setPlotProperties(PlotProperties properties) { if(!(plot instanceof MyPiePlot)) return; final MyPiePlot piePlot = (MyPiePlot)plot; - if(!datasets.isEmpty()) { + if(!properties.datasets.isEmpty()) { // We assume that a pie plot has only one dataset - final IDataset ds = datasets.get(0); + final IDataset ds = properties.datasets.get(0); Dataset dataset = ((PieDataset)ds).getDataset(); if(dataset == null) @@ -104,6 +102,8 @@ public class PiePlot extends AbstractPlot { pieDataset = (org.jfree.data.general.PieDataset)dataset; piePlot.setDataset(pieDataset); + + Boolean labelsVisible = (Boolean)properties.otherProperties.get("labelsVisible"); if(Boolean.FALSE.equals(labelsVisible)) piePlot.setLabelGenerator(null); else if(piePlot.getLabelGenerator() == null) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/XYDataset.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/XYDataset.java index b2981b9f..30f65e95 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/XYDataset.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/XYDataset.java @@ -60,61 +60,9 @@ import org.simantics.ui.SimanticsUI; public class XYDataset extends AbstractDataset { private XYLineAndShapeRenderer renderer; - private double[] domainValues; - private List seriesList; - private String realizationURI; public XYDataset(ReadGraph graph, final Resource datasetResource) { super(graph, datasetResource); - - try { - Layer0 l0 = Layer0.getInstance(graph); - ModelingResources mr = ModelingResources.getInstance(graph); - JFreeChartResource jfree = JFreeChartResource.getInstance(graph); - - // Find the model where the chart is located - Resource model = datasetResource; - do { - model = graph.getPossibleObject(model, l0.PartOf); - } while(model != null && !graph.isInstanceOf(model, mr.StructuralModel)); - - // Find the variable realization of the current experiment - realizationURI = null; - Resource realization = graph.syncRequest(new PossibleActiveExperiment(model)); - if (realization == null) { - Layer0X L0X = Layer0X.getInstance(graph); - realization = graph.getPossibleObject(model, L0X.HasBaseRealization); - } - if (realization != null) - realizationURI = graph.getURI(realization); - - if(realizationURI == null) - return; // No experiment -> No results - - - // Get a variable for the x-axis (if not time) - domainValues = null; - Resource domainAxis = graph.getPossibleObject(datasetResource, jfree.Dataset_mapToDomainAxis); - if(domainAxis != null) { - String rvi = graph.getPossibleRelatedValue(domainAxis, jfree.variableRVI); - if(rvi != null && !rvi.isEmpty()) { - try { - Variable domainVariable = Variables.getVariable(graph, realizationURI + rvi); - domainValues = domainVariable.getPossiblePropertyValue(graph, SysdynVariableProperties.VALUES , Bindings.DOUBLE_ARRAY); - } catch(MissingVariableException e) { - //Do nothing, use time as domain axis - } - } - } - - Resource seriesList = graph.getPossibleObject(resource, jfree.Dataset_seriesList); - if(seriesList != null) { - this.seriesList = ListUtils.toList(graph, seriesList); - } - - } catch (DatabaseException e) { - e.printStackTrace(); - } } private DefaultXYDataset dataset; @@ -123,10 +71,6 @@ public class XYDataset extends AbstractDataset { @Override public Dataset getDataset() { - - if(seriesList == null || seriesList.isEmpty()) - return null; - if(dataset == null) { dataset = new DefaultXYDataset(); } @@ -140,9 +84,32 @@ public class XYDataset extends AbstractDataset { JFreeChartResource jfree = JFreeChartResource.getInstance(graph); ArrayList series = new ArrayList(); + + String realizationURI = getRealizationURI(graph); + + if(realizationURI == null) + return series; // No experiment -> No results + + // Get a variable for the x-axis (if not time) + double[] domainValues = null; + Resource domainAxis = graph.getPossibleObject(resource, jfree.Dataset_mapToDomainAxis); + if(domainAxis != null) { + String rvi = graph.getPossibleRelatedValue(domainAxis, jfree.variableRVI); + if(rvi != null && !rvi.isEmpty()) { + try { + Variable domainVariable = Variables.getVariable(graph, realizationURI + rvi); + domainValues = domainVariable.getPossiblePropertyValue(graph, SysdynVariableProperties.VALUES , Bindings.DOUBLE_ARRAY); + } catch(MissingVariableException e) { + //Do nothing, use time as domain axis + } + } + } + + Resource seriesList = graph.getPossibleObject(resource, jfree.Dataset_seriesList); + // Get properties for all series if(seriesList != null) { - for(Resource r : seriesList) { + for(Resource r : ListUtils.toList(graph, seriesList)) { String rvi = graph.getPossibleRelatedValue(r, jfree.variableRVI); if(rvi == null) continue; @@ -237,18 +204,23 @@ public class XYDataset extends AbstractDataset { public Double perform(ReadGraph graph) throws DatabaseException { JFreeChartResource jfree = JFreeChartResource.getInstance(graph); // Get properties for all series - if(seriesList != null) { - for(Resource r : seriesList) { - String rvi = graph.getPossibleRelatedValue(r, jfree.variableRVI); - if(rvi == null) - continue; - try { - // Get a variable for the series - Variable v = Variables.getVariable(graph, realizationURI + rvi); - Double time = v.getPossiblePropertyValue(graph, SysdynVariableProperties.TIME , Bindings.DOUBLE); - return time; - } catch (MissingVariableException e) { - // Do nothing, if variable was not found. + Resource series = graph.getPossibleObject(resource, jfree.Dataset_seriesList); + if(series != null) { + List seriesList = ListUtils.toList(graph, series); + if(seriesList != null) { + String realizationURI = getRealizationURI(graph); + for(Resource r : seriesList) { + String rvi = graph.getPossibleRelatedValue(r, jfree.variableRVI); + if(rvi == null) + continue; + try { + // Get a variable for the series + Variable v = Variables.getVariable(graph, realizationURI + rvi); + Double time = v.getPossiblePropertyValue(graph, SysdynVariableProperties.TIME , Bindings.DOUBLE); + return time; + } catch (MissingVariableException e) { + // Do nothing, if variable was not found. + } } } } @@ -439,4 +411,39 @@ public class XYDataset extends AbstractDataset { } return renderer; } + + /** + * Get the realization uri of the current dataset resource + * @param graph ReadGraph + * @return realization uri for current dataset resource + * @throws DatabaseException + */ + private String getRealizationURI(ReadGraph graph) throws DatabaseException { + if(resource == null) + return null; + + Layer0 l0 = Layer0.getInstance(graph); + ModelingResources mr = ModelingResources.getInstance(graph); + + // Find the model where the chart is located + Resource model = resource; + do { + model = graph.getPossibleObject(model, l0.PartOf); + } while(model != null && !graph.isInstanceOf(model, mr.StructuralModel)); + + if(model == null) + return null; + + // Find the variable realization of the current experiment + String realizationURI = null; + Resource realization = graph.syncRequest(new PossibleActiveExperiment(model)); + if (realization == null) { + Layer0X L0X = Layer0X.getInstance(graph); + realization = graph.getPossibleObject(model, L0X.HasBaseRealization); + } + if (realization != null) + realizationURI = graph.getURI(realization); + + return realizationURI; + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/XYPlot.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/XYPlot.java index 1babb533..a4767c3c 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/XYPlot.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/XYPlot.java @@ -29,28 +29,24 @@ import org.simantics.sysdyn.JFreeChartResource; */ public class XYPlot extends AbstractPlot { - private Boolean visibleGrid; - public XYPlot(ReadGraph graph, Resource plotResource) { super(graph, plotResource); - - try { - JFreeChartResource jfree = JFreeChartResource.getInstance(graph); - // Visual properties - visibleGrid = graph.getPossibleRelatedValue(plotResource, jfree.Plot_visibleGrid); - } catch(DatabaseException e) { - e.printStackTrace(); - } - } @Override protected Plot newPlot() { return new org.jfree.chart.plot.XYPlot(null, null, null, null); } + + @Override + protected void getOtherProperties(ReadGraph graph, PlotProperties properties) throws DatabaseException { + JFreeChartResource jfree = JFreeChartResource.getInstance(graph); + Boolean visibleGrid = graph.getPossibleRelatedValue(resource, jfree.Plot_visibleGrid); + properties.otherProperties.put("visibleGrid", visibleGrid); + } @Override - protected void setPlotProperties() { + protected void setPlotProperties(PlotProperties properties) { if(!(plot instanceof org.jfree.chart.plot.XYPlot)) return; @@ -58,27 +54,28 @@ public class XYPlot extends AbstractPlot { xyplot.clearDomainAxes(); xyplot.clearRangeAxes(); - for(int i = 0; i < ranges.size(); i++) { - xyplot.setRangeAxis(i, (ValueAxis)ranges.get(i).getAxis()); + for(int i = 0; i < properties.ranges.size(); i++) { + xyplot.setRangeAxis(i, (ValueAxis)properties.ranges.get(i).getAxis()); } - for(int i = 0; i < domains.size(); i++) { - xyplot.setDomainAxis(i, (ValueAxis)domains.get(i).getAxis()); + for(int i = 0; i < properties.domains.size(); i++) { + xyplot.setDomainAxis(i, (ValueAxis)properties.domains.get(i).getAxis()); } IAxis axis; - for(int i = 0; i < datasets.size(); i++) { - IDataset dataset = datasets.get(i); + for(int i = 0; i < properties.datasets.size(); i++) { + IDataset dataset = properties.datasets.get(i); xyplot.setDataset(i, (XYDataset)dataset.getDataset()); xyplot.setRenderer(i, (XYItemRenderer)dataset.getRenderer()); - axis = rangeMappings.get(dataset); - if(axis != null && ranges.contains(axis)) - xyplot.mapDatasetToRangeAxis(i, ranges.indexOf(axis)); - axis = domainMappings.get(dataset); - if(axis != null && ranges.contains(axis)) - xyplot.mapDatasetToDomainAxis(i, domains.indexOf(axis)); + axis = properties.rangeMappings.get(dataset); + if(axis != null && properties.ranges.contains(axis)) + xyplot.mapDatasetToRangeAxis(i, properties.ranges.indexOf(axis)); + axis = properties.domainMappings.get(dataset); + if(axis != null && properties.ranges.contains(axis)) + xyplot.mapDatasetToDomainAxis(i, properties.domains.indexOf(axis)); } + Boolean visibleGrid = (Boolean)properties.otherProperties.get("visibleGrid"); if(visibleGrid == null) visibleGrid = true; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/element/ChartElementFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/element/ChartElementFactory.java index 1d994615..d6fdd57a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/element/ChartElementFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/element/ChartElementFactory.java @@ -106,6 +106,21 @@ public class ChartElementFactory extends SyncElementFactory { public void load(ReadGraph g, ICanvasContext canvas, IDiagram diagram, Resource element, IElement e) throws DatabaseException { Resource chartResource; + + ElementPropertySetter ps = e.getElementClass().getSingleItem(ElementPropertySetter.class); + ps.loadProperties(e, element, g); + + AffineTransform at = DiagramGraphUtil.getAffineTransform(g, element); + // Hack for disabling all rotations in chart elements + double x = at.getTranslateX(); + double y = at.getTranslateY(); + at.setToRotation(0); + at.setToTranslation(x, y); + ElementUtils.setTransform(e, at); // Set hint transform without rotations + ps.overrideProperty(e, "Transform", at); // Set property Transform without rotations + + e.setHint(SynchronizationHints.HINT_SYNCHRONIZER, HINT_SYNCHRONIZER); + Object o = e.getHint(KEY_CHART_COMPONENT); if(o == null || !(o instanceof Resource)) { @@ -114,11 +129,11 @@ public class ChartElementFactory extends SyncElementFactory { chartResource = (Resource)o; } - if(chartResource == null || !g.hasStatement(chartResource)) + if(chartResource == null || !g.hasStatement(chartResource)) { + e.removeHint(KEY_CHART); + e.removeHint(KEY_CHART_COMPONENT); return; - - ElementPropertySetter ps = e.getElementClass().getSingleItem(ElementPropertySetter.class); - ps.loadProperties(e, element, g); + } IJFreeChart ichart = g.adapt(chartResource, IJFreeChart.class); @@ -127,18 +142,6 @@ public class ChartElementFactory extends SyncElementFactory { e.setHint(KEY_CHART, chart); } - - AffineTransform at = DiagramGraphUtil.getAffineTransform(g, element); - // Hack for disabling all rotations in chart elements - double x = at.getTranslateX(); - double y = at.getTranslateY(); - at.setToRotation(0); - at.setToTranslation(x, y); - ElementUtils.setTransform(e, at); // Set hint transform without rotations - ps.overrideProperty(e, "Transform", at); // Set property Transform without rotations - - e.setHint(SynchronizationHints.HINT_SYNCHRONIZER, HINT_SYNCHRONIZER); - } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/element/ChartNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/element/ChartNode.java index b12f0897..f0460938 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/element/ChartNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/element/ChartNode.java @@ -188,8 +188,13 @@ public class ChartNode extends ComponentNode implements ISelectionPainte } g2d.setTransform(ot); - - + } else { + /* + * Component == null + * + * The related chart definition ha been removed. + */ + System.out.println("TÄÄLLÄ, TÄÄLLÄ"); } } -- 2.47.1