bug 1274348, superfluous newline at file end on formatting source
[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      * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
37      */
38     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
39         this.workbenchPart = targetPart;
40     }
41
42     protected Shell getShell() {    
43         return this.workbenchPart.getSite().getShell();
44     }
45
46     /**
47      * This method is called when a new selection has been made on one 
48      * of the views.
49      * 
50      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
51      */
52     public void selectionChanged(IAction action, ISelection selection) {
53         if (selection instanceof IStructuredSelection) {
54             IStructuredSelection structuredSelection =
55                 (IStructuredSelection) selection;
56             List list = new ArrayList();
57
58             for (Iterator i = structuredSelection.iterator(); i.hasNext();) {
59                 Object temp = i.next();
60                 if (temp != null && temp instanceof IFile) {
61                     list.add(temp);
62                 }
63             }
64             this.files = (IFile[]) list.toArray(new IFile[list.size()]);
65         }
66     }
67
68         /* (non-Javadoc)
69          * @see com.quantum.actions.BaseSQLAction#getQueries()
70          */
71         protected List getQueries() throws IOException, CoreException {
72                 List list = new ArrayList();
73                 for (int i = 0, length = this.files == null ? 0 : this.files.length; i < length; i++) {
74                         String fileContents = InputStreamHelper.readIntoString(this.files[i].getContents());
75                         List queryList = SQLParser.parse(fileContents);
76                         list.addAll(queryList);
77                 }
78                 return list;
79         }
80
81         protected IStatusLineManager getStatusLineManager() {
82                 if (this.workbenchPart instanceof IViewPart) {
83                         return ((IViewPart) this.workbenchPart).getViewSite().getActionBars().getStatusLineManager();
84                 } else {
85                         return null;
86                 }
87         }
88
89         /* (non-Javadoc)
90          * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
91          */
92         public void run(IAction action) {
93                 run();
94         }
95 }