@@ -31,8 +31,6 @@ public class FileNavigationPane extends JDAWindow implements FileDrop.Listener {
3131
3232 FileChangeNotifier fcn ;
3333 JCheckBox exact = new JCheckBox ("Match case" );
34- JButton open = new JButton ("Expand" );
35- JButton collapse = new JButton ("Collapse" );
3634
3735 FileNode treeRoot = new FileNode ("Loaded Files:" );
3836 FileTree tree = new FileTree (treeRoot );
@@ -113,29 +111,14 @@ public FileNavigationPane(final FileChangeNotifier fcn) {
113111 quickSearch .setForeground (Color .gray );
114112 setMinimumSize (new Dimension (200 , 50 ));
115113
116- this .open .addActionListener (e -> {
117- if (tree .getSelectionPaths () != null ) {
118- for (TreePath path : tree .getSelectionPaths ()) {
119- treeDfs (path , tree ::expandPath );
120- tree .expandPath (path );
121- }
122- }
123- });
124-
125- this .collapse .addActionListener (e -> {
126- if (tree .getSelectionPaths () != null ) {
127- for (TreePath path : tree .getSelectionPaths ()) {
128- treeDfs (path , tree ::collapsePath );
129- }
130- }
131- });
132-
133114 this .tree .addMouseListener (new MouseAdapter () {
134115 @ Override
135116 public void mousePressed (MouseEvent e ) {
136117 openPath (tree .getPathForLocation (e .getX (), e .getY ()));
137118 }
138119 });
120+ tree .setComponentPopupMenu (new TreeContextMenu ());
121+ tree .setInheritsPopupMenu (true );
139122
140123 this .tree .addKeyListener (new KeyListener () {
141124 public void keyTyped (KeyEvent e ) {
@@ -186,8 +169,6 @@ public void focusLost(final FocusEvent arg0) {
186169 JPanel p3 = new JPanel (new BorderLayout ());
187170 p3 .add (exact , BorderLayout .WEST );
188171 JPanel p4 = new JPanel (new BorderLayout ());
189- p4 .add (open , BorderLayout .EAST );
190- p4 .add (collapse , BorderLayout .WEST );
191172 p3 .add (p4 , BorderLayout .EAST );
192173 p2 .add (p3 , BorderLayout .SOUTH );
193174
@@ -279,18 +260,21 @@ public void updateTree() {
279260 }
280261
281262 @ SuppressWarnings ("rawtypes" )
282- private void treeDfs (final TreePath parent , Consumer <TreePath > onPreOrder ) {
283- onPreOrder .accept (parent );
263+ private void treeDfs (final TreePath parent , Consumer <TreePath > onPreOrder , Consumer <TreePath > onPostOrder ) {
264+ if (onPreOrder != null )
265+ onPreOrder .accept (parent );
284266
285- // Traverse children
286267 final TreeNode node = (TreeNode ) parent .getLastPathComponent ();
287268 if (node .getChildCount () >= 0 ) {
288269 for (final Enumeration e = node .children (); e .hasMoreElements (); ) {
289270 final TreeNode n = (TreeNode ) e .nextElement ();
290271 final TreePath path = parent .pathByAddingChild (n );
291- treeDfs (path , onPreOrder );
272+ treeDfs (path , onPreOrder , onPostOrder );
292273 }
293274 }
275+
276+ if (onPostOrder != null )
277+ onPostOrder .accept (parent );
294278 }
295279
296280 public class FileTree extends JTree {
@@ -518,4 +502,29 @@ public Component getTreeCellRendererComponent(JTree tree, Object value, boolean
518502 }
519503 }
520504
505+ private class TreeContextMenu extends JPopupMenu {
506+ JMenuItem expandAll , collapseAll ;
507+
508+ public TreeContextMenu (){
509+ add (expandAll = new JMenuItem ("Expand All" ));
510+ add (collapseAll = new JMenuItem ("Collapse All" ));
511+
512+ expandAll .addActionListener (e -> {
513+ if (tree .getSelectionPaths () != null ) {
514+ for (TreePath path : tree .getSelectionPaths ()) {
515+ treeDfs (path , tree ::expandPath , null );
516+ tree .expandPath (path );
517+ }
518+ }
519+ });
520+
521+ collapseAll .addActionListener (e -> {
522+ if (tree .getSelectionPaths () != null ) {
523+ for (TreePath path : tree .getSelectionPaths ()) {
524+ treeDfs (path , null , tree ::collapsePath );
525+ }
526+ }
527+ });
528+ }
529+ }
521530}
0 commit comments