private void expandCollapseNode(JTree jTree, TreePath parent, boolean collapse) { TreeNode node = (TreeNode) parent.getLastPathComponent(); if (node.isLeaf()) { return; } Enumeration<?> children = node.children(); while (children.hasMoreElements()) { TreeNode child = (TreeNode) children.nextElement(); TreePath path = parent.pathByAddingChild(child); expandCollapseNode(jTree, path, collapse); } if (collapse) { jTree.collapsePath(parent); } else { jTree.expandPath(parent); } } }
/** * This method performs the actual command processing. * * @param e the generic UI action event */ @Override public void doAction(ActionEvent e) { JMeterTreeListener treeListener = GuiPackage.getInstance().getTreeListener(); JTree jTree = GuiPackage.getInstance().getMainFrame().getTree(); JMeterTreeNode[] selectedNodes = treeListener.getSelectedNodes(); for (JMeterTreeNode currentNode : selectedNodes) { if (!currentNode.isLeaf()) { TreeNode[] nodes = GuiPackage.getInstance().getTreeModel().getPathToRoot(currentNode); TreePath path = new TreePath(nodes); boolean collapse = ActionNames.COLLAPSE.equals(e.getActionCommand()); expandCollapseNode(jTree, path, collapse); } } }