3 new apache icons; thanks to Anderson Forteleza
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / bookmark / BookmarkViewActionGroup.java
index 09f636b..ad120a6 100644 (file)
@@ -2,11 +2,11 @@ package com.quantum.view.bookmark;
 
 import java.util.Iterator;
 
-import com.quantum.QuantumPlugin;
+import com.quantum.ImageStore;
+import com.quantum.Messages;
 import com.quantum.actions.AddToQuickListAction;
 import com.quantum.actions.ConnectAction;
 import com.quantum.actions.DeleteAllRowsAction;
-import com.quantum.actions.DeleteBookmarkAction;
 import com.quantum.actions.DisconnectAction;
 import com.quantum.actions.NewBookmarkAction;
 import com.quantum.actions.NextSequenceAction;
@@ -17,37 +17,80 @@ import com.quantum.actions.RemoveFromQuickListAction;
 import com.quantum.actions.ViewTableAction;
 import com.quantum.actions.ViewTableDetailsAction;
 import com.quantum.model.Bookmark;
+import com.quantum.model.Entity;
+import com.quantum.util.versioning.VersioningHelper;
 
 import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.ActionContributionItem;
 import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.action.IMenuCreator;
 import org.eclipse.jface.action.IMenuManager;
 import org.eclipse.jface.action.IToolBarManager;
 import org.eclipse.jface.action.Separator;
 import org.eclipse.jface.viewers.ISelectionProvider;
 import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.swt.events.MenuAdapter;
+import org.eclipse.swt.events.MenuEvent;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Menu;
+import org.eclipse.swt.widgets.MenuItem;
 import org.eclipse.ui.IActionBars;
 import org.eclipse.ui.IViewPart;
 import org.eclipse.ui.IWorkbenchActionConstants;
 import org.eclipse.ui.actions.ActionGroup;
 import org.eclipse.ui.actions.ExportResourcesAction;
+import org.eclipse.ui.actions.ImportResourcesAction;
 import org.eclipse.ui.actions.SelectionListenerAction;
 import org.eclipse.ui.actions.SelectionProviderAction;
 import org.eclipse.ui.dialogs.PropertyDialogAction;
 
 /**
- * @author BC
+ * This class manages the list of actions for the bookmark view.
+ * 
+ * @author BC Holmes
  */
 public class BookmarkViewActionGroup extends ActionGroup 
     implements BookmarkClipboard {
+       
+       class SQLAction extends Action implements IMenuCreator {
+               public SQLAction() {
+                       setText(Messages.getString(BookmarkViewActionGroup.class, "sqlAction.text"));
+                       setMenuCreator(this);
+               }
+               public void dispose() {
+               }
+               public Menu getMenu(Control parent) {
+                       return null;
+               }
+               public Menu getMenu(Menu parent) {
+                       Menu menu = new Menu(parent);
+                       /**
+                        * Add listener to repopulate the menu each time
+                        * it is shown because the list of bookmarks may have changed.
+                        */
+                       menu.addMenuListener(new MenuAdapter() {
+                               public void menuShown(MenuEvent event) {
+                                       Menu menu = (Menu) event.widget;
+                                       MenuItem[] items = menu.getItems();
+                                       for (int i=0; i < items.length; i++) {
+                                               items[i].dispose();
+                                       }
+                                       fillSQLMenu(menu);
+                               }
+                       });
+                       return menu;
+               }
+       }
         
     private Bookmark bookmarkClipboard;
     
     private Action newBookmarkAction;
+    private Action sqlAction;
     
     // bookmark node actions
     private SelectionListenerAction connectAction;
     private SelectionListenerAction disconnectAction;
-    private SelectionListenerAction deleteBookmarkAction;
+    private SelectionListenerAction deleteAction;
 
     // Query node actions
     private SelectionListenerAction openQueryAction;
@@ -61,12 +104,16 @@ public class BookmarkViewActionGroup extends ActionGroup
     private SelectionListenerAction nextSequenceAction;
     private SelectionListenerAction previousSequenceAction;
     
+
+    private SelectionListenerAction dropAction;
+    
     
     // other actions
     private SelectionListenerAction refreshAction;
     private SelectionListenerAction renameAction;
 
     private ExportResourcesAction exportAction;
+    private ImportResourcesAction importAction;
 
     
     private SelectionProviderAction propertiesAction;
@@ -86,7 +133,8 @@ public class BookmarkViewActionGroup extends ActionGroup
         // bookmark actions
         this.connectAction = new ConnectAction(this.viewPart);
         this.disconnectAction = new DisconnectAction(this.viewPart);
-        this.deleteBookmarkAction = new DeleteBookmarkAction(this.viewPart);
+        
+        this.sqlAction = new SQLAction();
 
         // entity actions
         this.previousSequenceAction = new PrevSequenceAction(this.viewPart);
@@ -103,9 +151,15 @@ public class BookmarkViewActionGroup extends ActionGroup
         this.renameAction = new RenameAction(this.viewPart);
         this.copyAction = new CopyAction(this.viewPart, this, selectionProvider);
         this.pasteAction = new PasteAction(this.viewPart, this, selectionProvider);
-        this.exportAction = new ExportResourcesAction(
-            this.viewPart.getViewSite().getWorkbenchWindow());
-        this.exportAction.setImageDescriptor(QuantumPlugin.getImageDescriptor("export.gif"));
+        this.deleteAction = new DeleteAction(this.viewPart, selectionProvider);
+        this.exportAction = VersioningHelper.createExportResourcesAction(
+                       this.viewPart.getViewSite().getWorkbenchWindow());
+        this.exportAction.setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.EXPORT));
+        this.importAction = VersioningHelper.createImportResourcesAction(
+                       this.viewPart.getViewSite().getWorkbenchWindow());
+        this.importAction.setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.IMPORT));
+        
+        this.dropAction = new DropEntityAction(this.viewPart);
         
         this.propertiesAction = new PropertyDialogAction(
             this.viewPart.getSite().getShell(), selectionProvider);
@@ -113,6 +167,9 @@ public class BookmarkViewActionGroup extends ActionGroup
 
     
     /**
+     * Add all the appropriate actions to the popup menu. This method is 
+     * called whenever someone right-clicks on an object in the bookmark view.
+     * 
      * @see org.eclipse.ui.actions.ActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager)
      */
     public void fillContextMenu(IMenuManager menu) {
@@ -132,7 +189,7 @@ public class BookmarkViewActionGroup extends ActionGroup
         // TODO: paste needs to change enablement whenever something is added
         //       to the clipboard
         addToMenu(menu, this.pasteAction);
-        addToMenu(menu, this.deleteBookmarkAction);
+        addToMenu(menu, this.deleteAction);
 
         if (getStructuredSelection().size() == 1 && 
             isEverySelectionInstanceof(BookmarkNode.class)) {
@@ -142,15 +199,18 @@ public class BookmarkViewActionGroup extends ActionGroup
 
         menu.add(new Separator());
 
-        // NOTE: In Eclipse 3.0.0 M6, Export is no longer a sub-class of 
+        // NOTE: In Eclipse 3.0, Export is no longer a sub-class of 
         //       SelectionListenerAction.
-        this.exportAction.selectionChanged(getStructuredSelection());
-        menu.add(this.exportAction);
-
-        if (getStructuredSelection().size() == 1 && 
-            isEverySelectionInstanceof(BookmarkNode.class)) {
+        if (this.exportAction != null) {
+               this.exportAction.selectionChanged(getStructuredSelection());
+               menu.add(this.exportAction);
+        }
+        if (this.importAction != null) {
+               this.importAction.selectionChanged(getStructuredSelection());
+                       menu.add(this.importAction);
         }
 
+        
         if (isEverySelectionInstanceof(QueryNode.class)) {
             if (getStructuredSelection().size() == 1) {
                 addToMenu(menu, this.openQueryAction);
@@ -175,6 +235,11 @@ public class BookmarkViewActionGroup extends ActionGroup
             
             addToMenu(menu, this.addToQuickListAction);
             addToMenu(menu, this.removeFromQuickListAction);
+            
+            if (getStructuredSelection().size() == 1) {
+               menu.add(new Separator());
+               menu.add(this.sqlAction);
+            }
         }
         
         if (getStructuredSelection().size() == 1) {
@@ -187,7 +252,19 @@ public class BookmarkViewActionGroup extends ActionGroup
         if (getStructuredSelection().size() == 1) {
             addToMenu(menu, this.propertiesAction);
         }
-        
+    }
+    
+    protected void fillSQLMenu(Menu parent) {
+        if (getStructuredSelection().size() > 0 && 
+                isEverySelectionInstanceof(EntityNode.class)) {
+               addToMenu(parent, this.dropAction);
+        }
+    }
+
+    private void addToMenu(Menu menu, SelectionListenerAction action) {
+        action.selectionChanged(getStructuredSelection());
+               ActionContributionItem item = new ActionContributionItem(action);
+               item.fill(menu, -1);
     }
     
     private void addToMenu(IMenuManager menu, SelectionListenerAction action) {
@@ -215,6 +292,38 @@ public class BookmarkViewActionGroup extends ActionGroup
         
         return result;
     }
+    private boolean isEverySelectedGroupOfType(String type) {
+        boolean result = true;
+        IStructuredSelection selection = getStructuredSelection();
+        for (Iterator i = selection.iterator(); result && i.hasNext(); ) {
+            Object object = i.next();
+            result &= (( object instanceof GroupNode ) && 
+                               ((GroupNode)object).getType().equals(type));
+        }
+        
+        return result;
+    }
+    private boolean isEverySelectedEntityTable() {
+        boolean result = true;
+        IStructuredSelection selection = getStructuredSelection();
+        for (Iterator i = selection.iterator(); result && i.hasNext(); ) {
+            Object object = i.next();
+            result &= (( object instanceof EntityNode ) && 
+                               ((EntityNode)object).isTable());
+        }
+        
+        return result;
+    }
+    private boolean isEverySelectedEntityView() {
+        boolean result = true;
+        IStructuredSelection selection = getStructuredSelection();
+        for (Iterator i = selection.iterator(); result && i.hasNext(); ) {
+            Object object = i.next();
+            result &= (( object instanceof EntityNode ) && 
+                               ((EntityNode)object).isView());
+        }
+        return result;
+    }
     
     public IAction getOpenAction() {
         if (isEverySelectionInstanceof(BookmarkNode.class)) {
@@ -241,6 +350,7 @@ public class BookmarkViewActionGroup extends ActionGroup
         toolBar.add(this.newBookmarkAction);
         
         actionBars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, this.copyAction);
+        actionBars.setGlobalActionHandler(IWorkbenchActionConstants.DELETE, this.deleteAction);
     }