Chat lines are routed once, in core

session.ChatFeed decides which conversation a message, history, server
event or notice goes to and renders it; frontends get the lines through
chat.ChatSink and key conversations by chat.ChatKey. ServerSession.Listener
no longer passes on raw chat events. Swing now labels our own messages
with our nickname on the server and links our name to us in private chats.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-12 23:37:33 +00:00
co-authored by Claude Opus 5.5
parent 53585994a8
commit a2a12804ad
19 changed files with 447 additions and 372 deletions
@@ -1,6 +1,6 @@
package eu.lixko.ts3j.client.ui;
import eu.lixko.ts3j.client.chat.ChatScope;
import eu.lixko.ts3j.client.chat.ChatKey;
import eu.lixko.ts3j.client.myts.ChatAttachment;
import eu.lixko.ts3j.client.myts.ChatAttachments;
import eu.lixko.ts3j.client.myts.MyTsException;
@@ -45,7 +45,7 @@ final class ChatAttachmentActions implements ChatPanel.AttachmentHandler {
}
@Override
public void choose(ChatScope scope, int clientId) {
public void choose(ChatKey key) {
if (!signedIn()) return;
JFileChooser chooser = new JFileChooser(lastDirectory);
chooser.setMultiSelectionEnabled(true);
@@ -53,11 +53,11 @@ final class ChatAttachmentActions implements ChatPanel.AttachmentHandler {
if (chooser.showOpenDialog(host) != JFileChooser.APPROVE_OPTION) return;
File[] files = chooser.getSelectedFiles();
if (files.length > 0) lastDirectory = files[0].getParentFile();
share(scope, clientId, List.of(files));
share(key, List.of(files));
}
@Override
public void share(ChatScope scope, int clientId, List<File> files) {
public void share(ChatKey key, List<File> files) {
if (!signedIn()) return;
for (File file : files) {
if (!file.isFile()) continue;
@@ -66,7 +66,7 @@ final class ChatAttachmentActions implements ChatPanel.AttachmentHandler {
continue;
}
try {
session.sendAttachment(scope, clientId, host.attachments(),
session.sendAttachment(key, host.attachments(),
new FileInputStream(file), file.length(), file.getName());
} catch (IOException e) {
chat.appendSystem("Could not share " + file.getName() + ": " + e.getMessage());
@@ -75,7 +75,7 @@ final class ChatAttachmentActions implements ChatPanel.AttachmentHandler {
}
@Override
public void share(ChatScope scope, int clientId, BufferedImage image) {
public void share(ChatKey key, BufferedImage image) {
if (!signedIn()) return;
ByteArrayOutputStream png = new ByteArrayOutputStream();
try {
@@ -84,7 +84,7 @@ final class ChatAttachmentActions implements ChatPanel.AttachmentHandler {
chat.appendSystem("Could not share the picture: " + e.getMessage());
return;
}
session.sendAttachment(scope, clientId, host.attachments(),
session.sendAttachment(key, host.attachments(),
new ByteArrayInputStream(png.toByteArray()), png.size(), PASTED_NAME);
}
@@ -2,7 +2,7 @@ package eu.lixko.ts3j.client.ui;
import com.formdev.flatlaf.util.ScaledEmptyBorder;
import com.formdev.flatlaf.util.UIScale;
import eu.lixko.ts3j.client.chat.ChatScope;
import eu.lixko.ts3j.client.chat.ChatKey;
import eu.lixko.ts3j.client.config.Settings;
import eu.lixko.ts3j.client.myts.ChatAttachment;
import eu.lixko.ts3j.client.net.PictureDownloads;
@@ -53,7 +53,6 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;
/**
* Chat area with one tab per conversation (server, current channel and one per
@@ -64,8 +63,7 @@ import java.util.function.Supplier;
public final class ChatPanel extends JPanel {
public interface SendHandler {
/** {@code clientId} is only meaningful for {@link ChatScope#PRIVATE}. */
void send(ChatScope scope, int clientId, String text);
void send(ChatKey key, String text);
}
/** Invoked when a client or channel link in the log is clicked. */
@@ -81,11 +79,11 @@ public final class ChatPanel extends JPanel {
/** Files shared in the chats, which the host sends, fetches and shows. */
public interface AttachmentHandler {
/** Asks which files to share in the conversation. */
void choose(ChatScope scope, int clientId);
void choose(ChatKey key);
void share(ChatScope scope, int clientId, List<File> files);
void share(ChatKey key, List<File> files);
void share(ChatScope scope, int clientId, BufferedImage image);
void share(ChatKey key, BufferedImage image);
/**
* A shared file was clicked.
@@ -108,8 +106,8 @@ public final class ChatPanel extends JPanel {
private static final String NO_SERVER = "Server";
private static final String NO_CHANNEL = "Channel";
private final Tab serverTab = new Tab(ChatScope.SERVER, 0, NO_SERVER, Icons.server());
private final Tab channelTab = new Tab(ChatScope.CHANNEL, 0, NO_CHANNEL, Icons.channelChat());
private final Tab serverTab = new Tab(ChatKey.SERVER, NO_SERVER, Icons.server());
private final Tab channelTab = new Tab(ChatKey.CHANNEL, NO_CHANNEL, Icons.channelChat());
/** Private chats keyed by the peer's client id. */
private final Map<Integer, Tab> privateTabs = new LinkedHashMap<>();
/** Static-content tabs (e.g. a moved-out description), keyed by caller-chosen id. */
@@ -137,8 +135,6 @@ public final class ChatPanel extends JPanel {
private final TextZoom zoom;
private final PictureDownloads downloads;
private final DescriptionPictures pictures;
/** The server's unique identifier, which shared files are kept under; null while unknown. */
private final Supplier<String> room;
private final JButton attach = new JButton(Icons.of("UPLOAD"));
private boolean inputEnabled;
private boolean sharingAvailable;
@@ -146,12 +142,11 @@ public final class ChatPanel extends JPanel {
for (Tab t : allTabs()) t.picturesChanged();
});
public ChatPanel(TextZoom zoom, Settings settings, PictureDownloads downloads, Supplier<String> room) {
public ChatPanel(TextZoom zoom, Settings settings, PictureDownloads downloads) {
super(new BorderLayout());
this.zoom = zoom;
this.downloads = downloads;
this.pictures = new DescriptionPictures(downloads, settings);
this.room = room;
tabs.setFont(Theme.uiFont());
tabs.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
@@ -195,8 +190,8 @@ public final class ChatPanel extends JPanel {
attach.setFocusable(false);
attach.addActionListener(e -> {
Tab tab = selectedTab();
if (attachmentHandler != null && tab != null && tab.scope != null) {
attachmentHandler.choose(tab.scope, tab.clientId);
if (attachmentHandler != null && tab != null && tab.key != null) {
attachmentHandler.choose(tab.key);
}
});
@@ -249,16 +244,6 @@ public final class ChatPanel extends JPanel {
});
}
/** Names the server tab after the server we are on; blank falls back to "Server". */
public void setServerName(String name) {
edt(() -> serverTab.setTitle(name == null || name.isBlank() ? NO_SERVER : name));
}
/** Names the channel tab after the channel we are in; blank falls back to "Channel". */
public void setChannelName(String name) {
edt(() -> channelTab.setTitle(name == null || name.isBlank() ? NO_CHANNEL : name));
}
public void setInputEnabled(boolean enabled) {
input.setEnabled(enabled);
inputEnabled = enabled;
@@ -276,16 +261,16 @@ public final class ChatPanel extends JPanel {
@Override
public boolean files(List<File> files) {
Tab tab = selectedTab();
if (attachmentHandler == null || tab == null || tab.scope == null) return false;
attachmentHandler.share(tab.scope, tab.clientId, files);
if (attachmentHandler == null || tab == null || tab.key == null) return false;
attachmentHandler.share(tab.key, files);
return true;
}
@Override
public boolean image(BufferedImage image) {
Tab tab = selectedTab();
if (attachmentHandler == null || tab == null || tab.scope == null) return false;
attachmentHandler.share(tab.scope, tab.clientId, image);
if (attachmentHandler == null || tab == null || tab.key == null) return false;
attachmentHandler.share(tab.key, image);
return true;
}
});
@@ -293,28 +278,32 @@ public final class ChatPanel extends JPanel {
/** Opens (or focuses) the private chat with {@code clientId}. */
public void openPrivateChat(int clientId, String nickname) {
openPrivateChat(clientId, "", nickname);
}
/** Opens (or focuses) the private chat with {@code clientId}. */
public void openPrivateChat(int clientId, String uniqueId, String nickname) {
edt(() -> {
Tab tab = privateTab(clientId, uniqueId, nickname);
Tab tab = privateTab(clientId, nickname);
tabs.setSelectedComponent(tab.scroll);
input.requestFocusInWindow();
});
}
public void setServerHistory(List<String> htmlEntries) {
edt(() -> serverTab.setHistory(htmlEntries));
/** Adds a line to a conversation, opening a private one titled {@code title} if needed. */
public void append(ChatKey key, String title, String html) {
edt(() -> tab(key, title).append(html));
}
public void setChannelHistory(List<String> htmlEntries) {
edt(() -> channelTab.setHistory(htmlEntries));
public void setHistory(ChatKey key, String title, List<String> html) {
edt(() -> tab(key, title).setHistory(html));
}
public void setPrivateHistory(int clientId, String nickname, List<String> htmlEntries) {
edt(() -> privateTab(clientId, "", nickname).setHistory(htmlEntries));
/** Names the server or channel tab; blank falls back to "Server" or "Channel". */
public void rename(ChatKey key, String name) {
edt(() -> {
switch (key.scope()) {
case SERVER -> serverTab.setTitle(name.isBlank() ? NO_SERVER : name);
case CHANNEL -> channelTab.setTitle(name.isBlank() ? NO_CHANNEL : name);
case PRIVATE -> {
}
}
});
}
/** Drops every private chat tab, e.g. after disconnecting. */
@@ -363,8 +352,8 @@ public final class ChatPanel extends JPanel {
String text = input.getText().trim();
if (text.isEmpty() || sendHandler == null) return;
Tab tab = selectedTab();
if (tab == null || tab.scope == null) return;
sendHandler.send(tab.scope, tab.clientId, text);
if (tab == null || tab.key == null) return;
sendHandler.send(tab.key, text);
input.setText("");
}
@@ -375,58 +364,20 @@ public final class ChatPanel extends JPanel {
edt(() -> serverTab.append(ChatHtml.system(text)));
}
/**
* Server-side events (client joins/leaves/moves, group changes, channel edits, …).
* These lines name clients and channels with TeamSpeak's own links, which stay
* clickable here — everything else in them is literal text.
*/
public void appendServerLog(String text) {
edt(() -> serverTab.append(ChatHtml.serverLog(text)));
}
public void appendServerLogHtml(String html) {
edt(() -> serverTab.append(ChatHtml.logEntry(html)));
}
public void appendServerMessage(int fromId, String from, String text) {
appendServerMessage(fromId, "", from, text);
}
public void appendServerMessage(int fromId, String uniqueId, String from, String text) {
edt(() -> serverTab.appendMessage(fromId, uniqueId, from, text));
}
public void appendChannelMessage(int fromId, String from, String text) {
appendChannelMessage(fromId, "", from, text);
}
public void appendChannelMessage(int fromId, String uniqueId, String from, String text) {
edt(() -> channelTab.appendMessage(fromId, uniqueId, from, text));
}
/** Appends to the private chat with {@code peerId}, creating the tab if needed. */
public void appendPrivateMessage(int peerId, String peerName, int fromId, String from, String text) {
appendPrivateMessage(peerId, "", peerName, fromId, "", from, text);
}
/** Appends to the private chat with {@code peerId}, creating the tab if needed. */
public void appendPrivateMessage(int peerId, String peerUniqueId, String peerName,
int fromId, String fromUniqueId, String from, String text) {
edt(() -> privateTab(peerId, peerUniqueId, peerName).appendMessage(fromId, fromUniqueId, from, text));
private Tab tab(ChatKey key, String title) {
return switch (key.scope()) {
case SERVER -> serverTab;
case CHANNEL -> channelTab;
case PRIVATE -> privateTab(key.clientId(), title);
};
}
private Tab privateTab(int clientId, String nickname) {
return privateTab(clientId, "", nickname);
}
private Tab privateTab(int clientId, String uniqueId, String nickname) {
Tab tab = privateTabs.get(clientId);
if (tab == null) {
tab = new Tab(ChatScope.PRIVATE, clientId, uniqueId, nickname, Icons.privateChat());
tab = new Tab(ChatKey.privateWith(clientId), nickname, Icons.privateChat());
privateTabs.put(clientId, tab);
addTab(tab, true);
} else {
tab.setClientUniqueId(uniqueId);
}
return tab;
}
@@ -442,7 +393,7 @@ public final class ChatPanel extends JPanel {
noteTabs.remove(tab.noteKey);
if (tab.onClose != null) tab.onClose.run();
} else {
privateTabs.remove(tab.clientId);
privateTabs.remove(tab.key.clientId());
}
tabs.remove(tab.scroll);
}
@@ -494,11 +445,6 @@ public final class ChatPanel extends JPanel {
return all;
}
private String room() {
String id = room.get();
return id == null ? "" : id;
}
private void edt(Runnable r) {
if (SwingUtilities.isEventDispatchThread()) r.run();
else SwingUtilities.invokeLater(r);
@@ -507,9 +453,8 @@ public final class ChatPanel extends JPanel {
/** One conversation: its HTML log, its scroll pane and the tab-strip header. */
private final class Tab {
final ChatScope scope;
final int clientId;
private String clientUniqueId;
/** The conversation; null for a static-content tab. */
final ChatKey key;
private String title;
final JEditorPane log = HtmlStyles.pane("font-family:sans-serif; font-size:11px; margin:"
+ HtmlStyles.px(4) + " " + HtmlStyles.px(6) + ";");
@@ -528,24 +473,17 @@ public final class ChatPanel extends JPanel {
private String pictureState = "";
private int renderedPictureWidth;
Tab(ChatScope scope, int clientId, String title, Icon icon) {
this(scope, clientId, "", title, icon, null, null);
Tab(ChatKey key, String title, Icon icon) {
this(key, title, icon, null, null);
}
Tab(ChatScope scope, int clientId, String clientUniqueId, String title, Icon icon) {
this(scope, clientId, clientUniqueId, title, icon, null, null);
}
/** A static-content tab: no send scope, closing it runs {@code onClose}. */
/** A static-content tab: nothing to send to, closing it runs {@code onClose}. */
Tab(String noteKey, Icon icon, String title, Runnable onClose) {
this(null, 0, "", title, icon, noteKey, onClose);
this(null, title, icon, noteKey, onClose);
}
private Tab(ChatScope scope, int clientId, String clientUniqueId, String title,
Icon icon, String noteKey, Runnable onClose) {
this.scope = scope;
this.clientId = clientId;
this.clientUniqueId = clientUniqueId == null ? "" : clientUniqueId;
private Tab(ChatKey key, String title, Icon icon, String noteKey, Runnable onClose) {
this.key = key;
this.title = title;
this.icon = icon;
this.noteKey = noteKey;
@@ -573,10 +511,6 @@ public final class ChatPanel extends JPanel {
return element != null && element.getAttributes().getAttribute(StyleConstants.NameAttribute) == HTML.Tag.IMG;
}
void setClientUniqueId(String uniqueId) {
if (uniqueId != null && !uniqueId.isBlank()) clientUniqueId = uniqueId;
}
private void onLink(String href, boolean picture) {
if (href == null) return;
if (ChatAttachment.isHref(href)) {
@@ -652,19 +586,10 @@ public final class ChatPanel extends JPanel {
titleLabel.setForeground(unread ? Theme.accent() : null);
}
void appendMessage(int fromId, String fromUniqueId, String from, String text) {
append(ChatHtml.message(fromId,
fromUniqueId == null || fromUniqueId.isBlank() ? clientUniqueId : fromUniqueId, from, text,
room()));
}
void setHistory(List<String> htmlEntries) {
void setHistory(List<String> html) {
entries.clear();
pictureUrls.clear();
if (htmlEntries != null) {
String room = room();
for (String entry : htmlEntries) add(ChatHtml.logEntry(entry, room));
}
for (String entry : html) add(entry);
render();
}
@@ -1,6 +1,6 @@
package eu.lixko.ts3j.client.ui;
import eu.lixko.ts3j.client.chat.ChatScope;
import eu.lixko.ts3j.client.chat.ChatKey;
import eu.lixko.ts3j.client.config.Settings;
import eu.lixko.ts3j.client.notify.desktop.DesktopNotifications;
import eu.lixko.ts3j.client.notify.desktop.DesktopNotifications.Notice;
@@ -146,14 +146,14 @@ final class DesktopNotifier {
frame.selectTab(tab);
if (clientId != null) {
var client = tab.session().model().getClient(clientId);
if (client != null) tab.chat().openPrivateChat(clientId, client.uniqueId, client.nickname);
if (client != null) tab.chat().openPrivateChat(clientId, client.nickname);
}
});
}
private void reply(ServerTab tab, int clientId, String text) {
SwingUtilities.invokeLater(() -> {
if (tab.session().isConnected()) tab.session().sendChat(ChatScope.PRIVATE, clientId, text);
if (tab.session().isConnected()) tab.session().sendChat(ChatKey.privateWith(clientId), text);
});
}
}
@@ -67,12 +67,12 @@ final class ServerTab {
// up front), and the session must exist before the tree/chat panels and the other
// tab helpers that read from it — so wiring finishes with an explicit attach() once
// everything is built. Nothing fires callbacks before then.
ServerTabConnectionEvents events = new ServerTabConnectionEvents(host, this, settings);
ServerTabConnectionEvents events = new ServerTabConnectionEvents(host, this);
this.session = new ServerSession(settings, identities, audio, sounds, contacts, events);
session.setAutoReconnect(true);
session.setMyTeamSpeakLogin(host.myTeamSpeakLogin());
this.groupIcons = new GroupIcons(session.icons());
this.chatPanel = new ChatPanel(host.chatZoom(), settings, pictures, session::serverUniqueId);
this.chatPanel = new ChatPanel(host.chatZoom(), settings, pictures);
this.treeActions = new ServerTabTreeActions(host, this, session, chatPanel, infoPanel, groupIcons);
this.treePanel = new ServerTreePanel(session.model(), groupIcons, contacts, settings, treeActions);
treeActions.attach(treePanel);
@@ -1,8 +1,6 @@
package eu.lixko.ts3j.client.ui;
import eu.lixko.ts3j.client.chat.ChatScope;
import eu.lixko.ts3j.client.config.Settings;
import eu.lixko.ts3j.client.net.ChannelNode;
import eu.lixko.ts3j.client.chat.ChatKey;
import eu.lixko.ts3j.client.session.ServerSession;
import javax.swing.JOptionPane;
@@ -22,7 +20,6 @@ final class ServerTabConnectionEvents implements ServerSession.Listener {
private final MainFrame host;
private final ServerTab tab;
private final Settings settings;
private final AtomicBoolean modelChangePending = new AtomicBoolean();
@@ -31,10 +28,9 @@ final class ServerTabConnectionEvents implements ServerSession.Listener {
private ChatPanel chatPanel;
private ServerTabTreeActions treeActions;
ServerTabConnectionEvents(MainFrame host, ServerTab tab, Settings settings) {
ServerTabConnectionEvents(MainFrame host, ServerTab tab) {
this.host = host;
this.tab = tab;
this.settings = settings;
}
void attach(ServerSession session, ServerTreePanel treePanel, ChatPanel chatPanel,
@@ -54,21 +50,23 @@ final class ServerTabConnectionEvents implements ServerSession.Listener {
}
@Override
public void onSystemMessage(String text) {
SwingUtilities.invokeLater(() -> chatPanel.appendSystem(text));
public void append(ChatKey key, String title, String html) {
chatPanel.append(key, title, html);
}
@Override
public void onOwnChat(ChatScope scope, int peerClientId, String peerName, String text) {
String me = settings.nickname.get() + " (you)";
int myId = session.selfClientId();
SwingUtilities.invokeLater(() -> {
switch (scope) {
case SERVER -> chatPanel.appendServerMessage(myId, me, text);
case PRIVATE -> chatPanel.appendPrivateMessage(peerClientId, peerName, myId, me, text);
default -> chatPanel.appendChannelMessage(myId, me, text);
}
});
public void setHistory(ChatKey key, String title, List<String> html) {
chatPanel.setHistory(key, title, html);
}
@Override
public void rename(ChatKey key, String name) {
chatPanel.rename(key, name);
}
@Override
public void closePrivateChats() {
chatPanel.closePrivateChats();
}
@Override
@@ -79,11 +77,6 @@ final class ServerTabConnectionEvents implements ServerSession.Listener {
});
}
@Override
public void onStatus(String text) {
// Reaches the tab through onSessionChanged.
}
@Override
public void onConnected() {
SwingUtilities.invokeLater(() -> {
@@ -99,10 +92,7 @@ final class ServerTabConnectionEvents implements ServerSession.Listener {
treePanel.showDisconnected();
treeActions.clearSelection();
chatPanel.setInputEnabled(false);
chatPanel.closePrivateChats();
chatPanel.closeNoteTabs();
chatPanel.setServerName(null);
chatPanel.setChannelName(null);
tab.serverWindowsChanged(false);
host.tabDisconnected(tab);
});
@@ -118,11 +108,6 @@ final class ServerTabConnectionEvents implements ServerSession.Listener {
treeActions.renderInfo();
treeActions.refreshGroupsDialog();
host.refreshHostButton(tab);
if (session.isConnected()) {
ChannelNode channel = session.currentChannel();
chatPanel.setServerName(session.model().getServerName());
chatPanel.setChannelName(channel == null ? null : channel.name);
}
});
}
@@ -145,31 +130,8 @@ final class ServerTabConnectionEvents implements ServerSession.Listener {
}
@Override
public void onChat(ChatScope scope, int fromClientId, String fromUniqueId, String fromName, String message) {
SwingUtilities.invokeLater(() -> {
switch (scope) {
case PRIVATE -> {
chatPanel.appendPrivateMessage(fromClientId, fromUniqueId, fromName,
fromClientId, fromUniqueId, fromName, message);
host.notifier().privateMessage(tab, fromClientId, fromName, message);
}
case SERVER -> chatPanel.appendServerMessage(fromClientId, fromUniqueId, fromName, message);
default -> chatPanel.appendChannelMessage(fromClientId, fromUniqueId, fromName, message);
}
});
}
@Override
public void onChatHistory(ChatScope scope, List<String> htmlEntries) {
SwingUtilities.invokeLater(() -> {
if (scope == ChatScope.SERVER) chatPanel.setServerHistory(htmlEntries);
else if (scope == ChatScope.CHANNEL) chatPanel.setChannelHistory(htmlEntries);
});
}
@Override
public void onPrivateChatHistory(int clientId, String nickname, List<String> htmlEntries) {
SwingUtilities.invokeLater(() -> chatPanel.setPrivateHistory(clientId, nickname, htmlEntries));
public void onPrivateMessage(int fromClientId, String fromName, String message) {
SwingUtilities.invokeLater(() -> host.notifier().privateMessage(tab, fromClientId, fromName, message));
}
@Override
@@ -182,17 +144,7 @@ final class ServerTabConnectionEvents implements ServerSession.Listener {
@Override
public void onError(String message) {
// Reaches the chat through onSystemMessage and the tab through onSessionChanged.
}
@Override
public void onServerLog(String message) {
SwingUtilities.invokeLater(() -> chatPanel.appendServerLog(message));
}
@Override
public void onServerLogHtml(String html) {
SwingUtilities.invokeLater(() -> chatPanel.appendServerLogHtml(html));
// Reaches the chat as a line and the tab through onSessionChanged.
}
@Override
@@ -129,7 +129,7 @@ final class ServerTabTreeActions implements ServerTreePanel.Actions {
@Override
public void openPrivateChat(ClientEntry client) {
chatPanel.openPrivateChat(client.id, client.uniqueId, session.contacts().displayName(client));
chatPanel.openPrivateChat(client.id, session.contacts().displayName(client));
session.loadPrivateChatHistory(client.id, client.uniqueId);
}