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