]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Moved chart modifications to awt thread
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 7 Dec 2011 12:50:54 +0000 (12:50 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 7 Dec 2011 12:50:54 +0000 (12:50 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@23437 ac1ea38d-2e2b-0410-8846-a27921b304fc

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

index 661fdf48751e6cf6555639274bd59e0c98a7b4ce..87aa81ceb87e364c8819289ed264d05602dfe30a 100644 (file)
@@ -11,7 +11,9 @@
  *******************************************************************************/\r
 package org.simantics.sysdyn.ui.trend.chart;\r
 \r
-import java.util.HashMap;\r
+import java.util.Collection;\r
+\r
+import javax.swing.SwingUtilities;\r
 \r
 import org.jfree.chart.title.LegendTitle;\r
 import org.simantics.databoard.Bindings;\r
@@ -39,8 +41,7 @@ public class JFreeChart implements IJFreeChart {
     private ITitle title;\r
     private Boolean legendVisible;\r
 \r
-    private HashMap<Resource, IPlot> plots;\r
-    private IPlot firstPlot = null;\r
+    private IPlot plot;\r
 \r
     /**\r
      * \r
@@ -56,15 +57,11 @@ public class JFreeChart implements IJFreeChart {
             title = graph.adapt(titleResource, ITitle.class);\r
             legendVisible = graph.getPossibleRelatedValue(chartResource, jfree.Chart_visibleLegend, Bindings.BOOLEAN);\r
 \r
-            plots = new HashMap<Resource, IPlot>();\r
-            for(Resource plotResource : graph.syncRequest(new ObjectsWithType(chartResource, l0.ConsistsOf, jfree.Plot))) {\r
-                IPlot plot = graph.adapt(plotResource, IPlot.class);\r
-                plots.put(plotResource, plot);\r
-                if(firstPlot == null)\r
-                    firstPlot = plot;\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
-\r
         } catch(DatabaseException e) {\r
             e.printStackTrace();\r
         }\r
@@ -75,21 +72,27 @@ public class JFreeChart implements IJFreeChart {
      */\r
     @Override\r
     public org.jfree.chart.JFreeChart getChart() {\r
-        if(plots.size() == 0)\r
+        if(plot == null)\r
             return null;\r
-        jfreechart = new org.jfree.chart.JFreeChart(firstPlot.getPlot());\r
-\r
-        // setVisible does not work in TextTitle, have to use this workaround \r
-        org.jfree.chart.title.TextTitle  t = (org.jfree.chart.title.TextTitle)title.getTitle();\r
-        if(t.isVisible())\r
-            jfreechart.setTitle(t);\r
-\r
-        if(legendVisible != null && !legendVisible) {\r
-            for(Object title : jfreechart.getSubtitles()) {\r
-                if(title instanceof LegendTitle)\r
-                    ((LegendTitle)title).setVisible(legendVisible);\r
+        jfreechart = new org.jfree.chart.JFreeChart(plot.getPlot());\r
+\r
+        SwingUtilities.invokeLater(new Runnable() {\r
+            \r
+            @Override\r
+            public void run() {\r
+                // setVisible does not work in TextTitle, have to use this workaround \r
+                org.jfree.chart.title.TextTitle  t = (org.jfree.chart.title.TextTitle)title.getTitle();\r
+                if(t.isVisible())\r
+                    jfreechart.setTitle(t);\r
+\r
+                if(legendVisible != null && !legendVisible) {\r
+                    for(Object title : jfreechart.getSubtitles()) {\r
+                        if(title instanceof LegendTitle)\r
+                            ((LegendTitle)title).setVisible(legendVisible);\r
+                    }\r
+                }\r
             }\r
-        }\r
+        });\r
         return jfreechart;\r
     }\r
 \r
@@ -97,7 +100,7 @@ public class JFreeChart implements IJFreeChart {
     public void dispose() {\r
         // Call dispose to title and plots to disable their possible listeners\r
         title.dispose();\r
-        for(IPlot plot : plots.values())\r
+        if(plot != null)\r
             plot.dispose();\r
     }\r
 \r
index 62cc00d556feaf442e4c5e7f8baea7a56991f2d7..b3b07e4977c91b204abe7cf6f4eb1a829d04cc75 100644 (file)
@@ -14,6 +14,8 @@ package org.simantics.sysdyn.ui.trend.chart;
 import java.awt.Color;\r
 import java.util.HashMap;\r
 \r
+import javax.swing.SwingUtilities;\r
+\r
 import org.jfree.chart.plot.DefaultDrawingSupplier;\r
 import org.jfree.chart.plot.Plot;\r
 import org.jfree.data.general.Dataset;\r
@@ -43,25 +45,30 @@ public class PiePlot extends AbstractPlot {
         if(!datasets.isEmpty()) {\r
             // We assume that a pie plot has only one dataset\r
             IDataset ds = datasets.get(0);\r
-            Dataset dataset = ((PieDataset)ds).getDataset();\r
+            final Dataset dataset = ((PieDataset)ds).getDataset();\r
+            final HashMap<String, Color> colorMap = ((PieDataset)ds).getColorMap();\r
+            final HashMap<String, Boolean> explodedMap = ((PieDataset)ds).getExplodedMap();\r
             if(dataset instanceof org.jfree.data.general.PieDataset) {\r
-                plot.clearSectionPaints(true);\r
-                plot.setDrawingSupplier(new DefaultDrawingSupplier());\r
-                plot.setDataset(null);\r
-                \r
-                org.jfree.data.general.PieDataset pieDataset = (org.jfree.data.general.PieDataset)dataset;\r
-                plot.setDataset(pieDataset);\r
+                SwingUtilities.invokeLater(new Runnable() {\r
+\r
+                    @Override\r
+                    public void run() {\r
+                        plot.clearSectionPaints(true);\r
+                        plot.setDrawingSupplier(new DefaultDrawingSupplier());\r
+\r
+                        org.jfree.data.general.PieDataset pieDataset = (org.jfree.data.general.PieDataset)dataset;\r
+                        plot.setDataset(pieDataset);\r
 \r
-                HashMap<String, Color> colorMap = ((PieDataset)ds).getColorMap();\r
-                for(String name : colorMap.keySet())\r
-                    plot.setSectionPaint(name, colorMap.get(name));\r
+                        for(String name : colorMap.keySet())\r
+                            plot.setSectionPaint(name, colorMap.get(name));\r
 \r
-                HashMap<String, Boolean> explodedMap = ((PieDataset)ds).getExplodedMap();\r
-                for(String name : explodedMap.keySet()) {\r
-                    Boolean exploded = explodedMap.get(name);\r
-                    if(Boolean.TRUE.equals(exploded))\r
-                        plot.setExplodePercent(name, 0.3);\r
-                } \r
+                        for(String name : explodedMap.keySet()) {\r
+                            Boolean exploded = explodedMap.get(name);\r
+                            if(Boolean.TRUE.equals(exploded))\r
+                                plot.setExplodePercent(name, 0.3);\r
+                        }                     \r
+                    }\r
+                });\r
             }\r
         }\r
         return plot;\r