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