Example usage for javax.swing.tree TreeNode getParent

List of usage examples for javax.swing.tree TreeNode getParent

Introduction

In this page you can find the example usage for javax.swing.tree TreeNode getParent.

Prototype

TreeNode getParent();

Source Link

Document

Returns the parent TreeNode of the receiver.

Usage

From source file:Utils.java

public static TreePath getPath(TreeNode treeNode) {
    List<Object> nodes = new ArrayList<Object>();
    if (treeNode != null) {
        nodes.add(treeNode);// w  w  w .java2  s.co  m
        treeNode = treeNode.getParent();
        while (treeNode != null) {
            nodes.add(0, treeNode);
            treeNode = treeNode.getParent();
        }
    }

    return nodes.isEmpty() ? null : new TreePath(nodes.toArray());
}

From source file:ffx.potential.extended.ExtUtils.java

public static void printTreeFromNode(MSNode target) {
    StringBuilder sb = new StringBuilder();
    sb.append(format(" Tree Hierarchy for %s\n", target.getName()));
    sb.append(format(" ===================%s\n", StringUtils.repeat("=", target.getName().length())));
    TreeNode root = target;
    while (root.getParent() != null) {
        root = root.getParent();//from   w  w w  .ja v a  2 s.  c om
    }
    Enumeration fromRoot = target.pathFromAncestorEnumeration(root);
    List<MSNode> toLeaves = target.getDescendants(MSNode.class);
    boolean init = false;
    for (MSNode node : toLeaves) {
        if (!init) {
            sb.append(format(" %s> %s\n", StringUtils.repeat(">", node.getLevel()), node.getName()));
            init = true;
            continue;
        }
        if (!node.getName().isEmpty()) {
            sb.append(format(" %s> %s\n", StringUtils.repeat("-", node.getLevel()), node.getName()));
        }
    }
    logger.info(sb.toString());
}

From source file:com.mirth.connect.client.ui.components.MirthTree.java

/**
 * Get the index of a node in relation to other nodes with the same name. Returns -1 if there
 * isn't an index./*from  www  .ja  va 2 s .  co m*/
 * 
 * @param node
 * @return
 */
public static int getIndexOfNode(TreeNode node) {
    String nodeName = node.toString().replaceAll(" \\(.*\\)", ""); // Get the node name without the vocab
    TreeNode parent = node.getParent();

    // The parent will be null for the root node
    if (parent != null) {
        Enumeration children = parent.children();
        int indexCounter = 0;
        int foundIndex = -1;

        // Look through all of the children of the parent to see if there
        // are multiple children with the same name.
        while (children.hasMoreElements()) {
            TreeNode child = (TreeNode) children.nextElement();

            // Ignore the draggable nodes that have no children
            if (child.getChildCount() > 0) {
                if (nodeName.equals(child.toString().replaceAll(" \\(.*\\)", ""))) {
                    if (child != node) {
                        indexCounter++;
                    } else {
                        foundIndex = indexCounter;
                        indexCounter++;
                    }
                }
            }
        }

        // If there were multiple children, add the index to the nodeQ.
        if (indexCounter > 1) {
            return foundIndex;
        }
    }

    return -1;
}

From source file:Main.java

public TreePath getPath(TreeNode node) {
    List<TreeNode> list = new ArrayList<TreeNode>();

    while (node != null) {
        list.add(node);/*from  w ww. j a  va  2 s . c o m*/
        node = node.getParent();
    }
    Collections.reverse(list);

    return new TreePath(list.toArray());
}

From source file:it.unibas.spicygui.controllo.window.operator.ProjectTreeGenerator.java

private void analisiRicorsivaAggiuntaTC(Object root, Scenario scenario, DefaultTreeModel model,
        TopComponent topComponentAdded) {
    int indice = model.getChildCount(root);
    for (int i = 0; i < indice; i++) {
        analisiRicorsivaAggiuntaTC(model.getChild(root, i), scenario, model, topComponentAdded);
    }/*from  w w  w  . j  a va  2s . c  o  m*/
    //        if (indice == 2) {
    TreeTopComponentAdapter componentAdapter = (TreeTopComponentAdapter) ((DefaultMutableTreeNode) root)
            .getUserObject();
    TopComponent tc = componentAdapter.getTopComponent();
    if (tc != null && tc == scenario.getMappingTaskTopComponent()) {
        TreeNode treeNode = (TreeNode) root;
        int place = model.getChildCount(treeNode.getParent());
        DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(
                new TreeTopComponentAdapter(topComponentAdded, false, false, false));
        model.insertNodeInto(newNode, (DefaultMutableTreeNode) treeNode.getParent(), place);
    }
    //            return;
    //        }

}

