From 1c625c380eecc08cebe925086ff7c38114e6f39b Mon Sep 17 00:00:00 2001 From: melander Date: Wed, 17 Aug 2011 12:04:38 +0000 Subject: [PATCH] Possibility to create PNG-files from trend view. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@21799 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn.ui/plugin.xml | 35 ++++++++- .../simantics/sysdyn/ui/trend/TrendToPng.java | 45 +++++++++++ .../simantics/sysdyn/ui/trend/TrendToSvg.java | 74 +++++++++++++++++++ .../simantics/sysdyn/ui/trend/TrendView.java | 8 +- 4 files changed, 157 insertions(+), 5 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToPng.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToSvg.java diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index bcd2e245..0fe3695b 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -1,5 +1,6 @@ - - + @@ -269,6 +270,20 @@ style="toggle" tooltip="Pins the trend so that it does not react to selection changes"> + + + + @@ -552,6 +567,14 @@ id="org.simantics.sysdyn.ui.trend.view.pin.state"> + + + + @@ -671,6 +694,14 @@ class="org.simantics.sysdyn.ui.trend.PinTrend" commandId="org.simantics.sysdyn.ui.trend.view.pin"> + + + + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToPng.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToPng.java new file mode 100644 index 00000000..ba1b10e5 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToPng.java @@ -0,0 +1,45 @@ +package org.simantics.sysdyn.ui.trend; + +import java.io.File; +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.swt.SWT; +import org.eclipse.swt.widgets.FileDialog; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.handlers.HandlerUtil; +import org.jfree.chart.ChartUtilities; +import org.jfree.chart.JFreeChart; + +public class TrendToPng extends AbstractHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + + int width = TrendView.panel.getSize().width; + int height = TrendView.panel.getSize().height; + int compressionLevel = 0; + + final Shell shell = HandlerUtil.getActiveShellChecked(event); + FileDialog fd = new FileDialog(shell, SWT.SAVE); + fd.setText("Export trend to PNG"); + String[] ext = {"*.png"}; + fd.setFilterExtensions(ext); + String selected = fd.open(); + + if (!(selected == null)){ + File file = new File(selected); + JFreeChart chart = TrendView.chart; + + try { + ChartUtilities.saveChartAsPNG(file, chart, width, height, null, true, compressionLevel); + } catch (IOException e) { + e.printStackTrace(); + } + } + return null; + } + +} \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToSvg.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToSvg.java new file mode 100644 index 00000000..0bd78c8a --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToSvg.java @@ -0,0 +1,74 @@ +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 org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +//import org.eclipse.swt.SWT; +//import org.eclipse.swt.widgets.FileDialog; +//import org.eclipse.swt.widgets.Shell; +//import org.eclipse.ui.handlers.HandlerUtil; +//import org.jfree.chart.JFreeChart; +//import org.w3c.dom.DOMImplementation; +//import org.w3c.dom.Document; +//import org.apache.batik.*; +//import org.apache.batik.dom.GenericDOMImplementation; +//import org.apache.batik.svggen.SVGGraphics2D; +//import org.apache.batik.svggen.SVGGraphics2DIOException; + +//This class needs Batik libraries to be imported, if SVG picture is really needed. + +public class TrendToSvg extends AbstractHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + +// final Shell shell = HandlerUtil.getActiveShellChecked(event); +// FileDialog fd = new FileDialog(shell, SWT.SAVE); +// fd.setText("Export trend to PNG"); +// String[] ext = {"*.svg"}; +// fd.setFilterExtensions(ext); +// String selected = fd.open(); +// +// File file = new File(selected); +// JFreeChart chart = TrendView.chart; +// +// DOMImplementation domImpl +// = GenericDOMImplementation.getDOMImplementation(); +// // Create an instance of org.w3c.dom.Document +// Document document = domImpl.createDocument(null, "svg", null); +// // Create an instance of the SVG Generator +// SVGGraphics2D svgGenerator = new SVGGraphics2D(document); +// // set the precision to avoid a null pointer exception in Batik 1.5 +// svgGenerator.getGeneratorContext().setPrecision(6); +// // Ask the chart to render into the SVG Graphics2D implementation +// chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, 400, 300), null); +// // Finally, stream out SVG to a file using UTF-8 character to +// // byte encoding +// boolean useCSS = true; +// Writer out = null; +// try { +// out = new OutputStreamWriter( +// new FileOutputStream(file), "UTF-8"); +// } catch (UnsupportedEncodingException e) { +// e.printStackTrace(); +// } catch (FileNotFoundException e) { +// e.printStackTrace(); +// } +// try { +// svgGenerator.stream(out, useCSS); +// } catch (SVGGraphics2DIOException e) { +// e.printStackTrace(); +// } + System.out.println("Add Batik-libraries"); + return null; + } + +} \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java index c11c57b7..1d80da29 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java @@ -33,9 +33,10 @@ import org.simantics.utils.ui.jface.ActiveSelectionProvider; public class TrendView extends ViewPart { Frame frame; - ChartPanel panel; + public static ChartPanel panel; SysdynDatasets sysdynDatasets = new SysdynDatasets(); SysdynDatasetSelectionListener sysdynDatasetSelectionListener; + public static JFreeChart chart; @SuppressWarnings("serial") class SysdynDatasets extends AbstractXYDataset { @@ -98,10 +99,9 @@ public class TrendView extends ViewPart { new XYLineAndShapeRenderer(true, false) ); - JFreeChart chart = new JFreeChart(plot); + chart = new JFreeChart(plot); panel = new ChartPanel(chart); frame.add(panel); - panel.requestFocus(); } @@ -137,6 +137,8 @@ public class TrendView extends ViewPart { if(panel != null) panel.requestFocus(); } + + } -- 2.47.1