1 package net.sourceforge.phpdt.sql.actions;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.io.PrintWriter;
7 import java.util.StringTokenizer;
9 import org.eclipse.jface.action.Action;
10 import org.eclipse.jface.action.IAction;
11 import org.eclipse.jface.viewers.ISelection;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.widgets.FileDialog;
14 import org.eclipse.ui.IViewActionDelegate;
15 import org.eclipse.ui.IViewPart;
17 import net.sourceforge.phpdt.sql.Messages;
18 import net.sourceforge.phpdt.sql.view.LogProxy;
19 import net.sourceforge.phpdt.sql.view.SQLLogView;
20 import net.sourceforge.phpdt.sql.view.SQLQueryView;
22 public class ExportQueryAction extends Action implements IViewActionDelegate {
26 * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
28 public void init(IViewPart view) {
29 this.view = (SQLQueryView) view;
30 dialog = new FileDialog(view.getSite().getShell(), SWT.SAVE);
31 dialog.setFilterExtensions(new String[]{"*.sql", "*.ddl", "*.*"}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
32 dialog.setFilterNames(new String[]{Messages.getString("filedialog.sqlFiles"), //$NON-NLS-1$
33 Messages.getString("filedialog.ddlFiles"), Messages.getString("filedialog.allfiles")}); //$NON-NLS-1$ //$NON-NLS-2$
37 * @see org.eclipse.ui.IActionDelegate#run(IAction)
39 public void run(IAction action) {
44 String filename = dialog.open();
45 if (filename != null) {
47 /*Check for the presence of a "." - either indicates an
48 * extension has been provided or that a filename with a '.'
49 * has been specified - if the latter, it is assumed the user
50 * knows what they're doing - could be dangerous! :-)
52 if (filename.indexOf(".") >0) filename += ".sql";
53 File exportFile = new File(filename);
54 FileWriter fileWriter = new FileWriter(exportFile);
55 PrintWriter writer = new PrintWriter(fileWriter);
56 String output = view.getQuery();
57 StringTokenizer tokenizer = new StringTokenizer(output, "\n"); //$NON-NLS-1$
58 while (tokenizer.hasMoreElements()) {
59 String line = (String) tokenizer.nextElement();
63 } catch (IOException e) {
64 LogProxy.getInstance().addText(SQLLogView.ERROR, e.toString());
71 * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
73 public void selectionChanged(IAction action, ISelection selection) {