Ghidra is a software reverse engineering (SRE) framework
GP-2583 - Added the TableChooserExecutor.executeInBulk() method to allow script writers to process multiple selected table rows themselves instead of one-at-a-time.
dragonmachere87c050674b9
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/tablechooser/TableChooserDialog.java b/Ghidra/Features/Base/src/main/java/ghidra/app/tablechooser/TableChooserDialog.javaindex 44ab4669ba..e8068526ab 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/tablechooser/TableChooserDialog.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/tablechooser/TableChooserDialog.java@@ -147,6 +147,16 @@ public class TableChooserDialog extends DialogComponentProvidermodel.removeObject(rowObject);}+ /**+ * Returns true if the given object is still in the dialog. Clients can use this method to see+ * if the user has already processed the given item.+ * @param rowObject the row object+ * @return true if the object is still in the dialog+ */+ public boolean contains(AddressableRowObject rowObject) {+ return model.containsObject(rowObject);+ }+private void createTableModel() {// note: the task monitor is installed later when this model is added to the threaded panel@@ -227,6 +237,9 @@ public class TableChooserDialog extends DialogComponentProvidertry {List<AddressableRowObject> deleted = doProcessRowsInTransaction(rowObjects, monitor);+ if (monitor.isCancelled()) {+ return;+ }for (AddressableRowObject rowObject : deleted) {model.removeObject(rowObject);@@ -244,6 +257,10 @@ public class TableChooserDialog extends DialogComponentProviderTaskMonitor monitor) {List<AddressableRowObject> deleted = new ArrayList<>();+ if (executor.executeInBulk(rowObjects, deleted, monitor)) {+ return deleted;+ }+for (AddressableRowObject rowObject : rowObjects) {if (monitor.isCancelled()) {break;@@ -413,8 +430,7 @@ public class TableChooserDialog extends DialogComponentProvider}else {if (delegate instanceof GTableCellRenderer) {- renderer =- ((GTableCellRenderer) delegate).getTableCellRendererComponent(data);+ renderer = ((GTableCellRenderer) delegate).getTableCellRendererComponent(data);}else {renderer = delegate.getTableCellRendererComponent(data.getTable(),diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/tablechooser/TableChooserExecutor.java b/Ghidra/Features/Base/src/main/java/ghidra/app/tablechooser/TableChooserExecutor.javaindex 3414ea8f59..6a34041b17 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/tablechooser/TableChooserExecutor.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/tablechooser/TableChooserExecutor.java@@ -15,6 +15,10 @@*/package ghidra.app.tablechooser;+import java.util.List;++import ghidra.util.task.TaskMonitor;+/*** The interface clients must implement to use the {@link TableChooserDialog}. This class is the* callback that is used to process items from the dialog's table as users select one or more@@ -31,14 +35,48 @@ public interface TableChooserExecutor {public String getButtonName();/**- * Applies this executors action to the given rowObject. Return true if the given object- * should be removed from the table.- *+ * Applies this executors action to the given rowObject. Return true if the given object+ * should be removed from the table.+ ** <P>This method call will be wrapped in a transaction so the client does not have to do so.* Multiple selected rows will all be processed in a single transaction.- *+ ** @param rowObject the AddressRowObject to be executed upon* @return true if the rowObject should be removed from the table, false otherwise*/public boolean execute(AddressableRowObject rowObject);++ /**+ * A callback that clients can choose to use instead of {@link #execute(AddressableRowObject)}.+ * <p>+ * To use this method, simply override it to perform work on each item passed in. Due to+ * supporting backward compatibility, clients still have to implement+ * {@link #execute(AddressableRowObject)}. When using+ * {@link #executeInBulk(List, List, TaskMonitor)}, simply implement+ * {@link #execute(AddressableRowObject)} as a do-nothing method.+ * <p>+ * You are responsible for checking the cancelled state of the task monitor by calling+ * {@link TaskMonitor#isCancelled()}. This allows long-running operations to be cancelled. You+ * should also call {@link TaskMonitor#incrementProgress(long)} as you process each item in+ * order to show progress in the UI.+ * <p>+ * Note: the {@link #execute(AddressableRowObject)} method is only called with items that are+ * still in the dialog's table model. Some clients may programmatically manipulate the table+ * model by removing row objects via the dialog's add/remove methods. The+ * {@link #execute(AddressableRowObject)} method is only called for items that still exist in+ * the model. Contrastingly, this version of execute offers no such protection. Thus, if you+ * manipulate the table model yourself, you also need to ensure that any items you process in+ * this method are still in the dialog. To see if the item is still in the dialog, call+ * {@link TableChooserDialog#contains(AddressableRowObject)}.+ *+ * @param rowObjects the objects to be processed+ * @param deleted place any items to be removed from the table into this list+ * @param monitor the task monitor+ * @return true if you wish to execute items in bulk; always return true from this method if+ * you override it+ */+ public default boolean executeInBulk(List<AddressableRowObject> rowObjects,+ List<AddressableRowObject> deleted, TaskMonitor monitor) {+ return false;+ }}diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/tablechooser/TableChooserDialogTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/tablechooser/TableChooserDialogTest.javaindex 13e864c3a7..7cf4949a2d 100644--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/tablechooser/TableChooserDialogTest.java+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/tablechooser/TableChooserDialogTest.java@@ -45,6 +45,7 @@ import ghidra.program.model.data.DataType;import ghidra.program.model.listing.*;import ghidra.test.AbstractGhidraHeadedIntegrationTest;import ghidra.test.ToyProgramBuilder;+import ghidra.util.task.TaskMonitor;import resources.Icons;import util.CollectionUtils;@@ -54,7 +55,7 @@ public class TableChooserDialogTest extends AbstractGhidraHeadedIntegrationTestprivate static final TestExecutorDecision DEFAULT_DECISION = r -> true;private DummyPluginTool tool;- private SpyTableChooserExecutor executor;+ private SpyExecutor executor;private TableChooserDialog dialog;private TestAction testAction;@@ -63,7 +64,14 @@ public class TableChooserDialogTest extends AbstractGhidraHeadedIntegrationTest@Beforepublic void setUp() throws Exception {- executor = new SpyTableChooserExecutor();++ if (testName.getMethodName().contains("InBulk")) {+ executor = new SpyBulkTableChooserExecutor();+ }+ else {+ executor = new SpyTableChooserExecutor();+ }+createDialog(executor);}@@ -74,7 +82,7 @@ public class TableChooserDialogTest extends AbstractGhidraHeadedIntegrationTest});}- private void createDialog(SpyTableChooserExecutor dialogExecutor) throws Exception {+ private void createDialog(SpyExecutor dialogExecutor) throws Exception {executor = dialogExecutor;tool = new DummyPluginTool();@@ -237,8 +245,9 @@ public class TableChooserDialogTest extends AbstractGhidraHeadedIntegrationTesttestDecision = r -> {toProcess.remove(r);+ // Simulate the client removing items from the table while processing items// if not empty, remove one of the remaining items- if (!toProcess.isEmpty()) {+ if (!removedButNotExecuted.isEmpty()) {TestStubRowObject other = toProcess.remove(0);removedButNotExecuted.add(other);dialog.remove(other);@@ -365,6 +374,56 @@ public class TableChooserDialogTest extends AbstractGhidraHeadedIntegrationTestdialog.setSortState(TableSortState.createDefaultSortState(100));}+ @Test+ public void testCallback_ExecuteInBulk() throws Exception {++ int rowCount = getRowCount();+ List<TestStubRowObject> selected = selectRows(0, 1, 3);++ testDecision = r -> true;++ pressExecuteButton();+ waitForDialog();+ assertNotInDialog(selected);+ assertRowCount(rowCount - selected.size());+ }++ @Test+ public void testCalllbackRemovesItems_OtherItemNotSelected_ExecuteInBulk() {++ /*+ Select multiple items.+ Have the first callback remove one of the remaining *selected* items.+ The removed item should not itself get a callback.+ */++ int rowCount = getRowCount();+ List<TestStubRowObject> selected = selectRows(0, 1, 3);+ List<TestStubRowObject> toProcess = new ArrayList<>(selected);++ List<TestStubRowObject> removedButNotExecuted = new ArrayList<>();+ testDecision = r -> {+ toProcess.remove(r);++ // Simulate the client removing items from the table while processing items+ // if not empty, remove one of the remaining items+ if (removedButNotExecuted.isEmpty()) {+ TestStubRowObject other = toProcess.remove(0);+ removedButNotExecuted.add(other);+ dialog.remove(other);+ }+ return true; // remove 'r'+ };++ pressExecuteButton();+ waitForDialog();+ assertTrue(toProcess.isEmpty());++ assertNotInDialog(selected);+ assertRowCount(rowCount - selected.size());+ assertNotExecuted(removedButNotExecuted);+ }+//==================================================================================================// Private Methods//==================================================================================================@@ -517,9 +576,8 @@ public class TableChooserDialogTest extends AbstractGhidraHeadedIntegrationTestpublic boolean decide(AddressableRowObject rowObject);}- private class SpyTableChooserExecutor implements TableChooserExecutor {-- private Map<AddressableRowObject, AtomicInteger> callbacks = new HashMap<>();+ private abstract class SpyExecutor implements TableChooserExecutor {+ protected Map<AddressableRowObject, AtomicInteger> callbacks = new HashMap<>();@Overridepublic String getButtonName() {@@ -534,6 +592,13 @@ public class TableChooserDialogTest extends AbstractGhidraHeadedIntegrationTestreturn counter.get();}+ boolean wasExecuted(AddressableRowObject rowObject) {+ return callbacks.containsKey(rowObject);+ }+ }++ private class SpyTableChooserExecutor extends SpyExecutor {+@Overridepublic boolean execute(AddressableRowObject rowObject) {@@ -545,9 +610,42 @@ public class TableChooserDialogTest extends AbstractGhidraHeadedIntegrationTestboolean result = testDecision.decide(rowObject);return result;}+ }- boolean wasExecuted(AddressableRowObject rowObject) {- return callbacks.containsKey(rowObject);+ private class SpyBulkTableChooserExecutor extends SpyExecutor {++ @Override+ public boolean executeInBulk(List<AddressableRowObject> rowObjects,+ List<AddressableRowObject> deleted, TaskMonitor monitor) {++ for (AddressableRowObject rowObject : rowObjects) {+ if (monitor.isCancelled()) {+ break;+ }++ if (testDecision.decide(rowObject)) {+ deleted.add(rowObject);+ }++ if (!dialog.contains(rowObject)) {+ // this simulates the handling of the case where we remove items arbitrarily+ // while processing items in this list+ continue;+ }++ callbacks.merge(rowObject, new AtomicInteger(1), (k, v) -> {+ v.incrementAndGet();+ return v;+ });+ }++ return true;+ }++ @Override+ public boolean execute(AddressableRowObject rowObject) {+ fail("execute() called unexpectedly");+ return false;}}