📄 treenode.java
字号:
event = new ActionEvent ( this, ActionEvent.ACTION_PERFORMED, ACTION_NODE_COLLAPSED ); } else { boolExpanded = true; event = new ActionEvent ( this, ActionEvent.ACTION_PERFORMED, ACTION_NODE_EXPANDED ); } fireActionEvent ( event ); return ( boolExpanded ); } public void setCurrent ( boolean boolCurrent ) { TreeNode node; ActionEvent event; if ( this.boolCurrent == boolCurrent ) return; if ( boolCurrent == true ) { node = (TreeNode) getRoot (); node = node.getCurrent (); if ( node != null ) node.setCurrent ( false ); node = (TreeNode) nodeOwner; while ( node != null && node instanceof TreeNode ) { if ( node.isExpanded() == false ) node.setExpanded ( true ); node = (TreeNode) node.nodeOwner; } event = new ActionEvent ( this, ActionEvent.ACTION_PERFORMED, ACTION_NODE_SETCURRENT ); } else { event = new ActionEvent ( this, ActionEvent.ACTION_PERFORMED, ACTION_NODE_RESETCURRENT ); } this.boolCurrent = boolCurrent; computeImageSize (); fireActionEvent ( event ); } public TreeNode getCurrent () { int i; int nSize; TreeNode node; TreeNode nodeCurrent = null; if ( boolCurrent == true ) return ( this ); nSize = getImmediateSubElementsCount (); for ( i = 0; i < nSize && nodeCurrent == null; i++ ) { node = (TreeNode) getSubElement ( i ); nodeCurrent = node.getCurrent (); } return ( nodeCurrent ); } public void setImage ( Image imageNew ) { imageTreeElement = imageNew; computeImageSize (); } public void setImageCur ( Image imageNew ) { imageTreeElementCur = imageNew; computeImageSize (); } public Rectangle getNodeBounds () { return ( new Rectangle(rectElement) ); } public Rectangle getNodeBoundsFull () { return ( new Rectangle(rectElementFull) ); } public void onMouseDoubleclick ( Point pointMouse ) { int i; int nSize; TreeNode node; nSize = getImmediateSubElementsCount (); if ( !(rectElementFull.contains (pointMouse)) ) return; if ( rectElement.contains (pointMouse) && pointMouse.x >= rectElement.x + 2 * MARGIN_HORZ + BOXSIZE && nSize > 0 ) { switchExpanded (); return; } if ( boolExpanded == false ) return; for ( i = 0; i < nSize; i++ ) { node = (TreeNode) getSubElement ( i ); node.onMouseDoubleclick ( pointMouse ); } } public void onMousePressedLeft ( Point pointMouse, boolean boolShift, boolean boolCtrl, boolean boolAlt ) { int i; int nSize; TreeNode node; nSize = getImmediateSubElementsCount (); if ( !(rectElementFull.contains (pointMouse)) ) return; if ( rectElement.contains (pointMouse) ) { if ( pointMouse.x >= rectElement.x + 2 * MARGIN_HORZ + BOXSIZE ) setCurrent ( true ); else if ( nSize > 0 ) switchExpanded (); return; } if ( boolExpanded == false ) return; for ( i = 0; i < nSize; i++ ) { node = (TreeNode) getSubElement ( i ); node.onMousePressedLeft ( pointMouse, boolShift, boolCtrl, boolAlt ); } } public void onMousePressedRight ( Point pointMouse, boolean boolShift, boolean boolCtrl, boolean boolAlt ) { } public void drawElement ( Graphics graphics, int nOffsetX, int nOffsetY ) { int nX; int nY; int nWidth; int nHeight; int nSize; Rectangle rect; Rectangle rectLastSubElement; FontMetrics fontMetrics; TreeNode node; Image imageDraw; nSize = getImmediateSubElementsCount (); rect = new Rectangle ( rectElement ); rect.x -= nOffsetX; rect.y -= nOffsetY; fontMetrics = graphics.getFontMetrics (); graphics.setColor ( colorLines ); if ( nSize > 0 ) { nX = rect.x + MARGIN_HORZ; nY = rect.y + (rect.height - BOXSIZE) / 2; nWidth = BOXSIZE; nHeight = BOXSIZE; graphics.setColor ( colorBg ); graphics.fillRect ( nX, nY, nWidth, nHeight ); graphics.setColor ( colorLines ); graphics.drawRect ( nX, nY, nWidth, nHeight ); nX = rect.x + MARGIN_HORZ + BOXSIZE; nY = rect.y + rect.height / 2; drawDottedLine ( graphics, nX, nY, nX + MARGIN_HORZ, nY ); } else { nX = rect.x + MARGIN_HORZ + BOXSIZE / 2; nY = rect.y + rect.height / 2; drawDottedLine ( graphics, nX, nY, nX + MARGIN_HORZ, nY ); } if ( nSize > 0 && boolExpanded == true ) { node = (TreeNode) getSubElement ( nSize - 1 ); rectLastSubElement = node.getNodeBounds (); nX = rect.x + 2 * MARGIN_HORZ + BOXSIZE + nWidthImage / 2; nY = rect.y + (rect.height - nHeightImage) / 2 + nHeightImage + 1; nHeight = rectLastSubElement.y - nOffsetY - nY + rectLastSubElement.height / 2; drawDottedLine ( graphics, nX, nY, nX, nY + nHeight ); } graphics.setColor ( colorFg ); if ( nSize > 0 ) { nX = rect.x + MARGIN_HORZ + 2; nY = rect.y + rect.height / 2; nWidth = BOXSIZE - 2 * 2; graphics.drawLine ( nX, nY, nX + nWidth, nY ); if ( boolExpanded == false ) { nX = rect.x + MARGIN_HORZ + BOXSIZE / 2; nY = rect.y + (rect.height - BOXSIZE) / 2 + 2; nHeight = BOXSIZE - 2 * 2; graphics.drawLine ( nX, nY, nX, nY + nHeight ); } } nX = rect.x + 2 * MARGIN_HORZ + BOXSIZE; nY = rect.y + (rect.height - nHeightImage) / 2; if ( boolCurrent == true ) imageDraw = imageTreeElementCur; else imageDraw = imageTreeElement; if ( imageDraw != null ) graphics.drawImage ( imageDraw, nX, nY, colorBg, componentOwner ); if ( nWidthImage < 0 || nHeightImage < 0 ) { nWidthImage = imageDraw.getWidth ( componentOwner ); nHeightImage = imageDraw.getHeight ( componentOwner ); } nX = rect.x + 3 * MARGIN_HORZ + BOXSIZE + nWidthImage - MARGIN_HORZ / 2; nY = rect.y + 2; nWidth = fontMetrics.stringWidth ( strName ) + MARGIN_HORZ; nHeight = rect.height - 3; if ( boolCurrent == true ) { graphics.setColor ( colorCurBg ); graphics.fillRect ( nX, nY, nWidth, nHeight ); graphics.setColor ( colorCurFg ); } else { graphics.setColor ( colorFg ); } if ( boolCurrent == true && hasOwnerFocus() ) drawDottedRectangle ( graphics, nX, nY, nWidth, nHeight ); nHeight = fontMetrics.getAscent (); nX = rect.x + 3 * MARGIN_HORZ + BOXSIZE + nWidthImage; nY = rect.y + (rect.height - nHeight) / 2 + nHeight; graphics.drawString ( strName, nX, nY ); } public void drawDottedRectangle ( Graphics graphics, int nX, int nY, int nWidth, int nHeight ) { drawDottedLine ( graphics, nX, nY, nX + nWidth - 1, nY ); drawDottedLine ( graphics, nX + nWidth - 1, nY, nX + nWidth - 1, nY + nHeight - 1 ); drawDottedLine ( graphics, nX + nWidth - 1, nY + nHeight - 1, nX, nY + nHeight - 1 ); drawDottedLine ( graphics, nX, nY + nHeight - 1, nX, nY ); } public void drawDottedLine ( Graphics graphics, int nX1, int nY1, int nX2, int nY2 ) { int nX, nY; double dDiv; if ( nX1 == nX2 && nY1 == nY2 ) { drawDot ( graphics, nX1, nY1 ); return; } if ( nX1 > nX2 ) { nX = nX1; nX1 = nX2; nX2 = nX; } if ( nY1 > nY2 ) { nY = nY1; nY1 = nY2; nY2 = nY; } if ( nX2 - nX1 > nY2 - nY1 ) { dDiv = (double)(nY2 - nY1) / (nX2 - nX1); for ( nX = nX1; nX <= nX2; nX++ ) { nY = (int)Math.rint ( nY1 + (nX - nX1) * dDiv ); drawDot ( graphics, nX, nY ); } } else { dDiv = (nX2 - nX1) / (nY2 - nY1); for ( nY = nY1; nY <= nY2; nY++ ) { nX = (int)Math.rint ( nX1 + (nY - nY1) * dDiv ); drawDot ( graphics, nX, nY ); } } } public void drawDot ( Graphics graphics, int nX, int nY ) { if ( (nX + nY) % 2 == 0 ) graphics.drawLine ( nX, nY, nX, nY ); } public void computeImageSize () { if ( boolCurrent == false && imageTreeElement != null ) { nWidthImage = imageTreeElement.getWidth ( componentOwner ); nHeightImage = imageTreeElement.getHeight ( componentOwner ); } else if ( boolCurrent == true && imageTreeElementCur != null ) { nWidthImage = imageTreeElementCur.getWidth ( componentOwner ); nHeightImage = imageTreeElementCur.getHeight ( componentOwner ); } else { nWidthImage = 0; nHeightImage = 0; } } public Rectangle recomputeLayout ( Rectangle rect, FontMetrics fontMetrics ) { int i; int nSize; int nOffsetX; int nOffsetY; int nHeight; TreeNode node; Rectangle rectSubElement; computeImageSize (); rectElement.x = rect.x; rectElement.y = rect.y; if ( boolVisible == true ) { rectElement.height = Math.max ( fontMetrics.getHeight(), BOXSIZE + 2 * MARGIN_VERT ); rectElement.height = Math.max ( rectElement.height, nHeightImage + 2 * MARGIN_VERT ); rectElement.width = BOXSIZE + nWidthImage + fontMetrics.stringWidth(strName) + 4 * MARGIN_HORZ; } else { rectElement.height = 0; rectElement.width = 0; } rectElementFull.setBounds ( rectElement ); if ( boolExpanded == true ) { if ( boolVisible == true ) nOffsetX = BOXSIZE / 2 + MARGIN_HORZ + nWidthImage / 2; else nOffsetX = 0; nOffsetY = rectElement.height; rectSubElement = new Rectangle ( rect ); rectSubElement.x += nOffsetX; rectSubElement.y += nOffsetY; nSize = getImmediateSubElementsCount (); for ( i = 0; i < nSize; i++ ) { node = (TreeNode) getSubElement ( i ); rectSubElement = node.recomputeLayout ( rectSubElement, fontMetrics ); rectElementFull.height += rectSubElement.height; rectSubElement.y += rectSubElement.height; if ( rectElementFull.width < nOffsetX + rectSubElement.width ) rectElementFull.width = nOffsetX + rectSubElement.width; } } return ( new Rectangle(rectElementFull) ); } public void addToTreeElList ( Vector cw_vectorTreeElList ) { int i; int nSize; TreeNode node; if ( boolVisible == true ) cw_vectorTreeElList.addElement ( this ); if ( boolExpanded == false ) return; nSize = getImmediateSubElementsCount (); for ( i = 0; i < nSize; i++ ) { node = (TreeNode) getSubElement ( i ); node.addToTreeElList ( cw_vectorTreeElList ); } } private boolean hasOwnerFocus () { boolean boolResult = false; Component component; Window window; if ( componentOwner == null ) return ( false ); component = componentOwner; while ( component != null ) { if ( component instanceof Window ) { boolResult = (((Window)component).getFocusOwner() == componentOwner); break; } component = component.getParent (); } return ( boolResult ); } private void fireActionEvent ( ActionEvent event ) { int i; int nCount; ActionListener listener; nCount = vectorActionListeners.size (); for ( i = 0; i < nCount; i++ ) { listener = (ActionListener) vectorActionListeners.elementAt ( i ); listener.actionPerformed ( event ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -