Initial import of the phpmanual.htmlparser plugin
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / bookmark / DeleteAction.java
1 package com.quantum.view.bookmark;
2
3 import java.util.Iterator;
4 import java.util.List;
5
6 import com.quantum.ImageStore;
7 import com.quantum.Messages;
8 import com.quantum.model.BookmarkCollection;
9
10 import org.eclipse.jface.dialogs.MessageDialog;
11 import org.eclipse.jface.viewers.ISelectionProvider;
12 import org.eclipse.jface.viewers.IStructuredSelection;
13 import org.eclipse.ui.IViewPart;
14 import org.eclipse.ui.actions.SelectionListenerAction;
15
16 /**
17  * @author root
18  *
19  */
20 public class DeleteAction extends SelectionListenerAction {
21     
22     private IViewPart view;
23     
24         public DeleteAction(IViewPart view, ISelectionProvider selectionProvider) {
25         super(Messages.getString(DeleteAction.class.getName() + ".text"));
26         setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.DELETE));
27                 this.view = view;
28                 selectionProvider.addSelectionChangedListener(this);
29         }
30
31         public void run() {
32                 if (isEachSelectionInstanceOf(getSelectedNonResources().iterator(), BookmarkNode.class)) {
33                         deleteBookmarks();
34                 } else if (isEachSelectionInstanceOf(getSelectedNonResources().iterator(), QueryNode.class)) {
35                         deleteQueries();
36                 }
37         }
38
39         /**
40          * 
41          */
42         private void deleteQueries() {
43                 List nodes = getSelectedNonResources();
44                 for (Iterator i = nodes.iterator(); i.hasNext();) {
45                     QueryNode queryNode = (QueryNode) i.next();
46                     if (queryNode != null) {
47                         queryNode.getBookmark().removeQuery(queryNode.getQuery());
48                     }
49                 }
50         }
51
52         /**
53          * 
54          */
55         private void deleteBookmarks() {
56                 List bookmarkNodes = getSelectedNonResources();
57                 boolean flag = MessageDialog.openConfirm(view.getSite().getShell(), 
58                                 Messages.getString(getClass(), "confirmTitle"), 
59                                 Messages.getString(getClass(), "confirmText")); 
60
61                 for (Iterator i = bookmarkNodes.iterator(); flag && i.hasNext();) {
62                     BookmarkNode bookmarkNode = (BookmarkNode) i.next();
63                     if (bookmarkNode != null) {
64                         BookmarkCollection.getInstance().removeBookmark(bookmarkNode.getBookmark());
65                     }
66                 }
67         }
68
69         public boolean updateSelection(IStructuredSelection selection) {
70         return isEachSelectionInstanceOf(selection.iterator(), BookmarkNode.class) || 
71                         isEachSelectionInstanceOf(selection.iterator(), QueryNode.class);
72         }
73
74         /**
75          * @param selection
76          * @param enabled
77          * @param selectionClass
78          * @return
79          */
80         private boolean isEachSelectionInstanceOf(Iterator selection, Class selectionClass) {
81                 boolean result = true;
82                 for (Iterator i = selection; result && i.hasNext(); ) {
83             result &= (selectionClass.isInstance(i.next()));
84         }
85                 return result;
86         }
87
88 }