From 6d89a604c346c4362943e69c02538996e5c207af Mon Sep 17 00:00:00 2001 From: Wahaj18 <48466561+Wahaj18@users.noreply.github.com> Date: Thu, 19 Nov 2026 12:48:03 +0500 Subject: [PATCH] Copy selected event and message rows with Ctrl-C (#2691) The JavaFX Now Playing tables lost the Swing JTable copy action, so Ctrl-C in the Events and Messages tabs did nothing. The history table now allows selecting several rows and copies them to the clipboard, one line per row with the visible column values separated by tabs, as the Swing table did. Fixes #2687 --- .../gui/nowplaying/HistoryTableView.java | 44 +++++++ .../gui/nowplaying/HistoryTableViewTest.java | 110 ++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 src/test/java/io/github/dsheirer/gui/nowplaying/HistoryTableViewTest.java diff --git a/src/main/java/io/github/dsheirer/gui/nowplaying/HistoryTableView.java b/src/main/java/io/github/dsheirer/gui/nowplaying/HistoryTableView.java index 153ebd7d3..127c271dc 100644 --- a/src/main/java/io/github/dsheirer/gui/nowplaying/HistoryTableView.java +++ b/src/main/java/io/github/dsheirer/gui/nowplaying/HistoryTableView.java @@ -23,6 +23,7 @@ import io.github.dsheirer.preference.swing.SwingPreference; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.StringJoiner; import javafx.beans.property.IntegerProperty; import javafx.beans.property.SimpleIntegerProperty; import javafx.collections.FXCollections; @@ -30,8 +31,14 @@ import javafx.collections.ObservableList; import javafx.collections.transformation.FilteredList; import javafx.collections.transformation.SortedList; import javafx.scene.control.Label; +import javafx.scene.control.SelectionMode; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; +import javafx.scene.input.Clipboard; +import javafx.scene.input.ClipboardContent; +import javafx.scene.input.KeyCode; +import javafx.scene.input.KeyCodeCombination; +import javafx.scene.input.KeyCombination; import javafx.scene.layout.Priority; import javafx.scene.layout.VBox; @@ -45,6 +52,7 @@ import javafx.scene.layout.VBox; */ public abstract class HistoryTableView extends VBox { + private static final KeyCombination COPY = new KeyCodeCombination(KeyCode.C, KeyCombination.SHORTCUT_DOWN); private final ObservableList> mRows = FXCollections.observableArrayList(); private final FilteredList> mFilteredRows = new FilteredList<>(mRows); private final Map> mRowMap = new HashMap<>(); @@ -71,6 +79,16 @@ public abstract class HistoryTableView extends VBox mTableView.setItems(sortedRows); mTableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY_SUBSEQUENT_COLUMNS); mTableView.setPlaceholder(new Label("")); + mTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); + mTableView.setOnKeyPressed(event -> { + if(COPY.match(event)) + { + ClipboardContent content = new ClipboardContent(); + content.putString(getSelectedText()); + Clipboard.getSystemClipboard().setContent(content); + event.consume(); + } + }); mTableColumnMonitor = new TableColumnMonitor<>(mTableView, tableKey, swingPreference, swingTableKey); mHistoryControls = new HistoryControls<>(filterEditorTitle, this::clear, this::setHistorySize, mTableColumnMonitor, columnsTitle); @@ -160,6 +178,32 @@ public abstract class HistoryTableView extends VBox mRows.forEach(Row::refresh); } + /** + * Text of the selected rows, one line per row with the visible column values separated by tabs. + */ + private String getSelectedText() + { + StringJoiner text = new StringJoiner("\n"); + + for(int row = 0; row < mTableView.getItems().size(); row++) + { + if(mTableView.getSelectionModel().isSelected(row)) + { + StringJoiner line = new StringJoiner("\t"); + + for(TableColumn,?> column: mTableView.getVisibleLeafColumns()) + { + Object value = column.getCellData(row); + line.add(value == null ? "" : value.toString()); + } + + text.add(line.toString()); + } + } + + return text.toString(); + } + private void setHistorySize(int historySize) { mHistorySize = historySize; diff --git a/src/test/java/io/github/dsheirer/gui/nowplaying/HistoryTableViewTest.java b/src/test/java/io/github/dsheirer/gui/nowplaying/HistoryTableViewTest.java new file mode 100644 index 000000000..be9792ab7 --- /dev/null +++ b/src/test/java/io/github/dsheirer/gui/nowplaying/HistoryTableViewTest.java @@ -0,0 +1,110 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.gui.nowplaying; + +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import javafx.application.Platform; +import javafx.event.Event; +import javafx.geometry.Pos; +import javafx.scene.control.TableView; +import javafx.scene.input.Clipboard; +import javafx.scene.input.KeyCode; +import javafx.scene.input.KeyEvent; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests copying the selected history rows to the clipboard. + */ +public class HistoryTableViewTest +{ + @BeforeAll + public static void startToolkit() throws Exception + { + //Run without a display + System.setProperty("glass.platform", "Headless"); + System.setProperty("prism.order", "sw"); + CompletableFuture started = new CompletableFuture<>(); + + try + { + Platform.startup(() -> started.complete(null)); + } + catch(IllegalStateException e) + { + started.complete(null); //Already started + } + + started.get(10, TimeUnit.SECONDS); + } + + @Test + public void copiesSelectedRowsToClipboard() throws Exception + { + CompletableFuture copied = new CompletableFuture<>(); + + Platform.runLater(() -> { + try + { + TestView view = new TestView(); + view.add("a"); + view.add("b"); + view.add("c"); + + //Newest first: c, b, a + TableView table = (TableView)view.lookup(".table-view"); + table.getSelectionModel().select(0); + table.getSelectionModel().select(2); + + Clipboard.getSystemClipboard().clear(); + Event.fireEvent(table, new KeyEvent(KeyEvent.KEY_PRESSED, "", "", KeyCode.C, false, + true, false, false)); + copied.complete(Clipboard.getSystemClipboard().getString()); + } + catch(Throwable t) + { + copied.completeExceptionally(t); + } + }); + + assertEquals("c\tC\na\tA", copied.get(10, TimeUnit.SECONDS)); + } + + private static class TestView extends HistoryTableView + { + TestView() + { + super(List.of(NowPlayingCells.column("name", "Name", null, Pos.CENTER_LEFT, + (Row row) -> row.getItem(), null), + NowPlayingCells.column("upper", "Upper", null, Pos.CENTER_LEFT, + (Row row) -> row.getItem().toUpperCase(), null)), + "test.history.table", null, null, "Filter", "Columns"); + } + + @Override + protected boolean include(String item) + { + return true; + } + } +}