Files
ts3java/ts3-client/swing/src/main/java/com/ts3client/ui/IdentityChooser.java
ericek111 5db3f4d8c4 Two-column bookmark manager with connect-on-startup
Replace the modal add/edit form with a master/detail dialog: bookmark
list on the left, the selected bookmark's settings on the right. Edits
are flushed back into the bookmark when the selection changes or the
dialog closes.

Bookmarks gain a "connect on startup" flag (only one at a time, since
the client holds a single connection); MainFrame connects to it once
the window is up.

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

59 lines
2.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.ts3client.ui;
import com.ts3client.config.IdentityEntry;
import com.ts3client.config.IdentityStore;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JList;
import java.awt.Component;
/**
* Drop-down for picking the identity to connect with. Optionally offers a
* "use the default identity" entry for places where the choice may stay unset
* (bookmarks), so changing the default later applies retroactively.
*/
final class IdentityChooser extends JComboBox<Object> {
private static final String USE_DEFAULT = "‹Default identity›";
private final IdentityStore identities;
private final boolean allowDefault;
IdentityChooser(IdentityStore identities, boolean allowDefault, String selectedId) {
this.identities = identities;
this.allowDefault = allowDefault;
if (allowDefault) addItem(USE_DEFAULT);
for (IdentityEntry e : identities.all()) addItem(e);
setSelectedIdentityId(selectedId);
setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
String text = value instanceof IdentityEntry
? ((IdentityEntry) value).getName()
: String.valueOf(value);
return super.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus);
}
});
}
/** Selected identity id, or an empty string when the default is to be used. */
String getSelectedIdentityId() {
Object v = getSelectedItem();
return v instanceof IdentityEntry ? ((IdentityEntry) v).getId() : "";
}
/** Selects the identity with the given id, falling back to the default entry. */
void setSelectedIdentityId(String id) {
IdentityEntry selected = identities.byId(id);
if (selected != null) {
setSelectedItem(selected);
} else if (allowDefault) {
setSelectedItem(USE_DEFAULT);
}
}
}