Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-422 - Data Types - Improve performance of and and task monitor to the copy/move data types task

dragonmacher605a6b94b597
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/actions/PasteAction.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/actions/PasteAction.java
index 83d8ce2b75..90c77c2ada 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/actions/PasteAction.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/actions/PasteAction.java
@@ -158,7 +158,8 @@ public class PasteAction extends DockingAction {
ActionType actionType = getActionType(dataTypeTreeNode);
DataTypeTreeCopyMoveTask task =
- new DataTypeTreeCopyMoveTask(destinationNode, nodeList, actionType, gTree,
+ new DataTypeTreeCopyMoveTask(destinationNode, nodeList, actionType,
+ (DataTypeArchiveGTree) gTree,
plugin.getConflictHandler());
tool.execute(task, 250);
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/tree/DataTypeArchiveGTree.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/tree/DataTypeArchiveGTree.java
index 03187cd512..363df7e002 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/tree/DataTypeArchiveGTree.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/tree/DataTypeArchiveGTree.java
@@ -35,6 +35,7 @@ import ghidra.framework.plugintool.PluginTool;
import ghidra.program.model.data.*;
import ghidra.program.model.data.Composite;
import ghidra.program.model.data.Enum;
+import ghidra.program.model.listing.Program;
import ghidra.util.UniversalID;
import ghidra.util.task.TaskMonitor;
import resources.MultiIcon;
@@ -156,6 +157,10 @@ public class DataTypeArchiveGTree extends GTree {
reloadTree();
}
+ public Program getProgram() {
+ return plugin.getProgram();
+ }
+
private void reloadTree() {
GTreeState treeState = getTreeState();
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/tree/DataTypeDragNDropHandler.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/tree/DataTypeDragNDropHandler.java
index 8d24c35d32..1dbeca3bc4 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/tree/DataTypeDragNDropHandler.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/tree/DataTypeDragNDropHandler.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.
@@ -16,6 +15,16 @@
*/
package ghidra.app.plugin.core.datamgr.tree;
+import java.awt.datatransfer.*;
+import java.awt.dnd.DnDConstants;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import docking.dnd.GenericDataFlavor;
+import docking.widgets.tree.GTree;
+import docking.widgets.tree.GTreeNode;
+import docking.widgets.tree.support.GTreeDragNDropHandler;
import generic.jar.ResourceFile;
import ghidra.app.plugin.core.datamgr.DataTypeManagerPlugin;
import ghidra.app.plugin.core.datamgr.archive.FileArchive;
@@ -25,16 +34,6 @@ import ghidra.program.model.data.*;
import ghidra.util.Msg;
import ghidra.util.task.Task;
-import java.awt.Component;
-import java.awt.datatransfer.*;
-import java.awt.dnd.DnDConstants;
-import java.io.IOException;
-import java.util.*;
-
-import docking.dnd.GenericDataFlavor;
-import docking.widgets.tree.GTreeNode;
-import docking.widgets.tree.support.GTreeDragNDropHandler;
-
public class DataTypeDragNDropHandler implements GTreeDragNDropHandler {
private static DataFlavor localDataTypeTreeFlavor = createLocalTreeNodeFlavor();
@@ -46,7 +45,7 @@ public class DataTypeDragNDropHandler implements GTreeDragNDropHandler {
public static DataFlavor[] restrictedFlavors = { localDataTypeTreeFlavor };
- private final Component component;
+ private final GTree tree;
private final DataTypeManagerPlugin plugin;
@@ -62,39 +61,41 @@ public class DataTypeDragNDropHandler implements GTreeDragNDropHandler {
return null;
}
- public DataTypeDragNDropHandler(DataTypeManagerPlugin plugin, Component component) {
+ public DataTypeDragNDropHandler(DataTypeManagerPlugin plugin, GTree tree) {
this.plugin = plugin;
- this.component = component;
+ this.tree = tree;
}
@Override
- @SuppressWarnings("unchecked")
- // old API call
- public void drop(GTreeNode destUserData, Transferable transferable, int dropAction) {
+ @SuppressWarnings("unchecked") // old API call
+ public void drop(GTreeNode destinationNode, Transferable transferable, int dropAction) {
try {
List<GTreeNode> list =
(List<GTreeNode>) transferable.getTransferData(localDataTypeTreeFlavor);
- if (list.contains(destUserData)) { // don't allow drop on dragged nodes.
+ if (list.contains(destinationNode)) { // don't allow drop on dragged nodes.
return;
}
ActionType actionType =
dropAction == DnDConstants.ACTION_COPY ? ActionType.COPY : ActionType.MOVE;
Task task =
- new DataTypeTreeCopyMoveTask(destUserData, list, actionType, component,
+ new DataTypeTreeCopyMoveTask(destinationNode, list, actionType,
+ (DataTypeArchiveGTree) tree,
plugin.getConflictHandler());
plugin.getTool().execute(task, 250);
}
catch (UnsupportedFlavorException e) {
+ Msg.error(this, "Unable to perform drop operation", e);
}
catch (IOException e) {
+ Msg.error(this, "Unable to perform drop operation", e);
}
}
@Override
- public DataFlavor[] getSupportedDataFlavors(List<GTreeNode> dragUserData) {
+ public DataFlavor[] getSupportedDataFlavors(List<GTreeNode> draggedNodes) {
// single, datatype node supports both datatype dragging *and* local tree dragging
- if (dragUserData.size() == 1) {
- GTreeNode node = dragUserData.get(0);
+ if (draggedNodes.size() == 1) {
+ GTreeNode node = draggedNodes.get(0);
if (node instanceof DataTypeNode) {
DataType dataType = ((DataTypeNode) node).getDataType();
@@ -134,10 +135,10 @@ public class DataTypeDragNDropHandler implements GTreeDragNDropHandler {
else if (flavor.equals(DataFlavor.javaFileListFlavor)) {
List<?> nodeList = dragUserData;
ArrayList<ResourceFile> fileList = new ArrayList<ResourceFile>();
- for (Iterator<?> iterator = nodeList.iterator(); iterator.hasNext();) {
+ for (Object node : nodeList) {
- ArchiveNode node = (ArchiveNode) iterator.next();
- FileArchive archive = (FileArchive) node.getArchive();
+ ArchiveNode archiveNode = (ArchiveNode) node;
+ FileArchive archive = (FileArchive) archiveNode.getArchive();
ResourceFile file = archive.getFile();
fileList.add(file);
}
@@ -201,8 +202,8 @@ public class DataTypeDragNDropHandler implements GTreeDragNDropHandler {
}
private boolean containsFlavor(DataFlavor[] flavors, DataFlavor flavor) {
- for (int i = 0; i < flavors.length; i++) {
- if (flavors[i].equals(flavor)) {
+ for (DataFlavor f : flavors) {
+ if (f.equals(flavor)) {
return true;
}
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/util/DataTypeTreeCopyMoveTask.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/util/DataTypeTreeCopyMoveTask.java
index 0be129c791..350f3fb979 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/util/DataTypeTreeCopyMoveTask.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/util/DataTypeTreeCopyMoveTask.java
@@ -15,59 +15,69 @@
*/
package ghidra.app.plugin.core.datamgr.util;
+import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import docking.widgets.OptionDialog;
+import docking.widgets.tree.GTreeNode;
+import docking.widgets.tree.GTreeState;
import ghidra.app.plugin.core.datamgr.DataTypeSyncInfo;
import ghidra.app.plugin.core.datamgr.DataTypeSynchronizer;
-import ghidra.app.plugin.core.datamgr.archive.*;
+import ghidra.app.plugin.core.datamgr.archive.Archive;
+import ghidra.app.plugin.core.datamgr.archive.ProgramArchive;
import ghidra.app.plugin.core.datamgr.tree.*;
import ghidra.program.model.data.*;
+import ghidra.program.model.listing.Program;
import ghidra.util.*;
import ghidra.util.exception.*;
import ghidra.util.task.Task;
import ghidra.util.task.TaskMonitor;
-import java.awt.Component;
-import java.lang.reflect.InvocationTargetException;
-import java.util.*;
-
-import javax.swing.SwingUtilities;
-
-import docking.widgets.OptionDialog;
-import docking.widgets.tree.GTreeNode;
-
/**
* Task for handling drop operations.
*/
public class DataTypeTreeCopyMoveTask extends Task {
+
+ // If the total number of nodes is small, we won't need to collapse the tree before deleting
+ // the nodes to avoid excess tree events. This number is very arbitrary. This number is
+ // used to compare the number of dragged nodes, which may include categories whose child
+ // count is not reflected in this number. This could mean that thousands of nodes will be
+ // processed, but the actual drag count could be much less.
+ private static final int NODE_COUNT_FOR_COLLAPSING_TREE = 100;
+
public enum ActionType {
COPY, MOVE
}
+ private DataTypeArchiveGTree gTree;
private GTreeNode destinationNode;
- private List<GTreeNode> droppedNodeList;
- private ActionType actionType;
+ private List<GTreeNode> droppedNodes;
+ private Archive sourceArchive;
private Archive destinationArchive;
- private Component component;
+
+ private ActionType actionType;
private DataTypeConflictHandler conflictHandler;
+ private List<String> errors = new ArrayList<>();
+
+ // for testing
+ DataTypeTreeCopyMoveTask() {
+ super("Drag/Drop", true, true, true);
+ }
- /**
- *
- * @param treePanel
- * @param destNode destination node for the data; could be null
- * if this is a "drop anywhere" situation.
- * @param data may be a data type or a list of DataTypeTreeNodes
- * @param chosenFlavor data flavor that dictates what data is
- * @param actionType either ActionType.COPY or ActionTYPE.MOVE
- * @param conflictHandler data type conflict handler
- */
public DataTypeTreeCopyMoveTask(GTreeNode destinationNode, List<GTreeNode> droppedNodeList,
- ActionType actionType, Component component, DataTypeConflictHandler conflictHandler) {
+ ActionType actionType, DataTypeArchiveGTree gTree,
+ DataTypeConflictHandler conflictHandler) {
super("Drag/Drop", true, true, true);
this.destinationNode = destinationNode;
- this.droppedNodeList = droppedNodeList;
+ this.droppedNodes = droppedNodeList;
this.actionType = actionType;
- this.component = component;
- this.destinationArchive = findArchive(destinationNode);
+ this.gTree = gTree;
this.conflictHandler = conflictHandler;
+ this.destinationArchive = findArchive(destinationNode);
+
+ GTreeNode firstNode = droppedNodes.get(0);
+ this.sourceArchive = findArchive(firstNode);
}
private Archive findArchive(GTreeNode node) {
@@ -80,144 +90,255 @@ public class DataTypeTreeCopyMoveTask extends Task {
return null;
}
- /* (non-Javadoc)
- * @see ghidra.util.task.Task#run(ghidra.util.task.TaskMonitor)
- */
+ private boolean enableEvents(Program program, boolean enable) {
+ if (program == null) {
+ return false;
+ }
+
+ boolean wasEnabled = program.isSendingEvents();
+ program.setEventsEnabled(enable);
+ return wasEnabled;
+ }
+
@Override
- public void run(TaskMonitor monitor) {
- monitor.setMessage("Drag/Drop Categories/Data Types");
- monitor.initialize(droppedNodeList.size());
- droppedNodeList = filterList(droppedNodeList);
+ public void run(TaskMonitor monitor) throws CancelledException {
+
+ //
+ // Note: we disable the program's events to avoid UI painting deadlock as the copy/move
+ // is generating changes to the program.
+ //
+ Program program = gTree.getProgram();
+ boolean wasEnabled = enableEvents(program, false);
+ try {
+ doRun(monitor);
+ }
+ finally {
+ enableEvents(program, wasEnabled);
+ }
+ }
- GTreeNode firstNode = droppedNodeList.get(0);
- Archive sourceArchive = findArchive(firstNode);
- for (GTreeNode node : droppedNodeList) {
- if (sourceArchive != findArchive(node)) {
- Msg.showError(this, component, "Copy Failed",
- "All dragged data types must be from the same archive!");
- return;
- }
+ private void doRun(TaskMonitor monitor) {
+
+ int nodeCount = droppedNodes.size();
+ filterRedundantNodes();
+
+ if (checkForDifferentSourceArchives()) {
+ return;
}
- if (sourceArchive != destinationArchive &&
- !(destinationArchive instanceof ProgramArchive) &&
- (sourceArchive instanceof ProgramArchive) && sourceArchive.isModifiable()) {
- try {
- associateDataTypes(sourceArchive,
- destinationArchive.getDataTypeManager().getLocalSourceArchive());
+ //
+ // Note: we collapse the node before performing this work because there is a
+ // potential for a large number of events to be generated. Further, if the
+ // given archive node has many children (like 10s of thousands), then the
+ // copious events generated herein could lock the UI. By closing the node,
+ // the tree is not invalidating/validating its cache as a result of these
+ // events.
+ //
+ GTreeState treeState = gTree.getTreeState();
+ try {
+ if (nodeCount > NODE_COUNT_FOR_COLLAPSING_TREE) {
+ collapseArchives();
}
- catch (CancelledException e) {
- return;
+
+ if (needToCreateAssociation()) {
+ associateDataTypes(monitor);
}
+
+ doCopy(monitor);
+
}
- int transactionID =
- destinationArchive.getDataTypeManager().startTransaction("Copy/Move Category/DataType");
+ catch (CancelledException e) {
+ return; // nothing to report
+ }
+ finally {
+ gTree.restoreTreeState(treeState);
+ }
+
+ reportErrors();
+ }
+
+ private void reportErrors() {
+ if (errors.isEmpty()) {
+ return;
+ }
+
+ String message = errors.get(0);
+ int n = errors.size();
+ if (n > 1) {
+ message = "Encountered " + n + " errors copying/moving. See the log for details";
+ }
+
+ Msg.showError(this, gTree, "Encountered Errors Copying/Moving", message);
+ }
+
+ private boolean checkForDifferentSourceArchives() {
+
+ for (GTreeNode node : droppedNodes) {
+ if (sourceArchive != findArchive(node)) {
+ Msg.showError(this, gTree, "Copy Failed",
+ "All dragged data types must be from the same archive!");
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private void doCopy(TaskMonitor monitor) {
+ DataTypeManager dtm = destinationArchive.getDataTypeManager();
+ int txId = dtm.startTransaction("Copy/Move Category/DataType");
try {
if (destinationNode instanceof DataTypeNode) {
- dragNodeToDataType(monitor);
+ dragNodeToDataType();
}
else {
dragNodesToCategory(monitor);
}
}
finally {
- destinationArchive.getDataTypeManager().endTransaction(transactionID, true);
+ dtm.endTransaction(txId, true);
}
}
- /**
- *
- * @param archive
- * @param source
- * @param confirmed is a single element array containing tri-state Boolean flag which
- * has the following states:
- * null - (initial state) user has not yet been prompted
- * true - user has confirmed performing associations
- * false - user has denied performing associations
- * @throws CancelledException if user has cancelled the operation
- */
- private void associateDataTypes(Archive archive, SourceArchive source)
+ private boolean needToCreateAssociation() {
+
+ // copying from the program archive into another archive
+ return sourceArchive != destinationArchive &&
+ !(destinationArchive instanceof ProgramArchive) &&
+ (sourceArchive instanceof ProgramArchive);
+ }
+
+ private void collapseArchives() {
+ GTreeNode root = gTree.getModelRoot();
+ List<GTreeNode> children = root.getChildren();
+ for (GTreeNode archive : children) {
+ gTree.collapseAll(archive);
+ }
+ }
+
+ private void associateDataTypes(TaskMonitor monitor)
throws CancelledException {
- DataTypeManager dtm = archive.getDataTypeManager();
- int transactionID = dtm.startTransaction("Associate DataTypes");
+
+ if (!promptToAssociateTypes(monitor)) {
+ return;
+ }
+
+ monitor.initialize(droppedNodes.size());
+
+ SourceArchive destination = destinationArchive.getDataTypeManager().getLocalSourceArchive();
+ DataTypeManager dtm = sourceArchive.getDataTypeManager();
+ int txId = dtm.startTransaction("Associate DataTypes");
try {
- DataTypeAssociationConfirmation confirmation = new DataTypeAssociationConfirmation();
- for (GTreeNode node : droppedNodeList) {
+ for (GTreeNode node : droppedNodes) {
+ monitor.checkCanceled();
+
if (node instanceof DataTypeNode) {
- DataType replacementDataType = ((DataTypeNode) node).getDataType();
- if (!isLocal(replacementDataType)) {
- continue;
- }
- if (!confirmation.isConfirmed()) {
- return;
- }
- dtm.associateDataTypeWithArchive(replacementDataType, source);
+ DataType dt = ((DataTypeNode) node).getDataType();
+ associateDataType(dt, dtm, destination);
}
else if (node instanceof CategoryNode) {
Category cat = ((CategoryNode) node).getCategory();
- associateDataTypes(cat, dtm, source, confirmation);
- if (confirmation.haveAskedUser() && !confirmation.isConfirmed()) {
- return;
- }
+ associateDataTypes(cat, dtm, destination);
}
+
+ monitor.incrementProgress(1);
}
}
finally {
- archive.getDataTypeManager().endTransaction(transactionID, true);
+ dtm.endTransaction(txId, true);
}
}
- private void associateDataTypes(Category cat, DataTypeManager dtm, SourceArchive source,
- DataTypeAssociationConfirmation confirmation) throws CancelledException {
- DataType[] dataTypes = cat.getDataTypes();
- for (DataType dataType : dataTypes) {
- if (!isLocal(dataType)) {
- continue;
+ private boolean promptToAssociateTypes(TaskMonitor monitor) throws CancelledException {
+
+ if (!containsUnassociatedTypes(monitor)) {
+ return false; // nothing to associate
+ }
+
+ int result = askToAssociateDataTypes();
+ if (result == OptionDialog.CANCEL_OPTION) {
+ throw new CancelledException();
+ }
+
+ return result == OptionDialog.YES_OPTION;
+ }
+
+ private boolean containsUnassociatedTypes(TaskMonitor monitor) throws CancelledException {
+
+ monitor.setMessage("Checking for types to associate");
+ monitor.initialize(droppedNodes.size());
+ for (GTreeNode node : droppedNodes) {
+ monitor.checkCanceled();
+
+ if (node instanceof DataTypeNode) {
+ DataType dt = ((DataTypeNode) node).getDataType();
+ if (!isLocal(dt)) {
+ return true;
+ }
}
- if (!confirmation.isConfirmed()) {
- return;
+ else if (node instanceof CategoryNode) {
+ if (containsUnassociatedTypes(((CategoryNode) node).getCategory(), monitor)) {
+ return true;
+ }
}
- dtm.associateDataTypeWithArchive(dataType, source);
+
+ monitor.incrementProgress(1);
}
+
+ return false;
+ }
+
+ private boolean containsUnassociatedTypes(Category cat, TaskMonitor monitor)
+ throws CancelledException {
+
+ DataType[] types = cat.getDataTypes();
+ for (DataType dt : types) {
+ monitor.checkCanceled();
+ if (!isLocal(dt)) {
+ return true;
+ }
+ }
+
Category[] categories = cat.getCategories();
- for (Category category : categories) {
- associateDataTypes(category, dtm, source, confirmation);
- if (confirmation.haveAskedUser() && !confirmation.isConfirmed()) {
- return;
+ for (Category child : categories) {
+ monitor.checkCanceled();
+ if (containsUnassociatedTypes(child, monitor)) {
+ return true;
}
}
+
+ return false;
}
- private class DataTypeAssociationConfirmation {
- Boolean confirmed = null;
+ private void associateDataType(DataType dt, DataTypeManager dtm, SourceArchive source) {
- boolean isConfirmed() throws CancelledException {
- if (confirmed == null) {
- int result = askToAssociateDataTypes();
- if (result == OptionDialog.NO_OPTION) {
- confirmed = false;
- }
- else if (result == OptionDialog.YES_OPTION) {
- confirmed = true;
- }
- else {
- throw new CancelledException();
- }
- }
- return confirmed;
+ if (!isLocal(dt)) {
+ return;
}
- boolean haveAskedUser() {
- return confirmed != null;
+ dtm.associateDataTypeWithArchive(dt, source);
+ }
+
+ private void associateDataTypes(Category cat, DataTypeManager dtm, SourceArchive destination) {
+
+ DataType[] dataTypes = cat.getDataTypes();
+ for (DataType dataType : dataTypes) {
+ associateDataType(dataType, dtm, destination);
+ }
+
+ Category[] categories = cat.getCategories();
+ for (Category category : categories) {
+ associateDataTypes(category, dtm, destination);
}
}
private void dragNodesToCategory(TaskMonitor monitor) {
- int count = 0;
- Category destinationCategory = getCategory(destinationNode);
- Archive sourceArchive = findArchive(droppedNodeList.get(0));
- for (GTreeNode node : droppedNodeList) {
- monitor.setProgress(count++);
+
+ monitor.setMessage("Drag/Drop Categories/Data Types");
+ monitor.initialize(droppedNodes.size());
+
+ Category toCategory = getCategory(destinationNode);
+ for (GTreeNode node : droppedNodes) {
if (monitor.isCancelled()) {
break;
}
@@ -226,104 +347,90 @@ public class DataTypeTreeCopyMoveTask extends Task {
// COPY is only allowed action if the source and destination archives are different.
if (actionType == ActionType.COPY || sourceArchive != destinationArchive) {
- copyNode(destinationCategory, node, monitor);
+ copyNode(toCategory, node, monitor);
}
else {
- moveNode(destinationCategory, node, monitor);
+ moveNode(toCategory, node, monitor);
}
+
+ monitor.incrementProgress(1);
}
}
- private void copyNode(Category destinationCategory, GTreeNode node, TaskMonitor monitor) {
+ private void copyNode(Category toCategory, GTreeNode node, TaskMonitor monitor) {
if (node instanceof DataTypeNode) {
DataType nodeDt = ((DataTypeNode) node).getDataType();
- DataTypeManager dtm = destinationCategory.getDataTypeManager();
- boolean withinDataTypeManager = (dtm == nodeDt.getDataTypeManager());
- DataType dataType =
- !withinDataTypeManager ? nodeDt.clone(nodeDt.getDataTypeManager())
- : nodeDt.copy(nodeDt.getDataTypeManager());
- if (withinDataTypeManager &&
- dataType.getCategoryPath().equals(destinationCategory.getCategoryPath())) {
- renameAsCopy(destinationCategory, dataType);
- }
- final DataType resolvedDT = destinationCategory.addDataType(dataType, conflictHandler);
- if (resolvedDT instanceof Pointer || resolvedDT instanceof Array ||
- resolvedDT instanceof BuiltInDataType ||
- resolvedDT instanceof MissingBuiltInDataType) {
- return;
- }
- if (!resolvedDT.getCategoryPath().equals(destinationCategory.getCategoryPath())) {
- // We are in a task, must show dialog in the swing thread.
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- String msg =
- "Another copy of this data-type already exists at " +
- resolvedDT.getPathName();
- Msg.showInfo(getClass(), component, "DataType copy failed!", msg);
- }
- });
- }
+ copyDataType(toCategory, nodeDt);
}
else if (node instanceof CategoryNode) {
Category category = ((CategoryNode) node).getCategory();
- copyCategory(destinationCategory, category, monitor);
+ copyCategory(toCategory, category, monitor);
}
}
- private void renameAsCopy(Category destinationCategory, DataType dataType) {
- String baseName = dataType.getName();
- String prefix = "Copy_";
- String suffix = "of_";
- if (baseName.startsWith(prefix)) {
- int indexOf = baseName.indexOf(suffix);
- if (indexOf > 4) {
- if (indexOf == 5) {
- baseName = baseName.substring(indexOf + 3);
- }
- else {
- if (indexOf > 5) {
- if ((baseName.charAt(indexOf - 1) == '_')) {
- String copyNumber = baseName.substring(5, indexOf - 1);
- try {
- Integer.parseInt(copyNumber);
- baseName = baseName.substring(indexOf + 3);
- }
- catch (NumberFormatException e) {
- // If can't parse number then assume not one of our numbered copies.
- }
-
- }
- }
- }
- }
+ private void copyDataType(Category toCategory, DataType dataType) {
+
+ DataTypeManager dtm = toCategory.getDataTypeManager();
+ DataTypeManager nodeDtm = dataType.getDataTypeManager();
+ boolean sameManager = (dtm == nodeDtm);
+ DataType newDt = !sameManager ? dataType.clone(nodeDtm) : dataType.copy(nodeDtm);
+ if (sameManager &&
+ newDt.getCategoryPath().equals(toCategory.getCategoryPath())) {
+ renameAsCopy(toCategory, newDt);
+ }
+
+ DataType resolvedDt = toCategory.addDataType(newDt, conflictHandler);
+ if (resolvedDt instanceof Pointer || resolvedDt instanceof Array ||
+ resolvedDt instanceof BuiltInDataType ||
+ resolvedDt instanceof MissingBuiltInDataType) {
+ return;
+ }
+
+ if (!resolvedDt.getCategoryPath().equals(toCategory.getCategoryPath())) {
+ errors.add(
+ "Data type copy failed. Another copy of this data type already exists at " +
+ resolvedDt.getPathName());
}
+ }
+
+ private void renameAsCopy(Category destinationCategory, DataType dataType) {
+ String dtName = dataType.getName();
+ String baseName = getBaseName(dtName);
String copyName = getNextCopyName(destinationCategory, baseName);
try {
dataType.setName(copyName);
}
- catch (InvalidNameException e) {
- Msg.error(this, "Problem creating copy of " + baseName, e);
- }
- catch (DuplicateNameException e) {
- Msg.error(this, "Problem creating copy of " + baseName, e);
+ catch (DuplicateNameException | InvalidNameException e) {
+ errors.add("Problem creating copy of " + baseName + ". " + e.getMessage());
}
}
- private String getNextCopyName(final Category destinationCategory, final String baseName) {
- String prefix = "Copy_";
- String suffix = "of_";
- String copyName = prefix + suffix + baseName;
- if (destinationCategory.getDataType(copyName) == null) {
- return copyName;
+ String getBaseName(String dtName) {
+
+ // format: Copy_of_foobar
+ // Copy_2_of_foobar
+ Pattern p = Pattern.compile("Copy_(?:\\d+_)*of_(.*)");
+ Matcher matcher = p.matcher(dtName);
+ if (!matcher.matches()) {
+ return dtName;
}
- for (int i = 2; i < Integer.MAX_VALUE; i++) {
- copyName = prefix + i + "_" + suffix + baseName;
+
+ String baseName = matcher.group(1);
+ return baseName;
+ }
+
+ String getNextCopyName(Category destinationCategory, String baseName) {
+
+ String format = "Copy_%d_of_" + baseName;
+ for (int i = 1; i < 100; i++) {
+ String copyName = String.format(format, i);
if (destinationCategory.getDataType(copyName) == null) {
return copyName;
}
}
- return baseName;
+
+ // should never happen; do something reasonable
+ return String.format(format, System.currentTimeMillis());
}
private void moveNode(Category destinationCategory, GTreeNode node, TaskMonitor monitor) {
@@ -337,48 +444,57 @@ public class DataTypeTreeCopyMoveTask extends Task {
}
}
- private void moveCategory(Category destinationCategory, Category category, TaskMonitor monitor) {
- if (category.getParent() == destinationCategory) { // moving to same place
+ private void moveCategory(Category toCategory, Category category,
+ TaskMonitor monitor) {
+ if (category.getParent() == toCategory) { // moving to same place
return;
}
try {
- CategoryPath path = destinationCategory.getCategoryPath();
+ CategoryPath path = toCategory.getCategoryPath();
if (path.isAncestorOrSelf(category.getCategoryPath())) {
- Msg.showError(this, component, "Move Failed",
- "Cannot move a parent node onto a child node");
+ errors.add(
+ "Cannot move a parent node onto a child node. Moving " + category + " to " +
+ toCategory);
return;
}
- destinationCategory.moveCategory(category, monitor);
+ toCategory.moveCategory(category, monitor);
}
catch (DuplicateNameException e) {
- Msg.showError(this, component, "Move Failed", "Move failed due to duplicate name:\n" +
- e.getMessage());
+ errors.add("Move failed due to duplicate name. Moving " + category + " to " +
+ toCategory + ": " + e.getMessage());
}
}
- private void moveDataType(Category destinationCategory, DataType dataType) {
- if (dataType.getCategoryPath().equals(destinationCategory.getCategoryPath())) {
- Msg.showError(this, component, "Move Failed", "DataType is already in this category.");
+ private void moveDataType(Category toCategory, DataType dataType) {
+ if (dataType.getCategoryPath().equals(toCategory.getCategoryPath())) {
+ errors.add(
+ "Move failed. DataType is already in this category. Category " + toCategory +
+ "; Data type: " + dataType);
return;
}
try {
- destinationCategory.moveDataType(dataType, conflictHandler);
+ toCategory.moveDataType(dataType, conflictHandler);
}
catch (DataTypeDependencyException e) {
- Msg.showError(this, component, "Move Failed", e.getMessage());
+ errors.add(
+ "Move failed. DataType is already in this category. Category " + toCategory +
+ "; Data type: " + dataType + ". " + e.getMessage());
}
}
- private void copyCategory(Category destinationCategory, Category category, TaskMonitor monitor) {
- CategoryPath destinationPath = destinationCategory.getCategoryPath();
- boolean withinSameDataTypeManager = (destinationCategory.getDataTypeManager() == category.getDataTypeManager());
- if (withinSameDataTypeManager && destinationPath.isAncestorOrSelf(category.getCategoryPath())) {
- Msg.showError(this, component, "Copy Failed",
- "Cannot copy a parent node onto a child node");
+ private void copyCategory(Category toCategory, Category category,
+ TaskMonitor monitor) {
+ CategoryPath toPath = toCategory.getCategoryPath();
+ boolean sameManager =
+ (toCategory.getDataTypeManager() == category.getDataTypeManager());
+ if (sameManager && toPath.isAncestorOrSelf(category.getCategoryPath())) {
+ errors.add("Copy failed. " +
+ "Cannot copy a parent node onto a child node. Moving " + category + " to " +
+ toCategory);
return;
}
- destinationCategory.copyCategory(category, conflictHandler, monitor);
+ toCategory.copyCategory(category, conflictHandler, monitor);
}
private Category getCategory(GTreeNode node) {
@@ -392,169 +508,134 @@ public class DataTypeTreeCopyMoveTask extends Task {
"Expected node to be either an ArchiveNode or CategoryNode but was " + node.getClass());
}
- private boolean isAssociatedEitherWay(DataType dataType1, DataType dataType2) {
- return isAssociated(dataType1, dataType2) || isAssociated(dataType2, dataType1);
+ private boolean isAssociatedEitherWay(DataType dt1, DataType dt2) {
+ return isAssociated(dt1, dt2) || isAssociated(dt2, dt1);
}
- private boolean isAssociated(DataType sourceDataType, DataType destinationDataType) {
- UniversalID destinationID = destinationDataType.getUniversalID();
- if (destinationID == null || !destinationID.equals(sourceDataType.getUniversalID())) {
+ private boolean isAssociated(DataType sourceDt, DataType destinationDt) {
+ UniversalID destinationID = destinationDt.getUniversalID();
+ if (destinationID == null || !destinationID.equals(sourceDt.getUniversalID())) {
return false;
}
- if (!destinationDataType.getSourceArchive().getSourceArchiveID().equals(
- sourceDataType.getSourceArchive().getSourceArchiveID())) {
+ if (!haveSameSourceArchive(sourceDt, destinationDt)) {
return false;
}
- return isLocal(sourceDataType);
+ return isLocal(sourceDt);
+ }
+
+ private boolean haveSameSourceArchive(DataType dt1, DataType dt2) {
+ SourceArchive s1 = dt1.getSourceArchive();
+ SourceArchive s2 = dt2.getSourceArchive();
+ return s1.getSourceArchiveID().equals(s2.getSourceArchiveID());
}
- private boolean isLocal(DataType dataType) {
- return dataType.getSourceArchive().getSourceArchiveID().equals(
- dataType.getDataTypeManager().getUniversalID());
+ /**
+ * Returns true if the given data type's source archive is the same as it's current data
+ * type manager. This is false if copying a new type from the program to an
+ * external archive.
+ *
+ * @param dt the type
+ * @return true if the given type already lives in its source archive
+ */
+ private boolean isLocal(DataType dt) {
+ UniversalID sourceId = dt.getSourceArchive().getSourceArchiveID();
+ UniversalID dtmId = dt.getDataTypeManager().getUniversalID();
+ return sourceId.equals(dtmId);
}
- private void dragNodeToDataType(TaskMonitor monitor) {
- DataType destinationDataType = ((DataTypeNode) destinationNode).getDataType();
- GTreeNode node = droppedNodeList.get(0); // there must be exactly one and it must
- // be a dataTypeNode, because of isValidDropSite()
- DataType replacementDataType = ((DataTypeNode) node).getDataType();
- Archive sourceArchive = findArchive(node);
+ private void dragNodeToDataType() {
+ DataType destinationDt = ((DataTypeNode) destinationNode).getDataType();
+
+ // there must be exactly one and it must be a dataTypeNode, because of isValidDropSite()
+ GTreeNode node = droppedNodes.get(0);
+ DataType replacementDt = ((DataTypeNode) node).getDataType();
if (sourceArchive != destinationArchive) {
- if (isAssociatedEitherWay(replacementDataType, destinationDataType)) {
- if (isLocal(destinationDataType)) {
- DataTypeSyncInfo syncInfo =
- new DataTypeSyncInfo(replacementDataType,
- destinationDataType.getDataTypeManager());
- if (!syncInfo.canCommit()) {
- Msg.showInfo(getClass(), component, "Commit Data Type",
- "No changes to commit");
- }
- // destination data-type is local to an archive
- else if (confirmCommit()) {
- // if the destination dataType is local to its dataTypeManager
- // then we are committing.
- DataTypeSynchronizer.commit(destinationDataType.getDataTypeManager(),
- replacementDataType);
- }
- }
- else { // else we are updating
- DataTypeSyncInfo syncInfo =
- new DataTypeSyncInfo(destinationDataType,
- replacementDataType.getDataTypeManager());
- if (!syncInfo.canUpdate()) {
- Msg.showInfo(getClass(), component, "Update Data Type",
- "No changes to copy");
- }
- else if (confirmUpdate()) {
- DataTypeSynchronizer.update(destinationDataType.getDataTypeManager(),
- replacementDataType);
- }
- }
+ if (isAssociatedEitherWay(replacementDt, destinationDt)) {
+ handleAssociatedType(destinationDt, replacementDt);
return;
}
- actionType = ActionType.COPY;
- replacementDataType =
- replacementDataType.clone(replacementDataType.getDataTypeManager());
+
+ replacementDt = replacementDt.clone(replacementDt.getDataTypeManager());
}
else if (actionType == ActionType.COPY) { // Copy within a single data type manager.
- replacementDataType =
- replacementDataType.copy(replacementDataType.getDataTypeManager());
+ replacementDt = replacementDt.copy(replacementDt.getDataTypeManager());
}
- replaceDataType(destinationDataType, replacementDataType);
+ replaceDataType(destinationDt, replacementDt);
+ }
+
+ private void handleAssociatedType(DataType destinationDt, DataType replacementDt) {
+ if (isLocal(destinationDt)) {
+ DataTypeSyncInfo syncInfo = new DataTypeSyncInfo(replacementDt,
+ destinationDt.getDataTypeManager());
+ if (!syncInfo.canCommit()) {
+ Msg.showInfo(getClass(), gTree, "Commit Data Type",
+ "No changes to commit");
+ }
+ // destination data-type is local to an archive
+ else if (confirmCommit()) {
+ // if the destination dataType is local to its dataTypeManager
+ // then we are committing.
+ DataTypeSynchronizer.commit(destinationDt.getDataTypeManager(), replacementDt);
+ }
+ }
+ else { // else we are updating
+ DataTypeSyncInfo syncInfo = new DataTypeSyncInfo(destinationDt,
+ replacementDt.getDataTypeManager());
+ if (!syncInfo.canUpdate()) {
+ Msg.showInfo(getClass(), gTree, "Update Data Type", "No changes to copy");
+ }
+ else if (confirmUpdate()) {
+ DataTypeSynchronizer.update(destinationDt.getDataTypeManager(), replacementDt);
+ }
+ }
}
private boolean confirmCommit() {
return confirm("Commit Data Type?",
- "Do you want to commit the changes to this data type back to the source Archive? \n"
- + "(Warning: any changes in the source archive will be overwritten.)");
+ "Do you want to commit the changes to this data type back to the source Archive? \n" +
+ "(Warning: any changes in the source archive will be overwritten.)");
}
private boolean confirmUpdate() {
return confirm("Update Data Type?",
- "Do you want to update this data type with the changes in the source Archive?\n"
- + "(Warning: any local changes will be overwritten.)");
+ "Do you want to update this data type with the changes in the source Archive?\n" +
+ "(Warning: any local changes will be overwritten.)");
}
- private boolean confirm(final String title, final String message) {
- final BooleanResultsContainer results = new BooleanResultsContainer();
- try {
- SwingUtilities.invokeAndWait(new Runnable() {
- @Override
- public void run() {
- int selectedOption = OptionDialog.showYesNoDialog(component, title, message);
- results.value = selectedOption == OptionDialog.OPTION_ONE;
- }
- });
- }
- catch (InterruptedException e1) {
- }
- catch (InvocationTargetException e) {
- Msg.error(this, "Unexpected Exception: " + e.getMessage(), e);
- }
- return results.value;
+ private boolean confirm(String title, String message) {
+ int choice = OptionDialog.showYesNoDialog(gTree, title, message);
+ return choice == OptionDialog.YES_OPTION;
}
private int askToAssociateDataTypes() {
- final IntResultsContainer results = new IntResultsContainer();
- try {
- SwingUtilities.invokeAndWait(new Runnable() {
- @Override
- public void run() {
- results.value =
- OptionDialog.showYesNoCancelDialog(component, "Associate DataTypes?",
- "Do you want to associate local datatypes with the target archive?");
- }
- });
- }
- catch (InterruptedException e1) {
- }
- catch (InvocationTargetException e) {
- Msg.error(this, "Unexpected Exception: " + e.getMessage(), e);
- }
- return results.value;
- }
-
- class BooleanResultsContainer {
- public boolean value;
+ return OptionDialog.showYesNoCancelDialog(gTree, "Associate DataTypes?",
+ "Do you want to associate local datatypes with the target archive?");
}
- class IntResultsContainer {
- public int value;
- }
+ private void replaceDataType(DataType existingDt, DataType replacementDt) {
- private void replaceDataType(final DataType existingDT, final DataType replacementDT) {
+ int choice =
+ OptionDialog.showYesNoDialog(gTree, "Replace Data Type?", "Replace " +
+ existingDt.getPathName() + "\nwith " + replacementDt.getPathName() +
+ "?");
- try {
- SwingUtilities.invokeAndWait(new Runnable() {
- @Override
- public void run() {
- int selectedOption =
- OptionDialog.showYesNoDialog(component, "Replace Data Type?", "Replace " +
- existingDT.getPathName() + "\nwith " + replacementDT.getPathName() +
- "?");
-
- if (selectedOption == OptionDialog.OPTION_ONE) {
- try {
- DataTypeManager dtMgr = existingDT.getDataTypeManager();
- dtMgr.replaceDataType(existingDT, replacementDT, true);
- }
- catch (DataTypeDependencyException e) {
- Msg.showError(this, component, "Replace Failed", e.getMessage());
- }
- }
- }
- });
- }
- catch (InterruptedException e1) {
- }
- catch (InvocationTargetException e) {
- Msg.error(this, "Unexpected Exception: " + e.getMessage(), e);
+ if (choice == OptionDialog.YES_OPTION) {
+ try {
+ DataTypeManager dtMgr = existingDt.getDataTypeManager();
+ dtMgr.replaceDataType(existingDt, replacementDt, true);
+ }
+ catch (DataTypeDependencyException e) {
+ errors.add("Replace failed. Existing type " + existingDt + "; replacment type " +
+ replacementDt + ". " + e.getMessage());
+ }
}
}
- private List<GTreeNode> filterList(List<GTreeNode> nodeList) {
- Set<GTreeNode> nodeSet = new HashSet<GTreeNode>(nodeList);
+ // filters out nodes with categories in their path
+ private void filterRedundantNodes() {
+
+ Set<GTreeNode> nodeSet = new HashSet<GTreeNode>(droppedNodes);
List<GTreeNode> filteredList = new ArrayList<GTreeNode>();
for (GTreeNode node : nodeSet) {
@@ -563,7 +644,7 @@ public class DataTypeTreeCopyMoveTask extends Task {
}
}
- return filteredList;
+ droppedNodes = filteredList;
}
private boolean containsAncestor(Set<GTreeNode> nodeSet, GTreeNode node) {
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/datamgr/DataTypeCopyMoveDragTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/datamgr/DataTypeCopyMoveDragTest.java
index 6cc8e88e1c..095fa6ca29 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/datamgr/DataTypeCopyMoveDragTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/datamgr/DataTypeCopyMoveDragTest.java
@@ -23,7 +23,7 @@ import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
-import javax.swing.SwingUtilities;
+import javax.swing.tree.TreePath;
import org.junit.*;
@@ -52,7 +52,6 @@ import ghidra.util.exception.DuplicateNameException;
*/
public class DataTypeCopyMoveDragTest extends AbstractGhidraHeadedIntegrationTest {
private static final String PROGRAM_FILENAME = "notepad";
- private static final int TASK_TIMEOUT = 2000;
private TestEnv env;
private PluginTool tool;
@@ -64,10 +63,6 @@ public class DataTypeCopyMoveDragTest extends AbstractGhidraHeadedIntegrationTes
private ArchiveRootNode archiveRootNode;
private ArchiveNode programNode;
- public DataTypeCopyMoveDragTest() {
- super();
- }
-
@Before
public void setUp() throws Exception {
env = new TestEnv();
@@ -136,7 +131,7 @@ public class DataTypeCopyMoveDragTest extends AbstractGhidraHeadedIntegrationTes
});
// this handles the save changes dialog and potential analysis dialogs
- closeAllWindowsAndFrames();
+ closeAllWindows();
env.release(program);
env.dispose();
@@ -296,9 +291,17 @@ public class DataTypeCopyMoveDragTest extends AbstractGhidraHeadedIntegrationTes
CategoryNode miscNode = (CategoryNode) programNode.getChild("MISC");
expandNode(miscNode);
+ TreePath structureNodePath = structureNode.getTreePath();
+ TreePath category3Path = category3Node.getTreePath();
+ TreePath miscPath = miscNode.getTreePath();
+
// copy/drag ArrayStruct to MISC
copyNodeToNode(structureNode, miscNode);
+ structureNode = (DataTypeNode) tree.getViewNodeForPath(structureNodePath);
+ category3Node = (CategoryNode) tree.getViewNodeForPath(category3Path);
+ miscNode = (CategoryNode) tree.getViewNodeForPath(miscPath);
+
DataTypeNode node = (DataTypeNode) miscNode.getChild(structName + DataType.CONFLICT_SUFFIX);
assertNotNull(node);
assertEquals(category3Node, structureNode.getParent());
@@ -319,9 +322,17 @@ public class DataTypeCopyMoveDragTest extends AbstractGhidraHeadedIntegrationTes
CategoryNode miscNode = (CategoryNode) programNode.getChild("MISC");
expandNode(miscNode);
+ TreePath structureNodePath = structureNode.getTreePath();
+ TreePath category3Path = category3Node.getTreePath();
+ TreePath miscPath = miscNode.getTreePath();
+
// copy/drag ArrayStruct to MISC
copyNodeToNode(structureNode, miscNode);
+ structureNode = (DataTypeNode) tree.getViewNodeForPath(structureNodePath);
+ category3Node = (CategoryNode) tree.getViewNodeForPath(category3Path);
+ miscNode = (CategoryNode) tree.getViewNodeForPath(miscPath);
+
DataTypeNode node = (DataTypeNode) miscNode.getChild(structName);
assertNotNull(node);
assertEquals(category3Node, structureNode.getParent());
@@ -496,18 +507,25 @@ public class DataTypeCopyMoveDragTest extends AbstractGhidraHeadedIntegrationTes
CategoryNode miscNode = (CategoryNode) programNode.getChild("MISC");
expandNode(miscNode);
+ TreePath structureNodePath = structureNode.getTreePath();
+ TreePath category3Path = category3Node.getTreePath();
+ TreePath miscPath = miscNode.getTreePath();
+
// drag/move ArrayStruct to MISC/ArrayStruct
DataTypeNode miscStructureNode = (DataTypeNode) miscNode.getChild("ArrayStruct");
dragNodeToNode(structureNode, miscStructureNode);
pressButtonOnOptionDialog("No");
+ structureNode = (DataTypeNode) tree.getViewNodeForPath(structureNodePath);
assertNotNull(structureNode.getParent());
+
+ category3Node = (CategoryNode) tree.getViewNodeForPath(category3Path);
assertNotNull(category3Node.getChild("ArrayStruct"));
+ miscNode = (CategoryNode) tree.getViewNodeForPath(miscPath);
DataTypeNode node = (DataTypeNode) miscNode.getChild("ArrayStruct");
assertTrue(!structure.isEquivalent(node.getDataType()));
-
}
@Test
@@ -563,13 +581,21 @@ public class DataTypeCopyMoveDragTest extends AbstractGhidraHeadedIntegrationTes
DataTypeNode miscStructureNode = (DataTypeNode) miscNode.getChild("ArrayStruct");
DataType origDt = miscStructureNode.getDataType();
+ TreePath structureNodePath = structureNode.getTreePath();
+ TreePath category3Path = category3Node.getTreePath();
+ TreePath miscPath = miscNode.getTreePath();
+
copyNodeToNode(structureNode, miscStructureNode);
pressButtonOnOptionDialog("No");
+ structureNode = (DataTypeNode) tree.getViewNodeForPath(structureNodePath);
assertNotNull(structureNode.getParent());
+
+ category3Node = (CategoryNode) tree.getViewNodeForPath(category3Path);
assertNotNull(category3Node.getChild("ArrayStruct"));
+ miscNode = (CategoryNode) tree.getViewNodeForPath(miscPath);
DataTypeNode node = (DataTypeNode) miscNode.getChild(structName);
assertEquals(origDt, node.getDataType());
}
@@ -593,13 +619,21 @@ public class DataTypeCopyMoveDragTest extends AbstractGhidraHeadedIntegrationTes
DataTypeNode miscStructureNode = (DataTypeNode) miscNode.getChild("ArrayStruct");
DataType miscStructure = miscStructureNode.getDataType();
+ TreePath structureNodePath = structureNode.getTreePath();
+ TreePath category3Path = category3Node.getTreePath();
+ TreePath miscPath = miscNode.getTreePath();
+
copyNodeToNode(structureNode, miscStructureNode);
pressButtonOnOptionDialog("Yes");
+ structureNode = (DataTypeNode) tree.getViewNodeForPath(structureNodePath);
assertNotNull(structureNode.getParent());
+
+ category3Node = (CategoryNode) tree.getViewNodeForPath(category3Path);
assertNotNull(category3Node.getChild(structName));
+ miscNode = (CategoryNode) tree.getViewNodeForPath(miscPath);
DataTypeNode node = (DataTypeNode) miscNode.getChild(structName);
assertTrue(structure.isEquivalent(node.getDataType()));
@@ -678,85 +712,11 @@ public class DataTypeCopyMoveDragTest extends AbstractGhidraHeadedIntegrationTes
return miscNode;
}
-// private void renameConflict() throws Exception {
-// // test the conflict handler for the 3 options
-// // should get a dialog that asks what to do with the conflict:
-// // (1) rename new data type to struct_1.conflict,
-// // (2) replace the existing data type, or
-// // (3) use the existing data type (cancel the drop)
-// ConflictDialog dialog =
-// waitForDialogComponent(tool.getToolFrame(), ConflictDialog.class, TASK_TIMEOUT);
-// assertNotNull(dialog);
-// final JRadioButton renameButton = (JRadioButton) getInstanceField("renameRB", dialog);
-// final JButton okButton = (JButton) getInstanceField("okButton", dialog);
-// assertNotNull(renameButton);
-// assertNotNull(okButton);
-// runSwing(new Runnable() {
-// @Override
-// public void run() {
-// renameButton.setSelected(true);
-// okButton.doClick();
-// }
-// });
-// waitForProgram();
-// }
-//
-// private void replaceConflict() throws Exception {
-// // test the conflict handler for the 3 options
-// // should get a dialog that asks what to do with the conflict:
-// // (1) rename new data type to struct_1.conflict,
-// // (2) replace the existing data type, or
-// // (3) use the existing data type (cancel the drop)
-// // choose option 2
-// ConflictDialog dialog =
-// waitForDialogComponent(tool.getToolFrame(), ConflictDialog.class, TASK_TIMEOUT);
-// assertNotNull(dialog);
-// final JRadioButton replaceButton = (JRadioButton) getInstanceField("replaceRB", dialog);
-// final JButton okButton = (JButton) getInstanceField("okButton", dialog);
-// assertNotNull(replaceButton);
-// assertNotNull(okButton);
-// runSwing(new Runnable() {
-// @Override
-// public void run() {
-// replaceButton.setSelected(true);
-// okButton.doClick();
-// }
-// });
-// waitForProgram();
-// }
-//
-// private void useExistingConflict() throws Exception {
-// // test the conflict handler for the 3 options
-// // should get a dialog that asks what to do with the conflict:
-// // (1) rename new data type to struct_1.conflict,
-// // (2) replace the existing data type, or
-// // (3) use the existing data type (cancel the drop)
-// // choose option 3
-// ConflictDialog dialog =
-// waitForDialogComponent(tool.getToolFrame(), ConflictDialog.class, TASK_TIMEOUT);
-// assertNotNull(dialog);
-// final JRadioButton useExistingButton =
-// (JRadioButton) getInstanceField("useExistingRB", dialog);
-// final JButton okButton = (JButton) getInstanceField("okButton", dialog);
-// assertNotNull(useExistingButton);
-// assertNotNull(okButton);
-// runSwing(new Runnable() {
-// @Override
-// public void run() {
-// useExistingButton.setSelected(true);
-// okButton.doClick();
-// }
-// });
-// waitForProgram();
-// }
-
private void pressButtonOnOptionDialog(String buttonName) throws Exception {
- OptionDialog d =
- waitForDialogComponent(tool.getToolFrame(), OptionDialog.class, TASK_TIMEOUT);
- assertNotNull(d);
- final JButton button = findButtonByText(d, buttonName);
+ OptionDialog d = waitForDialogComponent(OptionDialog.class);
+ JButton button = findButtonByText(d, buttonName);
assertNotNull(button);
- SwingUtilities.invokeAndWait(() -> button.doClick());
+ pressButton(button);
waitForProgram();
}
@@ -766,20 +726,19 @@ public class DataTypeCopyMoveDragTest extends AbstractGhidraHeadedIntegrationTes
dropList.add(fromNode);
final Transferable transferable = new GTreeNodeTransferable(dragNDropHandler, dropList);
- executeOnSwingWithoutBlocking(
- () -> dragNDropHandler.drop(toNode, transferable, DnDConstants.ACTION_MOVE));
- waitForPostedSwingRunnables();
+ runSwing(
+ () -> dragNDropHandler.drop(toNode, transferable, DnDConstants.ACTION_MOVE), false);
+ waitForSwing();
}
- private void copyNodeToNode(GTreeNode fromNode, final GTreeNode toNode) {
+ private void copyNodeToNode(GTreeNode fromNode, final GTreeNode toNode) throws Exception {
final GTreeDragNDropHandler dragNDropHandler = tree.getDragNDropHandler();
List<GTreeNode> dropList = new ArrayList<>();
dropList.add(fromNode);
final Transferable transferable = new GTreeNodeTransferable(dragNDropHandler, dropList);
- executeOnSwingWithoutBlocking(
- () -> dragNDropHandler.drop(toNode, transferable, DnDConstants.ACTION_COPY));
- waitForPostedSwingRunnables();
+ runSwing(
+ () -> dragNDropHandler.drop(toNode, transferable, DnDConstants.ACTION_COPY), false);
}
//==================================================================================================
@@ -800,36 +759,17 @@ public class DataTypeCopyMoveDragTest extends AbstractGhidraHeadedIntegrationTes
}
private void waitForProgram() throws Exception {
+ waitForTasks();
program.flushEvents();
waitForTasks();
- waitForPostedSwingRunnables();
+ waitForSwing();
}
private void undo() throws Exception {
- runSwing(() -> {
- try {
- program.undo();
- program.flushEvents();
- }
- catch (Exception e) {
- e.printStackTrace();
- Assert.fail(e.getMessage());
- }
- });
- waitForTasks();
+ undo(program);
}
private void redo() throws Exception {
- runSwing(() -> {
- try {
- program.redo();
- program.flushEvents();
- }
- catch (Exception e) {
- e.printStackTrace();
- Assert.fail(e.getMessage());
- }
- });
- waitForTasks();
+ redo(program);
}
}
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/datamgr/util/DataTypeCopyMoveDrag2Test.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/datamgr/util/DataTypeCopyMoveDrag2Test.java
new file mode 100644
index 0000000000..3c79c8a5d2
--- /dev/null
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/datamgr/util/DataTypeCopyMoveDrag2Test.java
@@ -0,0 +1,76 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package ghidra.app.plugin.core.datamgr.util;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+import generic.test.AbstractGTest;
+import ghidra.program.model.TestDoubleCategory;
+import ghidra.program.model.data.DataType;
+
+public class DataTypeCopyMoveDrag2Test extends AbstractGTest {
+
+ @Test
+ public void testGetBaseName() {
+
+ DataTypeTreeCopyMoveTask task = new DataTypeTreeCopyMoveTask();
+ String name = "BaseName";
+ assertEquals(name, task.getBaseName(name));
+
+ String copyName = "Copy_of_" + name;
+ assertEquals(name, task.getBaseName(copyName));
+
+ copyName = "Copy_2_of_" + name;
+ assertEquals(name, task.getBaseName(copyName));
+ }
+
+ @Test
+ public void testGetNextCopyName() {
+
+ DataTypeTreeCopyMoveTask task = new DataTypeTreeCopyMoveTask();
+ String name = "BaseName";
+ CountBasedStubCategory category = new CountBasedStubCategory("Category", 0);
+ assertEquals("Copy_1_of_" + name, task.getNextCopyName(category, name));
+
+ category = new CountBasedStubCategory("Category", 1);
+ assertEquals("Copy_2_of_" + name, task.getNextCopyName(category, name));
+
+ category = new CountBasedStubCategory("Category", 10);
+ assertEquals("Copy_11_of_" + name, task.getNextCopyName(category, name));
+ }
+
+ private class CountBasedStubCategory extends TestDoubleCategory {
+
+ private int threshold;
+ private int count;
+ private DataType existingType = DataType.DEFAULT;
+
+ CountBasedStubCategory(String name, int threshold) {
+ super(name);
+ this.threshold = threshold;
+ }
+
+ @Override
+ public DataType getDataType(String name) {
+ if (count++ >= threshold) {
+ return null;
+ }
+ return existingType;
+ }
+ }
+}
diff --git a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/model/TestDoubleCategory.java b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/model/TestDoubleCategory.java
new file mode 100644
index 0000000000..0bb80eba2c
--- /dev/null
+++ b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/model/TestDoubleCategory.java
@@ -0,0 +1,152 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package ghidra.program.model;
+
+import java.util.List;
+
+import ghidra.program.model.data.*;
+import ghidra.util.InvalidNameException;
+import ghidra.util.exception.DuplicateNameException;
+import ghidra.util.task.TaskMonitor;
+
+/**
+ * A basic {@link Category} test double for tests to extend
+ */
+public class TestDoubleCategory implements Category {
+
+ private String categoryName;
+
+ public TestDoubleCategory(String name) {
+ this.categoryName = name;
+ }
+
+ @Override
+ public String getName() {
+ return categoryName;
+ }
+
+ @Override
+ public int compareTo(Category o) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void setName(String name) throws DuplicateNameException, InvalidNameException {
+ this.categoryName = name;
+ }
+
+ @Override
+ public Category[] getCategories() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public DataType[] getDataTypes() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public List<DataType> getDataTypesByBaseName(String name) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public DataType addDataType(DataType dt, DataTypeConflictHandler handler) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Category getCategory(String name) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public CategoryPath getCategoryPath() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public DataType getDataType(String name) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Category createCategory(String name) throws InvalidNameException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean removeCategory(String name, TaskMonitor monitor) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean removeEmptyCategory(String name, TaskMonitor monitor) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void moveCategory(Category category, TaskMonitor monitor) throws DuplicateNameException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Category copyCategory(Category category, DataTypeConflictHandler handler,
+ TaskMonitor monitor) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Category getParent() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean isRoot() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public String getCategoryPathName() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Category getRoot() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public DataTypeManager getDataTypeManager() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void moveDataType(DataType type, DataTypeConflictHandler handler)
+ throws DataTypeDependencyException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean remove(DataType type, TaskMonitor monitor) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public long getID() {
+ throw new UnsupportedOperationException();
+ }
+}