TreeWalker { public Stream<Component> descendantStream(DescendantOrder order) { return streamFromIterator(order.createIterator.fun(myRoot)); } TreeWalker(@NotNull Component root); static boolean isAncestor(@NotNull Container ancestor, @NotNull Component child); Stream<Component> ancestorStream(); Iterable<Component> ancestors(); Stream<Component> descendantStream(DescendantOrder order); Iterable<Component> descendants(DescendantOrder order); Stream<Component> descendantStream(); Iterable<Component> descendants();  }
@Test public void canStreamDescendants() throws Exception { JPanel panel1 = new JPanel(); JPanel panel11 = new JPanel(); JPanel panel12 = new JPanel(); JPanel panel123 = new JPanel(); JPanel panel13 = new JPanel(); panel1.add(panel11); panel1.add(panel12); panel12.add(panel123); panel1.add(panel13); JLabel label = new JLabel(); JButton button = new JButton(); JTextField textField = new JTextField(); panel11.add(label); panel123.add(button); panel13.add(textField); TreeWalker treeWalker = new TreeWalker(panel1); assertThat(treeWalker.descendantStream().filter(c -> c instanceof JLabel).findFirst().get()).isEqualTo(label); assertThat(treeWalker.descendantStream().filter(c -> c instanceof JButton).findFirst().get()).isEqualTo(button); assertThat(treeWalker.descendantStream().filter(c -> c instanceof JTextField).findFirst().get()).isEqualTo(textField); assertThat(treeWalker.descendantStream().anyMatch(c -> c instanceof JTable)).isFalse(); }
@Test public void descendantsIncludesSelf() throws Exception { JPanel panel1 = new JPanel(); JPanel panel11 = new JPanel(); JPanel panel12 = new JPanel(); panel1.add(panel11); panel1.add(panel12); TreeWalker treeWalker = new TreeWalker(panel1); assertThat(treeWalker.descendantStream().anyMatch(c -> c == panel1)).isTrue(); }
@Test public void descendantsAreReturnedInBreadthFirstSearchOrder() throws Exception { JPanel panelRoot = new JPanel(); JPanel panel1 = new JPanel(); JPanel panel11 = new JPanel(); JPanel panel111 = new JPanel(); JPanel panel112 = new JPanel(); JPanel panel2 = new JPanel(); JPanel panel3 = new JPanel(); JPanel panel31 = new JPanel(); JPanel panel32 = new JPanel(); panelRoot.add(panel1); panel1.add(panel11); panel11.add(panel111); panel11.add(panel112); panelRoot.add(panel2); panelRoot.add(panel3); panel3.add(panel31); panel3.add(panel32); TreeWalker treeWalker = new TreeWalker(panelRoot); assertThat(treeWalker.descendantStream().collect(Collectors.toList())). containsExactly(panelRoot, panel1, panel2, panel3, panel11, panel31, panel32, panel111, panel112).inOrder(); }
@Test public void descendantsCanBeReturnedInDepthFirstSearchOrder() throws Exception { JPanel panelRoot = new JPanel(); JPanel panel1 = new JPanel(); JPanel panel11 = new JPanel(); JPanel panel111 = new JPanel(); JPanel panel112 = new JPanel(); JPanel panel2 = new JPanel(); JPanel panel3 = new JPanel(); JPanel panel31 = new JPanel(); JPanel panel32 = new JPanel(); panelRoot.add(panel1); panel1.add(panel11); panel11.add(panel111); panel11.add(panel112); panelRoot.add(panel2); panelRoot.add(panel3); panel3.add(panel31); panel3.add(panel32); TreeWalker treeWalker = new TreeWalker(panelRoot); assertThat(treeWalker.descendantStream(TreeWalker.DescendantOrder.DEPTH_FIRST).collect(Collectors.toList())). containsExactly(panelRoot, panel1, panel11, panel111, panel112, panel2, panel3, panel31, panel32).inOrder(); }