From: Tuukka Lehtonen Date: Thu, 15 Mar 2018 07:52:00 +0000 (+0200) Subject: Fix ClassCastException from TrendTo{Png,Svg} X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=2238e6d5cc519907c34e6be09ff3a8420e70a933;p=simantics%2Fsysdyn.git Fix ClassCastException from TrendTo{Png,Svg} refs #7817 Change-Id: Iec10e30a0ac64142e39863d2494f71fe3d50ad00 --- diff --git a/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToPng.java b/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToPng.java index dae65d78..297d7a95 100644 --- a/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToPng.java +++ b/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToPng.java @@ -17,14 +17,17 @@ import java.io.IOException; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; -import org.eclipse.core.expressions.EvaluationContext; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.handlers.HandlerUtil; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; +import org.simantics.sysdyn.ui.Activator; /** * Exports the current chart in TrendView @@ -35,10 +38,9 @@ public class TrendToPng extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { - EvaluationContext c = (EvaluationContext)event.getApplicationContext(); - Object o = c.getParent().getVariable("activePart"); - if(o != null && o instanceof TrendView) { - TrendView trendView = (TrendView) o; + IWorkbenchPart part = HandlerUtil.getActivePart(event); + if (part instanceof TrendView) { + TrendView trendView = (TrendView) part; int width = trendView.getPanel().getSize().width; int height = trendView.getPanel().getSize().height; int compressionLevel = 0; @@ -47,7 +49,7 @@ public class TrendToPng extends AbstractHandler { while(true) { FileDialog fd = new FileDialog(shell, SWT.SAVE); - fd.setText("Export trend to PNG"); + fd.setText("Export Trend to PNG"); String[] ext = {"*.png"}; fd.setFilterExtensions(ext); String selected = fd.open(); @@ -69,7 +71,7 @@ public class TrendToPng extends AbstractHandler { try { ChartUtilities.saveChartAsPNG(file, chart, width, height, null, true, compressionLevel); } catch (IOException e) { - e.printStackTrace(); + Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Save as PNG failed.", e)); } return null; } diff --git a/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToSvg.java b/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToSvg.java index 9da9370e..c814758e 100644 --- a/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToSvg.java +++ b/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToSvg.java @@ -1,13 +1,5 @@ package org.simantics.sysdyn.ui.trend; -//import java.awt.geom.Rectangle2D; -//import java.io.File; -//import java.io.FileNotFoundException; -//import java.io.FileOutputStream; -//import java.io.OutputStreamWriter; -//import java.io.UnsupportedEncodingException; -//import java.io.Writer; - import java.awt.geom.Rectangle2D; import java.io.File; import java.io.IOException; @@ -15,28 +7,30 @@ import java.io.IOException; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; -import org.eclipse.core.expressions.EvaluationContext; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.handlers.HandlerUtil; import org.simantics.jfreechart.chart.ChartUtils; +import org.simantics.sysdyn.ui.Activator; public class TrendToSvg extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { - EvaluationContext c = (EvaluationContext)event.getApplicationContext(); - Object o = c.getParent().getVariable("activePart"); - if(o != null && o instanceof TrendView) { - TrendView trendView = (TrendView) o; + IWorkbenchPart part = HandlerUtil.getActivePart(event); + if (part instanceof TrendView) { + TrendView trendView = (TrendView) part; Shell shell = HandlerUtil.getActiveShellChecked(event); while(true) { FileDialog fd = new FileDialog(shell, SWT.SAVE); - fd.setText("Export trend to SVG"); + fd.setText("Export Trend to SVG"); String[] ext = {"*.svg"}; fd.setFilterExtensions(ext); String selected = fd.open(); @@ -60,9 +54,9 @@ public class TrendToSvg extends AbstractHandler { new Rectangle2D.Double(0, 0, trendView.getPanel().getWidth(), trendView.getPanel().getHeight()), file); } catch (IOException e) { - e.printStackTrace(); + Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Save as SVG failed.", e)); } - + return null; } }