Example usage for javax.swing JTree expandPath

List of usage examples for javax.swing JTree expandPath

Introduction

In this page you can find the example usage for javax.swing JTree expandPath.

Prototype

public void expandPath(TreePath path) 

Source Link

Document

Ensures that the node identified by the specified path is expanded and viewable.

Usage

From source file:Main.java

private void expandAll(JTree tree, TreePath parent) {
    TreeNode node = (TreeNode) parent.getLastPathComponent();
    if (node.getChildCount() >= 0) {
        for (Enumeration e = node.children(); e.hasMoreElements();) {
            TreeNode n = (TreeNode) e.nextElement();
            TreePath path = parent.pathByAddingChild(n);
            expandAll(tree, path);/*  w w  w. j a v  a 2s.  com*/
        }
    }
    tree.expandPath(parent);
    // tree.collapsePath(parent);
}

From source file:com.peter.mavenrunner.MavenRunnerTopComponent.java

private static void expandAll(JTree tree, TreePath treePath, boolean expand, int maxLevel, int currentLevel) {
    if (maxLevel != -1 && currentLevel >= maxLevel - 1) {
        return;//from   www.j  av a 2 s . co m
    }

    TreeNode node = (TreeNode) treePath.getLastPathComponent();
    if (node.getChildCount() >= 0) {
        for (Enumeration<TreeNode> e = node.children(); e.hasMoreElements();) {
            TreeNode n = e.nextElement();

            TreePath path = treePath.pathByAddingChild(n);
            expandAll(tree, path, expand, maxLevel, currentLevel + 1);
        }
    }

    // Expansion or collapse must be done bottom-up
    if (expand) {
        tree.expandPath(treePath);
    } else {
        tree.collapsePath(treePath);
    }
}

From source file:Main.java

public Main() {
    super(BoxLayout.Y_AXIS);
    glassPane.setOpaque(false);/* w  w  w  .  java 2s  .c o m*/
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    for (int i = 0; i < 14000; i++) {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode("Root" + i);
        node.add(new DefaultMutableTreeNode("Child" + i));
        root.add(node);
    }

    final JTree tree = new JTree(root);
    tree.setRootVisible(false);
    final JScrollPane pane = new JScrollPane(tree);
    add(pane);

    JButton button = new JButton("Expand");
    button.addActionListener(e -> {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                TreePath[] paths = tree.getSelectionPaths();
                if (paths == null || paths.length == 0) {
                    glassPane.setVisible(false);
                    return;
                }
                tree.setSelectionPath(paths[0]);

                for (int i = 0; i < paths.length; i++) {
                    tree.expandPath(paths[i]);
                }
                glassPane.setVisible(false);
            }
        });
        getRootPane().setGlassPane(glassPane);
        glassPane.setVisible(true);
        t.start();
    });
    add(button);
    glassPane.addMouseWheelListener(e -> {
        for (MouseWheelListener mwl : pane.getMouseWheelListeners()) {
            mwl.mouseWheelMoved(e);
        }
    });
}

From source file:gdt.jgui.entity.JEntityStructurePanel.java

private void expandAll(JTree tree, TreePath path, boolean expand) {
    TreeNode node = (TreeNode) path.getLastPathComponent();
    if (node.getChildCount() >= 0) {
        Enumeration enumeration = node.children();
        while (enumeration.hasMoreElements()) {
            TreeNode n = (TreeNode) enumeration.nextElement();
            TreePath p = path.pathByAddingChild(n);
            expandAll(tree, p, expand);/*  w ww.  j  av a  2s  .c  o m*/
        }
    }
    if (expand) {
        tree.expandPath(path);
    } else {
        tree.collapsePath(path);
    }
}

From source file:gdt.jgui.entity.JEntityDigestDisplay.java

