Rebuild Connection Info after the official dialog
Lay the window out as TeamSpeak 3's: name, times, ping and address (with a copy button) beside the avatar, then Total / Speech / Keep Alive / Control tabs holding a Total / In / Out table, and a Quota tab. Android gets the same tabs and table. Wait for both notifyconnectioninfo reports for another client (the server's cached copy, then the client's fresh counters) instead of taking the first, often all zeros. Ask the server about ourselves too, for our address and outgoing loss. Month transfer totals come from clientgetvariables, our quota from the needed permissions, and file transfers now count their bytes for our own transfer rate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,72 +2,84 @@ package com.ts3client.ui;
|
||||
|
||||
import com.formdev.flatlaf.util.ScaledEmptyBorder;
|
||||
import com.formdev.flatlaf.util.UIScale;
|
||||
import com.ts3client.net.ClientEntry;
|
||||
import com.ts3client.net.ConnectionStats;
|
||||
import com.ts3client.net.ConnectionStatsFormat;
|
||||
import com.ts3client.net.ConnectionStatsFormat.Row;
|
||||
import com.ts3client.net.ConnectionStatsFormat.TrafficRow;
|
||||
import com.ts3client.net.TeamspeakConnection;
|
||||
import com.ts3client.net.avatar.Avatar;
|
||||
|
||||
import javax.swing.Box;
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JSeparator;
|
||||
import javax.swing.JTabbedPane;
|
||||
import javax.swing.SwingConstants;
|
||||
import javax.swing.SwingUtilities;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Font;
|
||||
import java.awt.Frame;
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.Image;
|
||||
import java.awt.Insets;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.datatransfer.StringSelection;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* Live "Client Connection Info" window, reachable from a client's right-click
|
||||
* menu. Mirrors the TeamSpeak 3 dialog: a summary header (address, version,
|
||||
* platform, idle/connected time, ping, filetransfer) above a
|
||||
* <em>Total / Speech / Keep Alive / Control</em> tab strip, each tab showing
|
||||
* that category's packet loss, packet and byte totals and live bandwidth, plus a
|
||||
* <em>Voice received</em> tab with what our own playback made of the client's voice
|
||||
* (everyone's, for ourselves): missed, out-of-order, late and duplicate packets.
|
||||
*
|
||||
* <p>Polls the {@link TeamspeakConnection} once a second while open; for the
|
||||
* local client the figures update live from local counters, for a remote client
|
||||
* they refresh from the server's connection report.
|
||||
* Live "Client Connection Info" window, laid out as TeamSpeak 3's: name, times, ping and
|
||||
* address beside the avatar, above a tab per traffic category whose table has Total / In /
|
||||
* Out columns, and a Quota tab. A <em>Voice received</em> tab adds what our own playback
|
||||
* made of the client's voice (everyone's, for ourselves). Polls once a second while open.
|
||||
*/
|
||||
public final class ConnectionInfoDialog extends JDialog {
|
||||
|
||||
private static final int REFRESH_MS = 1000;
|
||||
/** Wide enough for the tab strip to stay on one row. */
|
||||
private static final int MIN_WIDTH = 560;
|
||||
private static final String SELF_ADDRESS = "This computer";
|
||||
/** Avatar box, about the official window's. */
|
||||
private static final int AVATAR_SIZE = 80;
|
||||
/** Sample texts sizing the columns, as the official window does, so they don't jitter. */
|
||||
private static final String TOTAL_SAMPLE = "100.00 %";
|
||||
private static final String VALUE_SAMPLE = "1023.00 KiB/s";
|
||||
|
||||
private final TeamspeakConnection conn;
|
||||
private final int clientId;
|
||||
private final String nickname;
|
||||
private final javax.swing.Timer timer;
|
||||
private final java.util.concurrent.atomic.AtomicBoolean inFlight =
|
||||
new java.util.concurrent.atomic.AtomicBoolean();
|
||||
private final AtomicBoolean inFlight = new AtomicBoolean();
|
||||
|
||||
private final Rows summary = new Rows(ConnectionStatsFormat.summaryRows(null, SELF_ADDRESS));
|
||||
private final Rows total = tab(ConnectionStatsFormat.trafficRows(null, null));
|
||||
private final Rows speech = tab(ConnectionStatsFormat.trafficRows(null, ConnectionStats.Kind.SPEECH));
|
||||
private final Rows keepAlive = tab(ConnectionStatsFormat.trafficRows(null, ConnectionStats.Kind.KEEPALIVE));
|
||||
private final Rows control = tab(ConnectionStatsFormat.trafficRows(null, ConnectionStats.Kind.CONTROL));
|
||||
private final Rows voice = tab(ConnectionStatsFormat.voiceRows(null));
|
||||
private final Figures summary;
|
||||
private final JButton copyAddress = new JButton(Icons.of("COPY"));
|
||||
private final JLabel avatar = new JLabel();
|
||||
private Avatar shownAvatar;
|
||||
private ConnectionStats latest;
|
||||
|
||||
private final List<TrafficTable> traffic = new ArrayList<>();
|
||||
private Figures quota;
|
||||
private final Figures voice = new Figures(List.of(ConnectionStatsFormat.voiceRows(null)), false);
|
||||
|
||||
public ConnectionInfoDialog(Frame owner, TeamspeakConnection conn, int clientId, String nickname) {
|
||||
super(owner, "Connection Info — " + nickname, false);
|
||||
super(owner, "Connection Info - " + nickname, false);
|
||||
this.conn = conn;
|
||||
this.clientId = clientId;
|
||||
this.nickname = nickname;
|
||||
this.summary = new Figures(List.of(ConnectionStatsFormat.summaryRows(null, nickname)), true);
|
||||
|
||||
JPanel content = new JPanel(new BorderLayout(0, UIScale.scale(10)));
|
||||
JPanel content = new JPanel(new BorderLayout(0, UIScale.scale(12)));
|
||||
content.setBackground(Theme.windowBg());
|
||||
content.setBorder(new ScaledEmptyBorder(12, 14, 12, 14));
|
||||
content.add(summary, BorderLayout.NORTH);
|
||||
content.setBorder(new ScaledEmptyBorder(10, 10, 10, 10));
|
||||
content.add(buildHeader(), BorderLayout.NORTH);
|
||||
content.add(buildTabs(), BorderLayout.CENTER);
|
||||
content.add(buildButtons(), BorderLayout.SOUTH);
|
||||
setContentPane(content);
|
||||
@@ -75,7 +87,7 @@ public final class ConnectionInfoDialog extends JDialog {
|
||||
Dialogs.closeOnEscape(this);
|
||||
// Disposing is what stops the refresh timer, so the window button must not merely hide.
|
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||
fit();
|
||||
pack();
|
||||
setLocationRelativeTo(owner);
|
||||
|
||||
addWindowListener(new WindowAdapter() {
|
||||
@@ -92,35 +104,68 @@ public final class ConnectionInfoDialog extends JDialog {
|
||||
|
||||
// ---- construction ----
|
||||
|
||||
private JPanel buildHeader() {
|
||||
copyAddress.setToolTipText("Copy client address to clipboard");
|
||||
copyAddress.setMargin(UIScale.scale(new Insets(1, 1, 1, 1)));
|
||||
copyAddress.setEnabled(false);
|
||||
copyAddress.addActionListener(e -> {
|
||||
String address = ConnectionStatsFormat.copyableAddress(latest);
|
||||
if (address != null) {
|
||||
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(address), null);
|
||||
}
|
||||
});
|
||||
summary.addTrailing(copyAddress);
|
||||
|
||||
avatar.setVerticalAlignment(SwingConstants.TOP);
|
||||
avatar.setBorder(new ScaledEmptyBorder(0, 16, 0, 0));
|
||||
|
||||
JPanel header = new JPanel(new BorderLayout());
|
||||
header.setOpaque(false);
|
||||
header.add(summary, BorderLayout.CENTER);
|
||||
header.add(avatar, BorderLayout.EAST);
|
||||
return header;
|
||||
}
|
||||
|
||||
private JTabbedPane buildTabs() {
|
||||
JTabbedPane tabs = new JTabbedPane();
|
||||
tabs.setFont(Theme.uiFont());
|
||||
tabs.setBackground(Theme.windowBg());
|
||||
tabs.addTab("Total", total);
|
||||
tabs.addTab("Speech", speech);
|
||||
tabs.addTab("Keep Alive", keepAlive);
|
||||
tabs.addTab("Control", control);
|
||||
tabs.addTab("Voice received", voice);
|
||||
for (ConnectionStatsFormat.Tab tab : ConnectionStatsFormat.TRAFFIC_TABS) {
|
||||
TrafficTable table = new TrafficTable(tab.kind());
|
||||
traffic.add(table);
|
||||
tabs.addTab(tab.title(), table);
|
||||
}
|
||||
tabs.addTab(ConnectionStatsFormat.QUOTA_TAB, buildQuota());
|
||||
tabs.addTab(ConnectionStatsFormat.VOICE_TAB, page(voice));
|
||||
return tabs;
|
||||
}
|
||||
|
||||
private JComponent buildQuota() {
|
||||
// Laid out for ourselves, the longer form; another client's extra rows hide on the first update.
|
||||
ConnectionStats self = new ConnectionStats();
|
||||
self.self = true;
|
||||
quota = new Figures(ConnectionStatsFormat.quotaGroups(self), false);
|
||||
return page(quota);
|
||||
}
|
||||
|
||||
private static JComponent page(JComponent body) {
|
||||
JPanel page = new JPanel(new BorderLayout());
|
||||
page.setOpaque(false);
|
||||
page.setBorder(new ScaledEmptyBorder(8, 10, 8, 10));
|
||||
page.add(body, BorderLayout.NORTH);
|
||||
return page;
|
||||
}
|
||||
|
||||
private JPanel buildButtons() {
|
||||
JPanel bar = new JPanel();
|
||||
bar.setLayout(new BoxLayout(bar, BoxLayout.X_AXIS));
|
||||
bar.setBackground(Theme.windowBg());
|
||||
bar.setOpaque(false);
|
||||
bar.add(Box.createHorizontalGlue());
|
||||
JButton close = new JButton("Close");
|
||||
JButton close = new JButton("Close", Icons.of("CLOSE"));
|
||||
close.addActionListener(e -> dispose());
|
||||
bar.add(close);
|
||||
return bar;
|
||||
}
|
||||
|
||||
private static Rows tab(List<Row> rows) {
|
||||
Rows tab = new Rows(rows);
|
||||
tab.setBorder(new ScaledEmptyBorder(10, 12, 10, 12));
|
||||
return tab;
|
||||
}
|
||||
|
||||
// ---- live update ----
|
||||
|
||||
private void refresh() {
|
||||
@@ -137,72 +182,156 @@ public final class ConnectionInfoDialog extends JDialog {
|
||||
|
||||
private void apply(ConnectionStats s) {
|
||||
if (!isDisplayable()) return;
|
||||
summary.update(ConnectionStatsFormat.summaryRows(s, SELF_ADDRESS));
|
||||
total.update(ConnectionStatsFormat.trafficRows(s, null));
|
||||
speech.update(ConnectionStatsFormat.trafficRows(s, ConnectionStats.Kind.SPEECH));
|
||||
keepAlive.update(ConnectionStatsFormat.trafficRows(s, ConnectionStats.Kind.KEEPALIVE));
|
||||
control.update(ConnectionStatsFormat.trafficRows(s, ConnectionStats.Kind.CONTROL));
|
||||
voice.update(ConnectionStatsFormat.voiceRows(s.voiceReceived));
|
||||
growToFit();
|
||||
latest = s;
|
||||
summary.update(List.of(ConnectionStatsFormat.summaryRows(s, nickname)));
|
||||
copyAddress.setEnabled(ConnectionStatsFormat.copyableAddress(s) != null);
|
||||
for (TrafficTable table : traffic) table.update(s);
|
||||
quota.update(ConnectionStatsFormat.quotaGroups(s));
|
||||
voice.update(List.of(ConnectionStatsFormat.voiceRows(s.voiceReceived)));
|
||||
showAvatar();
|
||||
}
|
||||
|
||||
private void showAvatar() {
|
||||
ClientEntry client = conn.getModel().getClient(clientId);
|
||||
Avatar current = conn.avatarOf(client);
|
||||
if (current == shownAvatar) return;
|
||||
shownAvatar = current;
|
||||
avatar.setIcon(current == null ? null : avatarIcon(current));
|
||||
pack();
|
||||
}
|
||||
|
||||
private static ImageIcon avatarIcon(Avatar avatar) {
|
||||
if (avatar.file() == null) return null;
|
||||
Dimension size = AvatarHtml.fit(AvatarHtml.dimensions(avatar.data()), UIScale.scale(AVATAR_SIZE));
|
||||
ImageIcon icon = new ImageIcon(avatar.data());
|
||||
if (size == null) return icon;
|
||||
return new ImageIcon(icon.getImage().getScaledInstance(size.width, size.height, Image.SCALE_SMOOTH));
|
||||
}
|
||||
|
||||
// ---- figures ----
|
||||
|
||||
private static JLabel label(String text) {
|
||||
JLabel l = new JLabel(text);
|
||||
l.setFont(Theme.uiFont());
|
||||
return l;
|
||||
}
|
||||
|
||||
/** Pins a label's width to that of {@code sample}, so a growing figure doesn't resize the window. */
|
||||
private static JLabel sized(JLabel l, String sample) {
|
||||
Dimension pref = l.getPreferredSize();
|
||||
int width = l.getFontMetrics(l.getFont()).stringWidth(sample);
|
||||
l.setPreferredSize(new Dimension(Math.max(pref.width, width), pref.height));
|
||||
return l;
|
||||
}
|
||||
|
||||
private static GridBagConstraints cell(int x, int y, int anchor, Insets insets) {
|
||||
GridBagConstraints c = new GridBagConstraints();
|
||||
c.gridx = x;
|
||||
c.gridy = y;
|
||||
c.anchor = anchor;
|
||||
c.insets = UIScale.scale(insets);
|
||||
return c;
|
||||
}
|
||||
|
||||
/**
|
||||
* The window is first packed around placeholder text; once real (longer)
|
||||
* values arrive it may need more room. Grow to fit, but never shrink, so the
|
||||
* window doesn't jitter as counters tick up each second.
|
||||
* Labelled figures, one per row, in groups set apart by a rule; refreshed in place, and
|
||||
* rows an update leaves out hide.
|
||||
*/
|
||||
private void growToFit() {
|
||||
Dimension pref = getContentPane().getPreferredSize();
|
||||
Dimension have = getContentPane().getSize();
|
||||
if (pref.width > have.width || pref.height > have.height) {
|
||||
fit();
|
||||
}
|
||||
}
|
||||
private static final class Figures extends JPanel {
|
||||
private final List<List<JLabel[]>> groups = new ArrayList<>();
|
||||
private int lastRow;
|
||||
|
||||
private void fit() {
|
||||
pack();
|
||||
setSize(Math.max(getWidth(), UIScale.scale(MIN_WIDTH)), getHeight());
|
||||
}
|
||||
|
||||
/** A grid of labelled figures, laid out once and refreshed in place. */
|
||||
private static final class Rows extends JPanel {
|
||||
private final JLabel[] values;
|
||||
|
||||
Rows(List<Row> rows) {
|
||||
Figures(List<List<Row>> rows, boolean valuesRight) {
|
||||
super(new GridBagLayout());
|
||||
setBackground(Theme.windowBg());
|
||||
values = new JLabel[rows.size()];
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
values[i] = new JLabel(rows.get(i).value());
|
||||
values[i].setFont(Theme.uiFont());
|
||||
values[i].setForeground(Theme.treeText());
|
||||
addRow(i, rows.get(i).label(), values[i]);
|
||||
setOpaque(false);
|
||||
int y = 0;
|
||||
for (List<Row> group : rows) {
|
||||
if (y > 0) {
|
||||
GridBagConstraints rule = cell(0, y++, GridBagConstraints.WEST, new Insets(6, 0, 6, 0));
|
||||
rule.gridwidth = 2;
|
||||
rule.fill = GridBagConstraints.HORIZONTAL;
|
||||
add(new JSeparator(), rule);
|
||||
}
|
||||
List<JLabel[]> labels = new ArrayList<>();
|
||||
for (Row row : group) {
|
||||
JLabel key = label(row.label());
|
||||
JLabel value = label(row.value());
|
||||
labels.add(new JLabel[]{key, value});
|
||||
add(key, cell(0, y, GridBagConstraints.WEST, new Insets(3, 0, 3, 16)));
|
||||
GridBagConstraints vc = cell(1, y, valuesRight ? GridBagConstraints.EAST : GridBagConstraints.WEST,
|
||||
new Insets(3, 0, 3, 0));
|
||||
vc.weightx = 1;
|
||||
add(value, vc);
|
||||
lastRow = y++;
|
||||
}
|
||||
groups.add(labels);
|
||||
}
|
||||
}
|
||||
|
||||
void update(List<Row> rows) {
|
||||
for (int i = 0; i < values.length; i++) values[i].setText(rows.get(i).value());
|
||||
/** Puts {@code c} after the last row's value. */
|
||||
void addTrailing(JComponent c) {
|
||||
add(c, cell(2, lastRow, GridBagConstraints.EAST, new Insets(0, 6, 0, 0)));
|
||||
}
|
||||
|
||||
private void addRow(int row, String label, JLabel valueLabel) {
|
||||
GridBagConstraints lc = new GridBagConstraints();
|
||||
lc.gridx = 0;
|
||||
lc.gridy = row;
|
||||
lc.anchor = GridBagConstraints.WEST;
|
||||
lc.insets = UIScale.scale(new Insets(2, 0, 2, 16));
|
||||
JLabel key = new JLabel(label);
|
||||
key.setFont(Theme.uiFont());
|
||||
key.setForeground(new Color(0x5A6B7B));
|
||||
add(key, lc);
|
||||
void update(List<List<Row>> rows) {
|
||||
for (int g = 0; g < groups.size(); g++) {
|
||||
List<JLabel[]> labels = groups.get(g);
|
||||
List<Row> group = rows.get(g);
|
||||
for (int i = 0; i < labels.size(); i++) {
|
||||
boolean shown = i < group.size();
|
||||
labels.get(i)[0].setVisible(shown);
|
||||
labels.get(i)[1].setVisible(shown);
|
||||
if (shown) labels.get(i)[1].setText(group.get(i).value());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GridBagConstraints vc = new GridBagConstraints();
|
||||
vc.gridx = 1;
|
||||
vc.gridy = row;
|
||||
vc.weightx = 1;
|
||||
vc.anchor = GridBagConstraints.WEST;
|
||||
vc.fill = GridBagConstraints.HORIZONTAL;
|
||||
vc.insets = UIScale.scale(new Insets(2, 0, 2, 0));
|
||||
add(valueLabel, vc);
|
||||
/** One traffic category: a row per figure under Total / In / Out headings. */
|
||||
private static final class TrafficTable extends JPanel {
|
||||
private final ConnectionStats.Kind kind;
|
||||
private final JLabel[][] cells;
|
||||
|
||||
TrafficTable(ConnectionStats.Kind kind) {
|
||||
super(new GridBagLayout());
|
||||
this.kind = kind;
|
||||
setOpaque(false);
|
||||
setBorder(new ScaledEmptyBorder(8, 10, 8, 10));
|
||||
|
||||
List<String> columns = ConnectionStatsFormat.COLUMNS;
|
||||
for (int c = 0; c < columns.size(); c++) {
|
||||
JLabel heading = label(columns.get(c));
|
||||
heading.setFont(heading.getFont().deriveFont(Font.ITALIC));
|
||||
add(heading, column(c + 1, 0));
|
||||
}
|
||||
List<TrafficRow> rows = ConnectionStatsFormat.trafficRows(null, kind);
|
||||
cells = new JLabel[rows.size()][];
|
||||
for (int r = 0; r < rows.size(); r++) {
|
||||
TrafficRow row = rows.get(r);
|
||||
add(label(row.label()), cell(0, r + 1, GridBagConstraints.WEST, new Insets(3, 0, 3, 8)));
|
||||
cells[r] = new JLabel[]{
|
||||
sized(label(row.total()), TOTAL_SAMPLE), sized(label(row.in()), VALUE_SAMPLE),
|
||||
sized(label(row.out()), VALUE_SAMPLE)};
|
||||
for (int c = 0; c < cells[r].length; c++) add(cells[r][c], column(c + 1, r + 1));
|
||||
}
|
||||
GridBagConstraints filler = cell(0, rows.size() + 1, GridBagConstraints.WEST, new Insets(0, 0, 0, 0));
|
||||
filler.weighty = 1;
|
||||
add(Box.createGlue(), filler);
|
||||
}
|
||||
|
||||
private static GridBagConstraints column(int x, int y) {
|
||||
GridBagConstraints c = cell(x, y, GridBagConstraints.WEST, new Insets(3, 8, 3, 8));
|
||||
c.weightx = 1;
|
||||
return c;
|
||||
}
|
||||
|
||||
void update(ConnectionStats s) {
|
||||
List<TrafficRow> rows = ConnectionStatsFormat.trafficRows(s, kind);
|
||||
for (int r = 0; r < rows.size(); r++) {
|
||||
cells[r][0].setText(rows.get(r).total());
|
||||
cells[r][1].setText(rows.get(r).in());
|
||||
cells[r][2].setText(rows.get(r).out());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user