Added the PHP wizards again
[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.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;
21
22 public class ExportQueryAction extends Action implements IViewActionDelegate  {
23         SQLQueryView view;
24         FileDialog dialog;
25         /**
26          * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
27          */
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$
34         }
35
36         /**
37          * @see org.eclipse.ui.IActionDelegate#run(IAction)
38          */
39         public void run(IAction action) {
40                 run();
41         }
42
43         public void run() {
44                 String filename = dialog.open();
45                 if (filename != null) {
46                         try {
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! :-)
51                          */
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();
60                                         writer.println(line);
61                                 }
62                                 writer.close();
63                         } catch (IOException e) {
64                                 LogProxy.getInstance().addText(SQLLogView.ERROR, e.toString());
65                                 e.printStackTrace();
66                         }
67                 }
68         }
69
70         /**
71          * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
72          */
73         public void selectionChanged(IAction action, ISelection selection) {
74         }
75 }