1 package com.quantum.view.bookmark;
3 import java.util.Iterator;
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;
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;
46 * This class manages the list of actions for the bookmark view.
50 public class BookmarkViewActionGroup extends ActionGroup
51 implements BookmarkClipboard {
53 class SQLAction extends Action implements IMenuCreator {
55 setText(Messages.getString(BookmarkViewActionGroup.class, "sqlAction.text"));
58 public void dispose() {
60 public Menu getMenu(Control parent) {
63 public Menu getMenu(Menu parent) {
64 Menu menu = new Menu(parent);
66 * Add listener to repopulate the menu each time
67 * it is shown because the list of bookmarks may have changed.
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++) {
83 private Bookmark bookmarkClipboard;
85 private Action newBookmarkAction;
86 private Action sqlAction;
88 // bookmark node actions
89 private SelectionListenerAction connectAction;
90 private SelectionListenerAction disconnectAction;
91 private SelectionListenerAction deleteAction;
94 private SelectionListenerAction openQueryAction;
96 // Entity node actions
97 private SelectionListenerAction addToQuickListAction;
98 private SelectionListenerAction removeFromQuickListAction;
99 private SelectionListenerAction viewTableAction;
100 private SelectionListenerAction viewTableDetailsAction;
102 private SelectionListenerAction nextSequenceAction;
103 private SelectionListenerAction previousSequenceAction;
106 private SelectionListenerAction dropAction;
110 private SelectionListenerAction refreshAction;
111 private SelectionListenerAction renameAction;
113 private ExportResourcesAction exportAction;
116 private SelectionProviderAction propertiesAction;
118 private SelectionListenerAction copyAction;
119 private SelectionListenerAction pasteAction;
120 private SelectionListenerAction deleteAllRowsAction;
122 private IViewPart viewPart;
124 public BookmarkViewActionGroup(
125 IViewPart viewPart, ISelectionProvider selectionProvider) {
126 this.viewPart = viewPart;
128 this.newBookmarkAction = new NewBookmarkAction(this.viewPart);
131 this.connectAction = new ConnectAction(this.viewPart);
132 this.disconnectAction = new DisconnectAction(this.viewPart);
134 this.sqlAction = new SQLAction();
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);
144 this.openQueryAction = new OpenQueryAction(this.viewPart);
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));
156 this.dropAction = new DropEntityAction(this.viewPart);
158 this.propertiesAction = new PropertyDialogAction(
159 this.viewPart.getSite().getShell(), selectionProvider);
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.
167 * @see org.eclipse.ui.actions.ActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager)
169 public void fillContextMenu(IMenuManager menu) {
171 menu.add(this.newBookmarkAction);
172 menu.add(new Separator());
174 if (getStructuredSelection().size() > 0 &&
175 isEverySelectionInstanceof(BookmarkNode.class)) {
177 addToMenu(menu, this.connectAction);
178 addToMenu(menu, this.disconnectAction);
181 menu.add(new Separator());
182 menu.add(this.copyAction);
183 // TODO: paste needs to change enablement whenever something is added
185 addToMenu(menu, this.pasteAction);
186 addToMenu(menu, this.deleteAction);
188 if (getStructuredSelection().size() == 1 &&
189 isEverySelectionInstanceof(BookmarkNode.class)) {
191 addToMenu(menu, this.renameAction);
194 menu.add(new Separator());
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);
203 if (getStructuredSelection().size() == 1 &&
204 isEverySelectionInstanceof(BookmarkNode.class)) {
207 if (isEverySelectionInstanceof(QueryNode.class)) {
208 if (getStructuredSelection().size() == 1) {
209 addToMenu(menu, this.openQueryAction);
213 if (getStructuredSelection().size() > 0 &&
214 isEverySelectionInstanceof(EntityNode.class)) {
216 menu.add(new Separator());
218 if (getStructuredSelection().size() == 1) {
219 if (((EntityNode) getStructuredSelection().getFirstElement()).isSequence()) {
220 addToMenu(menu, this.nextSequenceAction);
221 addToMenu(menu, this.previousSequenceAction);
223 addToMenu(menu, this.viewTableAction);
224 addToMenu(menu, this.deleteAllRowsAction);
225 addToMenu(menu, this.viewTableDetailsAction);
229 addToMenu(menu, this.addToQuickListAction);
230 addToMenu(menu, this.removeFromQuickListAction);
232 if (getStructuredSelection().size() == 1) {
233 menu.add(new Separator());
234 menu.add(this.sqlAction);
238 if (getStructuredSelection().size() == 1) {
239 menu.add(new Separator());
240 addToMenu(menu, this.refreshAction);
243 createMarkerForActionsProvidedByOtherPlugins(menu);
245 if (getStructuredSelection().size() == 1) {
246 addToMenu(menu, this.propertiesAction);
250 protected void fillSQLMenu(Menu parent) {
251 if (getStructuredSelection().size() > 0 &&
252 isEverySelectionInstanceof(EntityNode.class)) {
253 addToMenu(parent, this.dropAction);
257 private void addToMenu(Menu menu, SelectionListenerAction action) {
258 action.selectionChanged(getStructuredSelection());
259 ActionContributionItem item = new ActionContributionItem(action);
263 private void addToMenu(IMenuManager menu, SelectionListenerAction action) {
264 action.selectionChanged(getStructuredSelection());
268 private void addToMenu(IMenuManager menu, SelectionProviderAction action) {
269 action.selectionChanged(getStructuredSelection());
274 private IStructuredSelection getStructuredSelection() {
275 return (IStructuredSelection) getContext().getSelection();
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());
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;
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());
309 public void fillActionBars(IActionBars actionBars) {
310 IToolBarManager toolBar = actionBars.getToolBarManager();
311 toolBar.add(this.newBookmarkAction);
313 actionBars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, this.copyAction);
314 actionBars.setGlobalActionHandler(IWorkbenchActionConstants.DELETE, this.deleteAction);
319 * @see com.quantum.view.bookmark.BookmarkClipboard#setBookmark(com.quantum.model.Bookmark)
321 public void setBookmark(Bookmark bookmark) {
322 this.bookmarkClipboard = bookmark;
327 * @see com.quantum.view.bookmark.BookmarkClipboard#getBookmark()
329 public Bookmark getBookmark() {
330 return this.bookmarkClipboard;