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