public static SshToolsConnectionProfile showConnectionDialog(Component parent,
SshToolsConnectionProfile profile,
SshToolsConnectionTab[] optionalTabs) {
// If no properties are provided, then use the default
if (profile == null) {
profile = new SshToolsConnectionProfile();
profile.setHost(PreferencesStore.get(
SshToolsApplication.PREF_CONNECTION_LAST_HOST, ""));
profile.setPort(PreferencesStore.getInt(
SshToolsApplication.PREF_CONNECTION_LAST_PORT, DEFAULT_PORT));
profile.setUsername(PreferencesStore.get(
SshToolsApplication.PREF_CONNECTION_LAST_USER, ""));
}
final SshToolsConnectionPanel conx = new SshToolsConnectionPanel(true);
if (optionalTabs != null) {
for (int i = 0; i < optionalTabs.length; i++) {
conx.addTab(optionalTabs[i]);
}
}
conx.setConnectionProfile(profile);
JDialog d = null;
Window w = (Window) SwingUtilities.getAncestorOfClass(Window.class,
parent);
if (w instanceof JDialog) {
d = new JDialog((JDialog) w, "Connection Profile", true);
} else if (w instanceof JFrame) {
d = new JDialog((JFrame) w, "Connection Profile", true);
} else {
d = new JDialog((JFrame) null, "Connection Profile", true);
}
final JDialog dialog = d;
class UserAction {
boolean connect;
}
final UserAction userAction = new UserAction();
// Create the bottom button panel
final JButton cancel = new JButton("Cancel");
cancel.setMnemonic('c");
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
dialog.setVisible(false);
}
});
final JButton connect = new JButton("Connect");
connect.setMnemonic('t");
connect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (conx.validateTabs()) {
userAction.connect = true;
dialog.setVisible(false);
}
}
});
dialog.getRootPane().setDefaultButton(connect);
JPanel buttonPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(6, 6, 0, 0);
gbc.weighty = 1.0;
UIUtil.jGridBagAdd(buttonPanel, connect, gbc,
GridBagConstraints.RELATIVE);
UIUtil.jGridBagAdd(buttonPanel, cancel, gbc,
GridBagConstraints.REMAINDER);
JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
southPanel.add(buttonPanel);
//
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
mainPanel.add(conx, BorderLayout.CENTER);
mainPanel.add(southPanel, BorderLayout.SOUTH);
// Show the dialog
dialog.getContentPane().setLayout(new GridLayout(1, 1));
dialog.getContentPane().add(mainPanel);
dialog.pack();
dialog.setResizable(false);
UIUtil.positionComponent(SwingConstants.CENTER, dialog);
dialog.setVisible(true);
if (!userAction.connect) {
return null;
}
conx.applyTabs();
// Make sure we didn't cancel
PreferencesStore.put(SshToolsApplication.PREF_CONNECTION_LAST_HOST,
profile.getHost());
PreferencesStore.put(SshToolsApplication.PREF_CONNECTION_LAST_USER,
profile.getUsername());
PreferencesStore.putInt(SshToolsApplication.PREF_CONNECTION_LAST_PORT,
profile.getPort());
// Return the connection properties
return profile;
}
|