📄 treebrowser.java
字号:
for (int i=itemCount; i < items.size(); ++i) { node = (TreeNode) items.elementAt(i); dx = (node.level * DXLEVEL); labelwidth = g.getFontMetrics().stringWidth(node.label); maxwidth = Math.max(maxwidth, dx + DXLEVEL + labelwidth); } } hierarchyChanged = false; updateScrollbars(); } /** * this should be private. having it protected is a present * for dummy VM that doesn't know that an inner class can access * private method of its parent class */ protected TreeNode itemAt(int y) { for(int i = topItem; ((i < items.size()) && (y >0)); i++) { if(y < fontHeight) { return (TreeNode) items.elementAt(i); } y -= fontHeight; } return null; } public void update(Graphics pg) { Rectangle r = pg.getClipBounds(); Graphics offgc; Image offscreen = null; Dimension d = getSize(); // create the offscreen buffer and associated Graphics offscreen = ImageCache.getImage(this, d.width, d.height); offgc = offscreen.getGraphics(); if(r != null) { offgc.clipRect(r.x, r.y, r.width, r.height); } // clear the exposed area offgc.setColor(getBackground()); offgc.fillRect(0, 0, d.width, d.height); offgc.setColor(getForeground()); // do normal redraw paint(offgc); // transfer offscreen to window pg.drawImage(offscreen, 0, 0, this); } /** * Inserts new node. * * @param parent the parent node. * @item the abstract object this node refers to. may be null. * @handler the node handler, that will receive notifications for this node * @label the label displayed in the list. * @icon the icon displayed in the list. */ public void insert(TreeNode parent, Object item, NodeHandler handler, String label, Image icon) { boolean done; int j; if(parent == null) throw new IllegalArgumentException("null parent"); if((handler == null) && (label == null)) { throw new IllegalArgumentException("non-null item required"); } if(handler == null) { handler = parent.handler; } if(label == null) { label = handler.toString(); } if(parent.children == TreeNode.NOCHILD) { parent.children = 1; } else { parent.children += 1; } done = false; TreeNode node = null; int i = items.indexOf(parent)+parent.children; for (; (i < items.size() && ((TreeNode) items.elementAt(i)).level > parent.level); i++) {} items.insertElementAt(node=new TreeNode(item, label, handler, icon, parent.level+1), i); // hscroll hierarchyChanged = true; return; } /** * Removes the specified node. * This simply removes a node, without modifying its children if any. USE * WITH CAUTION. * @param node the node to remove */ public void remove(TreeNode node) { int ind = items.indexOf(node); TreeNode t = null; while (ind>=0) { t = (TreeNode) items.elementAt(ind); if (t.level >= node.level) ind--; else { t.children--; break; } } items.removeElement(node); if(node.selected) { unselect(node); } // hscroll hierarchyChanged = true; } /** * Removes the specified node and its children. * NOTE: if two threads are doing adds and removes, * this can lead to IndexOutOfBound exception. * You will probably have to use locks to get rid of that problem * @param node the node to remove */ public void removeBranch(TreeNode node) { int ist, iend; ist = items.indexOf(node)+1; iend = items.size()-1; for(int i = ist; i< iend; i++) { if(((TreeNode)items.elementAt(ist)).level > node.level) { remove((TreeNode)items.elementAt(ist)); } else break; } remove(node); // hscroll hierarchyChanged = true; } /** * Contracts the representation of the specified node. * removes all the children nodes of 'item'. It is caller's * responsibility to call repaint() afterwards. * @param item the node to contracts */ public synchronized void collapse(TreeNode item) { TreeNode node = (TreeNode)item; if(node.children != TreeNode.NOCHILD) { node.children = TreeNode.NOCHILD; for(int j = items.indexOf(item)+1; j <items.size(); /*nothing*/) { TreeNode child = (TreeNode)items.elementAt(j); if(child.level > node.level) { items.removeElementAt(j); if(child.selected) { unselect(child); } } else { // hscroll hierarchyChanged = true; // last children reached, exit return; } } } } /** * Sets the selection policy. * @param policy: SINGLE or MULTIPLE */ public void setSelectionPolicy(int policy) { selectionPolicy = policy; } /** * Gets the selection policy. */ public int getSelectionPolicy() { return selectionPolicy; } /** * Selects the specified node. * Selects the given node. If selectionPolicy is SINGLE any previously * selected node is unselected first. It is caller's responsibility to * call repaint() * @param node the node to select */ public void select(TreeNode node) { if(node == null) return; if(selectionPolicy == SINGLE) { unselectAll(); } selection.addElement(node); node.selected = true; } /** * Unselects the specified node. * It is caller's responsibility to call repaint() * @param node the node to unselect */ public void unselect(TreeNode node) { if(node == null) return; selection.removeElement(node); node.selected = false; } /** * Unselects all selected items. */ public void unselectAll() { for(Enumeration e = selection.elements(); e.hasMoreElements(); ) { TreeNode node = (TreeNode)e.nextElement(); node.selected = false; } } /** * Returns an Enumeraiton of selected items. */ public Enumeration selection() { return selection.elements(); } private void updateScrollbars() { int max = items.size()+1; if(items.size() > visibleItemCount) { vscroll.setMaximum(max); vscroll.setVisibleAmount(visibleItemCount); vscroll.setVisible(true); } else { vscroll.setValue(0); vscroll.setMaximum(max); vscroll.setVisibleAmount(max); if (scrollbarDisplayPolicy == SCROLLBARS_ASNEEDED) { vscroll.setVisible(false); } } int myWidth = getSize().width-HMARGIN*2; hscroll.setMaximum(maxwidth); hscroll.setVisibleAmount(myWidth); if (maxwidth > myWidth) { hscroll.setVisible(true); } else { if (scrollbarDisplayPolicy == SCROLLBARS_ASNEEDED) { hscroll.setVisible(false); } } } /** * Sets 'a' as vertical Scrollbar. * The Browser becomes an AdjustmentListener of this scrollbar. */ public void setVerticalScrollbar(Scrollbar a) { vscroll = a; vscroll.addAdjustmentListener(this); vscroll.setMaximum(visibleItemCount); vscroll.setVisibleAmount(visibleItemCount); vscroll.setBlockIncrement(visibleItemCount); } /** * Sets 'a' as horizontal Scrollbar. * The Browser becomes an AdjustmentListener of this scrollbar. */ public void setHorizontalScrollbar(Scrollbar a) { hscroll = a; hscroll.addAdjustmentListener(this); int myWidth = getSize().width-HMARGIN*2; hscroll.setMaximum(myWidth); hscroll.setVisibleAmount(myWidth); hscroll.setBlockIncrement(20); } /** * Updates graphical appearance in response to a scroll. */ public void adjustmentValueChanged(AdjustmentEvent evt) { if (evt.getSource() == vscroll) { topItem = evt.getValue(); } else { startx = evt.getValue(); } repaint(); } /** * Returns the parent node of the specified node. * If 'child' is a valid node belonging to the Tree and has a parent node, * returns its parent. Returns null otherwise. * @param child the child node you want to get its parent */ public TreeNode getParent(TreeNode child) { int n = items.indexOf(child); for(int i = n-1; i >= 0; i--) { TreeNode node = (TreeNode)(items.elementAt(i)); if(node.level < child.level) { return node; } } return null; } /** * Gets the node associated to the specified object, or null if any. * @param obj the object related to a node */ public TreeNode getNode(Object obj) { int imax = items.size(); for(int i=0; i < imax; i++) { if(obj.equals(((TreeNode)(items.elementAt(i))).getItem())) return (TreeNode)(items.elementAt(i)); } return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -