--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.charts.editor;\r
+\r
+import java.awt.Toolkit;\r
+import java.awt.datatransfer.Clipboard;\r
+import java.awt.datatransfer.StringSelection;\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.lang.reflect.InvocationTargetException;\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.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.jface.operation.IRunnableWithProgress;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.charts.Activator;\r
+import org.simantics.trend.impl.TrendNode;\r
+import org.simantics.trend.util.PrintUtil;\r
+import org.simantics.utils.ui.dialogs.ShowMessage;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ExportToPdfHandler extends AbstractHandler {\r
+\r
+ FileDialog fd;\r
+ Display display;\r
+\r
+ public ExportToPdfHandler() \r
+ {\r
+ super();\r
+ display = PlatformUI.createDisplay();\r
+ Shell shell = display.getActiveShell();\r
+ fd = new FileDialog( shell, SWT.SAVE );\r
+ fd.setText("Select PDF File");\r
+ fd.setFilterExtensions(new String[] {"*.pdf"});\r
+ fd.setFilterNames(new String[] {"PDF Documents"});\r
+ }\r
+\r
+ @Override\r
+ public Object execute(ExecutionEvent event) throws ExecutionException {\r
+ final TimeSeriesEditor editor = (TimeSeriesEditor) HandlerUtil.getActiveEditor(event);\r
+ final TrendNode trend = editor.trendNode;\r
+\r
+ // TODO: query file name from user, preferably create a wizard.\r
+ String name = trend.getTrendSpec().name;\r
+ fd.setText("Export "+name+" to PDF");\r
+\r
+ String filename = fd.open();\r
+ if (filename == null) return null;\r
+ final File f = new File( filename );\r
+\r
+ IRunnableWithProgress r = new IRunnableWithProgress() {\r
+ @Override\r
+ public void run(IProgressMonitor monitor)\r
+ throws InvocationTargetException, InterruptedException {\r
+ try {\r
+ // Ensure all views are built.\r
+ monitor.setTaskName("Exporting");\r
+ PrintUtil pu = new PrintUtil();\r
+ pu.addTrendPage(trend);\r
+ try {\r
+ //File f = File.createTempFile("Trend", ".pdf");\r
+ pu.printPdf(f);\r
+ System.out.println("Printed Trend to "+f);\r
+ } catch (IOException e1) {\r
+ throw new InvocationTargetException( e1 );\r
+ } \r
+ monitor.setTaskName("Done");\r
+\r
+ // Put exported file name into clipboard.\r
+ Toolkit toolkit = Toolkit.getDefaultToolkit();\r
+ Clipboard clipboard = toolkit.getSystemClipboard();\r
+ StringSelection strSel = new StringSelection(f.getAbsolutePath());\r
+ clipboard.setContents(strSel, null);\r
+ \r
+ } finally {\r
+ monitor.done();\r
+ }\r
+ }\r
+ };\r
+\r
+ try {\r
+ PlatformUI.getWorkbench().getProgressService().busyCursorWhile( r );\r
+ } catch (InvocationTargetException e) {\r
+ ShowMessage.showError(e.getCause().getClass().getName(), e.getCause().getMessage()); \r
+ Status s = new Status(IStatus.ERROR, Activator.PLUGIN_ID, "PDF Export failed: "+e.getCause().getMessage(), e.getCause());\r
+ Activator.getDefault().getLog().log(s);\r
+ } catch (InterruptedException e) {\r
+ Status s = new Status(IStatus.ERROR, Activator.PLUGIN_ID, "PDF Export failed: "+e.getCause().getMessage(), e.getCause());\r
+ Activator.getDefault().getLog().log(s);\r
+ }\r
+\r
+ return null;\r
+ }\r
+ \r
+ @Override\r
+ public void dispose() {\r
+ if (display!=null) {\r
+// display.dispose();\r
+ display = null;\r
+ }\r
+ super.dispose(); \r
+ }\r
+\r
+}\r