Skip to content

Ghidra is a software reverse engineering (SRE) framework

Added convenience method to fix a sort state to handle the case when columns are removed from a table

dragonmacher79290c00ede1
diff --git a/Ghidra/Framework/Docking/src/main/java/docking/widgets/table/AbstractSortedTableModel.java b/Ghidra/Framework/Docking/src/main/java/docking/widgets/table/AbstractSortedTableModel.java
index ba6a3dca78..23e6b68cbb 100644
--- a/Ghidra/Framework/Docking/src/main/java/docking/widgets/table/AbstractSortedTableModel.java
+++ b/Ghidra/Framework/Docking/src/main/java/docking/widgets/table/AbstractSortedTableModel.java
@@ -157,6 +157,7 @@ public abstract class AbstractSortedTableModel<T> extends AbstractGTableModel<T>
return false; // more columns than we have
}
+ // verify the requested columns are sortable
for (int i = 0; i < columnCount; i++) {
ColumnSortState state = tableSortState.getColumnSortState(i);
if (state == null) {
@@ -171,6 +172,34 @@ public abstract class AbstractSortedTableModel<T> extends AbstractGTableModel<T>
return true;
}
+ /**
+ * A convenience method that will take a given sort state and remove from it any columns
+ * that cannot be sorted. This is useful if the client is restoring a sort state that
+ * contains columns that have been removed or are no longer sortable (such as during major
+ * table model rewrites).
+ *
+ * @param state the state
+ * @return the updated state
+ */
+ protected TableSortState cleanupTableSortState(TableSortState state) {
+
+ int columnCount = getColumnCount();
+ TableSortStateEditor editor = new TableSortStateEditor(state);
+ int n = editor.getSortedColumnCount();
+ for (int i = 0; i < n; i++) {
+ ColumnSortState ss = editor.getColumnSortState(i);
+ int columnIndex = ss.getColumnModelIndex();
+ if (columnIndex >= columnCount) {
+ editor.removeSortedColumn(columnIndex);
+ }
+ if (!isSortable(columnIndex)) {
+ editor.removeSortedColumn(columnIndex);
+ }
+ }
+
+ return editor.createTableSortState();
+ }
+
private void doSetTableSortState(final TableSortState newSortState) {
if (newSortState.equals(pendingSortState)) {
return; // there is an upcoming sort that matches the current request
diff --git a/Ghidra/Framework/Docking/src/main/java/docking/widgets/table/TableSortStateEditor.java b/Ghidra/Framework/Docking/src/main/java/docking/widgets/table/TableSortStateEditor.java
index 69229e19a3..4f346890c6 100644
--- a/Ghidra/Framework/Docking/src/main/java/docking/widgets/table/TableSortStateEditor.java
+++ b/Ghidra/Framework/Docking/src/main/java/docking/widgets/table/TableSortStateEditor.java
@@ -1,6 +1,5 @@
/* ###
* IP: GHIDRA
- * REVIEWED: YES
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,7 +20,7 @@ import java.util.*;
import docking.widgets.table.ColumnSortState.SortDirection;
public class TableSortStateEditor {
- private List<ColumnSortState> sortStates = new ArrayList<ColumnSortState>();
+ private List<ColumnSortState> sortStates = new ArrayList<>();
public TableSortStateEditor() {
// for creating from scratch
@@ -46,6 +45,10 @@ public class TableSortStateEditor {
addSortedColumn(new ColumnSortState(columnIndex, direction, getNextColumnSortOrder()));
}
+ public ColumnSortState getColumnSortState(int sortStateIndex) {
+ return sortStates.get(sortStateIndex);
+ }
+
private int getNextColumnSortOrder() {
return getSortedColumnCount() + 1;
}
@@ -68,8 +71,8 @@ public class TableSortStateEditor {
public void flipColumnSortDirection(int columnIndex) {
int existingIndex = indexOf(columnIndex);
if (existingIndex < 0) {
- throw new IllegalArgumentException("Cannot change sort direction "
- + "when column is not sorted");
+ throw new IllegalArgumentException(
+ "Cannot change sort direction " + "when column is not sorted");
}
ColumnSortState sortState = sortStates.get(existingIndex);