📄 treeview2.java
字号:
package symantec.itools.awt;import java.awt.BorderLayout;import java.awt.Color;//import java.awt.SystemColor; JDK1.1import java.awt.Dimension;import java.awt.Event;import java.awt.FontMetrics;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.LayoutManager;import java.awt.Scrollbar;import java.awt.Panel;import java.awt.Rectangle;//import java.awt.ItemSelectable; JDK1.1//import java.awt.AWTEventMulticaster; JDK1.1//import java.awt.event.ItemEvent;//import java.awt.event.ItemListener;//import java.awt.event.KeyEvent;//import java.awt.event.KeyAdapter;//import java.awt.event.MouseEvent;//import java.awt.event.MouseAdapter;//import java.awt.event.FocusEvent;//import java.awt.event.FocusAdapter;//import java.awt.event.AdjustmentEvent;//import java.awt.event.AdjustmentListener;//import java.awt.event.ActionEvent;//import java.awt.event.ActionListener;import java.util.Vector;//import java.beans.PropertyVetoException;//import java.beans.PropertyChangeListener;//import java.beans.VetoableChangeListener;//import java.beans.PropertyChangeEvent;// 01/15/97 RKM Changed drawTree to make certain g1 has a font, before calling getFontMetrics on it// 01/15/07 RKM Added invalidate to setTreeStructure// 01/29/97 TWB Integrated changes from Windows and RKM's changes// 01/29/97 TWB Integrated changes from Macintosh// 02/05/97 MSH Changed so that draws from first visible node// 02/27/97 MSH Merged change to add SEL_CHANGED// 04/02/97 TNM Draw all vertical lines// 04/14/97 RKM Changed bogus invalidates to repaint// RKM Changed hard coded sbVWidth to use preferredSize.width// RKM Changed getTreeStructure so it actually returned a representation of what was in the treeview// RKM Rearranged a lot of stuff to get this to work// RKM Changed g1.drawRect in drawTree to not overlap the scrollbar// RKM Changed parseTreeStructure to not force a root node// 05/02/97 RKM Add arg to addSibling so caller could control whether the sible was added as the last sibling or not// RKM Changed insert to call addSibling with false when handling NEXT// RKM Kept addSibling with two params for compatibility// 05/31/97 RKM Updated to support Java 1.1// Made properties bound & constrained// Removed get/setBackground & get/setForeground overrides, getter did nothing but call the super// and setters were calling repaint, no one else does this// Deprecated foreground and background hilite colors, used system colors instead// Deprecated SEL_CHANGED, replaced by ItemSelectable interface// Hid scrollbar on creation, to avoid ugly redraw problems// Changed to triggered redraw// NOTE: SystemColor seems to be broken on Mac// 06/01/97 RKM Changed symantec.beans references to java.beans// 05/13/97 TNM Added horizontal scrollbar// 05/15/97 TNM Check for vendor to corect scrollbar problem// 06/09/97 CAR Modified check for vendor to include java.version 1.1.x// 06/19/97 TNM Merging changes/** * Creates an "outline view" of text headings and, optionally, images. * The headings are arranged in a hierarchical fashion, and can be * expanded to show their sub-headings or collapsed, hiding their * sub-headings. * <p> * A TreeView is typically used to display information that is organized in a * hierarchical fashion like an index or table of contents. * <p> * A TreeNode2 object is used for each heading. * @see TreeNode2 */public class TreeView2 extends Panel // implements ItemSelectable JDK1.1{ // constants for insertion /** * Constant indicating that the new node is to be a child * of the existing node. * @see #insert */ public static final int CHILD = 0; /** * Constant indicating that the new node is to be the next * sibling of the existing node. * @see #insert */ public static final int NEXT = CHILD + 1; /** * Constant indicating that the new node is to be the last * sibling of the existing node. * @see #insert */ public static final int LAST = CHILD + 2; /** * @deprecated As of JDK version 1.1, * replaced by ItemSelectable interface. * @see java.awt.ItemSelectable */ public static final int SEL_CHANGED = 1006; //selection changed event private TreeNode2 rootNode; // root node of tree private TreeNode2 selectedNode; // highlighted node private TreeNode2 topVisibleNode; // first node in window protected Scrollbar verticalScrollBar; // vertical scrollbar int sbVPosition = 0; // hold value of vertical scrollbar int sbVWidth; // width of vertical scrollbar long sbVTimer = -1; // time of last vert scrollbar event private boolean sbVShow = false; // show or hide vertical scrollbar protected int count = 0; // Number of nodes in the tree protected int viewCount = 0; // Number of viewable nodes in the tree // (A node is viewable if all of its // parents are expanded.) protected Scrollbar horizontalScrollBar; // horizontal scrollbar int sbHPosition=0; // hold value of horizontal scrollbar int sbHHeight = 0; // height of horizontal scrollbar private int sbHSize; // size of horizontal scrollbar private int newWidth = 0; // for horizontal scrollbar private boolean sbHShow = false; // show or hide horizontal scrollbar private int sbHLineIncrement = 4; private int viewHeight = 300; private int viewWidth = 300; // pixel size of tree display private int viewWidest = 0 ; // widest item displayable (for horz scroll) int cellSize = 16; // size of node image int clickSize = 8; // size of mouse toggle (plus or minus) int imageInset = 3; // left margin of node image int textInset = 6; // left margin for text int textBaseLine= 3; // position of font baseline from bottom of cell private FontMetrics fm; // current font metrics //long timeMouseDown; // save time of last mouse down (for double click) //int doubleClickResolution = 333; // double-click speed in milliseconds protected boolean isSun1_1; // checks for scrollbars that have different max value /** * Offscreen Image used for buffering the painting process. */ protected Image im1; // offscreen image /** * Offscreen graphics context used for buffering the painting process. */ protected Graphics g1 = null; // offscreen graphics context // // Constructors // /** * Constructs an empty TreeView2. */ public TreeView2() { super.setLayout(null); verticalScrollBar = new Scrollbar(Scrollbar.VERTICAL); verticalScrollBar.hide(); add(verticalScrollBar); horizontalScrollBar = new Scrollbar(Scrollbar.HORIZONTAL); horizontalScrollBar.hide(); add(horizontalScrollBar); // Sun has different max value for setValues method in ScrollBar //isSun1_1 = ( System.getProperty("java.vendor").startsWith("Sun Microsystems Inc.") // && (System.getProperty("java.version").startsWith("11") // || System.getProperty("java.version").indexOf("1.1") > -1) ); isSun1_1 = true; } /** * Constructs a TreeView2 with the given node. * * @param head the root node of the constructed tree */ public TreeView2(TreeNode2 head) { this(); selectedNode = rootNode = head; count = 1; } // // Properties // public synchronized void clear () { rootNode = selectedNode = null; count = 0; viewCount = 0; triggerRedraw (); } /** * Initializes the TreeView2 from a string array. * There is one string for each node in the array. That string * contains the text of the node indented with same number of * leading spaces as the depth of that node in the tree. * @param s the string array used for initialization * @see #getTreeStructure */ public void setTreeStructure(String s[]) { rootNode = selectedNode = null; try { parseTreeStructure(s); } catch(InvalidTreeNode2Exception e) { System.out.println(e); } triggerRedraw(); //!!!RKM!!! If preferredSize ever has to do with actual data (duh), uncomment this //invalidate(); } /** * Gets a string array that reflects the current TreeView2's contents. * There is one string for each node in the array. That string * contains the text of the node indented with same number of * leading spaces as the depth of that node in the tree. * @return the string array that reflects the TreeView2's contents * @see #setTreeStructure */ public String[] getTreeStructure() { //Create a vector representing current tree structure if (rootNode==null) return null; Vector nodesVector = new Vector(count); rootNode.depth = 0; vectorize(rootNode,false,nodesVector); //Convert this to a String[] int numNodes = nodesVector.size(); String[] treeStructure = new String[numNodes]; for (int i = 0;i < numNodes;i++) { TreeNode2 thisNode = (TreeNode2)nodesVector.elementAt(i); //Add appropriate number of blanks String treeString = ""; for (int numBlanks = 0;numBlanks < thisNode.depth;numBlanks++) treeString += ' '; //Add tree treeString += thisNode.text; //Put string into array treeStructure[i] = treeString; } return treeStructure; } // // Deprecated Properties // /** * @deprecated As of JDK version 1.1, * replaced by use of SystemColors.textHighlightText. */ public void setFgHilite(Color c) { } /** * @deprecated As of JDK version 1.1, * replaced by use of SystemColors.textHighlightText. */ public Color getFgHilite() { //return SystemColor.textHighlightText; return Color.black; } /** * @deprecated As of JDK version 1.1, * replaced by use of SystemColors.textHighlight. */ public void setBgHilite(Color c) { } /** * @deprecated As of JDK version 1.1, * replaced by use of SystemColors.textHighlight. */ public Color getBgHilite() { //return SystemColor.textHighlight; return Color.white; } // // ItemSelectable interface // /** * Returns the selected items or null if no items are selected. * <p> * This is a standard method of the ItemSelectable interface. */ public Object[] getSelectedObjects() { if (selectedNode == null) return null; TreeNode2[] selectedObjects = new TreeNode2[1]; selectedObjects[0] = selectedNode; return selectedObjects; } /** * Add a listener to recieve item events when the state of * an item changes. * <p> * This is a standard method of the ItemSelectable interface. * @param l the listener to recieve events * @see ItemEvent */ /* JDK1.1 public void addItemListener(ItemListener l) { itemListener = AWTEventMulticaster.add(itemListener, l); } */ /** * Removes an item listener. * <p> * This is a standard method of the ItemSelectable interface. * @param l the listener being removed * @see ItemEvent */ /* JDK1.1 public void removeItemListener(ItemListener l) { itemListener = AWTEventMulticaster.remove(itemListener, l); } */ // // Methods // // Insert a new node relative to a node in the tree. // position = CHILD inserts the new node as a child of the node // position = NEXT inserts the new node as the next sibling // position = LAST inserts the new node as the last sibling /** * Inserts a new node relative to an existing node in the tree. * @param newNode the new node to insert into the tree * @param relativeNode the existing node used for a position reference * @param position where to insert the new node relative to relativeNode. * Legal values are CHILD, NEXT and LAST. * @see #CHILD * @see #NEXT * @see #LAST * @see #append */ public void insert(TreeNode2 newNode, TreeNode2 relativeNode, int position) { if (newNode == null || relativeNode == null) return; if (exists(relativeNode)==false) return; switch (position) { case CHILD: addChild(newNode, relativeNode); break; case NEXT: addSibling(newNode, relativeNode, false); break; case LAST: addSibling(newNode, relativeNode, true); break; default: // invalid position return; } } /** * Returns the "root" node. * The root node is the first top-level node in the tree hierarchy. * All other nodes are either children or siblings of that one. * @return the root tree node */ public TreeNode2 getRootNode() { return rootNode; } /** * Returns the total number of nodes in the tree. */ public int getCount() { return count; } /** * Returns the total number of viewable nodes in the tree. * A node is viewable if all of its parents are expanded. */ public int getViewCount() { return viewCount; } /** * Determines if the given node is viewable. * A node is viewable if all of its parents are expanded. * @param node the node to check * @return true if the node is visible, false if it is not * @see #viewable(java.lang.String) */ boolean viewable(TreeNode2 node) { for (int i=0; i<viewCount; i++) { if (node == v.elementAt(i)) { return true; } } return false; } /** * Determines if the node with the given text is viewable. * A node is viewable if all of its parents are expanded.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -