]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Trend view could not be reopened. Got rid of anonymous inner class listeners and...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 14 Jun 2013 07:43:38 +0000 (07:43 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 14 Jun 2013 07:43:38 +0000 (07:43 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27541 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToPng.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java

index e0555cae620ea76dbb792822b9df16bd3d017a5d..dae65d789df9f4dc691d63cdac56acbb122436c3 100644 (file)
@@ -17,6 +17,8 @@ import java.io.IOException;
 import org.eclipse.core.commands.AbstractHandler;\r
 import org.eclipse.core.commands.ExecutionEvent;\r
 import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.expressions.EvaluationContext;\r
+import org.eclipse.jface.dialogs.MessageDialog;\r
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.widgets.FileDialog;\r
 import org.eclipse.swt.widgets.Shell;\r
@@ -33,29 +35,46 @@ public class TrendToPng extends AbstractHandler {
 \r
     @Override\r
     public Object execute(ExecutionEvent event) throws ExecutionException {\r
+        EvaluationContext c = (EvaluationContext)event.getApplicationContext();\r
+        Object o = c.getParent().getVariable("activePart");\r
+        if(o != null && o instanceof TrendView) {\r
+            TrendView trendView = (TrendView) o;\r
+            int width = trendView.getPanel().getSize().width;\r
+            int height =  trendView.getPanel().getSize().height;\r
+            int compressionLevel = 0;\r
 \r
-       int width = TrendView.panel.getSize().width;\r
-       int height = TrendView.panel.getSize().height;\r
-       int compressionLevel = 0;\r
-       \r
-       final Shell shell = HandlerUtil.getActiveShellChecked(event);\r
-       FileDialog fd = new FileDialog(shell, SWT.SAVE);\r
-       fd.setText("Export trend to PNG");\r
-               String[] ext = {"*.png"};\r
-               fd.setFilterExtensions(ext);\r
-       String selected = fd.open();\r
-       \r
-       if (!(selected == null)){\r
-               File file = new File(selected);\r
-               JFreeChart chart = TrendView.panel.getChart();\r
-\r
-               try {\r
-                       ChartUtilities.saveChartAsPNG(file, chart, width, height, null, true, compressionLevel);\r
-               } catch (IOException e) {\r
-                       e.printStackTrace();\r
-               }\r
-       }\r
+            final Shell shell = HandlerUtil.getActiveShellChecked(event);\r
+\r
+            while(true) {\r
+                FileDialog fd = new FileDialog(shell, SWT.SAVE);\r
+                fd.setText("Export trend to PNG");\r
+                String[] ext = {"*.png"};\r
+                fd.setFilterExtensions(ext);\r
+                String selected = fd.open();\r
+\r
+                if(selected == null)\r
+                    return null;\r
+\r
+                File file = new File(selected);\r
+\r
+                if(file.exists()) {\r
+                    MessageDialog dialog = new MessageDialog(shell, "Overwrite " + file.getName() + "?", null, file.getName() + " exits. Do you wan't to overwrite it?", 0,\r
+                            new String[] { "Yes", "No" }, 0);\r
+                    dialog.create();\r
+                    if (dialog.open() == 1)\r
+                        continue;\r
+                }\r
+\r
+                JFreeChart chart =  trendView.getPanel().getChart();\r
+                try {\r
+                    ChartUtilities.saveChartAsPNG(file, chart, width, height, null, true, compressionLevel);\r
+                } catch (IOException e) {\r
+                    e.printStackTrace();\r
+                }\r
+                return null;\r
+            }\r
+        }\r
         return null;\r
     }\r
-   \r
+\r
 }
\ No newline at end of file
index 83bcc56da4fb99cecbadea02f09044027c7b9a7e..9b2fa61b8fcd8ca6e19742f8bec447b981c173cc 100644 (file)
@@ -34,6 +34,7 @@ import org.simantics.db.request.Read;
 import org.simantics.jfreechart.chart.IJFreeChart;\r
 import org.simantics.sysdyn.manager.SysdynDataSet;\r
 import org.simantics.sysdyn.ui.viewUtils.SysdynDatasetSelectionListener;\r
+import org.simantics.utils.RunnableWithObject;\r
 \r
 /**\r
  * Trend view that shows all active simulation results for selected variables.\r
@@ -45,12 +46,28 @@ import org.simantics.sysdyn.ui.viewUtils.SysdynDatasetSelectionListener;
 public class TrendView extends ViewPart {\r
 \r
     private Frame frame;\r
-    public static ChartPanel panel;\r
-    private final SysdynDatasets sysdynDatasets = new SysdynDatasets();\r
+    private ChartPanel panel;\r
+    private SysdynDatasets sysdynDatasets = new SysdynDatasets();\r
     private SysdynDatasetSelectionListener sysdynDatasetSelectionListener;\r
-    public static JFreeChart defaultchart;\r
+    private JFreeChart defaultchart;\r
     private Composite composite;\r
-    private CustomChartListener listener;\r
+\r
+    \r
+    public Frame getFrame() {\r
+        return frame;\r
+    }\r
+\r
+    public ChartPanel getPanel() {\r
+        return panel;\r
+    }\r
+    \r
+    public void setPanel(ChartPanel panel) {\r
+        this.panel = panel;\r
+    }\r
+\r
+    public SysdynDatasets getSysdynDatasets() {\r
+        return sysdynDatasets;\r
+    }\r
 \r
     /**\r
      * Dataset for jFreeChart\r
@@ -110,61 +127,75 @@ public class TrendView extends ViewPart {
         displayDefaultChart();\r
 \r
         // Add a dataset listener that updates datasets for the chart according to current selection \r
-        sysdynDatasetSelectionListener = new SysdynDatasetSelectionListener() {\r
+        sysdynDatasetSelectionListener = new TrendViewSelectionListner(this);\r
 \r
-            @Override\r
-            protected void selectionChanged(final Collection<SysdynDataSet> activeDatasets) {\r
-                SwingUtilities.invokeLater(new Runnable() {\r
-\r
-                    @Override\r
-                    public void run() {\r
-                        if(listener != null) {\r
-                            listener.dispose();\r
-                        }\r
-                        sysdynDatasets.setDatasets(activeDatasets.toArray(new SysdynDataSet[activeDatasets.size()]));\r
-                        displayDefaultChart();\r
-                    }\r
+        getSite().getWorkbenchWindow().getSelectionService().addPostSelectionListener(sysdynDatasetSelectionListener);\r
 \r
-                });\r
-            }\r
+    }\r
+    \r
+    private class TrendViewSelectionListner extends SysdynDatasetSelectionListener {\r
+        \r
+        private TrendView trendView;\r
+        private CustomChartListener listener;\r
+\r
+        public TrendViewSelectionListner(TrendView trendView) {\r
+            this.trendView = trendView;\r
+        }\r
 \r
-            @Override\r
-            protected void selectionChanged(ReadGraph graph, final Resource chartResource) {\r
+        @Override\r
+        protected void selectionChanged(final Collection<SysdynDataSet> activeDatasets) {\r
+            SwingUtilities.invokeLater(new Runnable() {\r
 \r
-                if(listener != null) {\r
-                    listener.dispose();\r
+                @Override\r
+                public void run() {\r
+                    if(listener != null) {\r
+                        listener.dispose();\r
+                    }\r
+                    trendView.getSysdynDatasets().setDatasets(activeDatasets.toArray(new SysdynDataSet[activeDatasets.size()]));\r
+                    displayDefaultChart();\r
                 }\r
 \r
-                listener = new CustomChartListener();\r
+            });\r
+        }\r
 \r
-                graph.asyncRequest(new Read<JFreeChart>() {\r
+        @Override\r
+        protected void selectionChanged(ReadGraph graph, final Resource chartResource) {\r
 \r
-                    @Override\r
-                    public JFreeChart perform(ReadGraph graph) throws DatabaseException {\r
-                        if(graph.hasStatement(chartResource)) {\r
-                            IJFreeChart chart = graph.adapt(chartResource, IJFreeChart.class);\r
-                            if(chart != null) {\r
-                                return chart.getChart();\r
-                            }\r
-                        }\r
-                        return null;\r
-                    }\r
-                }, listener);\r
+            if(listener != null) {\r
+                listener.dispose();\r
             }\r
-        };\r
 \r
-        getSite().getWorkbenchWindow().getSelectionService().addPostSelectionListener(sysdynDatasetSelectionListener);\r
+            listener = new CustomChartListener(trendView);\r
+\r
+            graph.asyncRequest(new Read<JFreeChart>() {\r
 \r
+                @Override\r
+                public JFreeChart perform(ReadGraph graph) throws DatabaseException {\r
+                    if(graph.hasStatement(chartResource)) {\r
+                        IJFreeChart chart = graph.adapt(chartResource, IJFreeChart.class);\r
+                        if(chart != null) {\r
+                            return chart.getChart();\r
+                        }\r
+                    }\r
+                    return null;\r
+                }\r
+            }, listener);\r
+        }\r
     }\r
 \r
     private class CustomChartListener implements Listener<JFreeChart> {\r
 \r
         private boolean disposed = false;\r
+        private TrendView trendView;\r
+        \r
+        public CustomChartListener(TrendView trendView) {\r
+            this.trendView = trendView;\r
+        }\r
 \r
         @Override\r
         public void execute(JFreeChart result) {\r
             if(!disposed)\r
-                displayChart(result);\r
+                displayChart(result, trendView);\r
         }\r
 \r
         @Override\r
@@ -187,23 +218,36 @@ public class TrendView extends ViewPart {
      * Displays jFreeChart\r
      * @param jFreeChart\r
      */\r
-    private void displayChart(final JFreeChart jFreeChart) {\r
-        SwingUtilities.invokeLater(new Runnable() {\r
+    private void displayChart(JFreeChart jFreeChart, TrendView trendView) {\r
+        SwingUtilities.invokeLater(new RunnableWithObject(jFreeChart, trendView) {\r
 \r
             @Override\r
             public void run() {\r
+                if(count() != 2 || getObject(0) == null || getObject(1) == null)\r
+                    return;\r
+                \r
+                if(!(getObject(0) instanceof JFreeChart && getObject(1) instanceof TrendView))\r
+                    return;\r
+                \r
+                JFreeChart jFreeChart = (JFreeChart) getObject(0);\r
+                TrendView trendView = (TrendView) getObject(1);\r
+                \r
+                Frame frame = trendView.getFrame();\r
+                ChartPanel panel = trendView.getPanel();\r
+                \r
                 // Do not just simply frame.removeAll();\r
                 // Instead, use and reuse only the first component of the frame.\r
                 if(jFreeChart != null) {\r
-                    if (panel == null){\r
+                    if (panel == null || frame.getComponentCount() == 0){\r
                         panel = new ChartPanel(jFreeChart, false, true, true, true, true);\r
+                        trendView.setPanel(panel);\r
                         frame.add(panel);\r
                     } else {\r
-                        panel.setChart(jFreeChart);\r
+//                        panel.setChart(jFreeChart);\r
                         if ( frame.getComponent(0) instanceof ChartPanel ){\r
                             ChartPanel tempPanel = (ChartPanel)frame.getComponent(0);\r
                             tempPanel.setChart(jFreeChart);\r
-                            panel = tempPanel;\r
+                            trendView.setPanel(tempPanel);\r
                         }\r
                     }\r
                 }\r
@@ -228,7 +272,7 @@ public class TrendView extends ViewPart {
                     );\r
             defaultchart = new JFreeChart(plot);\r
         }\r
-        displayChart(defaultchart);\r
+        displayChart(defaultchart, this);\r
     }\r
 \r
     @Override\r