package example;
//-*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
//@homepage@
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;

public final class MainPanel extends JPanel {
    private MainPanel() {
        super(new BorderLayout());

        JTree tree = new JTree();
        //tree.setModel(makeModel());

        JPanel p = new JPanel(new GridLayout(0, 1, 2, 2));
        p.add(new JButton(new AbstractAction("expand A") {
            @Override public void actionPerformed(ActionEvent e) {
                expandAll(tree);
            }
        }));
        p.add(new JButton(new AbstractAction("collapse A") {
            @Override public void actionPerformed(ActionEvent e) {
                collapseAll(tree);
            }
        }));
        p.add(new JButton(new AbstractAction("expand B") {
            @Override public void actionPerformed(ActionEvent e) {
                TreeNode root = (TreeNode) tree.getModel().getRoot();
                visitAll(tree, new TreePath(root), true);
                //expandAPath(new TreePath(root));
            }
        }));
        p.add(new JButton(new AbstractAction("collapse B") {
            @Override public void actionPerformed(ActionEvent e) {
                TreeNode root = (TreeNode) tree.getModel().getRoot();
                visitAll(tree, new TreePath(root), false);
            }
        }));
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(p, BorderLayout.NORTH);
        add(panel, BorderLayout.EAST);
        add(new JScrollPane(tree));
        setPreferredSize(new Dimension(320, 240));
    }

//     private static DefaultTreeModel makeModel() {
//         DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
//         DefaultMutableTreeNode set1 = new DefaultMutableTreeNode("Set 001");
//         DefaultMutableTreeNode set2 = new DefaultMutableTreeNode("Set 002");
//         DefaultMutableTreeNode set3 = new DefaultMutableTreeNode("Set 003");
//         set1.add(new DefaultMutableTreeNode("111111111"));
//         set1.add(new DefaultMutableTreeNode("22222222222"));
//         set1.add(new DefaultMutableTreeNode("33333"));
//         set2.add(new DefaultMutableTreeNode("asdfasdfas"));
//         set2.add(new DefaultMutableTreeNode("asdf"));
//         set3.add(new DefaultMutableTreeNode("asdfasdfasdf"));
//         set3.add(new DefaultMutableTreeNode("zxcvzxcvzx"));
//         set3.add(new DefaultMutableTreeNode("qwerqwerqwerqwerqwer"));
//         root.add(set1);
//         root.add(set2);
//         set2.add(set3);
//         return new DefaultTreeModel(root);
//     }

    //Expanding or Collapsing All Nodes in a JTree Component (Java Developers Almanac Example)
    //http://www.exampledepot.com/egs/javax.swing.tree/ExpandAll.html
    protected static void visitAll(JTree tree, TreePath parent, boolean expand) {
        TreeNode node = (TreeNode) parent.getLastPathComponent();
        if (!node.isLeaf() && node.getChildCount() >= 0) {
            Enumeration<?> e = node.children();
            while (e.hasMoreElements()) {
                visitAll(tree, parent.pathByAddingChild(e.nextElement()), expand);
            }
        }
        if (expand) {
            tree.expandPath(parent);
        } else {
            tree.collapsePath(parent);
        }
    }

    //Expand or collapse a JTree - Real's Java How-to
    //http://www.rgagnon.com/javadetails/java-0210.html
    protected static void expandAll(JTree tree) {
        int row = 0;
        while (row < tree.getRowCount()) {
            tree.expandRow(row);
            row++;
        }
    }
    protected static void collapseAll(JTree tree) {
        int row = tree.getRowCount() - 1;
        while (row >= 0) {
            tree.collapseRow(row);
            row--;
        }
    }
//     // https://community.oracle.com/thread/1393385 How to Expand a JTree completely
//     private void expandAPath(TreePath p) {
//         tree.expandPath(p);
//         DefaultMutableTreeNode n = (DefaultMutableTreeNode) p.getLastPathComponent();
//         for (int i = 0; i < n.getChildCount(); i++) {
//             expandAPath(p.pathByAddingChild(n.getChildAt(i)));
//         }
//     }
    public static void main(String... args) {
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
                createAndShowGUI();
            }
        });
    }
    public static void createAndShowGUI() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException
               | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }
        JFrame frame = new JFrame("@title@");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MainPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

@@ -150,24 +150,56 @@
 ava.util.*;%0A
+import java.util.stream.Stream;%0A
 import javax
@@ -2706,24 +2706,240 @@
 ;%0A//     %7D%0A%0A
+    public static Stream%3CTreeNode%3E children(TreeNode node) %7B%0A        return Collections.list((Enumeration%3C?%3E) node.children())%0A          .stream().filter(TreeNode.class::isInstance).map(TreeNode.class::cast);%0A    %7D%0A%0A
     //Expand
@@ -3235,24 +3235,117 @@
 omponent();%0A
+        children(node).forEach(n -%3E visitAll(tree, parent.pathByAddingChild(n), expand));%0A// 
         if (
@@ -3383,32 +3383,35 @@
 Count() %3E= 0) %7B%0A
+// 
             Enum
@@ -3438,24 +3438,26 @@
 children();%0A
+//
             
@@ -3452,24 +3452,25 @@
             
+ 
 while (e.has
@@ -3483,24 +3483,27 @@
 ements()) %7B%0A
+// 
             
@@ -3573,16 +3573,19 @@
 xpand);%0A
+// 
         
@@ -3582,32 +3582,35 @@
 /             %7D%0A
+// 
         %7D%0A      

use Stream instead of while (Enumeration#hasMoreElements())