From e91e81439996d97641a0a48aabbff2d19f745222 Mon Sep 17 00:00:00 2001 From: jfifield Date: Sun, 22 Feb 2009 13:42:06 +0000 Subject: [PATCH] Changing DefaultUserInfo to use passed in Shell instead of trying to get the active shell (ran into a bug where there was no active shell). --- .../sshtunnel/model/ConnectionManager.java | 8 +++---- .../sshtunnel/ui/DefaultUserInfo.java | 24 +++++++------------ .../sshtunnel/ui/SshTunnelComposite.java | 4 ++-- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/programmerplanet/sshtunnel/model/ConnectionManager.java b/src/main/java/org/programmerplanet/sshtunnel/model/ConnectionManager.java index 8a4a8f8..46af1af 100644 --- a/src/main/java/org/programmerplanet/sshtunnel/model/ConnectionManager.java +++ b/src/main/java/org/programmerplanet/sshtunnel/model/ConnectionManager.java @@ -15,7 +15,6 @@ */ package org.programmerplanet.sshtunnel.model; -import java.awt.Frame; import java.io.File; import java.io.IOException; import java.util.HashMap; @@ -24,6 +23,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.eclipse.swt.widgets.Shell; import org.programmerplanet.sshtunnel.ui.DefaultUserInfo; import com.jcraft.jsch.JSch; @@ -48,7 +48,7 @@ public static ConnectionManager getInstance() { private Map connections = new HashMap(); - public void connect(Session session, Frame parent) throws IOException { + public void connect(Session session, Shell parent) throws IOException { log.info("Connecting session: " + session); clearTunnelExceptions(session); com.jcraft.jsch.Session jschSession = connections.get(session); @@ -61,9 +61,9 @@ public void connect(Session session, Frame parent) throws IOException { } UserInfo userInfo = null; if (session.getPassword() != null && session.getPassword().trim().length() > 0) { - userInfo = new DefaultUserInfo(session.getPassword()); + userInfo = new DefaultUserInfo(parent, session.getPassword()); } else { - userInfo = new DefaultUserInfo(); + userInfo = new DefaultUserInfo(parent); } jschSession.setUserInfo(userInfo); jschSession.connect(); diff --git a/src/main/java/org/programmerplanet/sshtunnel/ui/DefaultUserInfo.java b/src/main/java/org/programmerplanet/sshtunnel/ui/DefaultUserInfo.java index 2eff995..010ad23 100644 --- a/src/main/java/org/programmerplanet/sshtunnel/ui/DefaultUserInfo.java +++ b/src/main/java/org/programmerplanet/sshtunnel/ui/DefaultUserInfo.java @@ -16,7 +16,6 @@ package org.programmerplanet.sshtunnel.ui; import org.eclipse.swt.SWT; -import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; @@ -30,16 +29,19 @@ public class DefaultUserInfo implements UserInfo { private static final int MAX_ATTEMPTS = 3; + private Shell parent; private String password; private boolean savedPassword; private int attempt = 0; - public DefaultUserInfo() { + public DefaultUserInfo(Shell parent) { + this(parent, null); } - public DefaultUserInfo(String password) { + public DefaultUserInfo(Shell parent, String password) { + this.parent = parent; this.password = password; - this.savedPassword = true; + this.savedPassword = (password != null); } public boolean promptPassword(String message) { @@ -49,8 +51,7 @@ public boolean promptPassword(String message) { } else if (savedPassword && attempt == 1) { return true; } else { - Shell shell = getShell(); - PasswordDialog dialog = new PasswordDialog(shell); + PasswordDialog dialog = new PasswordDialog(parent); dialog.setMessage(message); int result = dialog.open(); if (result == SWT.OK) { @@ -75,8 +76,7 @@ public String getPassphrase() { } public boolean promptYesNo(String str) { - Shell shell = getShell(); - MessageBox messageBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.YES | SWT.NO); + MessageBox messageBox = new MessageBox(parent, SWT.ICON_WARNING | SWT.YES | SWT.NO); messageBox.setText("Warning"); messageBox.setMessage(str); int result = messageBox.open(); @@ -84,16 +84,10 @@ public boolean promptYesNo(String str) { } public void showMessage(String message) { - Shell shell = getShell(); - MessageBox messageBox = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK); + MessageBox messageBox = new MessageBox(parent, SWT.ICON_INFORMATION | SWT.OK); messageBox.setText("Message"); messageBox.setMessage(message); messageBox.open(); } - private Shell getShell() { - // is this safe to do, or should we pass the shell in to this class instead? - return Display.getCurrent().getActiveShell(); - } - } \ No newline at end of file diff --git a/src/main/java/org/programmerplanet/sshtunnel/ui/SshTunnelComposite.java b/src/main/java/org/programmerplanet/sshtunnel/ui/SshTunnelComposite.java index 9b8d03e..b97e8dc 100644 --- a/src/main/java/org/programmerplanet/sshtunnel/ui/SshTunnelComposite.java +++ b/src/main/java/org/programmerplanet/sshtunnel/ui/SshTunnelComposite.java @@ -332,7 +332,7 @@ private void connect(Session session) { save(); if (session != null && !ConnectionManager.getInstance().isConnected(session)) { try { - ConnectionManager.getInstance().connect(session, null); + ConnectionManager.getInstance().connect(session, shell); } catch (IOException ioe) { try { ConnectionManager.getInstance().disconnect(session); @@ -361,7 +361,7 @@ private void connectAll() { for (Iterator i = configuration.getSessions().iterator(); i.hasNext();) { Session session = (Session) i.next(); if (!ConnectionManager.getInstance().isConnected(session)) { - ConnectionManager.getInstance().connect(session, null); + ConnectionManager.getInstance().connect(session, shell); } } } catch (IOException ioe) {