Added the PHP wizards again
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / actions / ImportQueryAction.java
1 package net.sourceforge.phpdt.sql.actions;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileReader;
6 import java.io.IOException;
7
8 import org.eclipse.jface.action.Action;
9 import org.eclipse.jface.action.IAction;
10 import org.eclipse.jface.viewers.ISelection;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.widgets.FileDialog;
13 import org.eclipse.ui.IViewActionDelegate;
14 import org.eclipse.ui.IViewPart;
15
16 import net.sourceforge.phpdt.sql.Messages;
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 /**
22  * @author root
23  *
24  * To change this generated comment edit the template variable "typecomment":
25  * Window>Preferences>Java>Templates.
26  * To enable and disable the creation of type comments go to
27  * Window>Preferences>Java>Code Generation.
28  */
29 public class ImportQueryAction extends Action implements IViewActionDelegate {
30         SQLQueryView view;
31                 FileDialog dialog;
32         /**
33          * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
34          */
35         public void init(IViewPart view) {
36                 this.view = (SQLQueryView) view;
37                 dialog = new FileDialog(view.getSite().getShell(), SWT.OPEN);
38                 dialog.setFilterExtensions(new String[]{"*.sql", "*.ddl", "*.*"}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
39                 dialog.setFilterNames(new String[]{Messages.getString("filedialog.sqlFiles"), //$NON-NLS-1$
40                         Messages.getString("filedialog.ddlFiles"), Messages.getString("filedialog.allfiles")}); //$NON-NLS-1$ //$NON-NLS-2$
41         }
42
43         /**
44          * @see org.eclipse.ui.IActionDelegate#run(IAction)
45          */
46         public void run(IAction action) {
47                 run();
48         }
49
50         public void run() {
51                 String filename = dialog.open();
52                 if (filename != null) {
53                         try {
54                                 File importFile = new File(filename);
55                                 FileReader fileReader = new FileReader(importFile);
56                                 BufferedReader reader = new BufferedReader(fileReader);
57                                 String line;
58                                 StringBuffer buffer = new StringBuffer();
59                                 while ((line = reader.readLine()) != null) {
60                                         buffer.append(line);
61                                         buffer.append('\n');
62                                 }
63                                 view.setQuery(buffer.toString());
64                                 reader.close();
65                         } catch (IOException e) {
66                                 LogProxy.getInstance().addText(SQLLogView.ERROR, e.toString());
67                                 e.printStackTrace();
68                         }
69                 }
70         }
71
72         /**
73          * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
74          */
75         public void selectionChanged(IAction action, ISelection selection) {
76         }
77
78 }