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