One byte-size format and one chat scope

text.ByteSize writes sizes and rates as TeamSpeak does and replaces the
formatters in ChatHtml, FileBrowserDialog and ConnectionStatsFormat.
chat.ChatScope replaces ConnectionListener.ChatScope, ChatLogScope and
ChatPanel.Target, and the mappings between them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-12 23:01:42 +00:00
co-authored by Claude Opus 5.5
parent 203082d040
commit 53585994a8
24 changed files with 188 additions and 174 deletions
@@ -1,9 +1,9 @@
package eu.lixko.ts3j.client.ui;
import eu.lixko.ts3j.client.chat.ChatScope;
import eu.lixko.ts3j.client.myts.ChatAttachment;
import eu.lixko.ts3j.client.myts.ChatAttachments;
import eu.lixko.ts3j.client.myts.MyTsException;
import eu.lixko.ts3j.client.net.ConnectionListener.ChatScope;
import eu.lixko.ts3j.client.net.PictureDownloads;
import eu.lixko.ts3j.client.session.ServerSession;
@@ -19,7 +19,6 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.function.Function;
/**
* Shares files in one server's chats through myTeamSpeak, and shows and saves the files
@@ -37,19 +36,16 @@ final class ChatAttachmentActions implements ChatPanel.AttachmentHandler {
private final ServerSession session;
private final ChatPanel chat;
private final PictureDownloads pictures;
private final Function<ChatPanel.Target, ChatScope> scopes;
ChatAttachmentActions(MainFrame host, ServerSession session, ChatPanel chat, PictureDownloads pictures,
Function<ChatPanel.Target, ChatScope> scopes) {
ChatAttachmentActions(MainFrame host, ServerSession session, ChatPanel chat, PictureDownloads pictures) {
this.host = host;
this.session = session;
this.chat = chat;
this.pictures = pictures;
this.scopes = scopes;
}
@Override
public void choose(ChatPanel.Target target, int clientId) {
public void choose(ChatScope scope, int clientId) {
if (!signedIn()) return;
JFileChooser chooser = new JFileChooser(lastDirectory);
chooser.setMultiSelectionEnabled(true);
@@ -57,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(target, clientId, List.of(files));
share(scope, clientId, List.of(files));
}
@Override
public void share(ChatPanel.Target target, int clientId, List<File> files) {
public void share(ChatScope scope, int clientId, List<File> files) {
if (!signedIn()) return;
for (File file : files) {
if (!file.isFile()) continue;
@@ -70,7 +66,7 @@ final class ChatAttachmentActions implements ChatPanel.AttachmentHandler {
continue;
}
try {
session.sendAttachment(scopes.apply(target), clientId, host.attachments(),
session.sendAttachment(scope, clientId, host.attachments(),
new FileInputStream(file), file.length(), file.getName());
} catch (IOException e) {
chat.appendSystem("Could not share " + file.getName() + ": " + e.getMessage());
@@ -79,7 +75,7 @@ final class ChatAttachmentActions implements ChatPanel.AttachmentHandler {
}
@Override
public void share(ChatPanel.Target target, int clientId, BufferedImage image) {
public void share(ChatScope scope, int clientId, BufferedImage image) {
if (!signedIn()) return;
ByteArrayOutputStream png = new ByteArrayOutputStream();
try {
@@ -88,7 +84,7 @@ final class ChatAttachmentActions implements ChatPanel.AttachmentHandler {
chat.appendSystem("Could not share the picture: " + e.getMessage());
return;
}
session.sendAttachment(scopes.apply(target), clientId, host.attachments(),
session.sendAttachment(scope, clientId, host.attachments(),
new ByteArrayInputStream(png.toByteArray()), png.size(), PASTED_NAME);
}
@@ -2,6 +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.config.Settings;
import eu.lixko.ts3j.client.myts.ChatAttachment;
import eu.lixko.ts3j.client.net.PictureDownloads;
@@ -62,12 +63,9 @@ import java.util.function.Supplier;
*/
public final class ChatPanel extends JPanel {
/** Where an outgoing message should go. */
public enum Target {CHANNEL, SERVER, PRIVATE}
public interface SendHandler {
/** {@code clientId} is only meaningful for {@link Target#PRIVATE}. */
void send(Target target, int clientId, String text);
/** {@code clientId} is only meaningful for {@link ChatScope#PRIVATE}. */
void send(ChatScope scope, int clientId, String text);
}
/** Invoked when a client or channel link in the log is clicked. */
@@ -83,11 +81,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(Target target, int clientId);
void choose(ChatScope scope, int clientId);
void share(Target target, int clientId, List<File> files);
void share(ChatScope scope, int clientId, List<File> files);
void share(Target target, int clientId, BufferedImage image);
void share(ChatScope scope, int clientId, BufferedImage image);
/**
* A shared file was clicked.
@@ -110,8 +108,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(Target.SERVER, 0, NO_SERVER, Icons.server());
private final Tab channelTab = new Tab(Target.CHANNEL, 0, NO_CHANNEL, Icons.channelChat());
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 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. */
@@ -197,8 +195,8 @@ public final class ChatPanel extends JPanel {
attach.setFocusable(false);
attach.addActionListener(e -> {
Tab tab = selectedTab();
if (attachmentHandler != null && tab != null && tab.target != null) {
attachmentHandler.choose(tab.target, tab.clientId);
if (attachmentHandler != null && tab != null && tab.scope != null) {
attachmentHandler.choose(tab.scope, tab.clientId);
}
});
@@ -278,16 +276,16 @@ public final class ChatPanel extends JPanel {
@Override
public boolean files(List<File> files) {
Tab tab = selectedTab();
if (attachmentHandler == null || tab == null || tab.target == null) return false;
attachmentHandler.share(tab.target, tab.clientId, files);
if (attachmentHandler == null || tab == null || tab.scope == null) return false;
attachmentHandler.share(tab.scope, tab.clientId, files);
return true;
}
@Override
public boolean image(BufferedImage image) {
Tab tab = selectedTab();
if (attachmentHandler == null || tab == null || tab.target == null) return false;
attachmentHandler.share(tab.target, tab.clientId, image);
if (attachmentHandler == null || tab == null || tab.scope == null) return false;
attachmentHandler.share(tab.scope, tab.clientId, image);
return true;
}
});
@@ -365,8 +363,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.target == null) return;
sendHandler.send(tab.target, tab.clientId, text);
if (tab == null || tab.scope == null) return;
sendHandler.send(tab.scope, tab.clientId, text);
input.setText("");
}
@@ -424,7 +422,7 @@ public final class ChatPanel extends JPanel {
private Tab privateTab(int clientId, String uniqueId, String nickname) {
Tab tab = privateTabs.get(clientId);
if (tab == null) {
tab = new Tab(Target.PRIVATE, clientId, uniqueId, nickname, Icons.privateChat());
tab = new Tab(ChatScope.PRIVATE, clientId, uniqueId, nickname, Icons.privateChat());
privateTabs.put(clientId, tab);
addTab(tab, true);
} else {
@@ -509,7 +507,7 @@ public final class ChatPanel extends JPanel {
/** One conversation: its HTML log, its scroll pane and the tab-strip header. */
private final class Tab {
final Target target;
final ChatScope scope;
final int clientId;
private String clientUniqueId;
private String title;
@@ -530,22 +528,22 @@ public final class ChatPanel extends JPanel {
private String pictureState = "";
private int renderedPictureWidth;
Tab(Target target, int clientId, String title, Icon icon) {
this(target, clientId, "", title, icon, null, null);
Tab(ChatScope scope, int clientId, String title, Icon icon) {
this(scope, clientId, "", title, icon, null, null);
}
Tab(Target target, int clientId, String clientUniqueId, String title, Icon icon) {
this(target, clientId, clientUniqueId, 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 target, closing it runs {@code onClose}. */
/** A static-content tab: no send scope, closing it runs {@code onClose}. */
Tab(String noteKey, Icon icon, String title, Runnable onClose) {
this(null, 0, "", title, icon, noteKey, onClose);
}
private Tab(Target target, int clientId, String clientUniqueId, String title,
private Tab(ChatScope scope, int clientId, String clientUniqueId, String title,
Icon icon, String noteKey, Runnable onClose) {
this.target = target;
this.scope = scope;
this.clientId = clientId;
this.clientUniqueId = clientUniqueId == null ? "" : clientUniqueId;
this.title = title;
@@ -1,7 +1,7 @@
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.ConnectionListener.ChatScope;
import eu.lixko.ts3j.client.notify.desktop.DesktopNotifications;
import eu.lixko.ts3j.client.notify.desktop.DesktopNotifications.Notice;
import eu.lixko.ts3j.client.notify.desktop.FreedesktopNotifications;
@@ -6,6 +6,7 @@ import eu.lixko.ts3j.client.net.ChannelNode;
import eu.lixko.ts3j.client.session.ServerSession;
import eu.lixko.ts3j.client.net.filetransfer.FileTransfer;
import eu.lixko.ts3j.client.net.filetransfer.RemoteFile;
import eu.lixko.ts3j.client.text.ByteSize;
import javax.swing.BorderFactory;
import javax.swing.Box;
@@ -29,7 +30,6 @@ import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
@@ -360,11 +360,11 @@ public final class FileBrowserDialog extends JDialog {
label.setText(head + " — starting…");
break;
case ACTIVE:
label.setText(head + " — " + formatBytes(t.getTransferredBytes()) + " / "
+ formatBytes(t.getTotalBytes()) + " (" + formatRate(t.getAverageSpeed()) + ")");
label.setText(head + " — " + ByteSize.format(t.getTransferredBytes()) + " / "
+ ByteSize.format(t.getTotalBytes()) + " (" + formatRate(t.getAverageSpeed()) + ")");
break;
case COMPLETED:
label.setText(head + " — done (" + formatBytes(t.getTotalBytes()) + ")");
label.setText(head + " — done (" + ByteSize.format(t.getTotalBytes()) + ")");
break;
case CANCELLED:
label.setText(head + " — cancelled");
@@ -428,7 +428,7 @@ public final class FileBrowserDialog extends JDialog {
case 0:
return f.getName();
case 1:
return f.isDirectory() ? "" : formatBytes(f.getSize());
return f.isDirectory() ? "" : ByteSize.format(f.getSize());
case 2:
return f.isDirectory() ? "Folder" : "File";
case 3:
@@ -454,19 +454,8 @@ public final class FileBrowserDialog extends JDialog {
// ---- formatting ----
private static String formatBytes(long bytes) {
if (bytes < 0) return "—";
if (bytes < 1024) return bytes + " B";
double kib = bytes / 1024.0;
if (kib < 1024) return new DecimalFormat("0.0").format(kib) + " KiB";
double mib = kib / 1024.0;
if (mib < 1024) return new DecimalFormat("0.00").format(mib) + " MiB";
return new DecimalFormat("0.00").format(mib / 1024.0) + " GiB";
}
private static String formatRate(double bytesPerSecond) {
if (bytesPerSecond <= 0) return "—";
return formatBytes(Math.round(bytesPerSecond)) + "/s";
return bytesPerSecond > 0 ? ByteSize.rate(Math.round(bytesPerSecond)) : ByteSize.UNKNOWN;
}
private static String orEmpty(String s) {
@@ -7,7 +7,6 @@ import eu.lixko.ts3j.client.config.Settings;
import eu.lixko.ts3j.client.contacts.ContactStore;
import eu.lixko.ts3j.client.net.ChannelNode;
import eu.lixko.ts3j.client.net.ClientEntry;
import eu.lixko.ts3j.client.net.ConnectionListener.ChatScope;
import eu.lixko.ts3j.client.net.OfflineMessage;
import eu.lixko.ts3j.client.net.PictureDownloads;
import eu.lixko.ts3j.client.net.avatar.Avatar;
@@ -79,7 +78,7 @@ final class ServerTab {
treeActions.attach(treePanel);
events.attach(session, treePanel, chatPanel, treeActions);
chatPanel.setSendHandler(this::onSendChat);
chatPanel.setSendHandler(session::sendChat);
chatPanel.setLinkHandler(new ChatPanel.LinkHandler() {
@Override
public void onClientLink(TsLink.Ref ref, Component source, int x, int y) {
@@ -96,7 +95,7 @@ final class ServerTab {
showOfflineMessages();
}
});
chatPanel.setAttachmentHandler(new ChatAttachmentActions(host, session, chatPanel, pictures, ServerTab::scope));
chatPanel.setAttachmentHandler(new ChatAttachmentActions(host, session, chatPanel, pictures));
chatPanel.setSharingAvailable(host.canShareFiles());
chatPanel.setInputEnabled(false);
@@ -280,18 +279,6 @@ final class ServerTab {
// ---- chat ----
private void onSendChat(ChatPanel.Target target, int clientId, String text) {
session.sendChat(scope(target), clientId, text);
}
private static ChatScope scope(ChatPanel.Target target) {
return switch (target) {
case SERVER -> ChatScope.SERVER;
case PRIVATE -> ChatScope.PRIVATE;
default -> ChatScope.CHANNEL;
};
}
/** Opens the file repository browser for a channel, as the "Browse Files" hotkey/menu action does. */
void browseFiles(ChannelNode channel) {
treeActions.browseFiles(channel);
@@ -1,5 +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.session.ServerSession;