package org.simantics.jfreechart.chart;\r
\r
+import java.awt.Dimension;\r
+import java.awt.geom.Rectangle2D;\r
+import java.io.File;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.io.OutputStream;\r
+import java.io.OutputStreamWriter;\r
+import java.io.Writer;\r
import java.util.ArrayList;\r
import java.util.UUID;\r
\r
+import org.apache.batik.dom.GenericDOMImplementation;\r
+import org.apache.batik.svggen.SVGGraphics2D;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
import org.simantics.layer0.utils.direct.GraphUtils;\r
import org.simantics.operation.Layer0X;\r
import org.simantics.sysdyn.JFreeChartResource;\r
+import org.w3c.dom.DOMImplementation;\r
+import org.w3c.dom.Document;\r
\r
/**\r
* Utilities for handling charts\r
\r
return realizationURI; \r
}\r
+ \r
+\r
+ public static void writeSVG(org.jfree.chart.JFreeChart chart, Rectangle2D bounds, File file) throws IOException {\r
+\r
+ // Get a DOMImplementation.\r
+ DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();\r
+\r
+ // Create an instance of org.w3c.dom.Document.\r
+ Document document = domImpl.createDocument(null, "svg", null);\r
+\r
+ // Create an instance of the SVG Generator.\r
+ SVGGraphics2D svgGenerator = new SVGGraphics2D(document);\r
+\r
+ // Paint chart panel\r
+ svgGenerator.setSVGCanvasSize(new Dimension((int)bounds.getWidth(), (int)bounds.getHeight()));\r
+ chart.draw(svgGenerator, bounds);\r
+\r
+ // Finally, stream out SVG to the standard output using\r
+ // UTF-8 encoding.\r
+ OutputStream outputStream = new FileOutputStream(file);\r
+ Writer out = new OutputStreamWriter(outputStream, "UTF-8");\r
+ svgGenerator.stream(out, false);\r
+ outputStream.flush();\r
+ outputStream.close();\r
+ }\r
}\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
+\r
import org.eclipse.core.commands.AbstractHandler;\r
import org.eclipse.core.commands.ExecutionEvent;\r
import org.eclipse.core.commands.ExecutionException;\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.handlers.HandlerUtil;\r
-//import org.jfree.chart.JFreeChart;\r
-//import org.w3c.dom.DOMImplementation;\r
-//import org.w3c.dom.Document;\r
-//import org.apache.batik.*;\r
-//import org.apache.batik.dom.GenericDOMImplementation;\r
-//import org.apache.batik.svggen.SVGGraphics2D;\r
-//import org.apache.batik.svggen.SVGGraphics2DIOException;\r
-\r
-//This class needs Batik libraries to be imported, if SVG picture is really needed.\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
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.jfreechart.chart.ChartUtils;\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
+\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
+ String[] ext = {"*.svg"};\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
+ try {\r
+ ChartUtils.writeSVG(\r
+ trendView.getPanel().getChart(), \r
+ new Rectangle2D.Double(0, 0, trendView.getPanel().getWidth(), trendView.getPanel().getHeight()),\r
+ file);\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ } \r
+ \r
+ return null;\r
+ }\r
+ }\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 = {"*.svg"};\r
-// fd.setFilterExtensions(ext);\r
-// String selected = fd.open();\r
-// \r
-// File file = new File(selected);\r
-// JFreeChart chart = TrendView.chart;\r
-// \r
-// DOMImplementation domImpl\r
-// = GenericDOMImplementation.getDOMImplementation();\r
-// // Create an instance of org.w3c.dom.Document\r
-// Document document = domImpl.createDocument(null, "svg", null);\r
-// // Create an instance of the SVG Generator\r
-// SVGGraphics2D svgGenerator = new SVGGraphics2D(document);\r
-// // set the precision to avoid a null pointer exception in Batik 1.5\r
-// svgGenerator.getGeneratorContext().setPrecision(6);\r
-// // Ask the chart to render into the SVG Graphics2D implementation\r
-// chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, 400, 300), null);\r
-// // Finally, stream out SVG to a file using UTF-8 character to\r
-// // byte encoding\r
-// boolean useCSS = true;\r
-// Writer out = null;\r
-// try {\r
-// out = new OutputStreamWriter(\r
-// new FileOutputStream(file), "UTF-8");\r
-// } catch (UnsupportedEncodingException e) {\r
-// e.printStackTrace();\r
-// } catch (FileNotFoundException e) {\r
-// e.printStackTrace();\r
-// }\r
-// try {\r
-// svgGenerator.stream(out, useCSS);\r
-// } catch (SVGGraphics2DIOException e) {\r
-// e.printStackTrace();\r
-// }\r
- System.out.println("Add Batik-libraries");\r
return null;\r
}\r
\r