1 /*******************************************************************************
2 * Copyright (c) 2011 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.charts.editor;
14 import java.awt.Toolkit;
15 import java.awt.datatransfer.Clipboard;
16 import java.awt.datatransfer.StringSelection;
18 import java.io.IOException;
19 import java.lang.reflect.InvocationTargetException;
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;
40 * @author Tuukka Lehtonen
42 public class ExportToPdfHandler extends AbstractHandler {
47 public ExportToPdfHandler()
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"});
59 public Object execute(ExecutionEvent event) throws ExecutionException {
60 final TimeSeriesEditor editor = (TimeSeriesEditor) HandlerUtil.getActiveEditor(event);
61 final TrendNode trend = editor.trendNode;
63 // TODO: query file name from user, preferably create a wizard.
64 String name = trend.getTrendSpec().name;
65 fd.setText("Export "+name+" to PDF");
67 String filename = fd.open();
68 if (filename == null) return null;
69 final File f = new File( filename );
71 IRunnableWithProgress r = new IRunnableWithProgress() {
73 public void run(IProgressMonitor monitor)
74 throws InvocationTargetException, InterruptedException {
76 // Ensure all views are built.
77 monitor.setTaskName("Exporting");
78 PrintUtil pu = new PrintUtil();
79 pu.addTrendPage(trend);
81 //File f = File.createTempFile("Trend", ".pdf");
83 System.out.println("Printed Trend to "+f);
84 } catch (IOException e1) {
85 throw new InvocationTargetException( e1 );
87 monitor.setTaskName("Done");
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);
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);
116 public void dispose() {
118 // display.dispose();