Skip to content

Commit 0f9b64a

Browse files
committed
Fix collapse/expand all functionality
1 parent cdcc48b commit 0f9b64a

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

src/main/java/the/bytecode/club/jda/gui/FileNavigationPane.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.Comparator;
1616
import java.util.Enumeration;
1717
import java.util.Map.Entry;
18+
import java.util.function.Consumer;
1819

1920
/**
2021
* The file navigation pane.
@@ -29,9 +30,9 @@ public class FileNavigationPane extends JDAWindow implements FileDrop.Listener {
2930
private static final String quickSearchText = "Quick file search (no file extension)";
3031

3132
FileChangeNotifier fcn;
32-
JCheckBox exact = new JCheckBox("Exact");
33-
JButton open = new JButton("+");
34-
JButton close = new JButton("-");
33+
JCheckBox exact = new JCheckBox("Match case");
34+
JButton open = new JButton("Expand");
35+
JButton collapse = new JButton("Collapse");
3536

3637
FileNode treeRoot = new FileNode("Loaded Files:");
3738
FileTree tree = new FileTree(treeRoot);
@@ -113,14 +114,20 @@ public FileNavigationPane(final FileChangeNotifier fcn) {
113114
setMinimumSize(new Dimension(200, 50));
114115

115116
this.open.addActionListener(e -> {
116-
final TreeNode root = (TreeNode) tree.getModel().getRoot();
117-
expandAll(tree, new TreePath(root), true);
117+
if (tree.getSelectionPaths() != null) {
118+
for (TreePath path : tree.getSelectionPaths()) {
119+
treeDfs(path, tree::expandPath);
120+
tree.expandPath(path);
121+
}
122+
}
118123
});
119124

120-
this.close.addActionListener(e -> {
121-
final TreeNode root = (TreeNode) tree.getModel().getRoot();
122-
expandAll(tree, new TreePath(root), false);
123-
tree.expandPath(new TreePath(root));
125+
this.collapse.addActionListener(e -> {
126+
if (tree.getSelectionPaths() != null) {
127+
for (TreePath path : tree.getSelectionPaths()) {
128+
treeDfs(path, tree::collapsePath);
129+
}
130+
}
124131
});
125132

126133
this.tree.addMouseListener(new MouseAdapter() {
@@ -177,17 +184,15 @@ public void focusLost(final FocusEvent arg0) {
177184
p2.setLayout(new BorderLayout());
178185
p2.add(quickSearch, BorderLayout.NORTH);
179186
JPanel p3 = new JPanel(new BorderLayout());
180-
exact.setEnabled(false);
181187
p3.add(exact, BorderLayout.WEST);
182188
JPanel p4 = new JPanel(new BorderLayout());
183189
p4.add(open, BorderLayout.EAST);
184-
p4.add(close, BorderLayout.WEST);
190+
p4.add(collapse, BorderLayout.WEST);
185191
p3.add(p4, BorderLayout.EAST);
186192
p2.add(p3, BorderLayout.SOUTH);
187193

188194
getContentPane().add(p2, BorderLayout.SOUTH);
189195

190-
this.setVisible(true);
191196
new FileDrop(this, this);
192197
}
193198

@@ -271,27 +276,21 @@ public void updateTree() {
271276
} catch (java.util.ConcurrentModificationException e) {
272277
//ignore, the last file will reset everything
273278
}
274-
// expandAll(tree, true);
275279
}
276280

277281
@SuppressWarnings("rawtypes")
278-
private void expandAll(final JTree tree, final TreePath parent, final boolean expand) {
282+
private void treeDfs(final TreePath parent, Consumer<TreePath> onPreOrder) {
283+
onPreOrder.accept(parent);
284+
279285
// Traverse children
280286
final TreeNode node = (TreeNode) parent.getLastPathComponent();
281287
if (node.getChildCount() >= 0) {
282288
for (final Enumeration e = node.children(); e.hasMoreElements(); ) {
283289
final TreeNode n = (TreeNode) e.nextElement();
284290
final TreePath path = parent.pathByAddingChild(n);
285-
expandAll(tree, path, expand);
291+
treeDfs(path, onPreOrder);
286292
}
287293
}
288-
289-
// Expansion or collapse must be done bottom-up
290-
if (expand) {
291-
tree.expandPath(parent);
292-
} else {
293-
tree.collapsePath(parent);
294-
}
295294
}
296295

297296
public class FileTree extends JTree {

0 commit comments

Comments
 (0)