1 package com.quantum.actions;
3 import java.sql.Connection;
4 import java.util.Iterator;
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;
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;
19 public class ConnectAction extends SelectionListenerAction {
23 public ConnectAction(IViewPart view) {
24 super(Messages.getString(ConnectAction.class.getName() + ".text"));
27 ImageStore.getImageDescriptor(ImageStore.CONNECT));
30 private IViewPart view;
31 private ConnectionUtil connectionUtil = new ConnectionUtil();
34 * @see IActionDelegate#run(IAction)
36 public void run(IAction action) {
41 * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
43 public void init(IViewPart view) {
48 * @see org.eclipse.jface.action.IAction#run()
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) {
62 lastBookmarkName = bookmark.getName();
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)}));
81 private String getMessage(String key, Object[] arguments) {
82 return Messages.getString(getClass().getName() + "." + key, arguments);
86 * Updates the message shown in the status line.
88 * @param message the message to display
90 protected void updateStatusLine(String message) {
91 this.view.getViewSite().getActionBars().getStatusLineManager().setMessage(message);
94 protected Shell getShell() {
95 return this.view.getSite().getShell();
101 protected boolean updateSelection(IStructuredSelection selection) {
102 boolean enabled = super.updateSelection(selection);
103 for (Iterator i = selection.iterator();
104 enabled && i.hasNext();
106 Object object = i.next();
107 if (object instanceof BookmarkNode) {
108 BookmarkNode node = (BookmarkNode) object;
109 enabled &= !node.getBookmark().isConnected();