Java Code Examples for javax.swing.tree.TreeNode#getParent()

The following examples show how to use javax.swing.tree.TreeNode#getParent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElementTreePanel.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Invoke this method after you've changed how node is to be
 * represented in the tree.
 */
@Override
public void nodeChanged(TreeNode node) {
    if (listenerList != null && node != null) {
        TreeNode parent = node.getParent();

        if (parent == null && node != root) {
            parent = root;
        }
        if (parent != null) {
            int anIndex = getIndexOfChild(parent, node);

            if (anIndex != -1) {
                int[] cIndexs = new int[1];

                cIndexs[0] = anIndex;
                nodesChanged(parent, cIndexs);
            }
        }
    }
}
 
Example 2
Source File: ElementTreePanel.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Invoke this method after you've changed how node is to be
 * represented in the tree.
 */
@Override
public void nodeChanged(TreeNode node) {
    if (listenerList != null && node != null) {
        TreeNode parent = node.getParent();

        if (parent == null && node != root) {
            parent = root;
        }
        if (parent != null) {
            int anIndex = getIndexOfChild(parent, node);

            if (anIndex != -1) {
                int[] cIndexs = new int[1];

                cIndexs[0] = anIndex;
                nodesChanged(parent, cIndexs);
            }
        }
    }
}
 
Example 3
Source File: TreeSearch.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
/**
     * Returns the previous sibling of this node in the parent's children array.
     * Returns null if this node has no parent or is the parent's first child.
     * This method performs a linear search that is O(n) where n is the number
     * of children.
     *
     * @param node
     * @return the sibling of this node that immediately precedes this node
     */
    public TreeNode getPreviousSibling(TreeNode node) {
        TreeNode retval;

        TreeNode myParent = (TreeNode) node.getParent();

        if (myParent == null) {
            retval = null;
        } else {
            retval = getChildBefore(myParent, node);     // linear search
        }

        if (retval != null && !isNodeSibling(node, retval)) {
//            throw new Error("child of parent is not a sibling");
        }

        return retval;
    }
 
Example 4
Source File: ElementTreePanel.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Invoke this method after you've changed how node is to be
 * represented in the tree.
 */
@Override
public void nodeChanged(TreeNode node) {
    if (listenerList != null && node != null) {
        TreeNode parent = node.getParent();

        if (parent == null && node != root) {
            parent = root;
        }
        if (parent != null) {
            int anIndex = getIndexOfChild(parent, node);

            if (anIndex != -1) {
                int[] cIndexs = new int[1];

                cIndexs[0] = anIndex;
                nodesChanged(parent, cIndexs);
            }
        }
    }
}
 
Example 5
Source File: ElementTreePanel.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Invoke this method after you've changed how node is to be
 * represented in the tree.
 */
@Override
public void nodeChanged(TreeNode node) {
    if (listenerList != null && node != null) {
        TreeNode parent = node.getParent();

        if (parent == null && node != root) {
            parent = root;
        }
        if (parent != null) {
            int anIndex = getIndexOfChild(parent, node);

            if (anIndex != -1) {
                int[] cIndexs = new int[1];

                cIndexs[0] = anIndex;
                nodesChanged(parent, cIndexs);
            }
        }
    }
}
 
Example 6
Source File: TreeSearch.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the next sibling of this node in the parent's children array.
 * Returns null if this node has no parent or is the parent's last child.
 * This method performs a linear search that is O(n) where n is the number
 * of children; to traverse the entire array, use the parent's child
 * enumeration instead.
 *
 * @param node
 * @see #children
 * @return the sibling of this node that immediately follows this node
 */
public TreeNode getNextSibling(TreeNode node) {
    TreeNode retval;

    TreeNode myParent = node.getParent();

    if (myParent == null) {
        retval = null;
    } else {
        retval = getChildAfter(myParent, node);      // linear search
    }

    if (retval != null && !isNodeSibling(node, retval)) {
        //            throw new Error("child of parent is not a sibling");
    }

    return retval;
}
 
Example 7
Source File: ElementTreePanel.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the path to a particluar node. This is recursive.
 */
@Override
protected TreeNode[] getPathToRoot(TreeNode aNode, int depth) {
    TreeNode[] retNodes;

    /* Check for null, in case someone passed in a null node, or
    they passed in an element that isn't rooted at root. */
    if (aNode == null) {
        if (depth == 0) {
            return null;
        } else {
            retNodes = new TreeNode[depth];
        }
    } else {
        depth++;
        if (aNode == root) {
            retNodes = new TreeNode[depth];
        } else {
            TreeNode parent = aNode.getParent();

            if (parent == null) {
                parent = root;
            }
            retNodes = getPathToRoot(parent, depth);
        }
        retNodes[retNodes.length - depth] = aNode;
    }
    return retNodes;
}
 
