misc
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / actions / ImportQueryAction.java
1 package com.quantum.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 com.quantum.Messages;
9 import com.quantum.QuantumPlugin;
10 import com.quantum.view.LogProxy;
11 import com.quantum.view.SQLLogView;
12 import com.quantum.view.SQLQueryView;
13
14 import org.eclipse.jface.action.Action;
15 import org.eclipse.jface.action.IAction;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.widgets.FileDialog;
19 import org.eclipse.ui.IViewActionDelegate;
20 import org.eclipse.ui.IViewPart;
21
22 /**
23  * @author root
24  *
25  */
26 public class ImportQueryAction extends Action implements IViewActionDelegate {
27         SQLQueryView view;
28         FileDialog dialog;
29         
30         public ImportQueryAction() {
31                 setText(Messages.getString("sqlqueryview.importQuery"));
32                 setImageDescriptor(QuantumPlugin.getImageDescriptor("import.gif"));
33                 setToolTipText(Messages.getString("sqlqueryview.importQuery"));
34         }
35         
36         /**
37          * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
38          */
39         public void init(IViewPart view) {
40                 this.view = (SQLQueryView) view;
41                 dialog = new FileDialog(view.getSite().getShell(), SWT.OPEN);
42                 dialog.setFilterExtensions(new String[]{"*.sql", "*.ddl", "*.*"}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
43                 dialog.setFilterNames(new String[]{Messages.getString("filedialog.sqlFiles"), //$NON-NLS-1$
44                         Messages.getString("filedialog.ddlFiles"), Messages.getString("filedialog.allfiles")}); //$NON-NLS-1$ //$NON-NLS-2$
45         }
46
47         /**
48          * @see org.eclipse.ui.IActionDelegate#run(IAction)
49          */
50         public void run(IAction action) {
51                 run();
52         }
53
54         public void run() {
55                 dialog.setFilterPath(QuantumPlugin.getDefault().getPreferenceStore().getString("quantum.dialogs.importquery.path"));
56                 String filename = dialog.open();
57                 if (filename != null) {
58                         QuantumPlugin.getDefault().getPreferenceStore().setValue("quantum.dialogs.importquery.path", filename);
59
60                         try {
61                                 File importFile = new File(filename);
62                                 FileReader fileReader = new FileReader(importFile);
63                                 BufferedReader reader = new BufferedReader(fileReader);
64                                 String line;
65                                 StringBuffer buffer = new StringBuffer();
66                                 
67                                 while ((line = reader.readLine()) != null) {
68                                         buffer.append(line);
69                                         buffer.append('\n');
70                                 }
71                                 view.setQuery(buffer.toString());
72                                 reader.close();
73                         } catch (IOException e) {
74                                 LogProxy.getInstance().addText(SQLLogView.ERROR, e.toString());
75                                 e.printStackTrace();
76                         }
77                 }
78         }
79
80         /**
81          * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
82          */
83         public void selectionChanged(IAction action, ISelection selection) {
84         }
85
86 }