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