From source file:it.unibas.spicygui.controllo.window.operator.ProjectTreeGenerator.java

private boolean analisiRicorsivaCancellaScenario(Object root, Scenario scenario, DefaultTreeModel model) {
    int indice = model.getChildCount(root);
    //        if (indice == 1 || indice == 2) {
    TreeTopComponentAdapter componentAdapter = (TreeTopComponentAdapter) ((DefaultMutableTreeNode) root)
            .getUserObject();//from w ww  .  j a va 2s .c  om
    TopComponent tc = componentAdapter.getTopComponent();
    if (tc != null && tc == scenario.getMappingTaskTopComponent()) {
        TreeNode treeNode = (TreeNode) root;
        model.removeNodeFromParent((MutableTreeNode) treeNode.getParent());
        return false;
    }
    //        }

    for (int i = 0; i < indice; i++) {
        if (!analisiRicorsivaCancellaScenario(model.getChild(root, i), scenario, model)) {
            return false;
        }
    }
    return true;

    //        for (int i = 0; i < root.getChildCount(); i++) {
    //            TreePath treePath = jTree.getPathForRow(i);
    //            TreeNode treeNode = (TreeNode) treePath.getLastPathComponent();
    //            TreeTopComponentAdapter componentAdapter = (TreeTopComponentAdapter) ((DefaultMutableTreeNode) treeNode).getUserObject();
    //            TopComponent tc = componentAdapter.getTopComponent();
    //            if (tc != null && tc == scenario.getMappingTaskTopComponent()) {
    //                int place = model.getChildCount(treeNode.getParent());
    //                DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new TreeTopComponentAdapter(topComponentAdded, false, false));
    //                model.insertNodeInto(newNode, (DefaultMutableTreeNode) treeNode.getParent(), place);
    //                model.reload();
    //                break;
    //            }
    //        }
}

From source file:org.artifactory.webapp.wicket.actionable.tree.ActionableItemsTree.java

public TreeNode getNextTreeNode(TreeNode node) {
    ITreeState state = getTreeState();//w  ww  .j  a  va  2  s .  com
    DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
    if (parent == null) {
        return null;
    }
    if (!node.isLeaf() && node.getAllowsChildren() && state.isNodeExpanded(node)) {
        return node.getChildAt(0);
    }

    TreeNode nextNode = parent.getChildAfter(node);
    if (nextNode == null) {
        return getNextParent(parent);
    }
    return nextNode;
}

From source file:org.artifactory.webapp.wicket.actionable.tree.ActionableItemsTree.java

public TreeNode getPrevTreeNode(TreeNode node) {
    DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
    if (parent == null) {
        return null;
    }/*from w w w. ja  v  a2  s.  com*/

    TreeNode prevNode = parent.getChildBefore(node);
    if (prevNode != null) {
        ITreeState state = getTreeState();
        node = prevNode;
        while (!node.isLeaf() && node.getAllowsChildren() && state.isNodeExpanded(node)) {
            node = node.getChildAt(node.getChildCount() - 1);
        }
        return node;
    }

    DefaultTreeModel treeModel = getTreeModel();
    if (parent == treeModel.getRoot()) {
        return null;
    }
    return parent;
}

From source file:org.geopublishing.atlasViewer.GpCoreUtil.java

/**
 * e1027. Converting a {@link TreeNode} in a {@link JTree} Component to a
 * {@link TreePath} Returns a {@link TreePath} containing the specified
 * node./*from  w ww  . j av a  2 s  . c o m*/
 */
public static final TreePath getPath(TreeNode node) {
    final List<TreeNode> list = new ArrayList<TreeNode>();

    // Add all nodes to list
    while (node != null) {
        list.add(node);
        node = node.getParent();
    }
    Collections.reverse(list);

    // Convert array of nodes to TreePath
    return new TreePath(list.toArray());
}

From source file:org.geopublishing.atlasViewer.GpCoreUtil.java

/**
 * Expands a tree to the//from   w  w w .j  a  v a  2s .  c om
 * 
 * @param tree
 * @param droppedNode
 * @author <a href="mailto:skpublic@wikisquare.de">Stefan Alfons Tzeggai</a>
 */
public final static void expandToNode(final JTree tree, final MutableTreeNode droppedNode) {

    TreeNode lookAt = droppedNode;
    final List<TreeNode> parents = new ArrayList<TreeNode>();
    parents.add(lookAt);

    while (lookAt.getParent() != null) {
        lookAt = lookAt.getParent();
        parents.add(lookAt);
    }

    Collections.reverse(parents);
    for (final TreeNode node : parents) {
        final TreePath path = getPath(node);
        tree.expandPath(path);
    }
}