Example 8
Source File: FilesystemTreeNode.java    From settlers-remake with MIT License 5 votes vote down vote up
/**
 * @return Root node
 */
private RootTreeNode findRoot() {
	TreeNode node = getParent();
	while (true) {
		if (node == null) {
			return null;
		}
		if (node instanceof RootTreeNode) {
			return (RootTreeNode) node;
		}
		node = node.getParent();
	}
}
 
Example 9
Source File: ElementTreePanel.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the path to a particluar node. This is recursive.
 */
@Override
protected TreeNode[] getPathToRoot(TreeNode aNode, int depth) {
    TreeNode[] retNodes;

    /* Check for null, in case someone passed in a null node, or
    they passed in an element that isn't rooted at root. */
    if (aNode == null) {
        if (depth == 0) {
            return null;
        } else {
            retNodes = new TreeNode[depth];
        }
    } else {
        depth++;
        if (aNode == root) {
            retNodes = new TreeNode[depth];
        } else {
            TreeNode parent = aNode.getParent();

            if (parent == null) {
                parent = root;
            }
            retNodes = getPathToRoot(parent, depth);
        }
        retNodes[retNodes.length - depth] = aNode;
    }
    return retNodes;
}
 
Example 10
Source File: ElementTreePanel.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the path to a particluar node. This is recursive.
 */
@Override
protected TreeNode[] getPathToRoot(TreeNode aNode, int depth) {
    TreeNode[] retNodes;

    /* Check for null, in case someone passed in a null node, or
    they passed in an element that isn't rooted at root. */
    if (aNode == null) {
        if (depth == 0) {
            return null;
        } else {
            retNodes = new TreeNode[depth];
        }
    } else {
        depth++;
        if (aNode == root) {
            retNodes = new TreeNode[depth];
        } else {
            TreeNode parent = aNode.getParent();

            if (parent == null) {
                parent = root;
            }
            retNodes = getPathToRoot(parent, depth);
        }
        retNodes[retNodes.length - depth] = aNode;
    }
    return retNodes;
}
 
Example 11
Source File: OutlineView.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to expand nodes selected in the explorer manager.
 */
private void expandSelection() {
    Node[] arr = manager.getSelectedNodes ();
    for (int i = 0; i < arr.length; i++) {
        if ( (arr[i].getParentNode() == null) && (! outline.isRootVisible())) {
            // don't try to show root if it is invisible
            continue;
        }
        TreeNode tn = Visualizer.findVisualizer(arr[i]);
        if (tn != null) {
            ArrayList<TreeNode> al = new ArrayList<TreeNode> ();
            while (tn != null) {
                al.add(tn);
                tn = tn.getParent();
            }
            Collections.reverse(al);
            TreePath tp = new TreePath(al.toArray());
            Deque<TreePath> pathsStack = new ArrayDeque<TreePath>(al.size());
            while ((tp != null) && (tp.getPathCount() > 0)) {
                tp = tp.getParentPath();
                if (tp != null) {
                    pathsStack.addFirst(tp);
                }
            }
            for (TreePath etp : pathsStack) {
                outline.expandPath(etp);
            }
        }
    }
}
 
Example 12
Source File: NodeRenderDataProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public javax.swing.Icon getIcon(Object o) {
    if (!showIcons) {
        return emptyIcon;
    }
    Node n = Visualizer.findNode(o);
    if (n == null) {
        throw new IllegalStateException("TreeNode must be VisualizerNode but was: " + o + " of class " + o.getClass().getName());
    }
    boolean expanded = false;
    if (o instanceof TreeNode) {
        TreeNode tn = (TreeNode)o;
        ArrayList<TreeNode> al = new ArrayList<TreeNode> ();
        while (tn != null) {
            al.add(tn);
            tn = tn.getParent();
        }
        Collections.reverse(al);
        TreePath tp = new TreePath(al.toArray());
        AbstractLayoutCache layout = table.getLayoutCache();
        expanded = layout.isExpanded(tp);
    }
    java.awt.Image image = null;
    if (expanded) {
        image = n.getOpenedIcon(java.beans.BeanInfo.ICON_COLOR_16x16);
    } else {
        image = n.getIcon(java.beans.BeanInfo.ICON_COLOR_16x16);
    }
    return ImageUtilities.image2Icon(image);
}
 
