@Override public void treeNodesRemoved(TreeModelEvent e) { final TreeNode root = (TreeNode) application.getSubjectHierarchy().getRoot(); if (root.getChildCount() == 0) { isExpanded = false; } }
private void removeNodeFromParent(DefaultMutableTreeNode node) { TreeNode parent = node.getParent(); int idx = parent.getIndex(node); node.removeFromParent(); (getModel()).nodesWereRemoved(parent, new int[] {idx}, new TreeNode[] {node}); }
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 void updateClientState(Node nodeUpdate) { for (int i = 0; i < rootNode.getChildCount(); i++) { TreeNode treeNode = rootNode.getChildAt(i); // System.out.println(treeNode.toString()); if (treeNode.toString().equals(nodeUpdate.getName())) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeNode; if (nodeUpdate.isOnline() && nodeUpdate.isValid()) { // 删除”状态:在线,可信“ 那一行 node.remove(2); node.add(new DefaultMutableTreeNode("状态:在线,可信")); Log.i(this.getClass().getName(), nodeUpdate.getName() + " 已登录,状态可信."); } else if (nodeUpdate.isOnline() && !nodeUpdate.isValid()) { node.remove(2); node.add(new DefaultMutableTreeNode("状态:在线,不可信")); Log.v( this.getClass().getName(), nodeUpdate.getName() + "状态不可信,IP地址为:" + nodeUpdate.getIP()); } else { node.remove(2); node.add(new DefaultMutableTreeNode("状态:离线")); Log.i( this.getClass().getName(), nodeUpdate.getName() + "不在线,IP地址为:" + nodeUpdate.getIP()); } this.jTree.updateUI(); return; } } }
public void load(JarIndex index, boolean recurse) { // get all the child nodes for (EntryReference<BehaviorEntry, BehaviorEntry> reference : index.getBehaviorReferences(m_entry)) { add( new BehaviorReferenceTreeNode( m_deobfuscatingTranslator, reference, index.getAccess(m_entry))); } if (recurse && children != null) { for (Object child : children) { if (child instanceof BehaviorReferenceTreeNode) { BehaviorReferenceTreeNode node = (BehaviorReferenceTreeNode) child; // don't recurse into ancestor Set<Entry> ancestors = Sets.newHashSet(); TreeNode n = (TreeNode) node; while (n.getParent() != null) { n = n.getParent(); if (n instanceof BehaviorReferenceTreeNode) { ancestors.add(((BehaviorReferenceTreeNode) n).getEntry()); } } if (ancestors.contains(node.getEntry())) { continue; } node.load(index, true); } } } }
/** * add tree node function * * @version 1.0 * @author dinglinhui */ private boolean addTreeNode(String name) { for (int i = 0; i < root.getChildCount(); i++) { TreeNode node = root.getChildAt(i); if (name.equals(node.toString())) { return false; } } IconNode nodesChnl = new IconNode(name); IconNode[] nodesType = new IconNode[3]; nodesChnl.setIcon(getImageIcon(RESOURCE_IMAGES_PATH + "ligthbulb_on.png", 16, 16)); nodesType[0] = new IconNode("遥测"); nodesType[0].setIcon(getImageIcon(RESOURCE_IMAGES_PATH + "Orange Ball.png", 16, 16)); nodesChnl.add(nodesType[0]); nodesType[1] = new IconNode("遥信"); nodesType[1].setIcon(getImageIcon(RESOURCE_IMAGES_PATH + "Green Ball.png", 16, 16)); nodesChnl.add(nodesType[1]); nodesType[2] = new IconNode("遥控"); nodesType[2].setIcon(getImageIcon(RESOURCE_IMAGES_PATH + "Yellow Ball.png", 16, 16)); nodesChnl.add(nodesType[2]); root.add(nodesChnl); return true; }
public String toXPath() { /* HOW TO CLIMB UP !!!!!*/ TreeNode parent = getParent(); if (parent == null) return (String.valueOf(myNode.getName())); else return (parent.toString() + "/" + myNode.getName()); }
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; }
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 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; }
private static List<PathElement> createPath(final TreePath treePath) { final ArrayList<PathElement> result = new ArrayList<PathElement>(); for (int i = 0; i < treePath.getPathCount(); i++) { final Object pathComponent = treePath.getPathComponent(i); if (pathComponent instanceof DefaultMutableTreeNode) { final DefaultMutableTreeNode node = (DefaultMutableTreeNode) pathComponent; final TreeNode parent = node.getParent(); final Object userObject = node.getUserObject(); if (userObject instanceof NodeDescriptor) { final NodeDescriptor nodeDescriptor = (NodeDescriptor) userObject; // nodeDescriptor.update(); final int childIndex = parent != null ? parent.getIndex(node) : 0; result.add( new PathElement( getDescriptorKey(nodeDescriptor), getDescriptorType(nodeDescriptor), childIndex, nodeDescriptor)); } else { result.add(new PathElement("", "", 0, userObject)); } } else { return null; } } 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; }
/** *************************************************** */ private void setCopyList(TreeNode copiedNode) { if (index++ == 0) { copyFile = new Vector<TreeNode>(); copyFile.add(copiedNode); copiedList.add(copyFile); pos = copiedNode.getPath().toString().lastIndexOf(File.separator); } File[] copieFiles = copiedNode.getFile().listFiles(); if (copieFiles != null) { for (File file : copieFiles) { if (file.isDirectory()) { copyFile = new Vector<TreeNode>(); TreeNode node_ = new TreeNode(file); copyFile.add(node_); copiedList.add(copyFile); setCopyList(node_); } else { copyFile = new Vector<TreeNode>(); TreeNode node_ = new TreeNode(file); copyFile.add(node_); copiedList.add(copyFile); } } } }
public static boolean isAncestor(final TreeNode ancestor, final TreeNode node) { TreeNode parent = node; while (parent != null) { if (parent == ancestor) return true; parent = parent.getParent(); } return false; }
TreeNode buildBST(int[] a, int start, int end) { if (start > end) return null; int mid = (start + end) / 2; TreeNode n = new TreeNode(a[mid]); n.left = buildBST(a, start, mid - 1); n.right = buildBST(a, mid + 1, end); return n; }
public void invertTreeInternal(TreeNode root) { if (root == null) return; TreeNode temp = root.right; root.right = root.left; root.left = temp; invertTree(root.left); invertTree(root.right); }
private static void expand(JTree tree, TreePath path, int levels) { if (levels == 0) return; tree.expandPath(path); TreeNode node = (TreeNode) path.getLastPathComponent(); Enumeration children = node.children(); while (children.hasMoreElements()) { expand(tree, path.pathByAddingChild(children.nextElement()), levels - 1); } }
protected TreeNode findChild(TreeNode parent, Integer simpleKey) { int childIdx = simpleKey.intValue(); if (childIdx < parent.getChildCount()) { return parent.getChildAt(childIdx); } return null; }
private void collectExisting(MPSTreeNode node, Collection<String> existing) { for (int idx = 0; idx < node.getChildCount(); idx++) { TreeNode child = node.getChildAt(idx); if (child instanceof MPSTreeNode) { existing.add(child.toString()); collectExisting(((MPSTreeNode) child), existing); } } }
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); } }
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); } }
@Override public void adoptElement(SceneElement elem) { if (!(elem instanceof NenyaImageSceneElement || elem instanceof NenyaTileSceneElement || elem instanceof NenyaComponentSceneElement)) { enableEditor(false); return; } DefaultComboBoxModel dcm = (DefaultComboBoxModel) itemList.getModel(); // Important: Work on a copy, not on the original. Otherwise we mess up the undomanager sceneElement = elem.copy(); if ((sceneElement instanceof NenyaImageSceneElement) && !locked) { dcm.removeAllElements(); String[] tmp = ((NenyaImageSceneElement) sceneElement).getPath(); dcm.addElement(tmp[tmp.length - 1]); } if ((sceneElement instanceof NenyaTileSceneElement) && !locked) { dcm.removeAllElements(); dcm.addElement(((NenyaTileSceneElement) sceneElement).getTileName()); } if ((sceneElement instanceof NenyaComponentSceneElement) && !locked) { dcm.removeAllElements(); NenyaComponentItem[] ni = ((NenyaComponentSceneElement) sceneElement).getComponents(); for (NenyaComponentItem element : ni) { dcm.addElement(element); } } try { ClassedItem[] cols = null; if (elem instanceof NenyaTileSceneElement) cols = ((NenyaTileSceneElement) elem).getColorList(); if (elem instanceof NenyaImageSceneElement) cols = ((NenyaImageSceneElement) elem).getColorList(); if (elem instanceof NenyaComponentSceneElement) { NenyaComponentItem nci = (NenyaComponentItem) dcm.getSelectedItem(); cols = nci.getColorList(); } Vector<TreePath> collect = new Vector<TreePath>(); TreeNode root = (TreeNode) colors.getModel().getRoot(); for (ClassedItem col : cols) { String[] tmp = {root.toString(), col.getClassName(), col.getItemName()}; collect.add(TreeUtil.findPath(root, tmp)); } TreePath[] path = collect.toArray(new TreePath[0]); colors.getSelectionModel().setSelectionPaths(path); } catch (Exception e) { // Either the tree is filtered away or the selected item is not colorized. } enableEditor(true); itemList.setEnabled(elem instanceof NenyaComponentSceneElement); }
public void refreshNode() { try { Vector files = treeview.GetAll(currentnode.GetFile()); tablemodel.addAllFiles(files); appendNodes(currentnode, treeview.GetAllDirectories(currentnode.GetFile())); treemodel.nodeStructureChanged(currentnode); } catch (Exception e) { e.printStackTrace(); } }
/** * 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 initializeItems() { moveTo = new JMenu(model.getName("SPOT_POPUP_MOVE_ITEM")); add(moveTo); for (TreeNode n : groups) { JMenuItem item = new JMenuItem(n.toString()); item.addActionListener(this); moveTo.add(item); } }
/** * 刷新节点中文名称 * * @param node */ private void refreshNodeChName(TreeNode node) { TreeNode parNode = node.getParent(); int index = parNode.getIndex(node); Caliber cal = (Caliber) ((DefaultMutableTreeNode) node).getUserObject(); if (index == 0) { cal.setFirstNode(true); } else { cal.setFirstNode(false); } }
public void dragEnter(DropTargetDragEvent dtde) { TreeNode node = getNodeForEvent(dtde); if (node.isLeaf()) { dtde.rejectDrag(); } else { // start by supporting move operations // dtde.acceptDrag(DnDConstants.ACTION_MOVE); dtde.acceptDrag(dtde.getDropAction()); } }
@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; }