Quantum version 2.4.1
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / driver / DeleteDriverAction.java
1 package com.quantum.view.driver;
2
3 import java.util.List;
4
5 import com.quantum.ImageStore;
6 import com.quantum.Messages;
7 import com.quantum.model.Bookmark;
8 import com.quantum.model.BookmarkCollection;
9 import com.quantum.model.JDBCDriver;
10
11 import org.eclipse.jface.dialogs.MessageDialog;
12 import org.eclipse.jface.viewers.ISelectionProvider;
13 import org.eclipse.jface.viewers.IStructuredSelection;
14 import org.eclipse.swt.widgets.Shell;
15 import org.eclipse.ui.IViewPart;
16 import org.eclipse.ui.actions.SelectionListenerAction;
17
18 /**
19  * This action deletes JDBC Drivers from the JDBC Driver collection.
20  * 
21  * @author BC Holmes
22  */
23 public class DeleteDriverAction extends SelectionListenerAction {
24
25         private final IViewPart viewPart;
26
27         /**
28          * @param text
29          */
30         protected DeleteDriverAction(IViewPart viewPart, ISelectionProvider selectionProvider) {
31                 super(Messages.getString(DeleteDriverAction.class, "text"));
32                 this.viewPart = viewPart;
33                 selectionProvider.addSelectionChangedListener(this);
34                 setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.DELETE));
35         }
36         
37         public void run() {
38                 JDBCDriver driver = getJDBCDriver();
39
40                 if (driver != null) {
41                         Bookmark[] bookmarks = BookmarkCollection.getInstance().getBookmarks();
42                         boolean found = false;
43                         for (int i = 0, length = bookmarks == null ? 0 : bookmarks.length; 
44                                         driver != null && !found && i < length; i++) {
45                                 found |= driver.equals(bookmarks[i].getJDBCDriver());
46                         }
47                         
48                         if (found) {
49                                 MessageDialog.openWarning(getShell(), 
50                                                 Messages.getString(DeleteDriverAction.class, "failed"),
51                                                 Messages.getString(DeleteDriverAction.class, "inUse"));
52                         } else {
53                                 if (MessageDialog.openConfirm(getShell(), 
54                                         Messages.getString(DeleteDriverAction.class, "confirm"),
55                                         Messages.getString(DeleteDriverAction.class, "confirmText", 
56                                                         new Object[] { driver.getName() }))) {
57                 
58                                         if (!BookmarkCollection.getInstance().removeDriver(driver)) {
59                                                 MessageDialog.openWarning(getShell(), 
60                                                                 Messages.getString(DeleteDriverAction.class, "failed"),
61                                                                 Messages.getString(DeleteDriverAction.class, "cant"));
62                                         } 
63                                 }
64                         }
65                 }
66         }
67         
68         /**
69          * @return
70          */
71         private Shell getShell() {
72                 return this.viewPart.getViewSite().getShell();
73         }
74
75         /**
76          * @return
77          */
78         private JDBCDriver getJDBCDriver() {
79                 List list = getSelectedNonResources();
80                 return list == null || list.size() == 0 ? null : (JDBCDriver) list.get(0);
81         }
82
83         protected boolean updateSelection(IStructuredSelection selection) {
84                 return selection != null && !selection.isEmpty();
85         }
86 }