]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
fixes #6073
authorluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 2 Oct 2015 09:29:45 +0000 (09:29 +0000)
committerluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 2 Oct 2015 09:29:45 +0000 (09:29 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@31754 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.jfreechart/src/org/simantics/jfreechart/chart/AbstractPlot.java
org.simantics.jfreechart/src/org/simantics/jfreechart/chart/JFreeChart.java
org.simantics.jfreechart/src/org/simantics/jfreechart/chart/PiePlot.java

index 7ba5ea26f6ed689046f14d05056d6a1e507147b1..c24759cd467b4f7a6de19d5893dadb497a19c7be 100644 (file)
@@ -40,8 +40,8 @@ public abstract class AbstractPlot implements IPlot {
     protected Resource resource;\r
     protected Plot plot;\r
     protected PlotProperties currentProperties;\r
-    private PlotPropertyListener listener;\r
-    private org.jfree.chart.JFreeChart jfreechart;\r
+    protected PlotPropertyListener listener;\r
+    protected org.jfree.chart.JFreeChart jfreechart;\r
 \r
     public AbstractPlot(ReadGraph graph, Resource resource) {\r
         this.resource = resource;\r
@@ -94,77 +94,91 @@ public abstract class AbstractPlot implements IPlot {
 \r
         if(listener == null || listener.isDisposed()) {\r
             listener = new PlotPropertyListener();\r
-            SimanticsUI.getSession().asyncRequest(new Read<PlotProperties>() {\r
+            SimanticsUI.getSession().asyncRequest(new PlotPropertyQuery(), listener);\r
+        }\r
 \r
-                @Override\r
-                public PlotProperties perform(ReadGraph graph) throws DatabaseException {\r
+        return plot;\r
+    }\r
+    \r
+    protected IDataset getDataset(ReadGraph graph, Resource datasetResource) throws DatabaseException{\r
+         return graph.adapt(datasetResource, IDataset.class);\r
+    }\r
+    \r
+    @Override\r
+    public void configurePlot(PlotProperties properties) {\r
+       \r
+    }\r
 \r
-                    PlotProperties properties = new PlotProperties();\r
+    protected class PlotPropertyQuery implements Read<PlotProperties> {\r
+       \r
+       public PlotPropertyQuery() {\r
+               \r
+               }\r
 \r
-                    Layer0 l0 = Layer0.getInstance(graph);\r
-                    JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
+        @Override\r
+        public PlotProperties perform(ReadGraph graph) throws DatabaseException {\r
 \r
-                    HashMap<Resource, IAxis> axisMap = new HashMap<Resource, IAxis>();\r
+            PlotProperties properties = new PlotProperties();\r
 \r
-                    // Get all range axis\r
-                    Resource rangeList = graph.getPossibleObject(resource, jfree.Plot_rangeAxisList);\r
-                    if(rangeList != null) {\r
-                        for(Resource axisResource : ListUtils.toList(graph, rangeList)) {\r
-                            IAxis axis = graph.adapt(axisResource, IAxis.class);\r
-                            if(axis.getAxis() instanceof Axis) {\r
-                                properties.ranges.add(axis);\r
-                                axisMap.put(axisResource, axis);\r
-                            }\r
-                        }\r
-                    }\r
+            Layer0 l0 = Layer0.getInstance(graph);\r
+            JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
 \r
-                    // Get all domain axis\r
-                    // There usually is only one domain axis, but this supports also multiple domain axis\r
-                    for(Resource axisResource : graph.syncRequest(new ObjectsWithType(resource, jfree.Plot_domainAxis, jfree.Axis))) {\r
-                        IAxis axis = graph.adapt(axisResource, IAxis.class);\r
-                        if(axis.getAxis() instanceof Axis) {\r
-                            properties.domains.add(axis);\r
-                            axisMap.put(axisResource, axis);\r
-                        }\r
-                    }\r
+            HashMap<Resource, IAxis> axisMap = new HashMap<Resource, IAxis>();\r
 \r
-                    // Get all datasets and map them to axis\r
-                    for(Resource datasetResource : graph.syncRequest(new ObjectsWithType(resource, l0.ConsistsOf, jfree.Dataset))) {\r
-                        IDataset dataset = graph.adapt(datasetResource, IDataset.class);\r
-                        if(dataset != null) {\r
-                            properties.datasets.add(dataset);\r
-                            Resource axisResource = graph.getPossibleObject(datasetResource, jfree.Dataset_mapToRangeAxis);\r
-                            IAxis axis;\r
-                            if(axisMap.containsKey(axisResource)) {\r
-                                axis = axisMap.get(axisResource);\r
-                                properties.rangeMappings.put(dataset, axis);\r
-                            }\r
-\r
-                            axisResource = graph.getPossibleObject(datasetResource, jfree.Dataset_mapToDomainAxis);\r
-                            if(axisMap.containsKey(axisResource)) {\r
-                                axis = axisMap.get(axisResource);\r
-                                properties.domainMappings.put(dataset, axis);\r
-                            }\r
-                        }\r
+            // Get all range axis\r
+            Resource rangeList = graph.getPossibleObject(resource, jfree.Plot_rangeAxisList);\r
+            if(rangeList != null) {\r
+                for(Resource axisResource : ListUtils.toList(graph, rangeList)) {\r
+                    IAxis axis = graph.adapt(axisResource, IAxis.class);\r
+                    if(axis.getAxis() instanceof Axis) {\r
+                        properties.ranges.add(axis);\r
+                        axisMap.put(axisResource, axis);\r
+                    }\r
+                }\r
+            }\r
+\r
+            // Get all domain axis\r
+            // There usually is only one domain axis, but this supports also multiple domain axis\r
+            for(Resource axisResource : graph.syncRequest(new ObjectsWithType(resource, jfree.Plot_domainAxis, jfree.Axis))) {\r
+                IAxis axis = graph.adapt(axisResource, IAxis.class);\r
+                if(axis.getAxis() instanceof Axis) {\r
+                    properties.domains.add(axis);\r
+                    axisMap.put(axisResource, axis);\r
+                }\r
+            }\r
+\r
+            // Get all datasets and map them to axis\r
+            for(Resource datasetResource : graph.syncRequest(new ObjectsWithType(resource, l0.ConsistsOf, jfree.Dataset))) {\r
+               IDataset dataset = getDataset(graph, datasetResource);\r
+                if(dataset != null) {\r
+                    properties.datasets.add(dataset);\r
+                    Resource axisResource = graph.getPossibleObject(datasetResource, jfree.Dataset_mapToRangeAxis);\r
+                    IAxis axis;\r
+                    if(axisMap.containsKey(axisResource)) {\r
+                        axis = axisMap.get(axisResource);\r
+                        properties.rangeMappings.put(dataset, axis);\r
                     }\r
-                    getOtherProperties(graph, properties);\r
-                    return properties;\r
 \r
+                    axisResource = graph.getPossibleObject(datasetResource, jfree.Dataset_mapToDomainAxis);\r
+                    if(axisMap.containsKey(axisResource)) {\r
+                        axis = axisMap.get(axisResource);\r
+                        properties.domainMappings.put(dataset, axis);\r
+                    }\r
                 }\r
-            }, listener);\r
-        }\r
+            }\r
+            getOtherProperties(graph, properties);\r
+            return properties;\r
 \r
-        return plot;\r
+        }\r
     }\r
     \r
-    @Override\r
-    public void configurePlot(PlotProperties properties) {\r
-       \r
-    }\r
-\r
-    private class PlotPropertyListener implements Listener<PlotProperties> {\r
+    protected class PlotPropertyListener implements Listener<PlotProperties> {\r
 \r
         private boolean disposed = false;\r
+        \r
+        public PlotPropertyListener() {\r
+               \r
+        }\r
 \r
         public void dispose() {\r
             disposed = true;\r
index 2b39d630c1a80488771df6d2a818178c2b25df6f..a76cb1d215e9451db6eee261db19d5ecedce9023 100644 (file)
@@ -44,10 +44,11 @@ import org.simantics.utils.datastructures.Pair;
  */\r
 public class JFreeChart implements IJFreeChart {\r
 \r
-    private org.jfree.chart.JFreeChart jfreechart;\r
-    private IPlot plot;\r
-    private ITitle title;\r
-    private Resource chartResource;\r
+    protected org.jfree.chart.JFreeChart jfreechart;\r
+    protected JFreeChartListener listener;\r
+    protected IPlot plot;\r
+    protected ITitle title;\r
+    protected Resource chartResource;\r
 \r
     /**\r
      * \r
@@ -55,24 +56,35 @@ public class JFreeChart implements IJFreeChart {
      * @param chartResource Resource of type JFreeChart.Chart\r
      */\r
     public JFreeChart(ReadGraph graph, Resource chartResource) {\r
-        this.chartResource = chartResource;\r
-\r
-        try {\r
-            JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
-            Layer0 l0 = Layer0.getInstance(graph);\r
-\r
-            Collection<Resource> plotsCollection = graph.syncRequest(new ObjectsWithType(chartResource, l0.ConsistsOf, jfree.Plot));\r
-            for(Resource plotResource : plotsCollection) {\r
-                this.plot = graph.adapt(plotResource, IPlot.class);\r
-            } \r
-\r
-        } catch(DatabaseException e) {\r
-            e.printStackTrace();\r
-        }\r
+        this.chartResource = chartResource; \r
+        initialize(graph);\r
+    }\r
+    \r
+    protected JFreeChart() {\r
+       // default constructor is used with inherited classes.\r
+    }\r
+    \r
+    protected void initialize(ReadGraph graph) {\r
+        try {\r
+             JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
+             Layer0 l0 = Layer0.getInstance(graph);\r
+\r
+             Collection<Resource> plotsCollection = graph.syncRequest(new ObjectsWithType(chartResource, l0.ConsistsOf, jfree.Plot));\r
+             for(Resource plotResource : plotsCollection) {\r
+                 this.plot = getPlot(graph,plotResource);\r
+             } \r
+\r
+         } catch(DatabaseException e) {\r
+             e.printStackTrace();\r
+         }\r
+    }\r
+    \r
+    protected IPlot getPlot(ReadGraph graph, Resource plotResource) throws DatabaseException{\r
+       return graph.adapt(plotResource, IPlot.class);\r
     }\r
 \r
 \r
-    JFreeChartListener listener;\r
+    \r
 \r
     /**\r
      * Returns a new chart using the information collected in the constructor\r
@@ -89,22 +101,7 @@ public class JFreeChart implements IJFreeChart {
         \r
         if(listener == null) {\r
             listener = new JFreeChartListener();\r
-            SimanticsUI.getSession().asyncRequest(new Read<Pair<ITitle, Boolean>>() {\r
-\r
-                @Override\r
-                public Pair<ITitle, Boolean> perform(ReadGraph graph) throws DatabaseException {\r
-                    if(chartResource == null || !graph.hasStatement(chartResource))\r
-                        return null;\r
-                    \r
-                    JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
-                    Layer0 l0 = Layer0.getInstance(graph);\r
-\r
-                    Resource titleResource = graph.syncRequest(new PossibleObjectWithType(chartResource, l0.ConsistsOf, jfree.Title));\r
-                    title = graph.adapt(titleResource, ITitle.class);\r
-                    Boolean legendVisible = graph.getPossibleRelatedValue(chartResource, jfree.Chart_visibleLegend, Bindings.BOOLEAN);\r
-                    return new Pair<ITitle, Boolean>(title, legendVisible);\r
-                }\r
-            }, listener);\r
+            SimanticsUI.getSession().asyncRequest(new JFreeChartQuery(), listener);\r
         }\r
 \r
         return jfreechart;\r
@@ -126,11 +123,34 @@ public class JFreeChart implements IJFreeChart {
             plot.dispose();\r
     }\r
 \r
+    protected class JFreeChartQuery implements Read<Pair<ITitle, Boolean>> {\r
+       \r
+       public JFreeChartQuery() {\r
+               }\r
+\r
+        @Override\r
+        public Pair<ITitle, Boolean> perform(ReadGraph graph) throws DatabaseException {\r
+            if(chartResource == null || !graph.hasStatement(chartResource))\r
+                return null;\r
+            \r
+            JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
+            Layer0 l0 = Layer0.getInstance(graph);\r
 \r
-    private class JFreeChartListener implements Listener<Pair<ITitle, Boolean>> {\r
+            Resource titleResource = graph.syncRequest(new PossibleObjectWithType(chartResource, l0.ConsistsOf, jfree.Title));\r
+            title = graph.adapt(titleResource, ITitle.class);\r
+            Boolean legendVisible = graph.getPossibleRelatedValue(chartResource, jfree.Chart_visibleLegend, Bindings.BOOLEAN);\r
+            return new Pair<ITitle, Boolean>(title, legendVisible);\r
+        }\r
+    }\r
+    \r
+    protected class JFreeChartListener implements Listener<Pair<ITitle, Boolean>> {\r
 \r
         private boolean disposed = false;\r
         private LegendTitle legend;\r
+        \r
+        public JFreeChartListener() {\r
+               \r
+        }\r
 \r
         public void dispose() {\r
             disposed = true;\r
index c6242b6c90dbcbd4e555ea2527374e4f5d98c8ca..f06bdbea6f863c201ada5449d737cab913ba0715 100644 (file)
@@ -37,8 +37,8 @@ import org.simantics.sysdyn.JFreeChartResource;
  */\r
 public class PiePlot extends AbstractPlot {\r
 \r
-    private org.jfree.data.general.PieDataset pieDataset;\r
-    private DatasetChangeListener listener;\r
+    protected org.jfree.data.general.PieDataset pieDataset;\r
+    private DatasetChangeListener changeListener;\r
     \r
     public PiePlot(ReadGraph graph, Resource resource) {\r
         super(graph, resource);\r
@@ -102,8 +102,8 @@ public class PiePlot extends AbstractPlot {
             if(dataset == null)\r
                 return;\r
 \r
-            if(pieDataset != null && listener != null) {\r
-                pieDataset.removeChangeListener(listener);\r
+            if(pieDataset != null && changeListener != null) {\r
+                pieDataset.removeChangeListener(changeListener);\r
             }\r
             \r
             pieDataset = (org.jfree.data.general.PieDataset)dataset;\r
@@ -128,7 +128,7 @@ public class PiePlot extends AbstractPlot {
             else if(piePlot.getLabelGenerator() == null)\r
                 piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator());\r
             \r
-            listener = new DatasetChangeListener() {\r
+            changeListener = new DatasetChangeListener() {\r
                 \r
                                @Override\r
                 public void datasetChanged(DatasetChangeEvent event) {\r
@@ -159,7 +159,7 @@ public class PiePlot extends AbstractPlot {
                 }\r
             };\r
             \r
-            pieDataset.addChangeListener(listener);\r
+            pieDataset.addChangeListener(changeListener);\r
         }\r
         \r
         // Cleaner look: no outline borders\r