Concise creation of simple DynamicTableColumns

This commit is contained in:
Florian Magin 2025-01-03 13:52:23 +01:00 committed by dragonmacher
parent e0a060660b
commit 74022d3586

View file

@ -17,6 +17,8 @@ package docking.widgets.table;
import java.util.*;
import ghidra.docking.settings.Settings;
import ghidra.framework.plugintool.ServiceProvider;
import ghidra.util.Msg;
public class TableColumnDescriptor<ROW_TYPE> {
@ -95,6 +97,54 @@ public class TableColumnDescriptor<ROW_TYPE> {
addVisibleColumn(column, -1, true);
}
/**
* Adds a column to the descriptor via an anonymous accessor function instead of an anonymous object
*
* The column type needs to be explicitly specified because there is no way to prevent erasure of the generic
* types of an anonymous function
*
*/
public <COLUMN_TYPE> void addColumn(String name, Class<COLUMN_TYPE> columnTypeClass, RowObjectAccessor<ROW_TYPE, COLUMN_TYPE> rowObjectAccessor, boolean visible) {
AbstractDynamicTableColumn<ROW_TYPE, COLUMN_TYPE, Object> column = new AbstractDynamicTableColumn<>() {
@Override
public String getColumnName() {
return name;
}
@Override
public COLUMN_TYPE getValue(ROW_TYPE rowObject, Settings settings, Object data, ServiceProvider serviceProvider) throws IllegalArgumentException {
return rowObjectAccessor.access(rowObject);
}
@Override
public Class<COLUMN_TYPE> getColumnClass() {
return columnTypeClass;
}
@Override
public Class<ROW_TYPE> getSupportedRowType() {
// The reflection tricks in the regular implementation won't work and will always return null
// because of type erasure
return null;
}
};
if (visible) {
addVisibleColumn(column);
} else {
addHiddenColumn(column);
}
}
/**
* Adds a visible column to the descriptor via an anonymous accessor function instead of an anonymous object
*
* The column type needs to be explicitly specified because there is no way to prevent erasure of the generic
* types of an anonymous function
*
*/
public <COLUMN_TYPE> void addColumn(String name, Class<COLUMN_TYPE> columnTypeClass, RowObjectAccessor<ROW_TYPE, COLUMN_TYPE> rowObjectAccessor) {
addColumn(name, columnTypeClass, rowObjectAccessor, true);
}
/**
* @param column the column to add
* @param sortOrdinal the <b>ordinal (i.e., 1, 2, 3...n)</b>, not the index (i.e, 0, 1, 2...n).
@ -129,4 +179,16 @@ public class TableColumnDescriptor<ROW_TYPE> {
return sortIndex - o.sortIndex;
}
}
/**
* A functional interface for accessing a column value from a row object
* This allows for the creation of columns with anonymous functions via {@link #addColumn}
* @param <ROW_TYPE>
* @param <COLUMN_TYPE>
*/
@FunctionalInterface
public interface RowObjectAccessor<ROW_TYPE, COLUMN_TYPE> {
public COLUMN_TYPE access(ROW_TYPE rowObject) throws IllegalArgumentException;
}
}