mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-22 13:35:42 -05:00
#2742 Events view: select rows and copy them to the clipboard
Multiple row selection (Ctrl+A selects the page), Ctrl+C and context menu copy as tab separated text with a header of the visible columns. The selection is kept while live events update the rows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -34,6 +34,7 @@ import io.github.dsheirer.gui.calls.CallAliasResolver;
|
||||
import io.github.dsheirer.gui.calls.CallsView;
|
||||
import io.github.dsheirer.gui.table.ColumnConfigurationButton;
|
||||
import io.github.dsheirer.gui.table.TableColumnMonitor;
|
||||
import io.github.dsheirer.gui.table.TableRowCopy;
|
||||
import io.github.dsheirer.module.decode.event.DecodeEventType;
|
||||
import io.github.dsheirer.protocol.Protocol;
|
||||
import io.github.dsheirer.sample.Listener;
|
||||
@@ -51,6 +52,7 @@ import javafx.scene.control.DatePicker;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.ListCell;
|
||||
import javafx.scene.control.MenuItem;
|
||||
import javafx.scene.control.SelectionMode;
|
||||
import javafx.scene.control.SeparatorMenuItem;
|
||||
import javafx.scene.control.TableColumn;
|
||||
import javafx.scene.control.TableRow;
|
||||
@@ -84,6 +86,9 @@ import java.util.prefs.Preferences;
|
||||
*
|
||||
* The table shows one page of search results, newest first. On the first page, new events that match the search
|
||||
* criteria are added at the top as they arrive, and events that change (e.g. the duration of a call) are updated.
|
||||
*
|
||||
* Rows can be selected (Ctrl+A selects the page) and copied to the clipboard as tab separated text (Ctrl+C or the
|
||||
* context menu), which pastes into a spreadsheet.
|
||||
*/
|
||||
public class EventsView extends VBox
|
||||
{
|
||||
@@ -178,6 +183,14 @@ public class EventsView extends VBox
|
||||
mTableColumnMonitor = new TableColumnMonitor<>(mTableView, TABLE_KEY);
|
||||
mTableView.setPlaceholder(new Label("No events"));
|
||||
mTableView.setRowFactory(table -> createRow());
|
||||
mTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
|
||||
mTableView.setOnKeyPressed(event -> {
|
||||
if(TableRowCopy.isCopy(event))
|
||||
{
|
||||
copySelected();
|
||||
event.consume();
|
||||
}
|
||||
});
|
||||
VBox.setVgrow(mTableView, Priority.ALWAYS);
|
||||
|
||||
getChildren().addAll(createSearchBar(), mTableView, createPagingBar());
|
||||
@@ -463,7 +476,12 @@ public class EventsView extends VBox
|
||||
row.setOnContextMenuRequested(event -> {
|
||||
if(!row.isEmpty())
|
||||
{
|
||||
mTableView.getSelectionModel().select(row.getIndex());
|
||||
//Keep a multiple selection when the click is on one of the selected rows
|
||||
if(!mTableView.getSelectionModel().isSelected(row.getIndex()))
|
||||
{
|
||||
mTableView.getSelectionModel().clearAndSelect(row.getIndex());
|
||||
}
|
||||
|
||||
createContextMenu(row.getItem()).show(row, event.getScreenX(), event.getScreenY());
|
||||
event.consume();
|
||||
}
|
||||
@@ -517,9 +535,58 @@ public class EventsView extends VBox
|
||||
Clipboard.getSystemClipboard().setContent(content);
|
||||
});
|
||||
menu.getItems().add(copy);
|
||||
|
||||
int selected = mTableView.getSelectionModel().getSelectedIndices().size();
|
||||
MenuItem copyRows = new MenuItem(selected > 1 ? "Copy " + selected + " Selected Rows" : "Copy Selected Row");
|
||||
copyRows.setOnAction(e -> copySelected());
|
||||
MenuItem selectAll = new MenuItem("Select All Rows on This Page");
|
||||
selectAll.setOnAction(e -> mTableView.getSelectionModel().selectAll());
|
||||
menu.getItems().addAll(new SeparatorMenuItem(), copyRows, selectAll);
|
||||
return menu;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the selected rows to the clipboard as tab separated text, with a header line of the column names. The
|
||||
* visible columns are copied in their displayed order, and the rows in their displayed order.
|
||||
*/
|
||||
void copySelected()
|
||||
{
|
||||
int count = TableRowCopy.copySelected(mTableView);
|
||||
|
||||
if(count > 0)
|
||||
{
|
||||
mStatusLabel.setText("Copied " + count + (count == 1 ? " event" : " events") + " to the clipboard");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies a change to the rows and keeps the selected events selected.
|
||||
*/
|
||||
private void preserveSelection(Runnable change)
|
||||
{
|
||||
TableRowCopy.preserveSelection(mTableView, EventsView::getKeys, change);
|
||||
}
|
||||
|
||||
/**
|
||||
* Identity keys of an event row: the live event key and the database row id
|
||||
*/
|
||||
private static List<Object> getKeys(EventRecord event)
|
||||
{
|
||||
List<Object> keys = new ArrayList<>(2);
|
||||
|
||||
if(event.getEventKey() != 0)
|
||||
{
|
||||
keys.add("K" + event.getEventKey());
|
||||
}
|
||||
|
||||
if(event.getId() != 0)
|
||||
{
|
||||
keys.add("I" + event.getId());
|
||||
}
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
private void applySearch()
|
||||
{
|
||||
long start = toMillis(mFromDatePicker.getValue(), false);
|
||||
@@ -592,7 +659,6 @@ public class EventsView extends VBox
|
||||
return;
|
||||
}
|
||||
|
||||
EventRecord selected = mTableView.getSelectionModel().getSelectedItem();
|
||||
List<EventRecord> events = new ArrayList<>();
|
||||
|
||||
if(offset == 0)
|
||||
@@ -620,19 +686,9 @@ public class EventsView extends VBox
|
||||
events.add(live != null ? live : stored);
|
||||
}
|
||||
|
||||
mEvents.setAll(events);
|
||||
preserveSelection(() -> mEvents.setAll(events));
|
||||
mTotal = result.total();
|
||||
updatePaging();
|
||||
|
||||
if(selected != null)
|
||||
{
|
||||
int index = indexOf(selected);
|
||||
|
||||
if(index >= 0)
|
||||
{
|
||||
mTableView.getSelectionModel().select(index);
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -691,10 +747,11 @@ public class EventsView extends VBox
|
||||
|
||||
mLiveEvents.put(event.getEventKey(), event);
|
||||
int index = indexOf(event);
|
||||
final EventRecord update = event;
|
||||
|
||||
if(index >= 0)
|
||||
{
|
||||
mEvents.set(index, event);
|
||||
preserveSelection(() -> mEvents.set(index, update));
|
||||
}
|
||||
else if(mOffset == 0 && mCriteria.matches(event))
|
||||
{
|
||||
@@ -708,13 +765,16 @@ public class EventsView extends VBox
|
||||
|
||||
if(insert < mPageSize)
|
||||
{
|
||||
mEvents.add(insert, event);
|
||||
mTotal++;
|
||||
final int position = insert;
|
||||
preserveSelection(() -> {
|
||||
mEvents.add(position, update);
|
||||
|
||||
if(mEvents.size() > mPageSize)
|
||||
{
|
||||
mEvents.removeLast();
|
||||
}
|
||||
if(mEvents.size() > mPageSize)
|
||||
{
|
||||
mEvents.removeLast();
|
||||
}
|
||||
});
|
||||
mTotal++;
|
||||
|
||||
updatePaging();
|
||||
}
|
||||
@@ -743,7 +803,7 @@ public class EventsView extends VBox
|
||||
|
||||
if(index >= 0)
|
||||
{
|
||||
mEvents.set(index, updated);
|
||||
preserveSelection(() -> mEvents.set(index, updated));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
201
src/main/java/io/github/dsheirer/gui/table/TableRowCopy.java
Normal file
201
src/main/java/io/github/dsheirer/gui/table/TableRowCopy.java
Normal file
@@ -0,0 +1,201 @@
|
||||
/*
|
||||
* *****************************************************************************
|
||||
* 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.table;
|
||||
|
||||
import javafx.beans.value.ObservableValue;
|
||||
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.input.KeyEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Copying table rows to the clipboard as tab separated text (pastes into a spreadsheet), and keeping a multiple row
|
||||
* selection while the rows of a live table are replaced.
|
||||
*/
|
||||
public final class TableRowCopy
|
||||
{
|
||||
/**
|
||||
* Column property with a Function<T,String> that provides the copied text for a column whose cell value
|
||||
* isn't text (e.g. an icon column).
|
||||
*/
|
||||
public static final String COPY_TEXT = "table.row.copy.text";
|
||||
public static final KeyCombination COPY_KEY = new KeyCodeCombination(KeyCode.C, KeyCombination.SHORTCUT_DOWN);
|
||||
|
||||
private TableRowCopy()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the copied text function for a column whose cell value isn't the text to copy.
|
||||
*/
|
||||
public static <T> void setCopyText(TableColumn<T,?> column, Function<T,String> text)
|
||||
{
|
||||
column.getProperties().put(COPY_TEXT, text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if the key event is the copy shortcut (Ctrl+C, or Cmd+C on macOS)
|
||||
*/
|
||||
public static boolean isCopy(KeyEvent event)
|
||||
{
|
||||
return COPY_KEY.match(event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the selected rows of the table (in displayed order, with the visible columns) to the clipboard.
|
||||
* @return number of rows copied
|
||||
*/
|
||||
public static <T> int copySelected(TableView<T> table)
|
||||
{
|
||||
List<Integer> indices = new ArrayList<>(table.getSelectionModel().getSelectedIndices());
|
||||
indices.sort(null);
|
||||
List<T> rows = new ArrayList<>();
|
||||
|
||||
for(Integer index: indices)
|
||||
{
|
||||
if(index != null && index >= 0 && index < table.getItems().size())
|
||||
{
|
||||
rows.add(table.getItems().get(index));
|
||||
}
|
||||
}
|
||||
|
||||
if(!rows.isEmpty())
|
||||
{
|
||||
ClipboardContent content = new ClipboardContent();
|
||||
content.putString(toText(table.getVisibleLeafColumns(), rows));
|
||||
Clipboard.getSystemClipboard().setContent(content);
|
||||
}
|
||||
|
||||
return rows.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tab separated text for the rows: a header line with the column names and a line per row. Tabs and line breaks
|
||||
* in values are replaced with spaces so that each row stays on one line.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> String toText(List<TableColumn<T,?>> columns, List<T> rows)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
List<String> header = new ArrayList<>();
|
||||
|
||||
for(TableColumn<T,?> column: columns)
|
||||
{
|
||||
header.add(clean(TableColumnMonitor.getName(column)));
|
||||
}
|
||||
|
||||
sb.append(String.join("\t", header)).append(System.lineSeparator());
|
||||
|
||||
for(T row: rows)
|
||||
{
|
||||
List<String> cells = new ArrayList<>();
|
||||
|
||||
for(TableColumn<T,?> column: columns)
|
||||
{
|
||||
Object copyText = column.getProperties().get(COPY_TEXT);
|
||||
String text;
|
||||
|
||||
if(copyText instanceof Function<?,?> function)
|
||||
{
|
||||
text = ((Function<T,String>)function).apply(row);
|
||||
}
|
||||
else
|
||||
{
|
||||
ObservableValue<?> value = column.getCellObservableValue(row);
|
||||
text = value != null && value.getValue() != null ? value.getValue().toString() : "";
|
||||
}
|
||||
|
||||
cells.add(clean(text));
|
||||
}
|
||||
|
||||
sb.append(String.join("\t", cells)).append(System.lineSeparator());
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String clean(String value)
|
||||
{
|
||||
return value == null ? "" : value.replace("\r\n", " ").replace('\t', ' ').replace('\n', ' ')
|
||||
.replace('\r', ' ');
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies a change to the table rows and keeps the selected rows selected. Replacing a row (e.g. a live update)
|
||||
* would otherwise drop it from the selection.
|
||||
* @param table with the rows
|
||||
* @param keys identity keys of a row (e.g. live key and database id) - rows that share a key are the same row
|
||||
* @param change to apply to the rows
|
||||
*/
|
||||
public static <T> void preserveSelection(TableView<T> table, Function<T,Collection<?>> keys, Runnable change)
|
||||
{
|
||||
List<T> selected = new ArrayList<>(table.getSelectionModel().getSelectedItems());
|
||||
|
||||
if(selected.isEmpty())
|
||||
{
|
||||
change.run();
|
||||
return;
|
||||
}
|
||||
|
||||
Set<Object> selectedKeys = new HashSet<>();
|
||||
|
||||
for(T row: selected)
|
||||
{
|
||||
if(row != null)
|
||||
{
|
||||
selectedKeys.addAll(keys.apply(row));
|
||||
}
|
||||
}
|
||||
|
||||
change.run();
|
||||
|
||||
List<Integer> indices = new ArrayList<>();
|
||||
|
||||
for(int x = 0; x < table.getItems().size(); x++)
|
||||
{
|
||||
for(Object key: keys.apply(table.getItems().get(x)))
|
||||
{
|
||||
if(selectedKeys.contains(key))
|
||||
{
|
||||
indices.add(x);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
table.getSelectionModel().clearSelection();
|
||||
|
||||
if(!indices.isEmpty())
|
||||
{
|
||||
int[] rest = indices.subList(1, indices.size()).stream().mapToInt(Integer::intValue).toArray();
|
||||
table.getSelectionModel().selectIndices(indices.getFirst(), rest);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user