Changes for variablemodificatin
[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.List;
7 import java.util.Vector;
8
9 import com.quantum.ImageStore;
10 import com.quantum.Messages;
11 import com.quantum.model.Bookmark;
12 import com.quantum.model.BookmarkCollection;
13 import com.quantum.sql.SQLParser;
14 import com.quantum.view.SQLQueryView;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.jface.action.Action;
18 import org.eclipse.jface.action.ActionContributionItem;
19 import org.eclipse.jface.action.IMenuCreator;
20 import org.eclipse.jface.action.IStatusLineManager;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.swt.events.MenuAdapter;
23 import org.eclipse.swt.events.MenuEvent;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Menu;
26 import org.eclipse.swt.widgets.MenuItem;
27 import org.eclipse.swt.widgets.Shell;
28
29 /**
30  * Executes a query from the QueryView
31  *
32  * @author panic
33  */
34 public class ExecuteAction extends BaseExecuteAction implements IMenuCreator {
35         private SQLQueryView view;
36         
37         public ExecuteAction(SQLQueryView view) {
38                 this.view = view;
39                 setActionDefinitionId("com.quantum.actions.ExecuteAction");
40                 setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.PLAY));
41                 setText(Messages.getString("sqlqueryview.executeQuery"));
42                 setToolTipText(Messages.getString("sqlqueryview.executeQuery"));
43                 setMenuCreator(this);
44         }
45
46         
47         
48         protected void execute(Bookmark bookmark, Connection connection) 
49                         throws IOException, CoreException, SQLException {
50                 
51                 boolean autoCommitPreference = this.view.isAutoCommitPreference();
52                 boolean changed = false;
53                 if (connection.getAutoCommit() != autoCommitPreference) {
54                         connection.setAutoCommit(autoCommitPreference);
55                         changed = true;
56                 }
57                 
58                 super.execute(bookmark, connection);
59                 
60                 if (changed) {
61                         MessageDialog.openInformation(getShell(), 
62                                         Messages.getString(ExecuteAction.class, "autocommitTitle"), 
63                                         Messages.getString(ExecuteAction.class, "autocommitMessage", 
64                                                         new Object[] { bookmark.getName() }));
65                 }
66         }
67         /**
68          * @return
69          */
70         protected List getQueries() {
71                 getStatusLineManager().setMessage(
72                                 Messages.getString(ExecuteAction.class, "parsing")); //$NON-NLS-1$
73                 Vector queries = SQLParser.parse(view.getQuery());
74                 return queries;
75         }
76
77         /**
78          * @return
79          */
80         protected IStatusLineManager getStatusLineManager() {
81                 return this.view.getViewSite().getActionBars().getStatusLineManager();
82         }
83
84         protected Shell getShell() {
85                 return this.view.getViewSite().getShell();
86         }
87
88         public void dispose() {
89         }
90
91         public Menu getMenu(Control parent) {
92                 Menu menu = new Menu(parent);
93                 /**
94                  * Add listener to repopulate the menu each time
95                  * it is shown because the list of bookmarks may have changed.
96                  */
97                 menu.addMenuListener(new MenuAdapter() {
98                         public void menuShown(MenuEvent e) {
99                                 Menu menu = (Menu)e.widget;
100                                 MenuItem[] items = menu.getItems();
101                                 for (int i=0; i < items.length; i++) {
102                                         items[i].dispose();
103                                 }
104                                 fillMenu(menu);
105                         }
106                 });
107                 return menu;
108         }
109
110         public Menu getMenu(Menu parent) {
111                 // never called...
112                 return null;
113         }
114
115         protected void fillMenu(Menu menu) {
116                 Bookmark[] bookmarks = BookmarkCollection.getInstance().getBookmarks();
117                 for (int i = 0, length = bookmarks == null ? 0 : bookmarks.length; i < length; i++) {
118                         final Bookmark bookmark = bookmarks[i];
119                         Action action = new Action() {
120                                 public void run() {
121                                         ExecuteAction.this.execute(bookmark);
122                                 }
123                         };
124                         action.setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.BOOKMARK));
125                         action.setText(bookmark.getName());
126                         ActionContributionItem item = new ActionContributionItem(action);
127                         item.fill(menu, -1);
128                 }
129         }
130 }