📄 treeview2.java
字号:
* @param s the node text to find * @return true if the node is visible, false if it is not * @see #viewable(TreeNode2) */ boolean viewable(String s) { if (s==null) { return false; } for (int i=0; i<viewCount; i++) { TreeNode2 tn = (TreeNode2)v.elementAt(i); if (tn.text != null) { if (s.equals(tn.text)) { return true; } } } return false; } /** * Determines if the given node is in the TreeView2. * @param node the node to check * @return true if the node is in the TreeView2, false if it is not * @see #exists(java.lang.String) */ public boolean exists(TreeNode2 node) { recount(); for (int i=0; i<count; i++) { if (node == e.elementAt(i)) { return true; } } return false; } /** * Determines if the node with the given text is in the TreeView2. * @param s the node text to find * @return true if the node is in the TreeView2, false if it is not * @see #exists(TreeNode2) */ public boolean exists(String s) { recount(); if (s==null) { return false; } for (int i=0; i<count; i++) { TreeNode2 tn = (TreeNode2)e.elementAt(i); if (tn.text != null) { if (s.equals(tn.text)) { return true; } } } return false; } // add new node to level 0 /** * Adds a new node at root level. If there is no root node, the given * node is made the root node. If there is a root node, the given node * is made a sibling of the root node. * @param newNode the new node to add * @see #insert */ public void append(TreeNode2 newNode) { if (rootNode == null) { rootNode = newNode; selectedNode = rootNode; count = 1; triggerRedraw (); } else { addSibling(newNode, rootNode, true); } } void addChild(TreeNode2 newNode, TreeNode2 relativeNode) { if (relativeNode.child == null) { relativeNode.child = newNode; newNode.parent = relativeNode; count++; triggerRedraw (); } else { addSibling(newNode, relativeNode.child, true); } relativeNode.numberOfChildren++; } void addSibling(TreeNode2 newNode, TreeNode2 siblingNode) { addSibling(newNode,siblingNode,true); } void addSibling(TreeNode2 newNode, TreeNode2 siblingNode, boolean asLastSibling) { if (asLastSibling) { //Find last sibling TreeNode2 tempNode = siblingNode; while (tempNode.sibling != null) tempNode = tempNode.sibling; tempNode.sibling = newNode; } else { //Insert the newNode below the siblingNode newNode.sibling = siblingNode.sibling; siblingNode.sibling = newNode; } //Set the parent of the new node to the parent of the sibling newNode.parent = siblingNode.parent; count++; triggerRedraw (); } /** * Removes the node with the given text from the TreeView2. * @param s the node text to find * @return the TreeNode2 removed from this TreeView2 or null if not found * @see #remove(TreeNode2) * @see #removeSelected */ public TreeNode2 remove(String s) { recount(); for (int i=0; i<count; i++) { TreeNode2 tn = (TreeNode2)e.elementAt(i); if (tn.text != null) { if (s.equals(tn.text)) { remove(tn); triggerRedraw (); return tn; } } } return null; } /** * Removes the currently selected node from the TreeView2. * @see #remove(TreeNode2) * @see #remove(java.lang.String) */ public void removeSelected() { if (selectedNode != null) { remove(selectedNode); } } /** * Removes the given node from the TreeView2. * @param node the node to remove * @return the TreeNode2 removed from this TreeView2 or null if not found * @see #remove(java.lang.String) * @see #removeSelected */ public void remove(TreeNode2 node) { if (!exists(node)) { return; } if (node == selectedNode) { int index = v.indexOf(selectedNode); if (index == -1) { //not viewable index = e.indexOf(selectedNode); } if (index > viewCount-1) { index = viewCount-1; } if (index>0) { changeSelection((TreeNode2)v.elementAt(index-1)); } else if (viewCount>1) { changeSelection((TreeNode2)v.elementAt(1)); } } // remove node and its decendents if (node.parent != null) { if (node.parent.child == node) { if (node.sibling != null) { node.parent.child = node.sibling; } else { node.parent.child = null; node.parent.collapse(); } } else { TreeNode2 tn=node.parent.child; while (tn.sibling != node) { tn = tn.sibling; } if (node.sibling != null) { tn.sibling = node.sibling; } else { tn.sibling = null; } } } else { if (node == rootNode) { if (node.sibling == null) { rootNode=null; } else { rootNode=node.sibling; } } else { TreeNode2 tn = rootNode; while (tn.sibling != node) { tn = tn.sibling; } if (node.sibling != null) { tn.sibling = node.sibling; } else { tn.sibling = null; } } } recount(); triggerRedraw (); } // print each node of TreeView2 beginning with node /** * Print out the text of each node in the TreeView2 beginning with * the given node. * The nodes are printed out one per line with no indenting. * @param node the first node to print */ public void printTree(TreeNode2 node) { if (node==null) { return; } System.out.println(node.text); printTree(node.child); printTree(node.sibling); } private Vector e; // e is vector of existing nodes private void recount() { count = 0; e = new Vector(); if (rootNode != null) { rootNode.depth=0; traverse(rootNode); } } private void traverse(TreeNode2 node) { count++; e.addElement(node); if (node.child != null) { node.child.depth = node.depth+1; traverse(node.child); } if (node.sibling != null) { node.sibling.depth = node.depth; traverse(node.sibling); } } private Vector v; // v is vector of viewable nodes private void resetVector() { // Traverses tree to put nodes into vector v // for internal processing. Depths of nodes are set, // and viewCount and viewWidest is set. v = new Vector(count); viewWidest=30; if (count < 1) { viewCount = 0; return; } rootNode.depth=0; vectorize(rootNode,true,v); viewCount = v.size(); } private void vectorize (TreeNode2 node, boolean respectExpanded, Vector nodeVector) { if (node == null) return; nodeVector.addElement(node); if ((!respectExpanded && node.child != null) || node.isExpanded()) { node.child.depth = node.depth + 1; vectorize(node.child,respectExpanded,nodeVector); } if (node.sibling != null) { node.sibling.depth = node.depth; vectorize(node.sibling,respectExpanded,nodeVector); } } private void debugVector() { int vSize = v.size(); for (int i=0; i<count; i++) { TreeNode2 node = (TreeNode2) v.elementAt(i); System.out.println(node.text); } } // ----------------------------------------- // --------- event related methods --------- // ----------------------------------------- //class Adjustment implements AdjustmentListener //{ public synchronized boolean handleEvent (Event event) { if (event.target == verticalScrollBar) { if (sbVPosition != verticalScrollBar.getValue()) { sbVPosition = verticalScrollBar.getValue(); scrolled (); } return true; } else if (event.target == horizontalScrollBar) { if (sbHPosition != horizontalScrollBar.getValue()) { sbHPosition = horizontalScrollBar.getValue(); repaint(); } return true; } else return super.handleEvent (event); } //} //class Mouse extends MouseAdapter JDK1.1 //{ /** * Processes MOUSE_DOWN events. * This is a standard Java AWT method which gets called by the AWT * method handleEvent() in response to receiving a MOUSE_DOWN * event. These events occur when the mouse button is pressed while * inside this component. * * @param event the event * @param x the component-relative horizontal coordinate of the mouse * @param y the component-relative vertical coordinate of the mouse * * @return true if the event was handled * * @see java.awt.Component#mouseUp * @see #handleEvent */ public boolean mouseDown (Event event, int x, int y)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -