1) Fixed issue #764: Ignore strings when looking for single line comments.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / bookmark / BookmarkViewActionGroup.java
1 package com.quantum.view.bookmark;
2
3 import java.util.Iterator;
4
5 import com.quantum.ImageStore;
6 import com.quantum.Messages;
7 import com.quantum.actions.AddToQuickListAction;
8 import com.quantum.actions.ConnectAction;
9 import com.quantum.actions.DeleteAllRowsAction;
10 import com.quantum.actions.DisconnectAction;
11 import com.quantum.actions.NewBookmarkAction;
12 import com.quantum.actions.NextSequenceAction;
13 import com.quantum.actions.OpenQueryAction;
14 import com.quantum.actions.PrevSequenceAction;
15 import com.quantum.actions.RefreshBookmarkAction;
16 import com.quantum.actions.RemoveFromQuickListAction;
17 import com.quantum.actions.ViewTableAction;
18 import com.quantum.actions.ViewTableDetailsAction;
19 import com.quantum.model.Bookmark;
20 import com.quantum.model.Entity;
21 import com.quantum.util.versioning.VersioningHelper;
22
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.jface.action.ActionContributionItem;
25 import org.eclipse.jface.action.IAction;
26 import org.eclipse.jface.action.IMenuCreator;
27 import org.eclipse.jface.action.IMenuManager;
28 import org.eclipse.jface.action.IToolBarManager;
29 import org.eclipse.jface.action.Separator;
30 import org.eclipse.jface.viewers.ISelectionProvider;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.swt.events.MenuAdapter;
33 import org.eclipse.swt.events.MenuEvent;
34 import org.eclipse.swt.widgets.Control;
35 import org.eclipse.swt.widgets.Menu;
36 import org.eclipse.swt.widgets.MenuItem;
37 import org.eclipse.ui.IActionBars;
38 import org.eclipse.ui.IViewPart;
39 import org.eclipse.ui.IWorkbenchActionConstants;
40 import org.eclipse.ui.actions.ActionGroup;
41 import org.eclipse.ui.actions.ExportResourcesAction;
42 import org.eclipse.ui.actions.ImportResourcesAction;
43 import org.eclipse.ui.actions.SelectionListenerAction;
44 import org.eclipse.ui.actions.SelectionProviderAction;
45 import org.eclipse.ui.dialogs.PropertyDialogAction;
46
47 /**
48  * This class manages the list of actions for the bookmark view.
49  * 
50  * @author BC Holmes
51  */
52 public class BookmarkViewActionGroup extends ActionGroup 
53     implements BookmarkClipboard {
54         
55         class SQLAction extends Action implements IMenuCreator {
56                 public SQLAction() {
57                         setText(Messages.getString(BookmarkViewActionGroup.class, "sqlAction.text"));
58                         setMenuCreator(this);
59                 }
60                 public void dispose() {
61                 }
62                 public Menu getMenu(Control parent) {
63                         return null;
64                 }
65                 public Menu getMenu(Menu parent) {
66                         Menu menu = new Menu(parent);
67                         /**
68                          * Add listener to repopulate the menu each time
69                          * it is shown because the list of bookmarks may have changed.
70                          */
71                         menu.addMenuListener(new MenuAdapter() {
72                                 public void menuShown(MenuEvent event) {
73                                         Menu menu = (Menu) event.widget;
74                                         MenuItem[] items = menu.getItems();
75                                         for (int i=0; i < items.length; i++) {
76                                                 items[i].dispose();
77                                         }
78                                         fillSQLMenu(menu);
79                                 }
80                         });
81                         return menu;
82                 }
83         }
84         
85     private Bookmark bookmarkClipboard;
86     
87     private Action newBookmarkAction;
88     private Action sqlAction;
89     
90     // bookmark node actions
91     private SelectionListenerAction connectAction;
92     private SelectionListenerAction disconnectAction;
93     private SelectionListenerAction deleteAction;
94
95     // Query node actions
96     private SelectionListenerAction openQueryAction;
97
98     // Entity node actions
99     private SelectionListenerAction addToQuickListAction;
100     private SelectionListenerAction removeFromQuickListAction;
101     private SelectionListenerAction viewTableAction;
102     private SelectionListenerAction viewTableDetailsAction;
103
104     private SelectionListenerAction nextSequenceAction;
105     private SelectionListenerAction previousSequenceAction;
106     
107
108     private SelectionListenerAction dropAction;
109     
110     
111     // other actions
112     private SelectionListenerAction refreshAction;
113     private SelectionListenerAction renameAction;
114
115     private ExportResourcesAction exportAction;
116     private ImportResourcesAction importAction;
117
118     
119     private SelectionProviderAction propertiesAction;
120     
121     private SelectionListenerAction copyAction;
122     private SelectionListenerAction pasteAction;
123     private SelectionListenerAction deleteAllRowsAction;
124     
125     private IViewPart viewPart;
126     
127     public BookmarkViewActionGroup(
128         IViewPart viewPart, ISelectionProvider selectionProvider) {
129         this.viewPart = viewPart;
130         
131         this.newBookmarkAction = new NewBookmarkAction(this.viewPart);
132         
133         // bookmark actions
134         this.connectAction = new ConnectAction(this.viewPart);
135         this.disconnectAction = new DisconnectAction(this.viewPart);
136         
137         this.sqlAction = new SQLAction();
138
139         // entity actions
140         this.previousSequenceAction = new PrevSequenceAction(this.viewPart);
141         this.nextSequenceAction = new NextSequenceAction(this.viewPart);
142         this.addToQuickListAction = new AddToQuickListAction(this.viewPart);
143         this.removeFromQuickListAction = new RemoveFromQuickListAction(this.viewPart);
144         this.deleteAllRowsAction = new DeleteAllRowsAction(this.viewPart);
145         this.viewTableDetailsAction = new ViewTableDetailsAction(this.viewPart);
146
147         this.openQueryAction = new OpenQueryAction(this.viewPart);
148         
149         this.viewTableAction = new ViewTableAction(this.viewPart);
150         this.refreshAction = new RefreshBookmarkAction(this.viewPart);
151         this.renameAction = new RenameAction(this.viewPart);
152         this.copyAction = new CopyAction(this.viewPart, this, selectionProvider);
153         this.pasteAction = new PasteAction(this.viewPart, this, selectionProvider);
154         this.deleteAction = new DeleteAction(this.viewPart, selectionProvider);
155         this.exportAction = VersioningHelper.createExportResourcesAction(
156                         this.viewPart.getViewSite().getWorkbenchWindow());
157         this.exportAction.setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.EXPORT));
158         this.importAction = VersioningHelper.createImportResourcesAction(
159                         this.viewPart.getViewSite().getWorkbenchWindow());
160         this.importAction.setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.IMPORT));
161         
162         this.dropAction = new DropEntityAction(this.viewPart);
163         
164         this.propertiesAction = new PropertyDialogAction(
165             this.viewPart.getSite().getShell(), selectionProvider);
166     }
167
168     
169     /**
170      * Add all the appropriate actions to the popup menu. This method is 
171      * called whenever someone right-clicks on an object in the bookmark view.
172      * 
173      * @see org.eclipse.ui.actions.ActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager)
174      */
175     public void fillContextMenu(IMenuManager menu) {
176         
177         menu.add(this.newBookmarkAction);
178         menu.add(new Separator());
179         
180         if (getStructuredSelection().size() > 0 && 
181             isEverySelectionInstanceof(BookmarkNode.class)) {
182                 
183             addToMenu(menu, this.connectAction);
184             addToMenu(menu, this.disconnectAction);
185         }
186
187         menu.add(new Separator());
188         menu.add(this.copyAction);
189         // TODO: paste needs to change enablement whenever something is added
190         //       to the clipboard
191         addToMenu(menu, this.pasteAction);
192         addToMenu(menu, this.deleteAction);
193
194         if (getStructuredSelection().size() == 1 && 
195             isEverySelectionInstanceof(BookmarkNode.class)) {
196
197             addToMenu(menu, this.renameAction);
198         }
199
200         menu.add(new Separator());
201
202         // NOTE: In Eclipse 3.0, Export is no longer a sub-class of 
203         //       SelectionListenerAction.
204         if (this.exportAction != null) {
205                 this.exportAction.selectionChanged(getStructuredSelection());
206                 menu.add(this.exportAction);
207         }
208         if (this.importAction != null) {
209                 this.importAction.selectionChanged(getStructuredSelection());
210                         menu.add(this.importAction);
211         }
212
213         
214         if (isEverySelectionInstanceof(QueryNode.class)) {
215             if (getStructuredSelection().size() == 1) {
216                 addToMenu(menu, this.openQueryAction);
217             }
218         }
219
220         if (getStructuredSelection().size() > 0 && 
221             isEverySelectionInstanceof(EntityNode.class)) {
222                 
223             menu.add(new Separator());
224                 
225             if (getStructuredSelection().size() == 1) {
226                 if (((EntityNode) getStructuredSelection().getFirstElement()).isSequence()) {
227                     addToMenu(menu, this.nextSequenceAction);
228                     addToMenu(menu, this.previousSequenceAction);
229                 } else {
230                     addToMenu(menu, this.viewTableAction);
231                     addToMenu(menu, this.deleteAllRowsAction);
232                     addToMenu(menu, this.viewTableDetailsAction);
233                 }
234             }
235             
236             addToMenu(menu, this.addToQuickListAction);
237             addToMenu(menu, this.removeFromQuickListAction);
238             
239             if (getStructuredSelection().size() == 1) {
240                 menu.add(new Separator());
241                 menu.add(this.sqlAction);
242             }
243         }
244         
245         if (getStructuredSelection().size() == 1) {
246             menu.add(new Separator());
247             addToMenu(menu, this.refreshAction);
248         }
249         
250         createMarkerForActionsProvidedByOtherPlugins(menu);
251         
252         if (getStructuredSelection().size() == 1) {
253             addToMenu(menu, this.propertiesAction);
254         }
255     }
256     
257     protected void fillSQLMenu(Menu parent) {
258         if (getStructuredSelection().size() > 0 && 
259                 isEverySelectionInstanceof(EntityNode.class)) {
260                 addToMenu(parent, this.dropAction);
261         }
262     }
263
264     private void addToMenu(Menu menu, SelectionListenerAction action) {
265         action.selectionChanged(getStructuredSelection());
266                 ActionContributionItem item = new ActionContributionItem(action);
267                 item.fill(menu, -1);
268     }
269     
270     private void addToMenu(IMenuManager menu, SelectionListenerAction action) {
271         action.selectionChanged(getStructuredSelection());
272         menu.add(action);
273     }
274     
275     private void addToMenu(IMenuManager menu, SelectionProviderAction action) {
276         action.selectionChanged(getStructuredSelection());
277         menu.add(action);
278     }
279     
280     
281     private IStructuredSelection getStructuredSelection() {
282         return (IStructuredSelection) getContext().getSelection();
283     }
284     
285     private boolean isEverySelectionInstanceof(Class selectedClass) {
286         boolean result = true;
287         IStructuredSelection selection = getStructuredSelection();
288         for (Iterator i = selection.iterator(); result && i.hasNext(); ) {
289             Object object = i.next();
290             result &= selectedClass.isAssignableFrom(object.getClass());
291         }
292         
293         return result;
294     }
295     private boolean isEverySelectedGroupOfType(String type) {
296         boolean result = true;
297         IStructuredSelection selection = getStructuredSelection();
298         for (Iterator i = selection.iterator(); result && i.hasNext(); ) {
299             Object object = i.next();
300             result &= (( object instanceof GroupNode ) && 
301                                 ((GroupNode)object).getType().equals(type));
302         }
303         
304         return result;
305     }
306     private boolean isEverySelectedEntityTable() {
307         boolean result = true;
308         IStructuredSelection selection = getStructuredSelection();
309         for (Iterator i = selection.iterator(); result && i.hasNext(); ) {
310             Object object = i.next();
311             result &= (( object instanceof EntityNode ) && 
312                                 ((EntityNode)object).isTable());
313         }
314         
315         return result;
316     }
317     private boolean isEverySelectedEntityView() {
318         boolean result = true;
319         IStructuredSelection selection = getStructuredSelection();
320         for (Iterator i = selection.iterator(); result && i.hasNext(); ) {
321             Object object = i.next();
322             result &= (( object instanceof EntityNode ) && 
323                                 ((EntityNode)object).isView());
324         }
325         return result;
326     }
327     
328     public IAction getOpenAction() {
329         if (isEverySelectionInstanceof(BookmarkNode.class)) {
330             this.connectAction.selectionChanged(getStructuredSelection());
331             return this.connectAction.isEnabled() ? this.connectAction : null; 
332         } else if (isEverySelectionInstanceof(EntityNode.class)) {
333             this.viewTableAction.selectionChanged(getStructuredSelection());
334             return this.viewTableAction.isEnabled() ? this.viewTableAction : null; 
335         } else if (isEverySelectionInstanceof(QueryNode.class)) {
336             this.openQueryAction.selectionChanged(getStructuredSelection());
337             return this.openQueryAction.isEnabled() ? this.openQueryAction : null; 
338         } else {
339             return null;
340         }
341     }
342
343     private void createMarkerForActionsProvidedByOtherPlugins(IMenuManager mgr) {
344         mgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
345         mgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS + "-end")); //$NON-NLS-1$
346         mgr.add(new Separator());
347     }
348     public void fillActionBars(IActionBars actionBars) {
349         IToolBarManager toolBar = actionBars.getToolBarManager();
350         toolBar.add(this.newBookmarkAction);
351         
352         actionBars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, this.copyAction);
353         actionBars.setGlobalActionHandler(IWorkbenchActionConstants.DELETE, this.deleteAction);
354     }
355
356
357     /* (non-Javadoc)
358      * @see com.quantum.view.bookmark.BookmarkClipboard#setBookmark(com.quantum.model.Bookmark)
359      */
360     public void setBookmark(Bookmark bookmark) {
361         this.bookmarkClipboard = bookmark;
362     }
363
364
365     /* (non-Javadoc)
366      * @see com.quantum.view.bookmark.BookmarkClipboard#getBookmark()
367      */
368     public Bookmark getBookmark() {
369         return this.bookmarkClipboard;
370     }
371 }