1 /*******************************************************************************
\r
2 * Copyright (c) 2011 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.charts.editor;
\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
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
40 * @author Tuukka Lehtonen
\r
42 public class ExportToPdfHandler extends AbstractHandler {
\r
47 public ExportToPdfHandler()
\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
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
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
67 String filename = fd.open();
\r
68 if (filename == null) return null;
\r
69 final File f = new File( filename );
\r
71 IRunnableWithProgress r = new IRunnableWithProgress() {
\r
73 public void run(IProgressMonitor monitor)
\r
74 throws InvocationTargetException, InterruptedException {
\r
76 // Ensure all views are built.
\r
77 monitor.setTaskName("Exporting");
\r
78 PrintUtil pu = new PrintUtil();
\r
79 pu.addTrendPage(trend);
\r
81 //File f = File.createTempFile("Trend", ".pdf");
\r
83 System.out.println("Printed Trend to "+f);
\r
84 } catch (IOException e1) {
\r
85 throw new InvocationTargetException( e1 );
\r
87 monitor.setTaskName("Done");
\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
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
116 public void dispose() {
\r
117 if (display!=null) {
\r
118 // display.dispose();
\r