Files
ts3java/ts3-client/swing/src/main/java/com/ts3client/ui/ConnectDialog.java
ericek111 17ad6cde1c Add identity management
Manage several TeamSpeak identities like the official client instead of
using one auto-generated identity file.

- IdentityStore/IdentityEntry keep each identity as a TeamSpeak-format INI
  in ~/.ts3jclient/identities, so files interchange with the TS3 client;
  supports create, import, export, rename, remove and a cancellable
  security-level search. A legacy identity.ini is migrated in on first load.
- Settings hold the default identity; bookmarks may pin their own, falling
  back to the default when unset.
- TeamspeakConnection now connects with the identity it is handed.
- Swing: Tools -> Identities manager, plus an identity drop-down in the
  connect and bookmark forms.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-13 07:42:08 +00:00

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