1 package com.quantum.actions;
3 import java.sql.Connection;
4 import java.util.Iterator;
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;
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;
18 public class ConnectAction extends SelectionListenerAction {
22 public ConnectAction(IViewPart view) {
23 super(Messages.getString(ConnectAction.class.getName() + ".text"));
26 QuantumPlugin.getImageDescriptor("connect.gif")); //$NON-NLS-1$
29 private IViewPart view;
30 private ConnectionUtil connectionUtil = new ConnectionUtil();
33 * @see IActionDelegate#run(IAction)
35 public void run(IAction action) {
40 * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
42 public void init(IViewPart view) {
47 * @see org.eclipse.jface.action.IAction#run()
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) {
61 lastBookmarkName = bookmark.getName();
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)}));
80 private String getMessage(String key, Object[] arguments) {
81 return Messages.getString(getClass().getName() + "." + key, arguments);
85 * Updates the message shown in the status line.
87 * @param message the message to display
89 protected void updateStatusLine(String message) {
90 this.view.getViewSite().getActionBars().getStatusLineManager().setMessage(message);
93 protected Shell getShell() {
94 return this.view.getSite().getShell();
100 protected boolean updateSelection(IStructuredSelection selection) {
101 boolean enabled = super.updateSelection(selection);
102 for (Iterator i = selection.iterator();
103 enabled && i.hasNext();
105 Object object = i.next();
106 if (object instanceof BookmarkNode) {
107 BookmarkNode node = (BookmarkNode) object;
108 enabled &= !node.getBookmark().isConnected();