]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Possibility to create PNG-files from trend view.
authormelander <melander@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 17 Aug 2011 12:04:38 +0000 (12:04 +0000)
committermelander <melander@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 17 Aug 2011 12:04:38 +0000 (12:04 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@21799 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/plugin.xml
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToPng.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToSvg.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java

index bcd2e245d4b367a5f318e64126bd69d7da1fc867..0fe3695bfee24a9320b1653a12215f9034a19e79 100644 (file)
@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>\r
-<?eclipse version="3.4"?>\r<!--\r
+<?eclipse version="3.4"?>\r
+<!--\r
     Copyright (c) 2010 Association for Decentralized Information Management in\r
     Industry THTH ry.\r
     All rights reserved. This program and the accompanying materials\r
@@ -10,7 +11,7 @@
     Contributors:\r
         VTT Technical Research Centre of Finland - initial API and implementation\r
  -->\r
-
+\r
 <plugin>\r
    <extension\r
          point="org.eclipse.ui.editors">\r
                style="toggle"\r
                tooltip="Pins the trend so that it does not react to selection changes">\r
          </command>\r
+         <command\r
+               commandId="org.simantics.sysdyn.ui.trend.view.png"\r
+               icon="icons/images.png"\r
+               label="Export to PNG"\r
+               style="push"\r
+               tooltip="Exports the trend to PNG file">\r
+         </command>\r
+         <command\r
+               commandId="org.simantics.sysdyn.ui.trend.view.svg"\r
+               icon="icons/map.png"\r
+               label="Export to SVG"\r
+               style="push"\r
+               tooltip="Exports the trend to SVG file">\r
+         </command>\r
       </menuContribution>\r
       <menuContribution\r
             locationURI="popup:#SysdynBrowserPopup?after=wbStart">\r
                id="org.simantics.sysdyn.ui.trend.view.pin.state">\r
          </state>\r
       </command>\r
+      <command\r
+            id="org.simantics.sysdyn.ui.trend.view.png"\r
+            name="Export to PNG">\r
+      </command>\r
+      <command\r
+            id="org.simantics.sysdyn.ui.trend.view.svg"\r
+            name="Export to SVG">\r
+      </command>\r
       <command\r
             id="org.simantics.sysdyn.ui.showModule"\r
             name="Show Module">\r
             class="org.simantics.sysdyn.ui.trend.PinTrend"\r
             commandId="org.simantics.sysdyn.ui.trend.view.pin">\r
       </handler>\r
+      <handler\r
+            class="org.simantics.sysdyn.ui.trend.TrendToPng"\r
+            commandId="org.simantics.sysdyn.ui.trend.view.png">\r
+      </handler>\r
+      <handler\r
+            class="org.simantics.sysdyn.ui.trend.TrendToSvg"\r
+            commandId="org.simantics.sysdyn.ui.trend.view.svg">\r
+      </handler>\r
       <handler\r
             class="org.simantics.sysdyn.ui.handlers.ShowModuleHandler"\r
             commandId="org.simantics.sysdyn.ui.showModule">\r
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 (file)
index 0000000..ba1b10e
--- /dev/null
@@ -0,0 +1,45 @@
+package org.simantics.sysdyn.ui.trend;\r
+\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.ChartUtilities;\r
+import org.jfree.chart.JFreeChart;\r
+\r
+public class TrendToPng extends AbstractHandler {\r
+\r
+    @Override\r
+    public Object execute(ExecutionEvent event) throws ExecutionException {\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.chart;\r
+\r
+               try {\r
+                       ChartUtilities.saveChartAsPNG(file, chart, width, height, null, true, compressionLevel);\r
+               } catch (IOException e) {\r
+                       e.printStackTrace();\r
+               }\r
+       }\r
+        return null;\r
+    }\r
+   \r
+}
\ 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 (file)
index 0000000..0bd78c8
--- /dev/null
@@ -0,0 +1,74 @@
+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 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
+\r
+public class TrendToSvg extends AbstractHandler {\r
+\r
+    @Override\r
+    public Object execute(ExecutionEvent event) throws ExecutionException {\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
+}
\ No newline at end of file
index c11c57b7deb4530c542a126076d9124125927c6f..1d80da29a1a28669df4df95c99a84c503ae85c66 100644 (file)
@@ -33,9 +33,10 @@ import org.simantics.utils.ui.jface.ActiveSelectionProvider;
 public class TrendView extends ViewPart {\r
 \r
        Frame frame;\r
-       ChartPanel panel;\r
+       public static ChartPanel panel;\r
        SysdynDatasets sysdynDatasets = new SysdynDatasets();\r
        SysdynDatasetSelectionListener sysdynDatasetSelectionListener;\r
+       public static JFreeChart chart;\r
 \r
        @SuppressWarnings("serial")\r
        class SysdynDatasets extends AbstractXYDataset {\r
@@ -98,10 +99,9 @@ public class TrendView extends ViewPart {
                                                new XYLineAndShapeRenderer(true, false)\r
                                );\r
 \r
-                               JFreeChart chart = new JFreeChart(plot);\r
+                               chart = new JFreeChart(plot);\r
                                panel = new ChartPanel(chart);\r
                                frame.add(panel);\r
-\r
                                panel.requestFocus();\r
                        }\r
 \r
@@ -137,6 +137,8 @@ public class TrendView extends ViewPart {
                if(panel != null)\r
                        panel.requestFocus();\r
        }\r
+       \r
+\r
 \r
 \r
 }\r