From: luukkainen Date: Fri, 2 Oct 2015 09:29:45 +0000 (+0000) Subject: fixes #6073 X-Git-Tag: v1.29.0~54 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=ce8fc344e4c14f4d6a5d19140c681400d45fc462;p=simantics%2Fsysdyn.git fixes #6073 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@31754 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/AbstractPlot.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/AbstractPlot.java index 7ba5ea26..c24759cd 100644 --- a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/AbstractPlot.java +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/AbstractPlot.java @@ -40,8 +40,8 @@ public abstract class AbstractPlot implements IPlot { protected Resource resource; protected Plot plot; protected PlotProperties currentProperties; - private PlotPropertyListener listener; - private org.jfree.chart.JFreeChart jfreechart; + protected PlotPropertyListener listener; + protected org.jfree.chart.JFreeChart jfreechart; public AbstractPlot(ReadGraph graph, Resource resource) { this.resource = resource; @@ -94,77 +94,91 @@ public abstract class AbstractPlot implements IPlot { if(listener == null || listener.isDisposed()) { listener = new PlotPropertyListener(); - SimanticsUI.getSession().asyncRequest(new Read() { + SimanticsUI.getSession().asyncRequest(new PlotPropertyQuery(), listener); + } - @Override - public PlotProperties perform(ReadGraph graph) throws DatabaseException { + return plot; + } + + protected IDataset getDataset(ReadGraph graph, Resource datasetResource) throws DatabaseException{ + return graph.adapt(datasetResource, IDataset.class); + } + + @Override + public void configurePlot(PlotProperties properties) { + + } - PlotProperties properties = new PlotProperties(); + protected class PlotPropertyQuery implements Read { + + public PlotPropertyQuery() { + + } - Layer0 l0 = Layer0.getInstance(graph); - JFreeChartResource jfree = JFreeChartResource.getInstance(graph); + @Override + public PlotProperties perform(ReadGraph graph) throws DatabaseException { - HashMap axisMap = new HashMap(); + PlotProperties properties = new PlotProperties(); - // 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); - } - } - } + Layer0 l0 = Layer0.getInstance(graph); + JFreeChartResource jfree = JFreeChartResource.getInstance(graph); - // 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); - } - } + HashMap axisMap = new HashMap(); - // 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); - } - } + // 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 = getDataset(graph, datasetResource); + 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); } - getOtherProperties(graph, properties); - return properties; + axisResource = graph.getPossibleObject(datasetResource, jfree.Dataset_mapToDomainAxis); + if(axisMap.containsKey(axisResource)) { + axis = axisMap.get(axisResource); + properties.domainMappings.put(dataset, axis); + } } - }, listener); - } + } + getOtherProperties(graph, properties); + return properties; - return plot; + } } - @Override - public void configurePlot(PlotProperties properties) { - - } - - private class PlotPropertyListener implements Listener { + protected class PlotPropertyListener implements Listener { private boolean disposed = false; + + public PlotPropertyListener() { + + } public void dispose() { disposed = true; diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/JFreeChart.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/JFreeChart.java index 2b39d630..a76cb1d2 100644 --- a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/JFreeChart.java +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/JFreeChart.java @@ -44,10 +44,11 @@ import org.simantics.utils.datastructures.Pair; */ public class JFreeChart implements IJFreeChart { - private org.jfree.chart.JFreeChart jfreechart; - private IPlot plot; - private ITitle title; - private Resource chartResource; + protected org.jfree.chart.JFreeChart jfreechart; + protected JFreeChartListener listener; + protected IPlot plot; + protected ITitle title; + protected Resource chartResource; /** * @@ -55,24 +56,35 @@ 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); - - Collection plotsCollection = graph.syncRequest(new ObjectsWithType(chartResource, l0.ConsistsOf, jfree.Plot)); - for(Resource plotResource : plotsCollection) { - this.plot = graph.adapt(plotResource, IPlot.class); - } - - } catch(DatabaseException e) { - e.printStackTrace(); - } + this.chartResource = chartResource; + initialize(graph); + } + + protected JFreeChart() { + // default constructor is used with inherited classes. + } + + protected void initialize(ReadGraph graph) { + try { + JFreeChartResource jfree = JFreeChartResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + + Collection plotsCollection = graph.syncRequest(new ObjectsWithType(chartResource, l0.ConsistsOf, jfree.Plot)); + for(Resource plotResource : plotsCollection) { + this.plot = getPlot(graph,plotResource); + } + + } catch(DatabaseException e) { + e.printStackTrace(); + } + } + + protected IPlot getPlot(ReadGraph graph, Resource plotResource) throws DatabaseException{ + return graph.adapt(plotResource, IPlot.class); } - JFreeChartListener listener; + /** * Returns a new chart using the information collected in the constructor @@ -89,22 +101,7 @@ public class JFreeChart implements IJFreeChart { 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); - } - }, listener); + SimanticsUI.getSession().asyncRequest(new JFreeChartQuery(), listener); } return jfreechart; @@ -126,11 +123,34 @@ public class JFreeChart implements IJFreeChart { plot.dispose(); } + protected class JFreeChartQuery implements Read> { + + public JFreeChartQuery() { + } + + @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); - private class JFreeChartListener implements Listener> { + 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); + } + } + + protected class JFreeChartListener implements Listener> { private boolean disposed = false; private LegendTitle legend; + + public JFreeChartListener() { + + } public void dispose() { disposed = true; diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/PiePlot.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/PiePlot.java index c6242b6c..f06bdbea 100644 --- a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/PiePlot.java +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/PiePlot.java @@ -37,8 +37,8 @@ import org.simantics.sysdyn.JFreeChartResource; */ public class PiePlot extends AbstractPlot { - private org.jfree.data.general.PieDataset pieDataset; - private DatasetChangeListener listener; + protected org.jfree.data.general.PieDataset pieDataset; + private DatasetChangeListener changeListener; public PiePlot(ReadGraph graph, Resource resource) { super(graph, resource); @@ -102,8 +102,8 @@ public class PiePlot extends AbstractPlot { if(dataset == null) return; - if(pieDataset != null && listener != null) { - pieDataset.removeChangeListener(listener); + if(pieDataset != null && changeListener != null) { + pieDataset.removeChangeListener(changeListener); } pieDataset = (org.jfree.data.general.PieDataset)dataset; @@ -128,7 +128,7 @@ public class PiePlot extends AbstractPlot { else if(piePlot.getLabelGenerator() == null) piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator()); - listener = new DatasetChangeListener() { + changeListener = new DatasetChangeListener() { @Override public void datasetChanged(DatasetChangeEvent event) { @@ -159,7 +159,7 @@ public class PiePlot extends AbstractPlot { } }; - pieDataset.addChangeListener(listener); + pieDataset.addChangeListener(changeListener); } // Cleaner look: no outline borders