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.model.Entity;
21 import com.quantum.util.versioning.VersioningHelper;
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;
48 * This class manages the list of actions for the bookmark view.
52 public class BookmarkViewActionGroup extends ActionGroup
53 implements BookmarkClipboard {
55 class SQLAction extends Action implements IMenuCreator {
57 setText(Messages.getString(BookmarkViewActionGroup.class, "sqlAction.text"));
60 public void dispose() {
62 public Menu getMenu(Control parent) {
65 public Menu getMenu(Menu parent) {
66 Menu menu = new Menu(parent);
68 * Add listener to repopulate the menu each time
69 * it is shown because the list of bookmarks may have changed.
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++) {
85 private Bookmark bookmarkClipboard;
87 private Action newBookmarkAction;
88 private Action sqlAction;
90 // bookmark node actions
91 private SelectionListenerAction connectAction;
92 private SelectionListenerAction disconnectAction;
93 private SelectionListenerAction deleteAction;
96 private SelectionListenerAction openQueryAction;
98 // Entity node actions
99 private SelectionListenerAction addToQuickListAction;
100 private SelectionListenerAction removeFromQuickListAction;
101 private SelectionListenerAction viewTableAction;
102 private SelectionListenerAction viewTableDetailsAction;
104 private SelectionListenerAction nextSequenceAction;
105 private SelectionListenerAction previousSequenceAction;
108 private SelectionListenerAction dropAction;
112 private SelectionListenerAction refreshAction;
113 private SelectionListenerAction renameAction;
115 private ExportResourcesAction exportAction;
116 private ImportResourcesAction importAction;
119 private SelectionProviderAction propertiesAction;
121 private SelectionListenerAction copyAction;
122 private SelectionListenerAction pasteAction;
123 private SelectionListenerAction deleteAllRowsAction;
125 private IViewPart viewPart;
127 public BookmarkViewActionGroup(
128 IViewPart viewPart, ISelectionProvider selectionProvider) {
129 this.viewPart = viewPart;
131 this.newBookmarkAction = new NewBookmarkAction(this.viewPart);
134 this.connectAction = new ConnectAction(this.viewPart);
135 this.disconnectAction = new DisconnectAction(this.viewPart);
137 this.sqlAction = new SQLAction();
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);
147 this.openQueryAction = new OpenQueryAction(this.viewPart);
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));
162 this.dropAction = new DropEntityAction(this.viewPart);
164 this.propertiesAction = new PropertyDialogAction(
165 this.viewPart.getSite().getShell(), selectionProvider);
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.
173 * @see org.eclipse.ui.actions.ActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager)
175 public void fillContextMenu(IMenuManager menu) {
177 menu.add(this.newBookmarkAction);
178 menu.add(new Separator());
180 if (getStructuredSelection().size() > 0 &&
181 isEverySelectionInstanceof(BookmarkNode.class)) {
183 addToMenu(menu, this.connectAction);
184 addToMenu(menu, this.disconnectAction);
187 menu.add(new Separator());
188 menu.add(this.copyAction);
189 // TODO: paste needs to change enablement whenever something is added
191 addToMenu(menu, this.pasteAction);
192 addToMenu(menu, this.deleteAction);
194 if (getStructuredSelection().size() == 1 &&
195 isEverySelectionInstanceof(BookmarkNode.class)) {
197 addToMenu(menu, this.renameAction);
200 menu.add(new Separator());
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);
208 if (this.importAction != null) {
209 this.importAction.selectionChanged(getStructuredSelection());
210 menu.add(this.importAction);
214 if (isEverySelectionInstanceof(QueryNode.class)) {
215 if (getStructuredSelection().size() == 1) {
216 addToMenu(menu, this.openQueryAction);
220 if (getStructuredSelection().size() > 0 &&
221 isEverySelectionInstanceof(EntityNode.class)) {
223 menu.add(new Separator());
225 if (getStructuredSelection().size() == 1) {
226 if (((EntityNode) getStructuredSelection().getFirstElement()).isSequence()) {
227 addToMenu(menu, this.nextSequenceAction);
228 addToMenu(menu, this.previousSequenceAction);
230 addToMenu(menu, this.viewTableAction);
231 addToMenu(menu, this.deleteAllRowsAction);
232 addToMenu(menu, this.viewTableDetailsAction);
236 addToMenu(menu, this.addToQuickListAction);
237 addToMenu(menu, this.removeFromQuickListAction);
239 if (getStructuredSelection().size() == 1) {
240 menu.add(new Separator());
241 menu.add(this.sqlAction);
245 if (getStructuredSelection().size() == 1) {
246 menu.add(new Separator());
247 addToMenu(menu, this.refreshAction);
250 createMarkerForActionsProvidedByOtherPlugins(menu);
252 if (getStructuredSelection().size() == 1) {
253 addToMenu(menu, this.propertiesAction);
257 protected void fillSQLMenu(Menu parent) {
258 if (getStructuredSelection().size() > 0 &&
259 isEverySelectionInstanceof(EntityNode.class)) {
260 addToMenu(parent, this.dropAction);
264 private void addToMenu(Menu menu, SelectionListenerAction action) {
265 action.selectionChanged(getStructuredSelection());
266 ActionContributionItem item = new ActionContributionItem(action);
270 private void addToMenu(IMenuManager menu, SelectionListenerAction action) {
271 action.selectionChanged(getStructuredSelection());
275 private void addToMenu(IMenuManager menu, SelectionProviderAction action) {
276 action.selectionChanged(getStructuredSelection());
281 private IStructuredSelection getStructuredSelection() {
282 return (IStructuredSelection) getContext().getSelection();
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());
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));
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());
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());
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;
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());
348 public void fillActionBars(IActionBars actionBars) {
349 IToolBarManager toolBar = actionBars.getToolBarManager();
350 toolBar.add(this.newBookmarkAction);
352 actionBars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, this.copyAction);
353 actionBars.setGlobalActionHandler(IWorkbenchActionConstants.DELETE, this.deleteAction);
358 * @see com.quantum.view.bookmark.BookmarkClipboard#setBookmark(com.quantum.model.Bookmark)
360 public void setBookmark(Bookmark bookmark) {
361 this.bookmarkClipboard = bookmark;
366 * @see com.quantum.view.bookmark.BookmarkClipboard#getBookmark()
368 public Bookmark getBookmark() {
369 return this.bookmarkClipboard;