Add the Server Log window
Tools > Server Log (Ctrl+Shift+L), offered with b_virtualserver_log_view, reads the virtual server's log with logview, a hundred lines at a time, with Show Older going further back. As in TS3, lines can be narrowed by level, left out by exclude terms, coloured by keyword, and shown in UTC or local time; the choices are kept in the settings. The find bar, with its show-only-matches toggle, takes the place of TS3's separate search and filter fields. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -21,8 +21,9 @@ import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
/**
|
||||
* Find in the chat: highlights the query's matches in the selected conversation and
|
||||
* steps through them, newest first. Optionally shows only the messages that match.
|
||||
* Find in a log pane — the chat's selected conversation, or the server log: highlights the
|
||||
* query's matches and steps through them, newest first. Optionally shows only the
|
||||
* messages that match.
|
||||
*/
|
||||
final class ChatSearch implements FindBar.Listener {
|
||||
|
||||
@@ -40,7 +41,7 @@ final class ChatSearch implements FindBar.Listener {
|
||||
|
||||
private final Host host;
|
||||
private final FindBar bar = new FindBar(this);
|
||||
private final JToggleButton onlyMatching = bar.addFilterToggle("Show only matching messages");
|
||||
private final JToggleButton onlyMatching;
|
||||
|
||||
private final Highlighter.HighlightPainter matchPainter = new ThemedPainter(false);
|
||||
private final Highlighter.HighlightPainter currentPainter = new ThemedPainter(true);
|
||||
@@ -76,7 +77,12 @@ final class ChatSearch implements FindBar.Listener {
|
||||
};
|
||||
|
||||
ChatSearch(Host host) {
|
||||
this(host, "Show only matching messages");
|
||||
}
|
||||
|
||||
ChatSearch(Host host, String filterTooltip) {
|
||||
this.host = host;
|
||||
onlyMatching = bar.addFilterToggle(filterTooltip);
|
||||
}
|
||||
|
||||
FindBar bar() {
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
package com.ts3client.ui;
|
||||
|
||||
import com.formdev.flatlaf.util.ScaledEmptyBorder;
|
||||
import com.formdev.flatlaf.util.UIScale;
|
||||
|
||||
import javax.swing.Box;
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.JList;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.ListSelectionModel;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Component;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.Window;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A small modal editor for a list of text-keyed items: Add, Remove, optionally Move up and
|
||||
* down, and OK/Cancel. Edits a copy and writes it back only on OK.
|
||||
*/
|
||||
abstract class ListEditorDialog<T> extends JDialog {
|
||||
|
||||
protected final DefaultListModel<T> model = new DefaultListModel<>();
|
||||
protected final JList<T> list = new JList<>(model);
|
||||
private final String itemPrompt;
|
||||
private final JPanel south = new JPanel(new BorderLayout(0, UIScale.scale(8)));
|
||||
private boolean accepted;
|
||||
|
||||
/**
|
||||
* @param intro text above the list, or {@code null}
|
||||
* @param itemPrompt what Add and a double-click ask for
|
||||
* @param movable whether the order matters, offering Move up and down
|
||||
*/
|
||||
ListEditorDialog(Window owner, String title, String intro, String itemPrompt, List<T> items, boolean movable) {
|
||||
super(owner, title, ModalityType.APPLICATION_MODAL);
|
||||
this.itemPrompt = itemPrompt;
|
||||
items.forEach(model::addElement);
|
||||
|
||||
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
list.addListSelectionListener(e -> {
|
||||
if (!e.getValueIsAdjusting()) selectionChanged();
|
||||
});
|
||||
list.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
if (e.getClickCount() == 2 && list.getSelectedIndex() >= 0) rename(list.getSelectedIndex());
|
||||
}
|
||||
});
|
||||
JScrollPane scroll = new JScrollPane(list);
|
||||
scroll.setPreferredSize(UIScale.scale(new Dimension(260, 200)));
|
||||
|
||||
JPanel buttons = new JPanel();
|
||||
buttons.setLayout(new BoxLayout(buttons, BoxLayout.Y_AXIS));
|
||||
buttons.setOpaque(false);
|
||||
addButton(buttons, "Add", this::add);
|
||||
JButton remove = addButton(buttons, "Remove", this::remove);
|
||||
JButton up = movable ? addButton(buttons, "Move Up", () -> move(-1)) : null;
|
||||
JButton down = movable ? addButton(buttons, "Move Down", () -> move(1)) : null;
|
||||
list.addListSelectionListener(e -> {
|
||||
int i = list.getSelectedIndex();
|
||||
remove.setEnabled(i >= 0);
|
||||
if (up != null) up.setEnabled(i > 0);
|
||||
if (down != null) down.setEnabled(i >= 0 && i < model.size() - 1);
|
||||
});
|
||||
remove.setEnabled(false);
|
||||
if (up != null) up.setEnabled(false);
|
||||
if (down != null) down.setEnabled(false);
|
||||
|
||||
JPanel center = new JPanel(new BorderLayout(UIScale.scale(8), UIScale.scale(6)));
|
||||
center.setOpaque(false);
|
||||
if (intro != null) center.add(ReasonDialog.label(intro), BorderLayout.NORTH);
|
||||
center.add(scroll, BorderLayout.CENTER);
|
||||
center.add(buttons, BorderLayout.EAST);
|
||||
|
||||
JButton ok = new JButton("OK");
|
||||
ok.addActionListener(e -> {
|
||||
accepted = true;
|
||||
dispose();
|
||||
});
|
||||
JButton cancel = new JButton("Cancel");
|
||||
cancel.addActionListener(e -> dispose());
|
||||
JPanel okCancel = new JPanel(new FlowLayout(FlowLayout.RIGHT, UIScale.scale(6), 0));
|
||||
okCancel.setOpaque(false);
|
||||
okCancel.add(ok);
|
||||
okCancel.add(cancel);
|
||||
|
||||
south.setOpaque(false);
|
||||
south.add(okCancel, BorderLayout.SOUTH);
|
||||
|
||||
JPanel content = new JPanel(new BorderLayout(0, UIScale.scale(8)));
|
||||
content.setBackground(Theme.windowBg());
|
||||
content.setBorder(new ScaledEmptyBorder(10, 12, 10, 12));
|
||||
content.add(center, BorderLayout.CENTER);
|
||||
content.add(south, BorderLayout.SOUTH);
|
||||
setContentPane(content);
|
||||
getRootPane().setDefaultButton(ok);
|
||||
Dialogs.closeOnEscape(this);
|
||||
}
|
||||
|
||||
/** Controls under the list acting on the selected item, or {@code null}. */
|
||||
protected JComponent extraControls() {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void selectionChanged() {
|
||||
}
|
||||
|
||||
/** The item that {@code text} names, typed into Add, or a rename of {@code old} ({@code null} on Add). */
|
||||
protected abstract T item(String text, T old);
|
||||
|
||||
protected abstract String text(T item);
|
||||
|
||||
/** Shows the dialog; on OK copies the edited list into {@code target}. */
|
||||
protected boolean showAndApply(List<T> target) {
|
||||
JComponent extra = extraControls();
|
||||
if (extra != null) south.add(extra, BorderLayout.NORTH);
|
||||
selectionChanged();
|
||||
pack();
|
||||
setLocationRelativeTo(getOwner());
|
||||
setVisible(true);
|
||||
if (!accepted) return false;
|
||||
target.clear();
|
||||
target.addAll(Collections.list(model.elements()));
|
||||
return true;
|
||||
}
|
||||
|
||||
private JButton addButton(JPanel column, String text, Runnable action) {
|
||||
JButton b = new JButton(text);
|
||||
b.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||||
b.setMaximumSize(new Dimension(Integer.MAX_VALUE, b.getPreferredSize().height));
|
||||
b.addActionListener(e -> action.run());
|
||||
if (column.getComponentCount() > 0) column.add(Box.createVerticalStrut(UIScale.scale(4)));
|
||||
column.add(b);
|
||||
return b;
|
||||
}
|
||||
|
||||
private void add() {
|
||||
String text = ask(itemPrompt, "");
|
||||
if (text == null) return;
|
||||
model.addElement(item(text, null));
|
||||
list.setSelectedIndex(model.size() - 1);
|
||||
}
|
||||
|
||||
private void rename(int index) {
|
||||
String text = ask(itemPrompt, text(model.get(index)));
|
||||
if (text != null) model.set(index, item(text, model.get(index)));
|
||||
}
|
||||
|
||||
private String ask(String prompt, String initial) {
|
||||
Object answer = JOptionPane.showInputDialog(this, prompt, getTitle(), JOptionPane.PLAIN_MESSAGE,
|
||||
null, null, initial);
|
||||
String text = answer == null ? null : answer.toString().trim();
|
||||
return text == null || text.isEmpty() ? null : text;
|
||||
}
|
||||
|
||||
private void remove() {
|
||||
int i = list.getSelectedIndex();
|
||||
if (i < 0) return;
|
||||
model.remove(i);
|
||||
if (!model.isEmpty()) list.setSelectedIndex(Math.min(i, model.size() - 1));
|
||||
}
|
||||
|
||||
private void move(int delta) {
|
||||
int i = list.getSelectedIndex();
|
||||
int j = i + delta;
|
||||
if (i < 0 || j < 0 || j >= model.size()) return;
|
||||
List<T> items = new ArrayList<>(Collections.list(model.elements()));
|
||||
Collections.swap(items, i, j);
|
||||
model.clear();
|
||||
items.forEach(model::addElement);
|
||||
list.setSelectedIndex(j);
|
||||
}
|
||||
}
|
||||
@@ -318,6 +318,16 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
|
||||
if (selected != null) selected.showComplaints();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canViewServerLog() {
|
||||
return selected != null && selected.session().canViewServerLog();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onShowServerLog() {
|
||||
if (selected != null) selected.showServerLog();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canListPrivilegeKeys() {
|
||||
return selected != null && selected.session().canListPrivilegeKeys();
|
||||
|
||||
@@ -86,6 +86,11 @@ final class MainMenuBar extends JMenuBar {
|
||||
|
||||
void onShowComplaints();
|
||||
|
||||
/** Whether the current server lets us read its log; asked as the Tools menu opens. */
|
||||
boolean canViewServerLog();
|
||||
|
||||
void onShowServerLog();
|
||||
|
||||
/** Whether the current server lets us list its privilege keys; asked as the Permissions menu opens. */
|
||||
boolean canListPrivilegeKeys();
|
||||
|
||||
@@ -122,6 +127,7 @@ final class MainMenuBar extends JMenuBar {
|
||||
private JMenuItem deleteAvatarItem;
|
||||
private JMenuItem banListItem;
|
||||
private JMenuItem complaintsItem;
|
||||
private JMenuItem serverLogItem;
|
||||
private JMenuItem privilegeKeysItem;
|
||||
private JMenuItem usePrivilegeKeyItem;
|
||||
|
||||
@@ -276,6 +282,11 @@ final class MainMenuBar extends JMenuBar {
|
||||
complaintsItem.addActionListener(e -> {
|
||||
if (listener.canViewComplaints()) listener.onShowComplaints();
|
||||
});
|
||||
serverLogItem = new JMenuItem("Server Log", Icons.of("SERVER_LOG"));
|
||||
serverLogItem.setAccelerator(KeyStroke.getKeyStroke("control shift L"));
|
||||
serverLogItem.addActionListener(e -> {
|
||||
if (listener.canViewServerLog()) listener.onShowServerLog();
|
||||
});
|
||||
JMenuItem options = new JMenuItem("Options…", Icons.of("SETTINGS"));
|
||||
options.setAccelerator(KeyStroke.getKeyStroke("alt P"));
|
||||
options.addActionListener(e -> listener.onShowSettings());
|
||||
@@ -285,6 +296,7 @@ final class MainMenuBar extends JMenuBar {
|
||||
tools.addSeparator();
|
||||
tools.add(banListItem);
|
||||
tools.add(complaintsItem);
|
||||
tools.add(serverLogItem);
|
||||
tools.add(adminSeparator);
|
||||
tools.add(options);
|
||||
// Permissions arrive after the connection does, so the item is judged as the menu opens
|
||||
@@ -294,9 +306,11 @@ final class MainMenuBar extends JMenuBar {
|
||||
public void menuSelected(MenuEvent e) {
|
||||
boolean bans = banListItem.isEnabled() && listener.canViewBanList();
|
||||
boolean complaints = complaintsItem.isEnabled() && listener.canViewComplaints();
|
||||
boolean serverLog = serverLogItem.isEnabled() && listener.canViewServerLog();
|
||||
banListItem.setVisible(bans);
|
||||
complaintsItem.setVisible(complaints);
|
||||
adminSeparator.setVisible(bans || complaints);
|
||||
serverLogItem.setVisible(serverLog);
|
||||
adminSeparator.setVisible(bans || complaints || serverLog);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -338,6 +352,7 @@ final class MainMenuBar extends JMenuBar {
|
||||
deleteAvatarItem.setEnabled(connected);
|
||||
banListItem.setEnabled(connected);
|
||||
complaintsItem.setEnabled(connected);
|
||||
serverLogItem.setEnabled(connected);
|
||||
privilegeKeysItem.setEnabled(connected);
|
||||
usePrivilegeKeyItem.setEnabled(connected);
|
||||
awayItem.setSelected(away);
|
||||
|
||||
@@ -0,0 +1,366 @@
|
||||
package com.ts3client.ui;
|
||||
|
||||
import com.formdev.flatlaf.util.ScaledEmptyBorder;
|
||||
import com.formdev.flatlaf.util.UIScale;
|
||||
import com.ts3client.config.ServerLogSettings;
|
||||
import com.ts3client.config.ServerLogSettings.Highlight;
|
||||
import com.ts3client.config.Settings;
|
||||
import com.ts3client.net.ServerLogEntry;
|
||||
import com.ts3client.net.ServerLogEntry.Level;
|
||||
import com.ts3client.net.ServerLogPage;
|
||||
import com.ts3client.net.TeamspeakConnection;
|
||||
import com.ts3client.text.TextQuery;
|
||||
|
||||
import javax.swing.AbstractAction;
|
||||
import javax.swing.Box;
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.JEditorPane;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextPane;
|
||||
import javax.swing.KeyStroke;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.text.BadLocationException;
|
||||
import javax.swing.text.DefaultStyledDocument;
|
||||
import javax.swing.text.SimpleAttributeSet;
|
||||
import javax.swing.text.StyleConstants;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Font;
|
||||
import java.awt.Frame;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The virtual server's log, as the official client's Tools > Server Log window: the newest
|
||||
* hundred lines, more on Show Older, narrowed down by level and exclude terms and coloured
|
||||
* by keyword. Find (with "show only matches") stands in for TS3's separate search and
|
||||
* include-filter fields.
|
||||
*
|
||||
* <p>One per server tab and modeless, like the ban list; the tab closes it when the
|
||||
* connection goes away.
|
||||
*/
|
||||
final class ServerLogDialog extends JDialog {
|
||||
|
||||
private static final DateTimeFormatter TIME = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
private static final String TITLE = "Server Log";
|
||||
private static final int LEVEL_WIDTH = "Critical".length();
|
||||
|
||||
private final TeamspeakConnection conn;
|
||||
private final Settings settings;
|
||||
private final ServerLogSettings view;
|
||||
|
||||
private final JTextPane log = new JTextPane() {
|
||||
/** Long lines scroll sideways rather than wrap, keeping the columns lined up. */
|
||||
@Override
|
||||
public boolean getScrollableTracksViewportWidth() {
|
||||
return getParent() == null || getUI().getPreferredSize(this).width <= getParent().getWidth();
|
||||
}
|
||||
};
|
||||
private final DefaultStyledDocument doc = new DefaultStyledDocument();
|
||||
private final ChatSearch search = new ChatSearch(new ChatSearch.Host() {
|
||||
@Override
|
||||
public JEditorPane selectedLog() {
|
||||
return log;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showMatching(TextQuery filter) {
|
||||
onlyMatching = filter;
|
||||
render(false);
|
||||
}
|
||||
}, "Show only matching lines");
|
||||
private final Map<Level, JCheckBox> levelBoxes = new EnumMap<>(Level.class);
|
||||
private final JComboBox<String> timeZone = new JComboBox<>(new String[]{"UTC time", "Local time"});
|
||||
private final JButton olderButton = new JButton("Show Older");
|
||||
private final JLabel status = new JLabel(" ");
|
||||
|
||||
/** Everything read so far, oldest first. */
|
||||
private final List<ServerLogEntry> entries = new ArrayList<>();
|
||||
private long olderFrom;
|
||||
private TextQuery onlyMatching;
|
||||
private boolean loading;
|
||||
|
||||
ServerLogDialog(Frame owner, TeamspeakConnection conn, Settings settings) {
|
||||
super(owner, TITLE, false);
|
||||
this.conn = conn;
|
||||
this.settings = settings;
|
||||
this.view = settings.serverLog;
|
||||
|
||||
JPanel center = new JPanel(new BorderLayout());
|
||||
center.setOpaque(false);
|
||||
center.add(buildLog(), BorderLayout.CENTER);
|
||||
center.add(search.bar(), BorderLayout.SOUTH);
|
||||
|
||||
JPanel content = new JPanel(new BorderLayout(0, UIScale.scale(6)));
|
||||
content.setBackground(Theme.windowBg());
|
||||
content.setBorder(new ScaledEmptyBorder(10, 12, 10, 12));
|
||||
content.add(buildToolbar(), BorderLayout.NORTH);
|
||||
content.add(center, BorderLayout.CENTER);
|
||||
content.add(buildBottom(), BorderLayout.SOUTH);
|
||||
setContentPane(content);
|
||||
bindKeys();
|
||||
|
||||
serverNameChanged();
|
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||
setPreferredSize(UIScale.scale(new Dimension(900, 520)));
|
||||
pack();
|
||||
setLocationRelativeTo(owner);
|
||||
search.open();
|
||||
reload();
|
||||
}
|
||||
|
||||
// ---- construction ----
|
||||
|
||||
private JPanel buildToolbar() {
|
||||
JPanel bar = new JPanel();
|
||||
bar.setLayout(new BoxLayout(bar, BoxLayout.X_AXIS));
|
||||
bar.setOpaque(false);
|
||||
String[] labels = {"Critical", "Errors", "Warnings", "Info"};
|
||||
int[] mnemonics = {KeyEvent.VK_C, KeyEvent.VK_E, KeyEvent.VK_W, KeyEvent.VK_I};
|
||||
for (int i = 0; i < ServerLogSettings.SHOWN_LEVELS.size(); i++) {
|
||||
Level level = ServerLogSettings.SHOWN_LEVELS.get(i);
|
||||
JCheckBox box = new JCheckBox(labels[i], view.levels.contains(level));
|
||||
box.setMnemonic(mnemonics[i]);
|
||||
box.setOpaque(false);
|
||||
box.addActionListener(e -> {
|
||||
if (box.isSelected()) view.levels.add(level);
|
||||
else view.levels.remove(level);
|
||||
settingsChanged();
|
||||
});
|
||||
levelBoxes.put(level, box);
|
||||
bar.add(box);
|
||||
bar.add(Box.createHorizontalStrut(UIScale.scale(6)));
|
||||
}
|
||||
bar.add(Box.createHorizontalStrut(UIScale.scale(12)));
|
||||
|
||||
JButton highlighting = new JButton("Highlighting…");
|
||||
highlighting.setMnemonic(KeyEvent.VK_H);
|
||||
highlighting.addActionListener(e -> {
|
||||
if (ServerLogHighlightDialog.edit(this, view.highlights)) settingsChanged();
|
||||
});
|
||||
JButton exclude = new JButton("Exclude Filter…");
|
||||
exclude.setMnemonic(KeyEvent.VK_X);
|
||||
exclude.addActionListener(e -> {
|
||||
if (ServerLogExcludeDialog.edit(this, view.excludes)) settingsChanged();
|
||||
});
|
||||
bar.add(highlighting);
|
||||
bar.add(Box.createHorizontalStrut(UIScale.scale(6)));
|
||||
bar.add(exclude);
|
||||
bar.add(Box.createHorizontalGlue());
|
||||
|
||||
timeZone.setSelectedIndex(view.localTime ? 1 : 0);
|
||||
timeZone.setMaximumSize(timeZone.getPreferredSize());
|
||||
timeZone.addActionListener(e -> {
|
||||
view.localTime = timeZone.getSelectedIndex() == 1;
|
||||
settingsChanged();
|
||||
});
|
||||
bar.add(timeZone);
|
||||
return bar;
|
||||
}
|
||||
|
||||
private JScrollPane buildLog() {
|
||||
log.setEditable(false);
|
||||
log.setDocument(doc);
|
||||
log.setFont(new Font(Font.MONOSPACED, Font.PLAIN, UIScale.scale(12)));
|
||||
log.setBackground(Theme.chatBg());
|
||||
log.setForeground(Theme.chatText());
|
||||
log.setBorder(new ScaledEmptyBorder(4, 6, 4, 6));
|
||||
JScrollPane scroll = new JScrollPane(log);
|
||||
scroll.getViewport().setBackground(Theme.chatBg());
|
||||
return scroll;
|
||||
}
|
||||
|
||||
private JPanel buildBottom() {
|
||||
JButton reload = new JButton("Reload", Icons.of("REFRESH"));
|
||||
reload.setMnemonic(KeyEvent.VK_R);
|
||||
reload.setToolTipText("<html>Reload the newest lines<br>(F5)");
|
||||
reload.addActionListener(e -> reload());
|
||||
olderButton.setMnemonic(KeyEvent.VK_O);
|
||||
olderButton.setEnabled(false);
|
||||
olderButton.addActionListener(e -> loadOlder());
|
||||
JButton close = new JButton("Close");
|
||||
close.addActionListener(e -> dispose());
|
||||
|
||||
JPanel left = new JPanel();
|
||||
left.setLayout(new BoxLayout(left, BoxLayout.X_AXIS));
|
||||
left.setOpaque(false);
|
||||
left.add(reload);
|
||||
left.add(Box.createHorizontalStrut(UIScale.scale(6)));
|
||||
left.add(olderButton);
|
||||
|
||||
JPanel row = new JPanel(new BorderLayout());
|
||||
row.setOpaque(false);
|
||||
row.add(left, BorderLayout.WEST);
|
||||
row.add(close, BorderLayout.EAST);
|
||||
|
||||
JPanel bottom = new JPanel(new BorderLayout(0, UIScale.scale(8)));
|
||||
bottom.setOpaque(false);
|
||||
bottom.add(status, BorderLayout.NORTH);
|
||||
bottom.add(row, BorderLayout.SOUTH);
|
||||
status.setForeground(Theme.chatEvent());
|
||||
return bottom;
|
||||
}
|
||||
|
||||
private void bindKeys() {
|
||||
bind(KeyStroke.getKeyStroke("F5"), this::reload);
|
||||
bind(KeyStroke.getKeyStroke("control W"), this::dispose);
|
||||
bind(KeyStroke.getKeyStroke("control F"), search::open);
|
||||
Dialogs.closeOnEscape(this);
|
||||
}
|
||||
|
||||
private void bind(KeyStroke key, Runnable action) {
|
||||
String name = "ts3-serverlog-" + key;
|
||||
getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(key, name);
|
||||
getRootPane().getActionMap().put(name, new AbstractAction() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
action.run();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ---- data ----
|
||||
|
||||
void serverNameChanged() {
|
||||
String name = conn.getModel().getServerName();
|
||||
setTitle(name == null || name.isBlank() ? TITLE : TITLE + " - " + name);
|
||||
}
|
||||
|
||||
void connectionLost() {
|
||||
dispose();
|
||||
}
|
||||
|
||||
private void reload() {
|
||||
request(0);
|
||||
}
|
||||
|
||||
private void loadOlder() {
|
||||
if (olderFrom > 0) request(olderFrom);
|
||||
}
|
||||
|
||||
/** Reads the newest page ({@code from} 0, replacing what is shown) or the one ending at {@code from}. */
|
||||
private void request(long from) {
|
||||
if (loading) return;
|
||||
loading = true;
|
||||
olderButton.setEnabled(false);
|
||||
status.setForeground(Theme.chatEvent());
|
||||
status.setText("Loading…");
|
||||
conn.requestServerLog(from, (page, error) -> SwingUtilities.invokeLater(() -> {
|
||||
loading = false;
|
||||
if (!isDisplayable()) return;
|
||||
if (error != null) {
|
||||
failed(error);
|
||||
olderButton.setEnabled(olderFrom > 0);
|
||||
return;
|
||||
}
|
||||
received(page, from > 0);
|
||||
}));
|
||||
}
|
||||
|
||||
private void received(ServerLogPage page, boolean older) {
|
||||
if (!older) entries.clear();
|
||||
entries.addAll(0, page.entries());
|
||||
olderFrom = page.olderFrom();
|
||||
olderButton.setEnabled(page.hasOlder());
|
||||
render(!older);
|
||||
if (older) log.setCaretPosition(0);
|
||||
}
|
||||
|
||||
private void failed(String error) {
|
||||
status.setForeground(Theme.isDark() ? new Color(0xFF6B6B) : new Color(0xC00000));
|
||||
status.setText(error.toLowerCase(Locale.ROOT).contains("insufficient")
|
||||
? "Insufficient permissions to view Server Log"
|
||||
: "Error requesting server log: " + error);
|
||||
}
|
||||
|
||||
private void settingsChanged() {
|
||||
settings.save();
|
||||
render(false);
|
||||
}
|
||||
|
||||
// ---- rendering ----
|
||||
|
||||
/** Lays the log out again; {@code toEnd} scrolls to the newest line, otherwise the view stays put. */
|
||||
private void render(boolean toEnd) {
|
||||
int channelWidth = 0;
|
||||
for (ServerLogEntry e : entries) channelWidth = Math.max(channelWidth, e.channel().length());
|
||||
|
||||
int scrollY = log.getVisibleRect().y;
|
||||
int shown = 0;
|
||||
try {
|
||||
doc.remove(0, doc.getLength());
|
||||
for (ServerLogEntry e : entries) {
|
||||
String line = format(e, channelWidth);
|
||||
if (!view.shows(e.level(), line)) continue;
|
||||
if (onlyMatching != null && !onlyMatching.matches(line)) continue;
|
||||
doc.insertString(doc.getLength(), line + "\n", style(view.highlightFor(line)));
|
||||
shown++;
|
||||
}
|
||||
} catch (BadLocationException ignored) {
|
||||
// appending at the end never fails
|
||||
}
|
||||
|
||||
status.setForeground(Theme.chatEvent());
|
||||
if (entries.isEmpty()) status.setText("No log entries available");
|
||||
else if (shown == entries.size()) status.setText(lines(shown));
|
||||
else status.setText(shown + " of " + lines(entries.size()));
|
||||
|
||||
if (toEnd) {
|
||||
log.setCaretPosition(doc.getLength());
|
||||
} else {
|
||||
SwingUtilities.invokeLater(() -> log.scrollRectToVisible(
|
||||
new Rectangle(0, scrollY, 1, log.getVisibleRect().height)));
|
||||
}
|
||||
}
|
||||
|
||||
private static String lines(int n) {
|
||||
return n == 1 ? "1 line" : n + " lines";
|
||||
}
|
||||
|
||||
private String format(ServerLogEntry e, int channelWidth) {
|
||||
String time = "";
|
||||
if (e.timestamp() != null) {
|
||||
LocalDateTime t = view.localTime
|
||||
? e.timestamp().atOffset(ZoneOffset.UTC).atZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime()
|
||||
: e.timestamp();
|
||||
time = TIME.format(t);
|
||||
}
|
||||
return pad(time, 19) + " " + pad(e.channel(), channelWidth) + " "
|
||||
+ pad(levelName(e.level()), LEVEL_WIDTH) + " " + e.message();
|
||||
}
|
||||
|
||||
private static String levelName(Level level) {
|
||||
String name = level.name();
|
||||
return name.charAt(0) + name.substring(1).toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
private static String pad(String s, int width) {
|
||||
return s.length() >= width ? s : s + " ".repeat(width - s.length());
|
||||
}
|
||||
|
||||
private static SimpleAttributeSet style(Highlight h) {
|
||||
SimpleAttributeSet a = new SimpleAttributeSet();
|
||||
if (h == null) return a;
|
||||
if (h.foreground() != null) StyleConstants.setForeground(a, new Color(h.foreground()));
|
||||
if (h.background() != null) StyleConstants.setBackground(a, new Color(h.background()));
|
||||
return a;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.ts3client.ui;
|
||||
|
||||
import java.awt.Window;
|
||||
import java.util.List;
|
||||
|
||||
/** The Server Log's exclude filter: terms whose lines are left out. */
|
||||
final class ServerLogExcludeDialog extends ListEditorDialog<String> {
|
||||
|
||||
private ServerLogExcludeDialog(Window owner, List<String> terms) {
|
||||
super(owner, "Exclude Filter", "Exclude lines containing:", "Term:", terms, false);
|
||||
}
|
||||
|
||||
/** @return whether the terms were changed */
|
||||
static boolean edit(Window owner, List<String> terms) {
|
||||
return new ServerLogExcludeDialog(owner, terms).showAndApply(terms);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String item(String text, String old) {
|
||||
return text;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String text(String item) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package com.ts3client.ui;
|
||||
|
||||
import com.formdev.flatlaf.util.UIScale;
|
||||
import com.ts3client.config.ServerLogSettings.Highlight;
|
||||
|
||||
import javax.swing.DefaultListCellRenderer;
|
||||
import javax.swing.Icon;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JColorChooser;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JList;
|
||||
import javax.swing.JPanel;
|
||||
import java.awt.BasicStroke;
|
||||
import java.awt.Color;
|
||||
import java.awt.Component;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.Window;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
/** The Server Log's highlighting: keywords and the colours their lines are drawn in, first match first. */
|
||||
final class ServerLogHighlightDialog extends ListEditorDialog<Highlight> {
|
||||
|
||||
private JButton foreground;
|
||||
private JButton background;
|
||||
private JButton clearForeground;
|
||||
private JButton clearBackground;
|
||||
|
||||
private ServerLogHighlightDialog(Window owner, List<Highlight> highlights) {
|
||||
super(owner, "Highlighting", null, "Keyword:", highlights, true);
|
||||
list.setCellRenderer(new DefaultListCellRenderer() {
|
||||
@Override
|
||||
public Component getListCellRendererComponent(JList<?> l, Object value, int index,
|
||||
boolean selected, boolean focused) {
|
||||
Highlight h = (Highlight) value;
|
||||
super.getListCellRendererComponent(l, h.keyword(), index, selected, focused);
|
||||
if (!selected) {
|
||||
if (h.foreground() != null) setForeground(new Color(h.foreground()));
|
||||
if (h.background() != null) setBackground(new Color(h.background()));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** @return whether the highlights were changed */
|
||||
static boolean edit(Window owner, List<Highlight> highlights) {
|
||||
return new ServerLogHighlightDialog(owner, highlights).showAndApply(highlights);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Highlight item(String text, Highlight old) {
|
||||
return old == null ? new Highlight(text, null, null) : new Highlight(text, old.foreground(), old.background());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String text(Highlight item) {
|
||||
return item.keyword();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JComponent extraControls() {
|
||||
foreground = new JButton("Text Color…");
|
||||
foreground.addActionListener(e -> choose("Text Color", Highlight::foreground, true));
|
||||
clearForeground = new JButton("None");
|
||||
clearForeground.addActionListener(e -> update(null, true));
|
||||
background = new JButton("Background…");
|
||||
background.addActionListener(e -> choose("Background", Highlight::background, false));
|
||||
clearBackground = new JButton("None");
|
||||
clearBackground.addActionListener(e -> update(null, false));
|
||||
|
||||
JPanel row = new JPanel(new FlowLayout(FlowLayout.LEFT, UIScale.scale(4), 0));
|
||||
row.setOpaque(false);
|
||||
row.add(foreground);
|
||||
row.add(clearForeground);
|
||||
row.add(new JLabel(" "));
|
||||
row.add(background);
|
||||
row.add(clearBackground);
|
||||
return row;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void selectionChanged() {
|
||||
Highlight h = list.getSelectedValue();
|
||||
foreground.setEnabled(h != null);
|
||||
background.setEnabled(h != null);
|
||||
clearForeground.setEnabled(h != null && h.foreground() != null);
|
||||
clearBackground.setEnabled(h != null && h.background() != null);
|
||||
foreground.setIcon(new Swatch(h == null ? null : h.foreground()));
|
||||
background.setIcon(new Swatch(h == null ? null : h.background()));
|
||||
}
|
||||
|
||||
private void choose(String title, Function<Highlight, Integer> current, boolean isForeground) {
|
||||
Highlight h = list.getSelectedValue();
|
||||
if (h == null) return;
|
||||
Integer rgb = current.apply(h);
|
||||
Color chosen = JColorChooser.showDialog(this, title, rgb == null ? Theme.chatText() : new Color(rgb));
|
||||
if (chosen != null) update(chosen.getRGB() & 0xFFFFFF, isForeground);
|
||||
}
|
||||
|
||||
private void update(Integer rgb, boolean isForeground) {
|
||||
int i = list.getSelectedIndex();
|
||||
if (i < 0) return;
|
||||
Highlight h = model.get(i);
|
||||
model.set(i, isForeground ? new Highlight(h.keyword(), rgb, h.background())
|
||||
: new Highlight(h.keyword(), h.foreground(), rgb));
|
||||
selectionChanged();
|
||||
}
|
||||
|
||||
/** A colour sample; struck through when there is none. */
|
||||
private record Swatch(Integer rgb) implements Icon {
|
||||
@Override
|
||||
public void paintIcon(Component c, Graphics g, int x, int y) {
|
||||
Graphics2D g2 = (Graphics2D) g.create();
|
||||
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
int w = getIconWidth() - 1;
|
||||
int h = getIconHeight() - 1;
|
||||
if (rgb != null) {
|
||||
g2.setColor(new Color(rgb));
|
||||
g2.fillRect(x, y, w, h);
|
||||
}
|
||||
g2.setColor(Theme.border());
|
||||
g2.drawRect(x, y, w, h);
|
||||
if (rgb == null) {
|
||||
g2.setStroke(new BasicStroke(UIScale.scale(1f)));
|
||||
g2.drawLine(x, y + h, x + w, y);
|
||||
}
|
||||
g2.dispose();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIconWidth() {
|
||||
return UIScale.scale(14);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIconHeight() {
|
||||
return UIScale.scale(14);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,6 +56,7 @@ final class ServerTab {
|
||||
private BanListDialog banList;
|
||||
private PrivilegeKeysDialog privilegeKeys;
|
||||
private ComplaintsDialog complaints;
|
||||
private ServerLogDialog serverLog;
|
||||
|
||||
ServerTab(MainFrame host, Settings settings, IdentityStore identities, AudioBackend audio,
|
||||
SoundNotifier sounds, ContactStore contacts, PictureDownloads pictures) {
|
||||
@@ -201,6 +202,16 @@ final class ServerTab {
|
||||
complaints.toFront();
|
||||
}
|
||||
|
||||
/** Opens the server's log, or brings the already open window forward. */
|
||||
void showServerLog() {
|
||||
if (!session.isConnected()) return;
|
||||
if (serverLog == null || !serverLog.isDisplayable()) {
|
||||
serverLog = new ServerLogDialog(host, conn, settings);
|
||||
}
|
||||
serverLog.setVisible(true);
|
||||
serverLog.toFront();
|
||||
}
|
||||
|
||||
/** Asks for a privilege key and redeems it on this server. */
|
||||
void usePrivilegeKey() {
|
||||
if (!session.isConnected()) return;
|
||||
@@ -224,7 +235,7 @@ final class ServerTab {
|
||||
privilegeKeys.toFront();
|
||||
}
|
||||
|
||||
/** The server's name changed or the connection went; the ban list, complaints and privilege keys windows follow. */
|
||||
/** The server's name changed or the connection went; the server admin windows follow. */
|
||||
void serverWindowsChanged(boolean connected) {
|
||||
if (banList != null) {
|
||||
if (connected) banList.serverNameChanged();
|
||||
@@ -234,6 +245,10 @@ final class ServerTab {
|
||||
if (connected) complaints.serverNameChanged();
|
||||
else complaints.connectionLost();
|
||||
}
|
||||
if (serverLog != null) {
|
||||
if (connected) serverLog.serverNameChanged();
|
||||
else serverLog.connectionLost();
|
||||
}
|
||||
if (privilegeKeys != null) {
|
||||
if (connected) privilegeKeys.serverNameChanged();
|
||||
else privilegeKeys.connectionLost();
|
||||
|
||||
Reference in New Issue
Block a user