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); } }
@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()); } }
private TreePath find2(JTree tree, TreePath parent, Village pNode, int depth) { TreeNode node = (TreeNode) parent.getLastPathComponent(); DefaultMutableTreeNode o = (DefaultMutableTreeNode) node; // If equal, go down the branch if (o.getUserObject().equals(pNode)) { // If at end, return match return parent; } else { // Traverse children if (node.getChildCount() >= 0) { for (Enumeration e = node.children(); e.hasMoreElements(); ) { TreeNode n = (TreeNode) e.nextElement(); TreePath path = parent.pathByAddingChild(n); TreePath result = find2(tree, path, pNode, depth + 1); // Found a match if (result != null) { return result; } } } } // No match at this branch return null; }
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; }
/** * 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; }
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; }
@Override public void treeNodesRemoved(TreeModelEvent e) { final TreeNode root = (TreeNode) application.getSubjectHierarchy().getRoot(); if (root.getChildCount() == 0) { isExpanded = false; } }
public void expandRootChildren() { TreeNode root = (TreeNode) myTreeModel.getRoot(); if (root.getChildCount() == 1) { myTree.expandPath(new TreePath(new Object[] {root, root.getChildAt(0)})); } }
private TreePath find2(TreePath parent, Object[] nodes, int depth, boolean byName) { TreeNode node = (TreeNode) parent.getLastPathComponent(); if (depth > nodes.length - 1) { return parent; } if (node.getChildCount() >= 0) { for (Enumeration<TreeNode> e = node.children(); e.hasMoreElements(); ) { TreeNode n = e.nextElement(); TreePath path = parent.pathByAddingChild(n); boolean find; if (byName) { find = n.toString().toUpperCase().contains(nodes[depth].toString().toUpperCase()); } else { find = n.equals(nodes[depth]); } if (find) { TreePath result = find2(path, nodes, depth + 1, byName); if (result != null) { return result; } } else { TreePath result = find2(path, nodes, depth, byName); if (result != null) { return result; } } } } return null; }
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; }
private void expandTree(JTree tree, TreePath parent) { TreeNode node = (TreeNode) parent.getLastPathComponent(); if (node.getChildCount() >= 0) { for (Enumeration<?> e = node.children(); e.hasMoreElements(); ) { TreeNode n = (TreeNode) e.nextElement(); TreePath path = parent.pathByAddingChild(n); expandTree(tree, path); } } tree.expandPath(parent); }
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; }
/** * 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; }
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; }
/** * 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; }
@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; }
/** * * 完全展开或关闭一个树,用于递规执行 * * @param tree JTree * @param parent 父节点 * @param expand 为true则表示展开树,否则为关闭整棵树 */ private void expandAll(JTree tree, TreePath parent, boolean expand) { TreeNode node = (TreeNode) parent.getLastPathComponent(); if (node.getChildCount() >= 0) { for (Enumeration e = node.children(); e.hasMoreElements(); ) { TreeNode n = (TreeNode) e.nextElement(); TreePath path = parent.pathByAddingChild(n); expandAll(tree, path, expand); } } if (expand) { tree.expandPath(parent); } else { tree.collapsePath(parent); } }
/** * 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; }
/** * Traverses all the children of the last component of the given tree path and calls this method * recursively on them and also expands or collapse the tree path. * * @param tree The working JTree * @param expand true means expand the tree path, false collapse it. * @param parent */ private static void expandAll(JTree tree, boolean expand, TreePath parent) { // Traverse children TreeNode node = (TreeNode) parent.getLastPathComponent(); if (node.getChildCount() >= 0) { for (Enumeration<?> e = node.children(); e.hasMoreElements(); ) { TreeNode n = (TreeNode) e.nextElement(); TreePath path = parent.pathByAddingChild(n); expandAll(tree, expand, path); } } // Expansion or collapse must be done bottom-up if (expand) { tree.expandPath(parent); } else { tree.collapsePath(parent); } }
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(); } } }
public void getPaths(JTree tree, TreePath parent, boolean expanded, List list) { // Return if node is not expanded if (expanded && !tree.isVisible(parent)) { return; } // Add node to list list.add(parent); // Create paths for all children TreeNode node = (TreeNode) parent.getLastPathComponent(); if (node.getChildCount() >= 0) { for (Enumeration e = node.children(); e.hasMoreElements(); ) { TreeNode n = (TreeNode) e.nextElement(); TreePath path = parent.pathByAddingChild(n); getPaths(tree, path, expanded, list); } } }