Quantum version 2.4.2
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / actions / DisconnectAction.java
1 package com.quantum.actions;
2
3 import java.sql.SQLException;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Vector;
7
8 import com.quantum.ImageStore;
9 import com.quantum.Messages;
10 import com.quantum.model.Bookmark;
11 import com.quantum.ui.dialog.SQLExceptionDialog;
12 import com.quantum.view.bookmark.BookmarkNode;
13
14 import org.eclipse.jface.viewers.IStructuredSelection;
15 import org.eclipse.ui.IViewPart;
16 import org.eclipse.ui.actions.SelectionListenerAction;
17
18 /**
19  * Disconnects from the database
20  * 
21  * @author root
22  */
23 public class DisconnectAction extends SelectionListenerAction {
24     private IViewPart view;
25     private List selections = new Vector();
26
27     /**
28      * @param text
29      */
30     public DisconnectAction(IViewPart view) {
31         super(Messages.getString(DisconnectAction.class.getName() + ".text"));
32         this.view = view;
33         setImageDescriptor(
34                         ImageStore.getImageDescriptor(ImageStore.DISCONNECT));
35     }
36
37
38     public void run() {
39         for (Iterator i = this.selections.iterator(); i.hasNext(); ) {
40             Bookmark bookmark = (Bookmark) i.next();
41             try {
42                 bookmark.disconnect();
43             } catch (SQLException e) {
44                 SQLExceptionDialog.openException(
45                                 this.view.getViewSite().getShell(), bookmark, e);
46             }
47         }
48         updateStatusLine(getMessage("message"));
49     }
50
51     private String getMessage(String key) {
52         return Messages.getString(getClass().getName() + "." + key);
53     }
54     
55     /**
56      * Updates the message shown in the status line.
57      *
58      * @param message the message to display
59      */
60     protected void updateStatusLine(String message) {
61         this.view.getViewSite().getActionBars().getStatusLineManager().setMessage(message);
62     }
63     
64
65     /**
66      * 
67      */
68     public boolean updateSelection(IStructuredSelection selection) {
69         boolean enabled = super.updateSelection(selection);
70         this.selections.clear();
71         for (Iterator i = selection.iterator(); enabled && i.hasNext(); ) {
72             Object object = i.next();
73             if (object instanceof BookmarkNode) {
74                 BookmarkNode node = (BookmarkNode) object;
75                 this.selections.add(node.getBookmark());
76                 enabled &= node.getBookmark().isConnected();
77             } else {
78                 enabled = false;
79             }
80         }
81         return enabled;
82     }
83 }