/** * 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; }
public void editSelected() { TreePath selected = tree.getSelectionPath(); if (selected != null) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) selected.getLastPathComponent(); Object obj = node.getUserObject(); if (obj instanceof CavityDBObject) { CavityDBObject dbObj = (CavityDBObject) obj; new ObjectEditingFrame(new ObjectEditingPanel(dbObj)); } } }
TreePath getTreePath(TreeNode treeNode) { TreeNode parent = treeNode.getParent(); if (parent == null) { return new TreePath(treeNode); } else { TreePath ptp = getTreePath(parent); ptp = ptp.pathByAddingChild(treeNode); return ptp; } }
/** * 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; }
/** * isDescendant * * @param path TODO * @returns boolean */ public boolean isDescendant(TreePath path) { // Variables Object[] treepath; int index; int index2; // Get Descendant path treepath = path.getPath(); // Locate Start Index index = 0; index2 = 0; while (treepath[index] != this.path[index2]) { index++; } // while // Verify Paths while (treepath[index] == this.path[index2]) { index++; index2++; } // while // Check for descendant if (index2 != this.path.length) { return false; } // if // Is Descendant return true; } // isDescendant()
private String textGen(TreePath p) { MyNode selectedNode = (MyNode) p.getLastPathComponent(); String text = "men allt som"; int n = selectedNode.getPath().length; for (int i = n - 1; i >= 0; i--) { text = text + " är " + selectedNode.getPath()[i].toString(); } return text; }
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); } }
/** * Constructor TreePath * * @param path TODO * @param element TODO */ protected TreePath(TreePath path, Object element) { // Variables Object[] treepath; // Get Tree Path treepath = path.getPath(); // Create Tree Path this.path = new Object[treepath.length + 1]; System.arraycopy(treepath, 0, this.path, 0, treepath.length); this.path[treepath.length] = element; } // TreePath()
/** * 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; }
private void showDetails(TreePath p) { if (p == null) { return; } MyNode selectedNode = (MyNode) p.getLastPathComponent(); String textPath = textGen(p); JOptionPane.showMessageDialog( this, selectedNode.getLevelName() + ": " + selectedNode.getUserObject() + "\n" + selectedNode.getText() + "\n" + textPath); }