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
This commit is contained in:
Wahaj18
2026-11-19 12:48:03 +05:00
committed by GitHub
parent 3473150ee9
commit 6d89a604c3
2 changed files with 154 additions and 0 deletions

View File

@@ -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<T,F> extends VBox
{
private static final KeyCombination COPY = new KeyCodeCombination(KeyCode.C, KeyCombination.SHORTCUT_DOWN);
private final ObservableList<Row<T>> mRows = FXCollections.observableArrayList();
private final FilteredList<Row<T>> mFilteredRows = new FilteredList<>(mRows);
private final Map<T,Row<T>> mRowMap = new HashMap<>();
@@ -71,6 +79,16 @@ public abstract class HistoryTableView<T,F> 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<T,F> 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<Row<T>,?> 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;

View File

@@ -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 <http://www.gnu.org/licenses/>
* ****************************************************************************
*/
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<Void> 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<String> 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<String,String>
{
TestView()
{
super(List.of(NowPlayingCells.column("name", "Name", null, Pos.CENTER_LEFT,
(Row<String> row) -> row.getItem(), null),
NowPlayingCells.column("upper", "Upper", null, Pos.CENTER_LEFT,
(Row<String> row) -> row.getItem().toUpperCase(), null)),
"test.history.table", null, null, "Filter", "Columns");
}
@Override
protected boolean include(String item)
{
return true;
}
}
}