Quantum version 2.4.1
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / actions / ExecuteAction.java
1 package com.quantum.actions;
2
3 import java.io.IOException;
4 import java.sql.Connection;
5 import java.sql.SQLException;
6 import java.util.Arrays;
7 import java.util.List;
8 import java.util.Vector;
9
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;
18
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;
32
33 /**
34  * Executes a query from the QueryView
35  *
36  * @author panic
37  */
38 public class ExecuteAction extends BaseExecuteAction implements IMenuCreator {
39         
40 private static final String LAST_USED_BOOKMARK_PREFERENCE = ExecuteAction.class.getName() + ".bookmark";
41         private SQLQueryView view;
42         
43         public ExecuteAction(SQLQueryView view) {
44                 this.view = view;
45                 setActionDefinitionId("com.quantum.actions.ExecuteAction");
46                 setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.PLAY));
47                 initTextAndToolTip();
48                 setMenuCreator(this);
49         }
50
51         /**
52          * 
53          */
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"));
59                 } else {
60                         Object[] parameters = new Object[] { lastUsedBookmark.getName() };
61                         setText(Messages.getString(ExecuteAction.class, "text", parameters));
62                         setToolTipText(Messages.getString(ExecuteAction.class, "text", parameters));
63                 }
64         }
65
66         protected Bookmark getBookmark() {
67                 Bookmark lastUsedBookmark = getLastUsedBookmark();
68                 return lastUsedBookmark == null ? super.getBookmark() : lastUsedBookmark;
69         }
70         protected void execute(Bookmark bookmark, Connection connection) 
71                         throws IOException, CoreException, SQLException {
72                 
73                 try {
74                         boolean autoCommitPreference = this.view.isAutoCommitPreference();
75                         boolean changed = false;
76                         if (connection.getAutoCommit() != autoCommitPreference) {
77                                 connection.setAutoCommit(autoCommitPreference);
78                                 changed = true;
79                         }
80                         
81                         super.execute(bookmark, connection);
82                         
83                         if (changed) {
84                                 MessageDialog.openInformation(getShell(), 
85                                                 Messages.getString(ExecuteAction.class, "autocommitTitle"), 
86                                                 Messages.getString(ExecuteAction.class, "autocommitMessage", 
87                                                                 new Object[] { bookmark.getName() }));
88                         }
89                 } finally {
90                         QuantumPlugin.getDefault().getPreferenceStore().setValue(
91                                         LAST_USED_BOOKMARK_PREFERENCE, bookmark.getName());
92                         initTextAndToolTip();
93                 }
94         }
95         /**
96          * @return
97          */
98         protected List getQueries() {
99                 getStatusLineManager().setMessage(
100                                 Messages.getString(ExecuteAction.class, "parsing")); //$NON-NLS-1$
101                 Vector queries = SQLParser.parse(view.getQuery());
102                 return queries;
103         }
104
105         /**
106          * @return
107          */
108         protected IStatusLineManager getStatusLineManager() {
109                 return this.view.getViewSite().getActionBars().getStatusLineManager();
110         }
111
112         protected Shell getShell() {
113                 return this.view.getViewSite().getShell();
114         }
115
116         public void dispose() {
117         }
118
119         public Menu getMenu(Control parent) {
120                 Menu menu = new Menu(parent);
121                 /**
122                  * Add listener to repopulate the menu each time
123                  * it is shown because the list of bookmarks may have changed.
124                  */
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++) {
130                                         items[i].dispose();
131                                 }
132                                 fillMenu(menu);
133                         }
134                 });
135                 return menu;
136         }
137
138         public Menu getMenu(Menu parent) {
139                 // never called...
140                 return null;
141         }
142
143         protected void fillMenu(Menu menu) {
144                 Bookmark lastUsedBookmark = getLastUsedBookmark();
145                 
146                 if (lastUsedBookmark != null) {
147                         createSubAction(menu, lastUsedBookmark);
148                         Separator separator = new Separator();
149                         separator.fill(menu, -1);
150                 }
151                 
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);
157                 }
158         }
159
160
161
162         /**
163          * @return
164          */
165         private Bookmark getLastUsedBookmark() {
166                 String lastUsedName = QuantumPlugin.getDefault().getPreferenceStore().getString(
167                                 LAST_USED_BOOKMARK_PREFERENCE);
168                 Bookmark lastUsedBookmark = lastUsedName == null 
169                                 ? null 
170                                 : BookmarkCollection.getInstance().find(lastUsedName);
171                 return lastUsedBookmark;
172         }
173
174
175
176         /**
177          * @param menu
178          * @param bookmark
179          */
180         private void createSubAction(Menu menu, final Bookmark bookmark) {
181                 Action action = new Action() {
182                         public void run() {
183                                 ExecuteAction.this.execute(bookmark);
184                         }
185                 };
186                 action.setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.BOOKMARK));
187                 
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() + '@');
192                 } else {
193                         action.setText(bookmark.getName());
194                 }
195                 ActionContributionItem item = new ActionContributionItem(action);
196                 item.fill(menu, -1);
197         }
198 }