private void expandAll(JTree tree, TreePath path, boolean expand) {
    TreeNode node = (TreeNode) path.getLastPathComponent();
    if (node.getChildCount() >= 0) {
        Enumeration enumeration = node.children();
        while (enumeration.hasMoreElements()) {
            DefaultMutableTreeNode n = (DefaultMutableTreeNode) enumeration.nextElement();
            TreePath p = path.pathByAddingChild(n);
            expandAll(tree, p, expand);//w  w w  .j  a  v  a  2s.  c  o  m
        }
    }
    if (expand) {
        tree.expandPath(path);
    } else {
        tree.collapsePath(path);
    }
}

From source file:com.peter.mavenrunner.MavenRunnerTopComponent.java

public void expandAll(JTree tree, boolean expand, int maxLevel) {
    MyTreeNode tempRoot = (MyTreeNode) treeModel.getRoot();
    if (tempRoot != null) {
        //         log(" ---------- tempRoot 2 = " + tempRoot.getChildCount() + " , " + tempRoot);
        //         for (int x = 0; x < tempRoot.getChildCount(); x++) {
        //            log("                --------> " + x + " = " + tempRoot.getChildAt(x));
        //         }
        expandAll(tree, new TreePath(tempRoot), expand, maxLevel, 0);
        tree.expandPath(new TreePath(tempRoot));
    }/*from   w w w. j  a  va 2  s  .com*/
}

From source file:net.sf.jabref.gui.FindUnlinkedFilesDialog.java

/**
 * Expands or collapses the specified tree according to the
 * <code>expand</code>-parameter.
 *///from  w ww.  j a  va  2  s  .c o m
private void expandTree(JTree currentTree, TreePath parent, boolean expand) {
    TreeNode node = (TreeNode) parent.getLastPathComponent();
    if (node.getChildCount() >= 0) {
        for (Enumeration<TreeNode> e = node.children(); e.hasMoreElements();) {
            TreePath path = parent.pathByAddingChild(e.nextElement());
            expandTree(currentTree, path, expand);
        }
    }
    if (expand) {
        currentTree.expandPath(parent);
    } else {
        currentTree.collapsePath(parent);
    }
}

From source file:gdt.jgui.entity.index.JIndexPanel.java

private void expandAll(JTree tree, TreePath parent, boolean expand) {

    TreeNode node = (TreeNode) parent.getLastPathComponent();

    if (node.getChildCount() >= 0) {
        for (Enumeration e = node.children(); e.hasMoreElements();) {
            TreeNode n = (TreeNode) e.nextElement();
            TreePath path = parent.pathByAddingChild(n);
            expandAll(tree, path, expand);
        }// w  ww . j a v  a  2  s .  co  m
    }
    if (expand) {
        tree.expandPath(parent);
    } else {
        tree.collapsePath(parent);
    }
}

From source file:com.pironet.tda.TDA.java

/**
 * expand or collapse all nodes of the specified tree
 *
 * @param catTree the tree to expand all/collapse all
 * @param parent  the parent to start with
 * @param expand  expand=true, collapse=false
 *//*from  w  ww.java 2  s .  co m*/
private void expandAll(JTree catTree, TreePath parent, boolean expand) {
    // Traverse children
    TreeNode node = (TreeNode) parent.getLastPathComponent();
    if (node.getChildCount() >= 0) {
        for (Enumeration e = node.children(); e.hasMoreElements();) {
            TreeNode n = (TreeNode) e.nextElement();
            TreePath path = parent.pathByAddingChild(n);
            expandAll(catTree, path, expand);
        }
    }

    if (parent.getPathCount() > 1) {
        // Expansion or collapse must be done bottom-up
        if (expand) {
            catTree.expandPath(parent);
        } else {
            catTree.collapsePath(parent);
        }
    }
}

From source file:eu.europa.ec.markt.dss.applet.view.validationpolicy.EditView.java

