]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Changed updating methods for different chart components to support updating also...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 15 Dec 2011 12:05:34 +0000 (12:05 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 15 Dec 2011 12:05:34 +0000 (12:05 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@23570 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/AbstractAxis.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/AbstractPlot.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/CategoryDataset.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/CategoryPlot.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/JFreeChart.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/PiePlot.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/XYDataset.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/XYPlot.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/element/ChartElementFactory.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/element/ChartNode.java

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