/** * Compares this {@code TreePath} to the specified object. This returns {@code true} if {@code o} * is a {@code TreePath} with the exact same elements (as determined by using {@code equals} on * each element of the path). * * @param o the object to compare */ public boolean equals(Object o) { if (o == this) return true; if (o instanceof TreePath) { TreePath oTreePath = (TreePath) o; if (getPathCount() != oTreePath.getPathCount()) return false; for (TreePath path = this; path != null; path = path.getParentPath()) { if (!(path.getLastPathComponent().equals(oTreePath.getLastPathComponent()))) { return false; } oTreePath = oTreePath.getParentPath(); } return true; } return false; }
/** * Returns the number of elements in the path. * * @return the number of elements in the path */ public int getPathCount() { int result = 0; for (TreePath path = this; path != null; path = path.getParentPath()) { result++; } return result; }
/** * Returns an ordered array of the elements of this {@code TreePath}. The first element is the * root. * * @return an array of the elements in this {@code TreePath} */ public Object[] getPath() { int i = getPathCount(); Object[] result = new Object[i--]; for (TreePath path = this; path != null; path = path.getParentPath()) { result[i--] = path.getLastPathComponent(); } return result; }
/** * Returns the path element at the specified index. * * @param index the index of the element requested * @return the element at the specified index * @throws IllegalArgumentException if the index is outside the range of this path */ public Object getPathComponent(int index) { int pathLength = getPathCount(); if (index < 0 || index >= pathLength) throw new IllegalArgumentException("Index " + index + " is out of the specified range"); TreePath path = this; for (int i = pathLength - 1; i != index; i--) { path = path.getParentPath(); } return path.getLastPathComponent(); }
/** * Returns true if <code>aTreePath</code> is a descendant of this {@code TreePath}. A {@code * TreePath} {@code P1} is a descendant of a {@code TreePath} {@code P2} if {@code P1} contains * all of the elements that make up {@code P2's} path. For example, if this object has the path * {@code [a, b]}, and <code>aTreePath</code> has the path {@code [a, b, c]}, then <code>aTreePath * </code> is a descendant of this object. However, if <code>aTreePath</code> has the path {@code * [a]}, then it is not a descendant of this object. By this definition a {@code TreePath} is * always considered a descendant of itself. That is, <code>aTreePath.isDescendant(aTreePath) * </code> returns {@code true}. * * @param aTreePath the {@code TreePath} to check * @return true if <code>aTreePath</code> is a descendant of this path */ public boolean isDescendant(TreePath aTreePath) { if (aTreePath == this) return true; if (aTreePath != null) { int pathLength = getPathCount(); int oPathLength = aTreePath.getPathCount(); if (oPathLength < pathLength) // Can't be a descendant, has fewer components in the path. return false; while (oPathLength-- > pathLength) aTreePath = aTreePath.getParentPath(); return equals(aTreePath); } return false; }
private void myDoubleClick(int row, TreePath path) { if (path != null) { JFrame infoFrame = new JFrame("Object Info for: " + path); JLabel objectLabel; JLabel parentPathLabel; // Display Selected Object Name; objectLabel = new JLabel("Object: " + path.getLastPathComponent().toString()); objectLabel.setHorizontalAlignment(SwingConstants.CENTER); // Display specific object information parentPathLabel = new JLabel("Parent Path: " + path.getParentPath()); parentPathLabel.setHorizontalAlignment(SwingConstants.CENTER); // Construct infoFrame infoFrame.getContentPane().add(objectLabel, BorderLayout.CENTER); infoFrame.getContentPane().add(parentPathLabel, BorderLayout.SOUTH); infoFrame.pack(); infoFrame.setVisible(true); } }