0051eaff418cb4d6e8a6ea5993f82f012cf5c70e
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / actions / ExecuteAgainstAction.java
1 package com.quantum.actions;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Iterator;
6 import java.util.List;
7
8 import com.quantum.sql.parser.SQLParser;
9 import com.quantum.util.io.InputStreamHelper;
10
11 import org.eclipse.core.resources.IFile;
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.jface.action.IAction;
14 import org.eclipse.jface.action.IStatusLineManager;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.swt.widgets.Shell;
18 import org.eclipse.ui.IObjectActionDelegate;
19 import org.eclipse.ui.IViewPart;
20 import org.eclipse.ui.IWorkbenchPart;
21
22 /**
23  * This action can be executed against any .sql file, regardless of 
24  * whether or not the Quantum perspective is open.
25  * 
26  * @author BC Holmes
27  */
28 public class ExecuteAgainstAction extends BaseExecuteAction
29     implements IObjectActionDelegate {
30
31     private IFile[] files = null;
32
33     private IWorkbenchPart workbenchPart;
34     
35
36     /**
37      * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
38      */
39     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
40         this.workbenchPart = targetPart;
41     }
42
43     protected Shell getShell() {    
44         return this.workbenchPart.getSite().getShell();
45     }
46
47     /**
48      * This method is called when a new selection has been made on one 
49      * of the views.
50      * 
51      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
52      */
53     public void selectionChanged(IAction action, ISelection selection) {
54         if (selection instanceof IStructuredSelection) {
55             IStructuredSelection structuredSelection =
56                 (IStructuredSelection) selection;
57             List list = new ArrayList();
58
59             for (Iterator i = structuredSelection.iterator(); i.hasNext();) {
60                 Object temp = i.next();
61                 if (temp != null && temp instanceof IFile) {
62                     list.add(temp);
63                 }
64             }
65             this.files = (IFile[]) list.toArray(new IFile[list.size()]);
66         }
67     }
68
69         /* (non-Javadoc)
70          * @see com.quantum.actions.BaseSQLAction#getQueries()
71          */
72         protected List getQueries() throws IOException, CoreException {
73                 List list = new ArrayList();
74                 for (int i = 0, length = this.files == null ? 0 : this.files.length; i < length; i++) {
75                         String fileContents = InputStreamHelper.readIntoString(this.files[i].getContents());
76                         List queryList = SQLParser.parse(fileContents);
77                         list.addAll(queryList);
78                 }
79                 return list;
80         }
81
82         protected IStatusLineManager getStatusLineManager() {
83                 if (this.workbenchPart instanceof IViewPart) {
84                         return ((IViewPart) this.workbenchPart).getViewSite().getActionBars().getStatusLineManager();
85                 } else {
86                         return null;
87                 }
88         }
89
90         /* (non-Javadoc)
91          * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
92          */
93         public void run(IAction action) {
94                 run();
95         }
96 }