synchronized from quantum plugin
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / actions / ConnectAction.java
1 package com.quantum.actions;
2
3 import java.sql.Connection;
4 import java.util.Iterator;
5
6 import com.quantum.Messages;
7 import com.quantum.QuantumPlugin;
8 import com.quantum.model.Bookmark;
9 import com.quantum.util.connection.ConnectionUtil;
10 import com.quantum.view.bookmark.BookmarkNode;
11
12 import org.eclipse.jface.action.IAction;
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 public class ConnectAction extends SelectionListenerAction {
19         /**
20      * @param text
21      */
22     public ConnectAction(IViewPart view) {
23         super(Messages.getString(ConnectAction.class.getName() + ".text"));
24         this.view = view;
25         setImageDescriptor(
26             QuantumPlugin.getImageDescriptor("connect.gif")); //$NON-NLS-1$
27     }
28
29     private IViewPart view;
30     private ConnectionUtil connectionUtil = new ConnectionUtil();
31
32         /**
33          * @see IActionDelegate#run(IAction)
34          */
35         public void run(IAction action) {
36                 run();
37         }
38
39         /**
40          * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
41          */
42         public void init(IViewPart view) {
43                 this.view = view;
44         }
45
46         /**
47          * @see org.eclipse.jface.action.IAction#run()
48          */
49         public void run() {
50         
51         int bookmarks = 0;
52         int errors = 0;
53         String lastBookmarkName = null;
54         for (Iterator i = getSelectedNonResources().iterator(); i.hasNext();) {
55             Bookmark bookmark = ((BookmarkNode) i.next()).getBookmark();
56             Connection connection = this.connectionUtil.connect(bookmark, getShell());
57             if (connection == null) {
58                 errors++;
59             }
60             bookmarks++;
61             lastBookmarkName = bookmark.getName();
62         }
63         
64         if (bookmarks == 1 && errors == 0) {
65             updateStatusLine(getMessage("singleSuccessMessage", 
66                 new Object[] {lastBookmarkName}));
67         } else if (bookmarks == 1 && errors == 1) {
68             updateStatusLine(getMessage("singleFailureMessage", 
69                 new Object[] {lastBookmarkName}));
70         } else if (bookmarks > 1 && errors == 0) {
71             updateStatusLine(getMessage("multiSuccessMessage", 
72                 new Object[] {String.valueOf(bookmarks)}));
73         } else if (bookmarks > 1 && errors > 0) {
74             updateStatusLine(getMessage("multiFailureMessage", 
75             new Object[] {String.valueOf(bookmarks - errors), 
76                 String.valueOf(errors)}));
77         }
78         }
79     
80     private String getMessage(String key, Object[] arguments) {
81         return Messages.getString(getClass().getName() + "." + key, arguments);
82     }
83     
84     /**
85      * Updates the message shown in the status line.
86      *
87      * @param message the message to display
88      */
89     protected void updateStatusLine(String message) {
90         this.view.getViewSite().getActionBars().getStatusLineManager().setMessage(message);
91     }
92     
93     protected Shell getShell() {
94         return this.view.getSite().getShell();
95     }
96
97     /**
98      * 
99      */
100     protected boolean updateSelection(IStructuredSelection selection) {
101         boolean enabled = super.updateSelection(selection);
102         for (Iterator i = selection.iterator();
103             enabled && i.hasNext();
104             ) {
105             Object object = i.next();
106             if (object instanceof BookmarkNode) {
107                 BookmarkNode node = (BookmarkNode) object;
108                 enabled &= !node.getBookmark().isConnected();
109             } else {
110                 enabled = false;
111             }
112         }
113         return enabled;
114     }
115 }