private void nodeActionAdd(MouseEvent mouseEvent, final int selectedRow, final TreePath selectedPath,
        final XmlDomAdapterNode clickedItem, final JTree tree) {
    final Element clickedElement = (Element) clickedItem.node;
    // popup menu for list -> add
    final JPopupMenu popup = new JPopupMenu();
    //delete node
    final JMenuItem menuItemToDelete = new JMenuItem(ResourceUtils.getI18n("DELETE"));
    menuItemToDelete.addActionListener(new ActionListener() {
        @Override//from   w ww . j  a  v a 2 s.c  om
        public void actionPerformed(ActionEvent actionEvent) {
            // find the order# of the child to delete
            final int index = clickedItem.getParent().index(clickedItem);

            Node oldChild = clickedElement.getParentNode().removeChild(clickedElement);
            if (index > -1) {
                validationPolicyTreeModel.fireTreeNodesRemoved(selectedPath.getParentPath(), index, oldChild);
            }
        }
    });
    popup.add(menuItemToDelete);

    //Add node/value
    final Map<XsdNode, Object> xsdTree = validationPolicyTreeModel.getXsdTree();
    final List<XsdNode> children = getChildrenToAdd(clickedElement, xsdTree);
    for (final XsdNode xsdChild : children) {
        final String xmlName = xsdChild.getLastNameOfPath();

        final JMenuItem menuItem = new JMenuItem(ResourceUtils.getI18n("ADD") + " (" + xmlName + " "
                + xsdChild.getType().toString().toLowerCase() + ")");
        popup.add(menuItem);
        menuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                Document document = getModel().getValidationPolicy().getDocument();

                final Node newElement;
                if (xsdChild.getType() == XsdNodeType.TEXT) {
                    // TEXT element always appended (last child)
                    newElement = clickedElement.appendChild(document.createTextNode("VALUE"));
                } else if (xsdChild.getType() == XsdNodeType.ATTRIBUTE) {
                    newElement = document.createAttributeNS(null, xmlName);
                    ((Attr) newElement).setValue("VALUE");
                    clickedElement.setAttributeNode((Attr) newElement);
                } else if (xsdChild.getType() == XsdNodeType.ELEMENT) {
                    final Element childToAdd = document
                            .createElementNS("http://dss.markt.ec.europa.eu/validation/diagnostic", xmlName);
                    // find the correct possition to add the child
                    // Get all allowed children
                    Map<XsdNode, Object> childrenMap = getChild(getXPath(clickedElement), xsdTree);
                    boolean toAddSeen = false;
                    Element elementIsToAddBeforeThisOne = null;
                    for (final XsdNode allowed : childrenMap.keySet()) {
                        if (!toAddSeen && allowed == xsdChild) {
                            toAddSeen = true;
                            continue;
                        }
                        if (toAddSeen) {
                            final NodeList elementsByTagNameNS = clickedElement.getElementsByTagNameNS(
                                    "http://dss.markt.ec.europa.eu/validation/diagnostic",
                                    allowed.getLastNameOfPath());
                            if (elementsByTagNameNS.getLength() > 0) {
                                // we found an element that is supposed to be after the one to add
                                elementIsToAddBeforeThisOne = (Element) elementsByTagNameNS.item(0);
                                break;
                            }
                        }
                    }

                    if (elementIsToAddBeforeThisOne != null) {
                        newElement = clickedElement.insertBefore(childToAdd, elementIsToAddBeforeThisOne);
                    } else {
                        newElement = clickedElement.appendChild(childToAdd);
                    }
                } else {
                    throw new IllegalArgumentException("Unknow XsdNode NodeType " + xsdChild.getType());
                }

                document.normalizeDocument();

                int indexOfAddedItem = 0;
                final int childCount = clickedItem.childCount();
                for (int i = 0; i < childCount; i++) {
                    if (clickedItem.child(i).node == newElement) {
                        indexOfAddedItem = i;
                        break;
                    }
                }

                TreeModelEvent event = new TreeModelEvent(validationPolicyTreeModel, selectedPath,
                        new int[] { indexOfAddedItem }, new Object[] { newElement });
                validationPolicyTreeModel.fireTreeNodesInserted(event);
                tree.expandPath(selectedPath);

                //Update model
                getModel().getValidationPolicy().setXmlDom(new XmlDom(document));
            }
        });

    }
    popup.show(tree, mouseEvent.getX(), mouseEvent.getY());
}