Example usage for javax.swing.tree TreePath pathByAddingChild

List of usage examples for javax.swing.tree TreePath pathByAddingChild

Introduction

In this page you can find the example usage for javax.swing.tree TreePath pathByAddingChild.

Prototype

public TreePath pathByAddingChild(Object child) 

Source Link

Document

Returns a new path containing all the elements of this path plus child.

Usage

From source file:Main.java

protected static void expandTree(JTree tree, TreePath path, Hashtable lookedAt) {
    Object node = path.getLastPathComponent();
    if (lookedAt.containsKey(node))
        return;//  ww  w  . ja  v a 2 s . c o m
    lookedAt.put(node, node);

    Vector paths = new Vector();
    tree.makeVisible(path);
    int childCount = tree.getModel().getChildCount(node);
    for (int i = 0; i < childCount; i++) {
        Object child = tree.getModel().getChild(node, i);
        TreePath p = path.pathByAddingChild(child);
        expandTree(tree, p, lookedAt);
    }
}

From source file:jshm.gui.GuiUtil.java

private static void expandTreeBelowNode(JXTreeTable tree, TreeTableModel model, TreePath path) {
    Object parent = path.getLastPathComponent();

    int childCount = model.getChildCount(parent);

    for (int i = 0; i < childCount; i++) {
        TreePath childPath = path.pathByAddingChild(model.getChild(parent, i));

        // using childCount is faster and plain better than isLeaf
        // since a non-leaf might have 0 children which would be
        // useless to expand
        if (0 < model.getChildCount(childPath.getLastPathComponent())) {
            tree.expandPath(childPath);// ww w .  j a v  a2 s  . c o  m
            expandTreeBelowNode(tree, model, childPath);
        }
    }
}

From source file:jshm.gui.GuiUtil.java

private static void getExpandedPaths(JXTreeTable tree, TreeTableModel model, TreePath path,
        List<TreePath> paths) {
    //      System.out.println("Checking: " + path);

    Object parent = path.getLastPathComponent();
    final int childCount = model.getChildCount(parent);

    for (int i = 0; i < childCount; i++) {
        Object curNode = model.getChild(parent, i);
        TreePath curPath = path.pathByAddingChild(curNode);

        if (model.isLeaf(curNode))
            continue;

        if (tree.isExpanded(curPath))
            paths.add(curPath);/*  ww  w  . j a  v  a 2 s.  co m*/

        getExpandedPaths(tree, model, curPath, paths);
    }
}

From source file:TreeUtil.java

public static TreePath makeTreePath(String path, DefaultMutableTreeNode parentNode) {
    DefaultMutableTreeNode tempNode = parentNode;
    TreePath res = new TreePath(parentNode);
    StringTokenizer tok = new StringTokenizer(path, ".");
    String currentPath = null;/*  www  .  j a v  a2s  .c  om*/
    while (tok.hasMoreTokens()) {
        String myTok = tok.nextToken();
        currentPath = (currentPath == null) ? myTok : currentPath + "." + myTok;
        for (int j = 0; j < tempNode.getChildCount(); j++) {
            DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) tempNode.getChildAt(j);
            if (childNode.toString().equals(myTok)) {
                tempNode = childNode;
                res = res.pathByAddingChild(tempNode);
                break;
            }
        }
    }
    return res;
}

From source file:jshm.gui.GuiUtil.java

private static void collapseTreeToDepth(JXTreeTable tree, TreeTableModel model, Object parent, TreePath path,
        int curDepth, int stopDepth) {
    curDepth++;/*w w  w .j a va 2 s .c o  m*/

    if (curDepth == stopDepth) {
        //         System.out.println("collapsing: " + path);
        tree.collapsePath(path);
        return;
    }

    for (int i = 0; i < model.getChildCount(parent); i++) {
        Object child = model.getChild(parent, i);

        if (model.isLeaf(child)) {
            continue;
        }

        collapseTreeToDepth(tree, model, child, path.pathByAddingChild(child), curDepth, stopDepth);
    }
}

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);/*from www .  ja v  a  2s .c om*/
        }
    }
    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;//www  . j  av a 2  s  . c  o  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 void getPaths(JTree tree, TreePath parent, boolean expanded, List<TreePath> list) {
    if (expanded && !tree.isVisible(parent)) {
        return;/* w  w w. j a  va2  s. c o m*/
    }
    list.add(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);
            getPaths(tree, path, expanded, list);
        }
    }
}

From source file:org.robotframework.swing.tree.TreePathWaitable.java

private TreePath buildTreePath(String[] nodeNames) {
    Object root = getRoot();/*w  w w.j av  a 2 s . co  m*/
    TreePath treePathToNode = new TreePath(root);
    Iterator<Object> currentLevelChildren = getChildren(root);
    for (String nodeName : nodeNames) {
        boolean foundMatch = false;

        while (currentLevelChildren.hasNext()) {
            Object currentNode = currentLevelChildren.next();
            if (nodeTextEquals(nodeName, currentNode)) {
                currentLevelChildren = getChildren(currentNode);
                treePathToNode = treePathToNode.pathByAddingChild(currentNode);
                foundMatch = true;
                break;
            }
        }

        if (!foundMatch)
            return null;
    }
    return treePathToNode;
}

From source file:cz.lidinsky.editor.Editor.java

/**
 *  Add a link component -> tree to each new node.
 *  And select last inserted node./*  w w w . j  a va 2s  .  c o  m*/
 */
public void treeNodesInserted(TreeModelEvent e) {
    // add a link component -> tree
    TreePath parentPath = e.getTreePath();
    Node<GuiObject> parent = (Node<GuiObject>) parentPath.getLastPathComponent();
    int[] indexes = e.getChildIndices();
    for (int i : indexes) {
        Node<GuiObject> child = parent.getChild(i);
        setComponent2TreeLink(child);
    }
    // select last inserted node
    Node<GuiObject> child = parent.getChild(indexes[indexes.length - 1]);
    guiStructureTree.setSelectionPath(parentPath.pathByAddingChild(child));
}