6d9654b72c92162e39e0cd13649399f9eb85e7be
[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          * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
31          */
32         public void init(IViewPart view) {
33                 this.view = (SQLQueryView) view;
34                 dialog = new FileDialog(view.getSite().getShell(), SWT.OPEN);
35                 dialog.setFilterExtensions(new String[]{"*.sql", "*.ddl", "*.*"}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
36                 dialog.setFilterNames(new String[]{Messages.getString("filedialog.sqlFiles"), //$NON-NLS-1$
37                         Messages.getString("filedialog.ddlFiles"), Messages.getString("filedialog.allfiles")}); //$NON-NLS-1$ //$NON-NLS-2$
38                 
39         }
40
41         /**
42          * @see org.eclipse.ui.IActionDelegate#run(IAction)
43          */
44         public void run(IAction action) {
45                 run();
46         }
47
48         public void run() {
49                 dialog.setFilterPath(QuantumPlugin.getDefault().getPreferenceStore().getString("quantum.dialogs.importquery.path"));
50                 String filename = dialog.open();
51                 if (filename != null) {
52                         QuantumPlugin.getDefault().getPreferenceStore().setValue("quantum.dialogs.importquery.path", filename);
53
54                         try {
55                                 File importFile = new File(filename);
56                                 FileReader fileReader = new FileReader(importFile);
57                                 BufferedReader reader = new BufferedReader(fileReader);
58                                 String line;
59                                 StringBuffer buffer = new StringBuffer();
60                                 
61                                 while ((line = reader.readLine()) != null) {
62                                         buffer.append(line);
63                                         buffer.append('\n');
64                                 }
65                                 view.setQuery(buffer.toString());
66                                 reader.close();
67                         } catch (IOException e) {
68                                 LogProxy.getInstance().addText(SQLLogView.ERROR, e.toString());
69                                 e.printStackTrace();
70                         }
71                 }
72         }
73
74         /**
75          * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
76          */
77         public void selectionChanged(IAction action, ISelection selection) {
78         }
79
80 }