Skip to content

Commit

Permalink
Changing DefaultUserInfo to use passed in Shell instead of trying to …
Browse files Browse the repository at this point in the history
…get the active shell (ran into a bug where there was no active shell).
  • Loading branch information
jfifield committed Feb 22, 2009
1 parent cfbb93f commit e91e814
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -48,7 +48,7 @@ public static ConnectionManager getInstance() {

private Map<Session, com.jcraft.jsch.Session> connections = new HashMap<Session, com.jcraft.jsch.Session>();

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);
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -75,25 +76,18 @@ 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();
return result == SWT.YES;
}

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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e91e814

Please sign in to comment.