Skip to content

Commit d46419c

Browse files
committed
Tweaked the GTree for restoring selection and expanded nodes when
filtering
1 parent 98a59a5 commit d46419c

File tree

4 files changed

+49
-18
lines changed

4 files changed

+49
-18
lines changed

Ghidra/Framework/Docking/src/main/java/docking/widgets/tree/GTree.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ public GTreeState getRestoreTreeState() {
325325
* <p>
326326
* <b>Note: </b>See the usage note at the header of this class concerning how tree state
327327
* is used relative to the <tt>equals()</tt> method.
328+
* @return the saved state
328329
*/
329330
public GTreeState getTreeState() {
330331
return new GTreeState(this);
@@ -340,10 +341,11 @@ public GTreeState getTreeState(GTreeNode node) {
340341
* <p>
341342
* <b>Note: </b>See the usage note at the header of this class concerning how tree state
342343
* is used relative to the <tt>equals()</tt> method.
344+
*
345+
* @param state the state to restore
343346
*
344347
* @see #getTreeState()
345348
* @see #getTreeState(GTreeNode)
346-
* @see #cloneTreeState()
347349
*/
348350
public void restoreTreeState(GTreeState state) {
349351
runTask(new GTreeRestoreTreeStateTask(this, state));
@@ -355,12 +357,16 @@ public void restoreTreeState(GTreeState state) {
355357
* that should be opened have been opened. Thus any other nodes are closed and can be
356358
* disposed, if desired.
357359
*
358-
* @param taskMonitor
360+
* @param taskMonitor the TaskMonitor
359361
*/
360-
public void expandedStateRestored(TaskMonitor onitor) {
362+
public void expandedStateRestored(TaskMonitor taskMonitor) {
361363
// optional
362364
}
363365

366+
public List<TreePath> getExpandedPaths() {
367+
return getExpandedPaths(getViewRoot());
368+
}
369+
364370
public List<TreePath> getExpandedPaths(GTreeNode node) {
365371
Enumeration<TreePath> expandedPaths = tree.getExpandedDescendants(node.getTreePath());
366372
if (expandedPaths == null) {
@@ -592,7 +598,7 @@ private GTreeNode getNodeForPath(GTreeNode root, TreePath path) {
592598

593599
GTreeNode node = (GTreeNode) path.getLastPathComponent();
594600
if (path.getPathCount() == 1) {
595-
if (root.getId() == node.getId()) {
601+
if (root.equals(node)) {
596602
return root;
597603
}
598604
return null; // invalid path--the root of the path is not equal to our root!
@@ -609,7 +615,7 @@ private GTreeNode getNodeForPath(GTreeNode root, TreePath path) {
609615
GTreeNode lastPathComponent = (GTreeNode) path.getLastPathComponent();
610616
List<GTreeNode> children = parentNode.getChildren();
611617
for (GTreeNode child : children) {
612-
if (child.getId() == lastPathComponent.getId()) {
618+
if (child.equals(lastPathComponent)) {
613619
return child;
614620
}
615621
}
@@ -825,7 +831,8 @@ public GTreeNode getViewRoot() {
825831

826832
/**
827833
* This method is useful for debugging tree problems. Don't know where else to put it.
828-
* @param name - Use this to indicate what tree event occurred ("node inserted" "node removed", etc.)
834+
* @param out the output writer
835+
* @param name use this to indicate what tree event occurred ("node inserted" "node removed", etc.)
829836
* @param e the TreeModelEvent;
830837
*/
831838
public static void printEvent(PrintWriter out, String name, TreeModelEvent e) {

Ghidra/Framework/Docking/src/main/java/docking/widgets/tree/GTreeNode.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -393,15 +393,6 @@ public String toString() {
393393
return getName();
394394
}
395395

396-
/**
397-
* Returns an id for this node that is unique among all GTreeNodes in this running JVM.
398-
* If this node is cloned, the clone will have the same id.
399-
* @return the unique id for this node.
400-
*/
401-
public long getId() {
402-
return id;
403-
}
404-
405396
/**
406397
* Notifies the tree that the node has different children. This method
407398
* @param node the node that has changed.

Ghidra/Framework/Docking/src/test.slow/java/docking/widgets/tree/GTreeTest.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,39 @@ public void testGetAndRestoreTreeState() {
527527
assertEquals(originalNode, selectionPath.getLastPathComponent());
528528
}
529529

530+
@Test
531+
public void testGetAndRestoreTreeState_ExpandedStateOnly() {
532+
//
533+
// Test that we can setup the tree, record its expanded state, change the tree
534+
// and then restore the saved state
535+
//
536+
537+
GTreeNode originalNode = findNodeInTree("Leaf Child - Many B1").getParent();
538+
assertNotNull("Did not find existing child node in non filtered tree", originalNode);
539+
540+
gTree.expandPath(originalNode);
541+
waitForTree();
542+
543+
List<TreePath> expandedPaths = gTree.getExpandedPaths();
544+
assertEquals(3, expandedPaths.size());
545+
assertEquals(originalNode, expandedPaths.get(0).getLastPathComponent());
546+
547+
GTreeState treeState = gTree.getTreeState();
548+
549+
gTree.collapseAll(gTree.getViewRoot());
550+
waitForTree();
551+
552+
expandedPaths = gTree.getExpandedPaths();
553+
assertTrue(expandedPaths.isEmpty());
554+
555+
gTree.restoreTreeState(treeState);
556+
waitForTree();
557+
558+
expandedPaths = gTree.getExpandedPaths();
559+
assertEquals(3, expandedPaths.size());
560+
assertEquals(originalNode, expandedPaths.get(0).getLastPathComponent());
561+
}
562+
530563
@Test
531564
public void testFilterPathsRestoredWithFurtherFiltering_NoSelection() throws Exception {
532565

@@ -872,8 +905,8 @@ private GTreeNode findNodeInTree(GTreeNode node, String name) {
872905
private void setFilterOptions(final TextFilterStrategy filterStrategy, final boolean inverted) {
873906
runSwing(() -> {
874907
FilterOptions filterOptions = new FilterOptions(filterStrategy, false, false, inverted);
875-
((DefaultGTreeFilterProvider) gTree.getFilterProvider()).setFilterOptions(
876-
filterOptions);
908+
((DefaultGTreeFilterProvider) gTree.getFilterProvider())
909+
.setFilterOptions(filterOptions);
877910
});
878911
waitForTree();
879912

Ghidra/Framework/Docking/src/test/java/docking/widgets/tree/GTreeNodeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void setUp() {
6161
public void testClone() throws CloneNotSupportedException {
6262
GTreeNode clone = node0.clone();
6363

64-
assertEquals(node0.getId(), clone.getId());
64+
assertTrue(node0.equals(clone));
6565
assertNull(clone.getParent());
6666
assertFalse(clone.isLoaded());
6767
}

0 commit comments

Comments
 (0)