Example 13
Source File: ElementTreePanel.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the path to a particluar node. This is recursive.
 */
@Override
protected TreeNode[] getPathToRoot(TreeNode aNode, int depth) {
    TreeNode[] retNodes;

    /* Check for null, in case someone passed in a null node, or
    they passed in an element that isn't rooted at root. */
    if (aNode == null) {
        if (depth == 0) {
            return null;
        } else {
            retNodes = new TreeNode[depth];
        }
    } else {
        depth++;
        if (aNode == root) {
            retNodes = new TreeNode[depth];
        } else {
            TreeNode parent = aNode.getParent();

            if (parent == null) {
                parent = root;
            }
            retNodes = getPathToRoot(parent, depth);
        }
        retNodes[retNodes.length - depth] = aNode;
    }
    return retNodes;
}
 
Example 14
Source File: IndexTreePathState.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static int[] pathToChildIndecies(TreePath path) {
  int[] result = new int[path.getPathCount()];
  for (int i = 0; i < path.getPathCount(); i++) {
    TreeNode node = (TreeNode) path.getPathComponent(i);
    TreeNode parent = node.getParent();
    result[i] = parent != null ? parent.getIndex(node) : 0;
  }
  return result;
}
 
Example 15
Source File: TaskContainmentHierarchyFacadeImpl.java    From ganttproject with GNU General Public License v3.0 5 votes vote down vote up
@Override
public List<Integer> getOutlinePath(Task task) {
  int depth = getDepth(task);
  List<Integer> result = Lists.newArrayListWithExpectedSize(depth);
  TreeNode node = myTask2treeNode.get(task);
  for (int i = 0; i < depth; i++) {
    TreeNode containerNode = node.getParent();
    result.add(i, containerNode.getIndex(node) + 1);
    node = containerNode;
  }
  return Lists.reverse(result);
}
 
Example 16
Source File: ElementTreePanel.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the path to a particluar node. This is recursive.
 */
@Override
protected TreeNode[] getPathToRoot(TreeNode aNode, int depth) {
    TreeNode[] retNodes;

    /* Check for null, in case someone passed in a null node, or
    they passed in an element that isn't rooted at root. */
    if (aNode == null) {
        if (depth == 0) {
            return null;
        } else {
            retNodes = new TreeNode[depth];
        }
    } else {
        depth++;
        if (aNode == root) {
            retNodes = new TreeNode[depth];
        } else {
            TreeNode parent = aNode.getParent();

            if (parent == null) {
                parent = root;
            }
            retNodes = getPathToRoot(parent, depth);
        }
        retNodes[retNodes.length - depth] = aNode;
    }
    return retNodes;
}
 
Example 17
Source File: NodeFileOrFolder.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
@Nonnull
public TreePath makeTreePath() {
  final List<Object> path = new ArrayList<>();
  TreeNode node = this;
  while (node != null) {
    path.add(0, node);
    node = node.getParent();
  }
  return new TreePath(path.toArray());
}
 
Example 18
Source File: ElementTreePanel.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the path to a particluar node. This is recursive.
 */
@Override
protected TreeNode[] getPathToRoot(TreeNode aNode, int depth) {
    TreeNode[] retNodes;

    /* Check for null, in case someone passed in a null node, or
    they passed in an element that isn't rooted at root. */
    if (aNode == null) {
        if (depth == 0) {
            return null;
        } else {
            retNodes = new TreeNode[depth];
        }
    } else {
        depth++;
        if (aNode == root) {
            retNodes = new TreeNode[depth];
        } else {
            TreeNode parent = aNode.getParent();

            if (parent == null) {
                parent = root;
            }
            retNodes = getPathToRoot(parent, depth);
        }
        retNodes[retNodes.length - depth] = aNode;
    }
    return retNodes;
}
 
Example 19
Source File: UpdateInfoTree.java    From consulo with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
private GroupTreeNode findParentGroupTreeNode(@Nonnull TreeNode treeNode) {
  TreeNode currentNode = treeNode;
  while (currentNode != null && !(currentNode instanceof GroupTreeNode)) {
    currentNode = currentNode.getParent();
  }
  return (GroupTreeNode)currentNode;
}
 
Example 20
Source File: TodoPanel.java    From consulo with Apache License 2.0 4 votes vote down vote up
private boolean isFirst(final TreeNode node) {
  final TreeNode parent = node.getParent();
  return parent == null || parent.getIndex(node) == 0 && isFirst(parent);
}