]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Fix ClassCastException from TrendTo{Png,Svg} 76/1576/1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Thu, 15 Mar 2018 07:52:00 +0000 (09:52 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Thu, 15 Mar 2018 07:52:00 +0000 (09:52 +0200)
refs #7817

Change-Id: Iec10e30a0ac64142e39863d2494f71fe3d50ad00

bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToPng.java
bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToSvg.java

index dae65d789df9f4dc691d63cdac56acbb122436c3..297d7a9539641a885405d8e5f47782e3172f09a2 100644 (file)
@@ -17,14 +17,17 @@ 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.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\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
+import org.eclipse.ui.IWorkbenchPart;\r
 import org.eclipse.ui.handlers.HandlerUtil;\r
 import org.jfree.chart.ChartUtilities;\r
 import org.jfree.chart.JFreeChart;\r
+import org.simantics.sysdyn.ui.Activator;\r
 \r
 /**\r
  * Exports the current chart in TrendView\r
@@ -35,10 +38,9 @@ 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
+        IWorkbenchPart part = HandlerUtil.getActivePart(event);\r
+        if (part instanceof TrendView) {\r
+            TrendView trendView = (TrendView) part;\r
             int width = trendView.getPanel().getSize().width;\r
             int height =  trendView.getPanel().getSize().height;\r
             int compressionLevel = 0;\r
@@ -47,7 +49,7 @@ public class TrendToPng extends AbstractHandler {
 \r
             while(true) {\r
                 FileDialog fd = new FileDialog(shell, SWT.SAVE);\r
-                fd.setText("Export trend to PNG");\r
+                fd.setText("Export Trend to PNG");\r
                 String[] ext = {"*.png"};\r
                 fd.setFilterExtensions(ext);\r
                 String selected = fd.open();\r
@@ -69,7 +71,7 @@ public class TrendToPng extends AbstractHandler {
                 try {\r
                     ChartUtilities.saveChartAsPNG(file, chart, width, height, null, true, compressionLevel);\r
                 } catch (IOException e) {\r
-                    e.printStackTrace();\r
+                    Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Save as PNG failed.", e));\r
                 }\r
                 return null;\r
             }\r
index 9da9370e8b03592689d1c595e3d8c7beda66809b..c814758ee5ac90b5a445e16b8e6c8aa30d5594ab 100644 (file)
@@ -1,13 +1,5 @@
 package org.simantics.sysdyn.ui.trend;\r
 \r
-//import java.awt.geom.Rectangle2D;\r
-//import java.io.File;\r
-//import java.io.FileNotFoundException;\r
-//import java.io.FileOutputStream;\r
-//import java.io.OutputStreamWriter;\r
-//import java.io.UnsupportedEncodingException;\r
-//import java.io.Writer;\r
-\r
 import java.awt.geom.Rectangle2D;\r
 import java.io.File;\r
 import java.io.IOException;\r
@@ -15,28 +7,30 @@ 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.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\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
+import org.eclipse.ui.IWorkbenchPart;\r
 import org.eclipse.ui.handlers.HandlerUtil;\r
 import org.simantics.jfreechart.chart.ChartUtils;\r
+import org.simantics.sysdyn.ui.Activator;\r
 \r
 public class TrendToSvg extends AbstractHandler {\r
 \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
+        IWorkbenchPart part = HandlerUtil.getActivePart(event);\r
+        if (part instanceof TrendView) {\r
+            TrendView trendView = (TrendView) part;\r
 \r
             Shell shell = HandlerUtil.getActiveShellChecked(event);\r
             \r
             while(true) {\r
                 FileDialog fd = new FileDialog(shell, SWT.SAVE);\r
-                fd.setText("Export trend to SVG");\r
+                fd.setText("Export Trend to SVG");\r
                 String[] ext = {"*.svg"};\r
                 fd.setFilterExtensions(ext);\r
                 String selected = fd.open();\r
@@ -60,9 +54,9 @@ public class TrendToSvg extends AbstractHandler {
                             new Rectangle2D.Double(0, 0, trendView.getPanel().getWidth(), trendView.getPanel().getHeight()),\r
                             file);\r
                 } catch (IOException e) {\r
-                    e.printStackTrace();\r
+                    Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Save as SVG failed.", e));\r
                 } \r
-                \r
+\r
                 return null;\r
             }\r
         }\r