1 package com.quantum.actions;
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5 import java.io.IOException;
6 import java.sql.Connection;
7 import java.sql.SQLException;
8 import java.util.ArrayList;
9 import java.util.Iterator;
10 import java.util.List;
12 import com.quantum.Messages;
13 import com.quantum.model.Bookmark;
14 import com.quantum.model.BookmarkCollection;
15 import com.quantum.model.ConnectionException;
16 import com.quantum.sql.MultiSQLServer;
17 import com.quantum.sql.SQLParser;
18 import com.quantum.sql.SQLResults;
19 import com.quantum.ui.dialog.BookmarkSelectionDialog;
20 import com.quantum.ui.dialog.ExceptionDisplayDialog;
21 import com.quantum.util.io.InputStreamHelper;
22 import com.quantum.view.tableview.DefaultSizes;
23 import com.quantum.view.tableview.TableView;
25 import org.eclipse.core.resources.IFile;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.jface.action.IAction;
28 import org.eclipse.jface.dialogs.ErrorDialog;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.window.Window;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.ui.IObjectActionDelegate;
34 import org.eclipse.ui.IWorkbenchPart;
37 * This action can be executed against any .sql file, regardless of
38 * whether or not the Quantum perspective is open.
42 public class ExecuteAgainstAction extends BaseSQLAction
43 implements IObjectActionDelegate, PropertyChangeListener {
45 private String selectedBookmark = null;
46 private IFile[] files = null;
48 private IWorkbenchPart workbenchPart;
51 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
53 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
54 this.workbenchPart = targetPart;
57 protected Shell getShell() {
58 return this.workbenchPart.getSite().getShell();
62 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
64 public void run(IAction action) {
66 BookmarkSelectionDialog dialog = new BookmarkSelectionDialog(getShell());
67 dialog.addPropertyChangeListener(this);
68 int result = dialog.open();
70 if (result == Window.OK) {
72 executeAgainstBookmark();
73 } catch (SQLException e) {
74 ExceptionDisplayDialog.openError(getShell(),
77 } catch (IOException e) {
78 ExceptionDisplayDialog.openError(getShell(),
79 Messages.getString("ExecuteAgainstAction.title"),
80 Messages.getString("ExecuteAgainstAction.IOException"), e);
81 } catch (ConnectionException e) {
82 ExceptionDisplayDialog.openError(getShell(),
85 } catch (CoreException e) {
86 ErrorDialog.openError(getShell(), null, null, e.getStatus());
91 protected Bookmark getBookmark() {
92 return BookmarkCollection.getInstance().find(this.selectedBookmark);
95 private void executeAgainstBookmark()
96 throws SQLException, IOException, CoreException, ConnectionException {
97 Bookmark bookmark = getBookmark();
98 if (bookmark != null) {
99 boolean alreadyConnected = bookmark.isConnected();
100 Connection connection = getConnection();
103 length = (this.files == null) ? 0 : this.files.length;
104 connection != null && i < length;
106 executeAgainstBookmark(bookmark, connection, this.files[i]);
109 if (!alreadyConnected && connection != null) {
110 bookmark.disconnect();
116 private void executeAgainstBookmark(
118 Connection connection,
120 throws SQLException, IOException, CoreException {
121 executeAgainstBookmark(
124 InputStreamHelper.readIntoString(file.getContents()));
127 private void executeAgainstBookmark(
129 Connection connection,
131 throws SQLException {
132 List queryList = SQLParser.parse(queries);
133 MultiSQLServer server = MultiSQLServer.getInstance();
135 for (Iterator i = queryList.iterator(); i.hasNext();) {
136 String query = (String) i.next();
142 DefaultSizes.PAGE_SIZE,
143 DefaultSizes.MAX_COLUMN_SIZE);
144 if (results.isResultSet()) {
145 TableView view = TableView.getInstance();
147 view.loadQuery(bookmark, results);
154 * This method is called with a new selection has been made on one
157 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
159 public void selectionChanged(IAction action, ISelection selection) {
160 if (selection instanceof IStructuredSelection) {
161 IStructuredSelection structuredSelection =
162 (IStructuredSelection) selection;
163 List list = new ArrayList();
165 for (Iterator i = structuredSelection.iterator(); i.hasNext();) {
166 Object temp = i.next();
167 if (temp != null && temp instanceof IFile) {
168 System.out.println(((IFile) temp).getName());
172 this.files = (IFile[]) list.toArray(new IFile[list.size()]);
177 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
179 public void propertyChange(PropertyChangeEvent event) {
180 if ("selection".equals(event.getPropertyName())) {
181 this.selectedBookmark = (String) event.getNewValue();