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