Only the option-pane prompts closed on Escape, because Swing installs that binding on the option pane itself. Dialogs.closeOnEscape puts the same window-wide binding on a dialog's root pane, routed to whatever that dialog does when its Close or Cancel button is pressed — reverting the live-previewing settings, or flushing the bookmark being edited. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
130 lines
4.1 KiB
Java
130 lines
4.1 KiB
Java
package com.ts3client.ui;
|
|
|
|
import com.ts3client.config.IdentityStore;
|
|
import com.ts3client.config.Settings;
|
|
|
|
import javax.swing.BorderFactory;
|
|
import javax.swing.JButton;
|
|
import javax.swing.JDialog;
|
|
import javax.swing.JLabel;
|
|
import javax.swing.JPanel;
|
|
import javax.swing.JPasswordField;
|
|
import javax.swing.JTextField;
|
|
import java.awt.BorderLayout;
|
|
import java.awt.Dimension;
|
|
import java.awt.Frame;
|
|
import java.awt.GridBagConstraints;
|
|
import java.awt.GridBagLayout;
|
|
import java.awt.Insets;
|
|
|
|
/** Modal "Connect to Server" dialog. */
|
|
public final class ConnectDialog extends JDialog {
|
|
|
|
private final JTextField addressField;
|
|
private final JTextField portField;
|
|
private final JTextField nickField;
|
|
private final JPasswordField passwordField;
|
|
private final IdentityChooser identityChooser;
|
|
|
|
private boolean confirmed;
|
|
|
|
public ConnectDialog(Frame owner, Settings settings, IdentityStore identities) {
|
|
super(owner, "Connect to Server", true);
|
|
|
|
String addr = settings.lastAddress;
|
|
int port = 9987;
|
|
int colon = addr.lastIndexOf(':');
|
|
if (colon > 0) {
|
|
try {
|
|
port = Integer.parseInt(addr.substring(colon + 1));
|
|
addr = addr.substring(0, colon);
|
|
} catch (NumberFormatException ignored) {
|
|
}
|
|
}
|
|
|
|
addressField = new JTextField(addr, 18);
|
|
portField = new JTextField(Integer.toString(port), 6);
|
|
nickField = new JTextField(settings.nickname, 18);
|
|
passwordField = new JPasswordField(settings.serverPassword, 18);
|
|
identityChooser = new IdentityChooser(identities, false, settings.defaultIdentityId);
|
|
|
|
JPanel form = new JPanel(new GridBagLayout());
|
|
form.setBorder(BorderFactory.createEmptyBorder(12, 12, 8, 12));
|
|
GridBagConstraints c = new GridBagConstraints();
|
|
c.insets = new Insets(4, 4, 4, 4);
|
|
c.anchor = GridBagConstraints.WEST;
|
|
c.fill = GridBagConstraints.HORIZONTAL;
|
|
|
|
int row = 0;
|
|
add(form, c, row++, "Server address:", addressField);
|
|
add(form, c, row++, "Port:", portField);
|
|
add(form, c, row++, "Nickname:", nickField);
|
|
add(form, c, row++, "Password (optional):", passwordField);
|
|
add(form, c, row++, "Identity:", identityChooser);
|
|
|
|
JPanel buttons = new JPanel(new BorderLayout());
|
|
JPanel right = new JPanel();
|
|
JButton connect = new JButton("Connect");
|
|
JButton cancel = new JButton("Cancel");
|
|
connect.addActionListener(e -> {
|
|
confirmed = true;
|
|
dispose();
|
|
});
|
|
cancel.addActionListener(e -> dispose());
|
|
right.add(connect);
|
|
right.add(cancel);
|
|
buttons.add(right, BorderLayout.EAST);
|
|
getRootPane().setDefaultButton(connect);
|
|
|
|
getContentPane().setLayout(new BorderLayout());
|
|
getContentPane().add(form, BorderLayout.CENTER);
|
|
getContentPane().add(buttons, BorderLayout.SOUTH);
|
|
|
|
Dialogs.closeOnEscape(this);
|
|
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
|
pack();
|
|
setMinimumSize(new Dimension(340, getHeight()));
|
|
setLocationRelativeTo(owner);
|
|
}
|
|
|
|
private void add(JPanel form, GridBagConstraints c, int row, String label, java.awt.Component field) {
|
|
c.gridx = 0;
|
|
c.gridy = row;
|
|
c.weightx = 0;
|
|
form.add(new JLabel(label), c);
|
|
c.gridx = 1;
|
|
c.weightx = 1;
|
|
form.add(field, c);
|
|
}
|
|
|
|
public boolean isConfirmed() {
|
|
return confirmed;
|
|
}
|
|
|
|
public String getAddress() {
|
|
return addressField.getText().trim();
|
|
}
|
|
|
|
public int getPort() {
|
|
try {
|
|
return Integer.parseInt(portField.getText().trim());
|
|
} catch (NumberFormatException e) {
|
|
return 9987;
|
|
}
|
|
}
|
|
|
|
public String getNickname() {
|
|
String n = nickField.getText().trim();
|
|
return n.isEmpty() ? "TS3J User" : n;
|
|
}
|
|
|
|
public String getPassword() {
|
|
return new String(passwordField.getPassword());
|
|
}
|
|
|
|
/** Chosen identity id, or an empty string to fall back to the default identity. */
|
|
public String getIdentityId() {
|
|
return identityChooser.getSelectedIdentityId();
|
|
}
|
|
}
|