📄 treecanvas.java
字号:
/*File: TreeCanvas.javaAuthor: zerksis d. umrigar (zdu@acm.org)Copyright (C) 1997 Zerksis D. UmrigarLast Update Time-stamp: "97/07/28 01:13:24 zdu"This code is distributed under the terms of the GNU General Public License.See the file COPYING with this distribution, or http://www.fsf.org/copyleft/gpl.htmlTHERE IS ABSOLUTELY NO WARRANTY FOR THIS PROGRAM.*/package zdu.zydebug;import zdu.zydebug.OffsetForest;import zdu.zydebug.ScrollableCanvas;import java.applet.*;import java.awt.*;import java.io.*;import java.util.Vector;class TreeCanvas extends ScrollableCanvas implements Selectable { TreeCanvas(int width, int height, String mouseHint) { super(width, height, mouseHint == null ? null : ""); hintMsg= mouseHint; forest= null; locator= new GridLocator(0, 0, width, height, colSep, rowSep); } TreeCanvas(int width, int height) { this(width, height, null); } public void setFont(Font f) { super.setFont(f); FontMetrics fontMetrics= getFontMetrics(f); hScroll.setLineIncrement(fontMetrics.stringWidth("M")); vScroll.setLineIncrement(fontMetrics.getHeight()); repaint(); } public void setRowSep(int sep) { Dimension s= size(); rowSep= sep; locator= new GridLocator(0, 0, s.width, s.height, colSep, rowSep); } public void setColSep(int sep) { Dimension s= size(); colSep= sep < MIN_NODE_WIDTH + SLOP ? MIN_NODE_WIDTH + SLOP : sep; locator= new GridLocator(0, 0, s.width, s.height, colSep, rowSep); } public void setShadows(boolean doShadow) { this.doShadow= doShadow; } public void resize(int x, int y, int w, int h) { Rectangle rect= locator.getBounds(); if (w > rect.width || h > rect.height) { locator= new GridLocator(0, 0, w, h, colSep, rowSep); } } public void reset() { rowLo= colLo= 0; if (forest != null) forest.removeAllElements(); } public void select(Object xSpec, Object ySpec) { selectedNode= (OffsetTree)xSpec; didSelect= true; } public void paint(Graphics g) { Dimension s= size(); if (forest != null && forest.size() > 0) { locator.locatorReset(); int n= forest.size(); int wLeft= forest.leftWidth(); int wRight= forest.rightWidth(); int h= forest.height(); int x= hScroll.getValue(); int y= vScroll.getValue(); if (didSelect) { x= wLeft; y= 0; for (OffsetTree t= selectedNode; t != null; t= (OffsetTree)t.parent()) { y++; x+= t.getOffset(); } x= x > 1 ? (x - 1)*colSep : 0; y= y > 2 ? (y - 2)*rowSep : 0; didSelect= false; } updateScrollbars(x, y, (wLeft + wRight + 1) * colSep, (h + 1) * rowSep); g.translate(-hScroll.getValue(), -vScroll.getValue()); int minX= hScroll.getValue(); int minY= vScroll.getValue(); int maxX= minX + s.width; int maxY= minY + s.height; for (int i= 0; i < n; i++) { OffsetTree tree= forest.elementAt(i); drawTree(g, tree, (wLeft + tree.getOffset())*colSep + colSep/2, 10, minX, minY, maxX, maxY); } } else { updateScrollbars(0, 0, s.width, s.height); } } public boolean mouseExit(Event event, int x, int y) { if (mouseNode != null) { mouseNode= null; isMouseDown= false; repaint(); } return super.mouseExit(event, x, y); } public boolean mouseDown(Event evt, int x, int y) { BoundedObj boundedObj= locator.locate(x, y); isMouseDown= true; if (boundedObj != null) { OffsetTree t= (OffsetTree)(boundedObj.getObject()); mouseDownNode= mouseNode= t; repaint(); } else { mouseDownNode= mouseNode= null; } return super.mouseDown(evt, x, y); } public boolean mouseDrag(Event evt, int x, int y) { BoundedObj boundedObj= locator.locate(x, y); isMouseDown= true; if (boundedObj == null) { if (mouseNode != null) { isMouseDown= false; mouseNode= null; repaint(); } } else { OffsetTree t= (OffsetTree)(boundedObj.getObject()); if (t != mouseNode) { mouseNode= t; repaint(); } } return super.mouseDrag(evt, x, y); } public boolean mouseMove(Event evt, int x, int y) { BoundedObj boundedObj= locator.locate(x, y); if (boundedObj == null) { if (mouseNode != null) { mouseNode= null; repaint(); } } else { OffsetTree t= (OffsetTree)(boundedObj.getObject()); if (t != mouseNode) { mouseNode= t; repaint(); } } return super.mouseMove(evt, x, y); } public boolean mouseUp(Event evt, int x, int y) { BoundedObj boundedObj= locator.locate(x, y); if (boundedObj == null) { if (mouseNode != null) { mouseNode= null; repaint(); } } else { OffsetTree t= (OffsetTree)(boundedObj.getObject()); mouseNode= t; if (t == mouseDownNode) { mouseNode.setExpand(!mouseNode.getExpand()); } repaint(); } isMouseDown= false; mouseDownNode= null; return super.mouseUp(evt, x, y); } void setForest(OffsetForest forest) { this.forest= forest; } protected void updateScrollbars(int x, int y, int width, int height) { Dimension s= size(); int xMax= s.width > width ? s.width : width; int w1= width - s.width + SLOP; int x1= (x <= width) ? x : width; setScrollbar(hScroll, x1, s.width, 0, xMax + SLOP); int yMax= s.height > height ? s.height : height; int y1= (y <= height) ? y : height; setScrollbar(vScroll, y1, s.height, 0, yMax + SLOP); FontMetrics fontMetrics= getFontMetrics(getFont()); hScroll.setLineIncrement(fontMetrics.stringWidth("M")); hScroll.setPageIncrement(colSep); vScroll.setLineIncrement(fontMetrics.getHeight()); vScroll.setPageIncrement(rowSep); } Image getHintImage() { FontMetrics m= getFontMetrics(getFont()); Dimension s= size(); if (hintMsg != null && (hintBuf == null || hintBufDim.width != s.width || hintBufDim.height != m.getHeight() + 2*HINT_Y_INSET)) { int h= m.getHeight(); hintBufDim= new Dimension(s.width, h + 2*HINT_Y_INSET); hintBuf= createImage(hintBufDim.width, hintBufDim.height); Graphics g= hintBuf.getGraphics(); int xMsg= SLOP; int xTerm= xMsg + m.stringWidth(hintMsg) + HINT_SPACE; int xNonTerm= xTerm + m.stringWidth(TERM_HINT) + HINT_SPACE; int xError= xNonTerm + m.stringWidth(NON_TERM_HINT) + HINT_SPACE; g.setColor(HINT_BG_COLOR); g.fillRect(0, 0, s.width, h + 2*HINT_Y_INSET); g.setColor(TERM_COLOR); g.fillRect(xTerm, HINT_Y_INSET, m.stringWidth(TERM_HINT), h); g.setColor(NON_TERM_COLOR); g.fillRect(xNonTerm, HINT_Y_INSET, m.stringWidth(NON_TERM_HINT), h); g.setColor(ERROR_COLOR); g.fillRect(xError, HINT_Y_INSET, m.stringWidth(ERROR_HINT), h); g.setColor(HINT_FG_COLOR); int a= m.getAscent(); g.drawString(hintMsg, xMsg, HINT_Y_INSET + a); g.drawString(TERM_HINT, xTerm, HINT_Y_INSET + a); g.drawString(NON_TERM_HINT, xNonTerm, HINT_Y_INSET + a); g.drawString(ERROR_HINT, xError, HINT_Y_INSET + a); } return hintBuf; } private void drawNode(Graphics g, OffsetTree t, int x, int y) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -