c374b2770e0fc950d14fded3bdd8be71c9e0ace6
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / actions / ExportQueryAction.java
1 package net.sourceforge.phpdt.sql.actions;
2
3 import java.io.File;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.io.PrintWriter;
7 import java.util.StringTokenizer;
8
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;
16
17 import net.sourceforge.phpdt.sql.view.LogProxy;
18 import net.sourceforge.phpdt.sql.view.SQLLogView;
19 import net.sourceforge.phpdt.sql.view.SQLQueryView;
20
21 public class ExportQueryAction extends Action implements IViewActionDelegate  {
22         SQLQueryView view;
23         FileDialog dialog;
24         /**
25          * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
26          */
27         public void init(IViewPart view) {
28                 this.view = (SQLQueryView) view;
29                 dialog = new FileDialog(view.getSite().getShell(), SWT.SAVE);
30                 dialog.setFilterExtensions(new String[]{"*.sql", "*.ddl", "*.*"}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
31                 dialog.setFilterNames(new String[]{Messages.getString("filedialog.sqlFiles"), //$NON-NLS-1$
32                         Messages.getString("filedialog.ddlFiles"), Messages.getString("filedialog.allfiles")}); //$NON-NLS-1$ //$NON-NLS-2$
33         }
34
35         /**
36          * @see org.eclipse.ui.IActionDelegate#run(IAction)
37          */
38         public void run(IAction action) {
39                 run();
40         }
41
42         public void run() {
43                 String filename = dialog.open();
44                 if (filename != null) {
45                         try {
46                                 File exportFile = new File(filename);
47                                 FileWriter fileWriter = new FileWriter(exportFile);
48                                 PrintWriter writer = new PrintWriter(fileWriter);
49                                 String output = view.getQuery();
50                                 StringTokenizer tokenizer = new StringTokenizer(output, "\n");
51                                 while (tokenizer.hasMoreElements()) {
52                                         String line = (String) tokenizer.nextElement();
53                                         writer.println(line);
54                                 }
55                                 writer.close();
56                         } catch (IOException e) {
57                                 LogProxy.getInstance().addText(SQLLogView.ERROR, e.toString());
58                                 e.printStackTrace();
59                         }
60                 }
61         }
62
63         /**
64          * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
65          */
66         public void selectionChanged(IAction action, ISelection selection) {
67         }
68 }