1 package com.quantum.actions;
3 import java.io.IOException;
4 import java.sql.Connection;
5 import java.sql.SQLException;
6 import java.util.Arrays;
8 import java.util.Vector;
10 import com.quantum.ImageStore;
11 import com.quantum.Messages;
12 import com.quantum.QuantumPlugin;
13 import com.quantum.model.Bookmark;
14 import com.quantum.model.BookmarkCollection;
15 import com.quantum.model.DisplayableComparator;
16 import com.quantum.sql.parser.SQLParser;
17 import com.quantum.view.SQLQueryView;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.jface.action.Action;
21 import org.eclipse.jface.action.ActionContributionItem;
22 import org.eclipse.jface.action.IMenuCreator;
23 import org.eclipse.jface.action.IStatusLineManager;
24 import org.eclipse.jface.action.Separator;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.swt.events.MenuAdapter;
27 import org.eclipse.swt.events.MenuEvent;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Menu;
30 import org.eclipse.swt.widgets.MenuItem;
31 import org.eclipse.swt.widgets.Shell;
34 * Executes a query from the QueryView
38 public class ExecuteAction extends BaseExecuteAction implements IMenuCreator {
40 private static final String LAST_USED_BOOKMARK_PREFERENCE = ExecuteAction.class.getName() + ".bookmark";
41 private SQLQueryView view;
43 public ExecuteAction(SQLQueryView view) {
45 setActionDefinitionId("com.quantum.actions.ExecuteAction");
46 setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.PLAY));
54 private void initTextAndToolTip() {
55 Bookmark lastUsedBookmark = getLastUsedBookmark();
56 if (lastUsedBookmark == null) {
57 setText(Messages.getString(ExecuteAction.class, "textNoBookmark"));
58 setToolTipText(Messages.getString(ExecuteAction.class, "textNoBookmark"));
60 Object[] parameters = new Object[] { lastUsedBookmark.getName() };
61 setText(Messages.getString(ExecuteAction.class, "text", parameters));
62 setToolTipText(Messages.getString(ExecuteAction.class, "text", parameters));
66 protected Bookmark getBookmark() {
67 Bookmark lastUsedBookmark = getLastUsedBookmark();
68 return lastUsedBookmark == null ? super.getBookmark() : lastUsedBookmark;
70 protected void execute(Bookmark bookmark, Connection connection)
71 throws IOException, CoreException, SQLException {
74 boolean autoCommitPreference = this.view.isAutoCommitPreference();
75 boolean changed = false;
76 if (connection.getAutoCommit() != autoCommitPreference) {
77 connection.setAutoCommit(autoCommitPreference);
81 super.execute(bookmark, connection);
84 MessageDialog.openInformation(getShell(),
85 Messages.getString(ExecuteAction.class, "autocommitTitle"),
86 Messages.getString(ExecuteAction.class, "autocommitMessage",
87 new Object[] { bookmark.getName() }));
90 QuantumPlugin.getDefault().getPreferenceStore().setValue(
91 LAST_USED_BOOKMARK_PREFERENCE, bookmark.getName());
98 protected List getQueries() {
99 getStatusLineManager().setMessage(
100 Messages.getString(ExecuteAction.class, "parsing")); //$NON-NLS-1$
101 Vector queries = SQLParser.parse(view.getQuery());
108 protected IStatusLineManager getStatusLineManager() {
109 return this.view.getViewSite().getActionBars().getStatusLineManager();
112 protected Shell getShell() {
113 return this.view.getViewSite().getShell();
116 public void dispose() {
119 public Menu getMenu(Control parent) {
120 Menu menu = new Menu(parent);
122 * Add listener to repopulate the menu each time
123 * it is shown because the list of bookmarks may have changed.
125 menu.addMenuListener(new MenuAdapter() {
126 public void menuShown(MenuEvent e) {
127 Menu menu = (Menu)e.widget;
128 MenuItem[] items = menu.getItems();
129 for (int i=0; i < items.length; i++) {
138 public Menu getMenu(Menu parent) {
143 protected void fillMenu(Menu menu) {
144 Bookmark lastUsedBookmark = getLastUsedBookmark();
146 if (lastUsedBookmark != null) {
147 createSubAction(menu, lastUsedBookmark);
148 Separator separator = new Separator();
149 separator.fill(menu, -1);
152 Bookmark[] bookmarks = BookmarkCollection.getInstance().getBookmarks();
153 Arrays.sort(bookmarks, new DisplayableComparator());
154 for (int i = 0, length = bookmarks == null ? 0 : bookmarks.length; i < length; i++) {
155 final Bookmark bookmark = bookmarks[i];
156 createSubAction(menu, bookmark);
165 private Bookmark getLastUsedBookmark() {
166 String lastUsedName = QuantumPlugin.getDefault().getPreferenceStore().getString(
167 LAST_USED_BOOKMARK_PREFERENCE);
168 Bookmark lastUsedBookmark = lastUsedName == null
170 : BookmarkCollection.getInstance().find(lastUsedName);
171 return lastUsedBookmark;
180 private void createSubAction(Menu menu, final Bookmark bookmark) {
181 Action action = new Action() {
183 ExecuteAction.this.execute(bookmark);
186 action.setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.BOOKMARK));
188 // The last '@' sign is treated specially, so if the
189 // bookmark name contains an '@', then add an extra one to the end
190 if (bookmark.getName().indexOf('@') >= 0) {
191 action.setText(bookmark.getName() + '@');
193 action.setText(bookmark.getName());
195 ActionContributionItem item = new ActionContributionItem(action);