public static int getHeight(TreeNode node) { if (node.isLeaf()) { return 1; } int heightl = 0; int heightr = 0; if (node.getChildCount() > 0) { heightl = getHeight(node.getChildAt(0)); if (heightl == -1) { return -1; } } if (node.getChildCount() > 1) { heightr = getHeight(node.getChildAt(1)); if (heightr == -1) { return -1; } } if (Math.abs(heightl - heightr) > 1) { return -1; } else { return Math.max(heightl + 1, heightr + 1); } }
public void expandRootChildren() { TreeNode root = (TreeNode) myTreeModel.getRoot(); if (root.getChildCount() == 1) { myTree.expandPath(new TreePath(new Object[] {root, root.getChildAt(0)})); } }
public static ArrayList<TreeNode> childrenToArray(final TreeNode node) { final ArrayList<TreeNode> result = new ArrayList<TreeNode>(); for (int i = 0; i < node.getChildCount(); i++) { result.add(node.getChildAt(i)); } return result; }
public static boolean traverseDepth(final TreeNode node, final Traverse traverse) { if (!traverse.accept(node)) return false; final int childCount = node.getChildCount(); for (int i = 0; i < childCount; i++) if (!traverseDepth(node.getChildAt(i), traverse)) return false; return true; }
/** * Recursive traverse of tree to determine selections A leaf is selected if rsm is selected. * Nonleaf nodes are selected if all children are selected. * * @param tn node in the tree for which to determine selection * @param nodeidx the ordinal postion in the segments array * @param gsm the graph segments selection model * @param rsm the table row selection model * @return true if given node tn is selected, else false */ private boolean selTraverse( TreeNode tn, int[] nodeidx, ListSelectionModel gsm, ListSelectionModel rsm) { boolean selected = true; if (!tn.isLeaf()) { // A nonleaf node is selected if all its children are selected. for (int i = 0; i < tn.getChildCount(); i++) { TreeNode cn = tn.getChildAt(i); selected &= selTraverse(cn, nodeidx, gsm, rsm); } } else { if (tn instanceof RowCluster) { // get the row index of the leaf node int ri = ((RowCluster) tn).getIndex(); // A leaf is selected if its row is selected in the row selection rsm. selected = rsm.isSelectedIndex(ri); } } // Get the offset into the segments array int idx = nodeidx[0] * segOffset; if (selected) { gsm.addSelectionInterval(idx, idx + (segOffset - 1)); } else { gsm.removeSelectionInterval(idx, idx + (segOffset - 1)); } // Increment the nodeidx in the tree nodeidx[0]++; return selected; }
protected TreeNode findChild(TreeNode parent, Integer simpleKey) { int childIdx = simpleKey.intValue(); if (childIdx < parent.getChildCount()) { return parent.getChildAt(childIdx); } return null; }
private static void resetNode(TreeNode node, CodeStyleSettings settings) { if (node instanceof MyTreeNode) { ((MyTreeNode) node).reset(settings); } for (int j = 0; j < node.getChildCount(); j++) { TreeNode child = node.getChildAt(j); resetNode(child, settings); } }
private static void applyNode(TreeNode node, final CodeStyleSettings settings) { if (node instanceof MyTreeNode) { ((MyTreeNode) node).apply(settings); } for (int j = 0; j < node.getChildCount(); j++) { TreeNode child = node.getChildAt(j); applyNode(child, settings); } }
/** * Return a count of this node and all its descendents. * * @param node A node in the Treemodel. * @return The number of nodes in this branch of the Treemodel. */ private static int getNodeCount(TreeNode tn) { int nc = 1; // this node if (!tn.isLeaf()) { for (int i = 0; i < tn.getChildCount(); i++) { TreeNode cn = tn.getChildAt(i); nc += getNodeCount(cn); } } return nc; }
@Override public void mouseClicked(MouseEvent e) { int clickedIndex = table.getSelectedRow(); // 获取鼠标点击的行数 if (clickedIndex >= 0) { EditSelectAll.setEnabled(true); SelectAll.setEnabled(true); isTable = true; currentfile = tablemodel.getFile(clickedIndex); addressText.setText(currentfile.getAbsolutePath()); // 地址栏显示路径 if (!currentnode.getFile().getName().equals(fsv.getHomeDirectory())) { FileDelete.setEnabled(true); Delete.setEnabled(true); } } if (e.getClickCount() >= 2) { EditSelectAll.setEnabled(true); SelectAll.setEnabled(true); if (currentfile.isDirectory()) // 当单击的是目录 { try { upbtn.setEnabled(true); if (!tree.isExpanded(currentPath)) tree.expandPath(currentPath); // 将树中对应的节点打开 if (currentnode.getChildCount() > 0) { for (int i = 0; i < currentnode.getChildCount(); i++) { FileTreeNode temp = (FileTreeNode) currentnode.getChildAt(i); // 返回currentNode的子节点数组中指定索引处的子节点。 if (temp.GetFile() .getPath() .equalsIgnoreCase(currentfile.getPath())) // 判断子节点路径是否等于列表中选的的路径 { TreeNode[] nodes = treemodel.getPathToRoot(temp); // 获取从根节点到 temp的路径 currentPath = new TreePath(nodes); tree.setSelectionPath(currentPath); // 选择子节点的父节点 break; } } } } catch (Exception ee) { ee.printStackTrace(); } } if (currentfile.isFile()) { Runtime ce = Runtime.getRuntime(); String Temp = new String(currentfile.getAbsolutePath()); String cmdarray = "cmd /c start " + Temp; try { ce.exec(cmdarray); } catch (IOException e1) { e1.printStackTrace(); } } } else if (e.getButton() == e.BUTTON3) { popup.show(e.getComponent(), e.getX(), e.getY()); } }
int getTotalUnseen(TreeNode n) { int unseen = 0; for (int i = 0; i < n.getChildCount(); i++) { TreeNode child = n.getChildAt(i); if (child instanceof PlaylistTreeNode) unseen += ((PlaylistTreeNode) child).numUnseenTracks; else if (child instanceof LibraryTreeNode) unseen += ((LibraryTreeNode) child).numUnseenTracks; else unseen += getTotalUnseen(child); } return unseen; }
private static boolean isModified(TreeNode node, final CodeStyleSettings settings) { if (node instanceof MyTreeNode) { if (((MyTreeNode) node).isModified(settings)) return true; } for (int j = 0; j < node.getChildCount(); j++) { TreeNode child = node.getChildAt(j); if (isModified(child, settings)) { return true; } } return false; }
/** * Return the number of leaf nodes that descend from the given node. * * @param node A node in the Treemodel. * @return The number of leaf nodes that descend from the given node. */ private static int getLeafCount(TreeNode tn) { int lc = 0; if (!tn.isLeaf()) { for (int i = 0; i < tn.getChildCount(); i++) { TreeNode cn = tn.getChildAt(i); lc += getLeafCount(cn); } } else { lc = 1; } return lc; }
/** * Traververse the tree selecting rows coresponding to leaf nodes of the given node tn * * @param tn the node from which to start traversing * @param rsm the selection model in which to mark selected rows */ private void selectTraverse(TreeNode tn, ListSelectionModel rsm) { if (!tn.isLeaf()) { for (int i = 0; i < tn.getChildCount(); i++) { TreeNode cn = tn.getChildAt(i); selectTraverse(cn, rsm); } } else { if (tn instanceof RowCluster) { int ri = ((RowCluster) tn).getIndex(); rsm.addSelectionInterval(ri, ri); } } }
/** Collapses the tree of algorithms */ public void collapseAll() { final TreeNode root = (TreeNode) jTree.getModel().getRoot(); final TreePath path = new TreePath(root); expandAll(jTree, path, false); jTree.expandPath(path); final int iChildCount = root.getChildCount(); for (int i = 0; i < iChildCount; i++) { final TreeNode node = root.getChildAt(i); final TreePath subpath = path.pathByAddingChild(node); jTree.expandPath(subpath); } }
@Nullable private DefaultMutableTreeNode findNodeOnToolbar(String actionId) { final TreeNode toolbar = ((DefaultMutableTreeNode) myActionsTree.getModel().getRoot()).getChildAt(1); for (int i = 0; i < toolbar.getChildCount(); i++) { final DefaultMutableTreeNode child = (DefaultMutableTreeNode) toolbar.getChildAt(i); final String childId = getActionId(child); if (childId != null && childId.equals(actionId)) { return child; } } return null; }
boolean anyComments(TreeNode n) { for (int i = 0; i < n.getChildCount(); i++) { TreeNode child = n.getChildAt(i); if (child instanceof PlaylistTreeNode) { if (((PlaylistTreeNode) child).hasComments) return true; } else if (child instanceof LibraryTreeNode) { if (((LibraryTreeNode) child).hasComments) return true; } else { if (anyComments(child)) return true; } } return false; }
/** * Expand the given tree to the given level, starting from the given node and path. * * @param tree The tree to be expanded * @param node The node to start from * @param path The path to start from * @param level The number of levels to expand to */ private static void expandNode( final JTree tree, final TreeNode node, final TreePath path, final int level) { if (level <= 0) { return; } tree.expandPath(path); for (int i = 0; i < node.getChildCount(); ++i) { final TreeNode childNode = node.getChildAt(i); expandNode(tree, childNode, path.pathByAddingChild(childNode), level - 1); } }
// returns the project based on the project header id public Element getProject(String id) { Element project = null; TreeNode root = (TreeNode) treeModel.getRoot(); for (int i = 0; i < root.getChildCount(); i++) { ItTreeNode child = (ItTreeNode) root.getChildAt(i); Element projectHeader = child.getReference(); if (id.equals(projectHeader.getAttributeValue("id"))) { project = child.getData(); break; } } return project; }
/** * Returns the position of the specified LayerNode or -1 if it is not contained in the ContentTree.<br> * In the following example this method will return 4 for the 'X'-LayerNode. * * <pre> * -O * |-O * |-O * |-O * -O * |-O * |-X * |-O * * @param layerNode * @return * */ public int positionOf(LayerNode layerNode) { int position = 0; for (int i = 0; i < root.getChildCount(); i++) { TreeNode storageNode = root.getChildAt(i); for (int j = 0; j < storageNode.getChildCount(); j++) { if (storageNode.getChildAt(j) != layerNode) { position++; } else { return position; } } } return -1; }
private static void hideActions( @NotNull CustomActionsSchema schema, @NotNull DefaultMutableTreeNode root, @NotNull final TreeNode actionGroup, Set<String> items) { for (int i = 0; i < actionGroup.getChildCount(); i++) { final DefaultMutableTreeNode child = (DefaultMutableTreeNode) actionGroup.getChildAt(i); final int childCount = child.getChildCount(); final String childId = getItemId(child); if (childId != null && items.contains(childId)) { final TreePath treePath = TreeUtil.getPath(root, child); final ActionUrl url = CustomizationUtil.getActionUrl(treePath, ActionUrl.DELETED); schema.addAction(url); } else if (childCount > 0) { hideActions(schema, child, child, items); } } }
public void dragOver(DropTargetDragEvent dtde) { /* * The idea here is to change the selection path as the user drags an * item over - it works but it does not seem to expand... */ final TreePath path = getPathForLocation(dtde.getLocation().x, dtde.getLocation().y); if (path != null) { final TreeNode leafNode = (TreeNode) path.getLastPathComponent(); if (leafNode.getChildCount() > 0) { final TreePath newPath = path.pathByAddingChild(leafNode.getChildAt(0)); expandPath(newPath); setSelectionPath(path); repaint(); } } }
@Nullable private static TreePath getFirstErrorPath(TreePath treePath) { TreeNode treeNode = (TreeNode) treePath.getLastPathComponent(); if (treeNode instanceof MessageNode) { AntBuildMessageView.MessageType type = ((MessageNode) treeNode).getType(); if (type == AntBuildMessageView.MessageType.ERROR) { return treePath; } } if (treeNode.getChildCount() == 0) { return null; } for (int i = 0; i < treeNode.getChildCount(); i++) { TreeNode childNode = treeNode.getChildAt(i); TreePath childPath = treePath.pathByAddingChild(childNode); TreePath usagePath = getFirstErrorPath(childPath); if (usagePath != null) { return usagePath; } } return null; }
/** * Traverse the tree creating dendogram graph line segments, a nodemap, and a leafmap. Traversal * is depth first. return child position currentleafcnt * * @param tn the tree node to traverse * @param leafcnt the leafcount prior to this node * @param parentDistance the distance of the parent node * @param nodeidx the current node index, incremented after each node is traversed * @param nodemap an ordered list of nodes traversed * @param leafmap an order list of row indices to the leafnode traversed * @param segs line segments comprising the dendogram * @param childpos position of child node returned to parent * @return the leafcount after traversing this node */ private int traverse( TreeNode tn, int leafcnt, double parentDistance, int[] nodeidx, TreeNode[] nodemap, int[] leafmap, double[] segs, double childpos[]) { int lc = leafcnt; double distance = 0.; double height = 0.; double minChildx = Double.NaN; double maxChildx = Double.NaN; double minChildy = Double.NaN; double maxChildy = Double.NaN; if (tn instanceof Cluster) { distance = ((Cluster) tn).getSimilarity(); } else { distance = ((DefaultMutableTreeNode) tn).getDepth(); } if (!tn.isLeaf()) { for (int i = 0; i < tn.getChildCount(); i++) { TreeNode cn = tn.getChildAt(i); lc = traverse(cn, lc, distance, nodeidx, nodemap, leafmap, segs, childpos); if (Double.isNaN(minChildx) || childpos[0] < minChildx) { minChildx = childpos[0]; } if (Double.isNaN(maxChildx) || childpos[0] > maxChildx) { maxChildx = childpos[0]; } if (Double.isNaN(minChildy) || childpos[1] < minChildy) { minChildy = childpos[1]; } if (Double.isNaN(maxChildy) || childpos[1] > maxChildy) { maxChildy = childpos[1]; } } } else { if (tn instanceof RowCluster) { leafmap[lc] = ((RowCluster) tn).getIndex(); } minChildx = distance; maxChildx = distance; minChildy = lc; maxChildy = lc; lc++; } // offset into segs int offset = nodeidx[0] * segOffset * 4; nodemap[nodeidx[0]] = tn; nodeidx[0]++; if (segs.length < offset + 8) { double tmp[] = segs; segs = new double[offset + 8]; System.arraycopy(tmp, 0, segs, 0, tmp.length); } // segment from minchild to maxchild segs[offset++] = distance; // X segs[offset++] = minChildy; // Y segs[offset++] = distance; // X segs[offset++] = maxChildy; // Y // segment from node to parent of length distance // postision half way between first and last child double y = minChildy + (maxChildy - minChildy) / 2.; segs[offset++] = distance; // X segs[offset++] = y; // Y segs[offset++] = parentDistance; // X segs[offset++] = y; // Y /* System.err.println(tn.toString() + " \tmlc " + leafcnt + " lc " + lc + " pd " + parentDistance + " d " + distance); for (int i = 8, j = leafcnt * 8; i < 8; i++,j++) { System.err.print("\t" + segs[j]); } System.err.println(""); */ // Update return values childpos[0] = distance; childpos[1] = y; return lc; }