]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/editor/ExportToPdfHandler.java
Sync git svn branch with SVN repository r33269.
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / editor / ExportToPdfHandler.java
1 /*******************************************************************************\r
2  * Copyright (c) 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.charts.editor;\r
13 \r
14 import java.awt.Toolkit;\r
15 import java.awt.datatransfer.Clipboard;\r
16 import java.awt.datatransfer.StringSelection;\r
17 import java.io.File;\r
18 import java.io.IOException;\r
19 import java.lang.reflect.InvocationTargetException;\r
20 \r
21 import org.eclipse.core.commands.AbstractHandler;\r
22 import org.eclipse.core.commands.ExecutionEvent;\r
23 import org.eclipse.core.commands.ExecutionException;\r
24 import org.eclipse.core.runtime.IProgressMonitor;\r
25 import org.eclipse.core.runtime.IStatus;\r
26 import org.eclipse.core.runtime.Status;\r
27 import org.eclipse.jface.operation.IRunnableWithProgress;\r
28 import org.eclipse.swt.SWT;\r
29 import org.eclipse.swt.widgets.Display;\r
30 import org.eclipse.swt.widgets.FileDialog;\r
31 import org.eclipse.swt.widgets.Shell;\r
32 import org.eclipse.ui.PlatformUI;\r
33 import org.eclipse.ui.handlers.HandlerUtil;\r
34 import org.simantics.charts.Activator;\r
35 import org.simantics.trend.impl.TrendNode;\r
36 import org.simantics.trend.util.PrintUtil;\r
37 import org.simantics.utils.ui.dialogs.ShowMessage;\r
38 \r
39 /**\r
40  * @author Tuukka Lehtonen\r
41  */\r
42 public class ExportToPdfHandler extends AbstractHandler {\r
43 \r
44     FileDialog fd;\r
45     Display display;\r
46 \r
47     public ExportToPdfHandler() \r
48     {\r
49         super();\r
50         display = PlatformUI.createDisplay();\r
51         Shell shell = display.getActiveShell();\r
52         fd = new FileDialog( shell, SWT.SAVE );\r
53         fd.setText("Select PDF File");\r
54         fd.setFilterExtensions(new String[] {"*.pdf"});\r
55         fd.setFilterNames(new String[] {"PDF Documents"});\r
56     }\r
57 \r
58     @Override\r
59     public Object execute(ExecutionEvent event) throws ExecutionException {\r
60         final TimeSeriesEditor editor = (TimeSeriesEditor) HandlerUtil.getActiveEditor(event);\r
61         final TrendNode trend = editor.trendNode;\r
62 \r
63         // TODO: query file name from user, preferably create a wizard.\r
64         String name = trend.getTrendSpec().name;\r
65         fd.setText("Export "+name+" to PDF");\r
66 \r
67         String filename = fd.open();\r
68         if (filename == null) return null;\r
69         final File f = new File( filename );\r
70 \r
71         IRunnableWithProgress r = new IRunnableWithProgress() {\r
72             @Override\r
73             public void run(IProgressMonitor monitor)\r
74                     throws InvocationTargetException, InterruptedException {\r
75                 try {\r
76                     // Ensure all views are built.\r
77                     monitor.setTaskName("Exporting");\r
78                     PrintUtil pu = new PrintUtil();\r
79                     pu.addTrendPage(trend);\r
80                     try {\r
81                         //File f = File.createTempFile("Trend", ".pdf");\r
82                         pu.printPdf(f);\r
83                         System.out.println("Printed Trend to "+f);\r
84                     } catch (IOException e1) {\r
85                         throw new InvocationTargetException( e1 );\r
86                     } \r
87                     monitor.setTaskName("Done");\r
88 \r
89                     // Put exported file name into clipboard.\r
90                     Toolkit toolkit = Toolkit.getDefaultToolkit();\r
91                     Clipboard clipboard = toolkit.getSystemClipboard();\r
92                     StringSelection strSel = new StringSelection(f.getAbsolutePath());\r
93                     clipboard.setContents(strSel, null);\r
94                     \r
95                 } finally {\r
96                     monitor.done();\r
97                 }\r
98             }\r
99         };\r
100 \r
101         try {\r
102             PlatformUI.getWorkbench().getProgressService().busyCursorWhile( r );\r
103         } catch (InvocationTargetException e) {\r
104                 ShowMessage.showError(e.getCause().getClass().getName(), e.getCause().getMessage());            \r
105                 Status s = new Status(IStatus.ERROR, Activator.PLUGIN_ID, "PDF Export failed: "+e.getCause().getMessage(), e.getCause());\r
106                 Activator.getDefault().getLog().log(s);\r
107         } catch (InterruptedException e) {\r
108                 Status s = new Status(IStatus.ERROR, Activator.PLUGIN_ID, "PDF Export failed: "+e.getCause().getMessage(), e.getCause());\r
109                 Activator.getDefault().getLog().log(s);\r
110         }\r
111 \r
112         return null;\r
113     }\r
114     \r
115     @Override\r
116     public void dispose() {\r
117         if (display!=null) {\r
118 //              display.dispose();\r
119                 display = null;\r
120         }\r
121         super.dispose();        \r
122     }\r
123 \r
124 }\r