Ghidra is a software reverse engineering (SRE) framework
GP-3812 - Added Collapse and Expand actions to trees
dragonmacher59108420d74a
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/calltree/CallTreeProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/calltree/CallTreeProvider.javaindex f90e289f2c..d89f30d1da 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/calltree/CallTreeProvider.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/calltree/CallTreeProvider.java@@ -351,7 +351,7 @@ public class CallTreeProvider extends ComponentProviderAdapter implements Domain}};goToSourceAction- .setPopupMenuData(new MenuData(new String[] { "Go To Call Source" }, goToMenu));+ .setPopupMenuData(new MenuData(new String[] { "Go To Call Source" }, goToMenu));goToSourceAction.setHelpLocation(new HelpLocation(plugin.getName(), "Call_Tree_Context_Action_Goto_Source"));tool.addLocalAction(this, goToSourceAction);@@ -365,16 +365,15 @@ public class CallTreeProvider extends ComponentProviderAdapter implements DomaindoUpdate();}};- filterDuplicates- .setToolBarData(new ToolBarData(new GIcon("icon.plugin.calltree.filter.duplicates"),- filterOptionsToolbarGroup, "1"));+ filterDuplicates.setToolBarData(new ToolBarData(+ new GIcon("icon.plugin.calltree.filter.duplicates"), filterOptionsToolbarGroup, "1"));filterDuplicates.setSelected(true);filterDuplicates- .setHelpLocation(new HelpLocation(plugin.getName(), "Call_Tree_Action_Filter"));+ .setHelpLocation(new HelpLocation(plugin.getName(), "Call_Tree_Action_Filter"));tool.addLocalAction(this, filterDuplicates);//- // recurse depth+ // recurse depth//recurseDepthAction = new DockingAction("Recurse Depth", plugin.getName()) {@Override@@ -394,7 +393,7 @@ public class CallTreeProvider extends ComponentProviderAdapter implements Domain"<br> will go. Example operations include <b>Expand All</b> and filtering");recurseIcon = new NumberIcon(recurseDepth.get());recurseDepthAction- .setToolBarData(new ToolBarData(recurseIcon, filterOptionsToolbarGroup, "2"));+ .setToolBarData(new ToolBarData(recurseIcon, filterOptionsToolbarGroup, "2"));recurseDepthAction.setHelpLocation(new HelpLocation(plugin.getName(), "Call_Tree_Action_Recurse_Depth"));@@ -402,7 +401,7 @@ public class CallTreeProvider extends ComponentProviderAdapter implements Domain//// navigate outgoing nodes on selection- //+ //navigationOutgoingAction =new ToggleDockingAction("Navigate Outgoing Nodes", plugin.getName()) {@@ -415,11 +414,11 @@ public class CallTreeProvider extends ComponentProviderAdapter implements DomainnavigationOutgoingAction.setToolBarData(new ToolBarData(Icons.NAVIGATE_ON_OUTGOING_EVENT_ICON, navigationOptionsToolbarGroup, "1"));navigationOutgoingAction- .setHelpLocation(new HelpLocation(plugin.getName(), "Call_Tree_Action_Navigation"));+ .setHelpLocation(new HelpLocation(plugin.getName(), "Call_Tree_Action_Navigation"));tool.addLocalAction(this, navigationOutgoingAction);//- // navigate incoming nodes on selection+ // navigate incoming nodes on selection//navigateIncomingToggleAction =new ToggleDockingAction("Navigation Incoming Location Changes", plugin.getName()) {@@ -438,8 +437,8 @@ public class CallTreeProvider extends ComponentProviderAdapter implements Domain}};- // note: the default state is to follow navigation events for the primary provider;- // non-primary providers will function like snapshots of the function with+ // note: the default state is to follow navigation events for the primary provider;+ // non-primary providers will function like snapshots of the function with// which they were activated.navigateIncomingToggleAction.setSelected(isPrimary);navigateIncomingToggleAction.setToolBarData(new ToolBarData(@@ -596,7 +595,7 @@ public class CallTreeProvider extends ComponentProviderAdapter implements DomainrefreshAction.setDescription("<html>Push at any time to refresh the current trees.<br>" +"This is highlighted when the data <i>may</i> be stale.<br>");refreshAction- .setHelpLocation(new HelpLocation(plugin.getName(), "Call_Tree_Action_Refresh"));+ .setHelpLocation(new HelpLocation(plugin.getName(), "Call_Tree_Action_Refresh"));tool.addLocalAction(this, refreshAction);//@@ -703,7 +702,7 @@ public class CallTreeProvider extends ComponentProviderAdapter implements Domainreturn;}- // no goto service...navigate the old fashioned way (this doesn't have history)+ // no goto service...navigate the old fashioned way (this doesn't have history)plugin.firePluginEvent(new ProgramLocationPluginEvent(getName(), location, currentProgram));isFiringNavigationEvent = false;}@@ -812,7 +811,14 @@ public class CallTreeProvider extends ComponentProviderAdapter implements Domain}private GTree createTree() {- GTree tree = new GTree(new EmptyRootNode());+ GTree tree = new GTree(new EmptyRootNode()) {+ @Override+ protected boolean supportsPopupActions() {+ // The base tree adds collapse/ expand actions, which we already provide, so signal+ // that we do not want those actions.+ return false;+ }+ };tree.setPaintHandlesForLeafNodes(false);// tree.setFilterVisible(false);return tree;@@ -1128,7 +1134,7 @@ public class CallTreeProvider extends ComponentProviderAdapter implements Domainreturn;}- // first, if the given node represents the function we have, then we don't need to+ // first, if the given node represents the function we have, then we don't need to// go any furtherif (function.equals(node.getRemoteFunction())) {GTreeNode parent = node.getParent();@@ -1153,7 +1159,7 @@ public class CallTreeProvider extends ComponentProviderAdapter implements Domain}}-// TODO if we ever want specific staleness detection+// TODO if we ever want specific staleness detection// private CallNode findNode(CallNode node, Function newFunction) {// Function function = node.getContainingFunction();// if (function == null) {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.javaindex a68a8ed61f..8fe7703330 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@@ -119,6 +119,13 @@ public class DataTypeArchiveGTree extends GTree {}}+ @Override+ protected boolean supportsPopupActions() {+ // The base tree adds collapse/ expand actions, which we already provide, so signal that we+ // do not want those actions.+ return false;+ }+@Overridepublic void dispose() {((ArchiveRootNode) getModelRoot()).dispose();diff --git a/Ghidra/Framework/Docking/src/main/java/docking/actions/SharedActionRegistry.java b/Ghidra/Framework/Docking/src/main/java/docking/actions/SharedActionRegistry.javaindex 755bebc3f7..1750ad08fa 100644--- a/Ghidra/Framework/Docking/src/main/java/docking/actions/SharedActionRegistry.java+++ b/Ghidra/Framework/Docking/src/main/java/docking/actions/SharedActionRegistry.java@@ -19,12 +19,13 @@ import docking.Tool;import docking.action.DockingActionIf;import docking.tool.ToolConstants;import docking.widgets.table.GTable;+import docking.widgets.tree.GTree;/*** A place used to hold {@link DockingActionIf}s that are meant to be used by components. Some* components do not have access to the tool that is required to register their actions. This- * class helps those components by enabling the installation of shared actions for those- * components.+ * class helps those components by enabling the installation of shared actions for those+ * components.*/public class SharedActionRegistry {@@ -35,5 +36,7 @@ public class SharedActionRegistry {*/public static void installSharedActions(Tool tool, ToolActions toolActions) {GTable.createSharedActions(tool, toolActions, ToolConstants.SHARED_OWNER);++ GTree.createSharedActions(tool, toolActions, ToolConstants.SHARED_OWNER);}}diff --git a/Ghidra/Framework/Docking/src/main/java/docking/widgets/tree/GTree.java b/Ghidra/Framework/Docking/src/main/java/docking/widgets/tree/GTree.javaindex 82d129954b..08283b06b9 100644--- a/Ghidra/Framework/Docking/src/main/java/docking/widgets/tree/GTree.java+++ b/Ghidra/Framework/Docking/src/main/java/docking/widgets/tree/GTree.java@@ -15,13 +15,13 @@*/package docking.widgets.tree;+import static docking.action.MenuData.*;import static docking.widgets.tree.support.GTreeSelectionEvent.EventOrigin.*;import static ghidra.util.SystemUtilities.*;import java.awt.*;import java.awt.dnd.Autoscroll;-import java.awt.event.MouseEvent;-import java.awt.event.MouseListener;+import java.awt.event.*;import java.io.PrintWriter;import java.util.*;import java.util.List;@@ -36,8 +36,10 @@ import javax.swing.tree.*;import org.apache.commons.lang3.StringUtils;-import docking.DockingWindowManager;+import docking.*;+import docking.action.*;import docking.actions.KeyBindingUtils;+import docking.actions.ToolActions;import docking.widgets.JTreeMouseListenerDelegate;import docking.widgets.filter.FilterTextField;import docking.widgets.table.AutoscrollAdapter;@@ -51,6 +53,7 @@ import ghidra.util.*;import ghidra.util.exception.CancelledException;import ghidra.util.task.*;import ghidra.util.worker.PriorityWorker;+import resources.Icons;/*** Class for creating a JTree that supports filtering, threading, and a progress bar.@@ -1394,6 +1397,16 @@ public class GTree extends JPanel implements BusyListener {node.fireNodeChanged();}+ /**+ * A method that subclasses can override to signal that they wish not to have this tree's+ * built-in popup actions. Subclasses will almost never need to override this method.+ *+ * @return true if popup actions are supported+ */+ protected boolean supportsPopupActions() {+ return true;+ }+//==================================================================================================// Inner Classes//==================================================================================================@@ -1440,11 +1453,15 @@ public class GTree extends JPanel implements BusyListener {this.scrollableUnitIncrementOverride = increment;}+ GTree getGTree() {+ return GTree.this;+ }+@Overridepublic String getToolTipText(MouseEvent event) {// Use the GTree's method so clients can override the behavior; provide the// default method below so we they can get the default behavior when needed.- return GTree.this.getToolTipText(event);+ return getGTree().getToolTipText(event);}public String getDefaultToolTipText(MouseEvent event) {@@ -1463,7 +1480,7 @@ public class GTree extends JPanel implements BusyListener {@Overridepublic boolean isPathEditable(TreePath path) {- return GTree.this.isPathEditable(path);+ return getGTree().isPathEditable(path);}@Override@@ -1568,10 +1585,173 @@ public class GTree extends JPanel implements BusyListener {}}+ private abstract static class GTreeAction extends DockingAction+ implements ComponentBasedDockingAction {++ GTreeAction(String name, String owner) {+ super(name, owner);+ }++ @Override+ public boolean isAddToPopup(ActionContext context) {+ if (!isEnabledForContext(context)) {+ return false;+ }++ GTree gTree = getTree(context);+ return gTree.supportsPopupActions();+ }++ @Override+ public boolean isEnabledForContext(ActionContext context) {+ return getTree(context) != null;+ }++ @Override+ public boolean isValidComponentContext(ActionContext context) {+ return getTree(context) != null;+ }++ protected GTree getTree(ActionContext context) {+ Component c = context.getSourceComponent();+ if (c instanceof GTree) {+ return (GTree) c;+ }+ if (c instanceof AutoScrollTree) {+ return ((AutoScrollTree) c).getGTree();+ }+ return null;+ }+ }+//==================================================================================================// Static Methods//==================================================================================================+ public static void createSharedActions(Tool tool, ToolActions toolActions, String owner) {++ String actionMenuGroup = "zzzTreeGroup";+ tool.setMenuGroup(new String[] { "Collapse" }, actionMenuGroup, "1");+ tool.setMenuGroup(new String[] { "Expand" }, actionMenuGroup, "2");++ int subGroupIndex = 1; // order by insertion+ GTreeAction collapseAction = new GTreeAction("Tree Collapse Node", owner) {+ @Override+ public void actionPerformed(ActionContext context) {+ GTree gTree = getTree(context);+ List<GTreeNode> nodes = gTree.getSelectedNodes();+ for (GTreeNode node : nodes) {+ gTree.collapseAll(node);+ }+ }++ @Override+ public boolean isEnabledForContext(ActionContext context) {++ if (!super.isEnabledForContext(context)) {+ return false;+ }++ GTree gTree = getTree(context);+ List<GTreeNode> nodes = gTree.getSelectedNodes();+ return !nodes.isEmpty();+ }+ };+ //@formatter:off+ collapseAction.setPopupMenuData(new MenuData(+ new String[] { "Collapse" },+ Icons.COLLAPSE_ALL_ICON,+ actionMenuGroup, NO_MNEMONIC,+ Integer.toString(subGroupIndex++)+ )+ );+ collapseAction.setKeyBindingData(new KeyBindingData(KeyEvent.VK_UP, InputEvent.ALT_DOWN_MASK));+ collapseAction.setHelpLocation(new HelpLocation("Trees", "Collapse"));+ //@formatter:on++ GTreeAction expandAction = new GTreeAction("Tree Expand Node", owner) {+ @Override+ public void actionPerformed(ActionContext context) {+ GTree gTree = getTree(context);+ List<GTreeNode> nodes = gTree.getSelectedNodes();+ for (GTreeNode node : nodes) {+ gTree.expandTree(node);+ }+ }++ @Override+ public boolean isEnabledForContext(ActionContext context) {++ if (!super.isEnabledForContext(context)) {+ return false;+ }++ GTree gTree = getTree(context);+ List<GTreeNode> nodes = gTree.getSelectedNodes();+ return !nodes.isEmpty();+ }+ };+ //@formatter:off+ expandAction.setPopupMenuData(new MenuData(+ new String[] { "Exapnd" },+ Icons.EXPAND_ALL_ICON,+ actionMenuGroup, NO_MNEMONIC,+ Integer.toString(subGroupIndex++)+ )+ );+ expandAction.setKeyBindingData(new KeyBindingData(KeyEvent.VK_DOWN, InputEvent.ALT_DOWN_MASK));+ expandAction.setHelpLocation(new HelpLocation("Trees", "Expand"));+ //@formatter:on++ GTreeAction collapseTreeAction = new GTreeAction("Tree Collapse All", owner) {+ @Override+ public void actionPerformed(ActionContext context) {+ GTree gTree = getTree(context);+ GTreeNode root = gTree.getViewRoot();+ gTree.collapseAll(root);+ }+ };+ //@formatter:off+ collapseTreeAction.setPopupMenuData(new MenuData(+ new String[] { "Collapse Tree" },+ null,+ actionMenuGroup, NO_MNEMONIC,+ Integer.toString(subGroupIndex++)+ )+ );+ collapseTreeAction.setHelpLocation(new HelpLocation("Trees", "Collapse_Tree"));+ //@formatter:on++ GTreeAction expandTreeAction = new GTreeAction("Tree Expand All", owner) {+ @Override+ public void actionPerformed(ActionContext context) {+ GTree gTree = getTree(context);+ gTree.expandAll();+ }+ };+ //@formatter:off+ expandTreeAction.setPopupMenuData(new MenuData(+ new String[] { "Expand Tree" },+ null,+ actionMenuGroup, NO_MNEMONIC,+ Integer.toString(subGroupIndex++)+ )+ );+ expandTreeAction.setHelpLocation(new HelpLocation("Trees", "Expand_Tree"));+ //@formatter:on++ // these actions are self-explanatory and do need help+ collapseAction.markHelpUnnecessary();+ expandAction.markHelpUnnecessary();+ collapseTreeAction.markHelpUnnecessary();+ expandTreeAction.markHelpUnnecessary();++ toolActions.addGlobalAction(collapseAction);+ toolActions.addGlobalAction(expandAction);+ toolActions.addGlobalAction(collapseTreeAction);+ toolActions.addGlobalAction(expandTreeAction);+ }+private static String generateFilterPreferenceKey() {Throwable throwable = new Throwable();StackTraceElement[] stackTrace = throwable.getStackTrace();diff --git a/Ghidra/Framework/Project/src/main/java/ghidra/framework/main/datatree/DataTree.java b/Ghidra/Framework/Project/src/main/java/ghidra/framework/main/datatree/DataTree.javaindex 747d24725c..5a1593bf9a 100644--- a/Ghidra/Framework/Project/src/main/java/ghidra/framework/main/datatree/DataTree.java+++ b/Ghidra/Framework/Project/src/main/java/ghidra/framework/main/datatree/DataTree.java@@ -63,6 +63,13 @@ public class DataTree extends GTree {KeyStroke.getKeyStroke(KeyEvent.VK_X, DockingUtils.CONTROL_KEY_MODIFIER_MASK));}+ @Override+ protected boolean supportsPopupActions() {+ // The base tree adds collapse/ expand actions, which we already provide, so signal that we+ // do not want those actions.+ return false;+ }+void setProjectActive(boolean isActive) {if (dragNDropHandler != null) {dragNDropHandler.setProjectActive(isActive);