📄 layeredtreelayout.java
字号:
*/ public boolean visit( Vertex vertexToVisit ) { Tree tree = (Tree) this.vgraph.getGraph(); Vertex parent; List siblings = new ArrayList(); List children = null; logCategory.debug( "Visiting " + vertexToVisit ); try { parent = tree.getParent( vertexToVisit ); if( parent != null ) siblings = tree.getChildren( parent ); else siblings.clear(); } catch( Exception ex ) { ex.printStackTrace(); return false; } // Remove the vertex being visited since it is not a sibling // but it is itself. siblings.remove( vertexToVisit ); try { children = tree.getChildren( vertexToVisit ); } catch( GraphException ex ) { ex.printStackTrace(); } // If it is a leaf, the layout the leaf by itself if( children.size() == 0 ) { logCategory.debug( "Visiting leaf" ); this.layout( vertexToVisit, children ); } return true; } public void routeEdge( Graphics2D g2d, VisualEdge vEdge ) { Rectangle fromvertexBounds; Rectangle tovertexBounds; GeneralPath drawPath; VisualVertex visualVertexA = vEdge.getVisualVertexA(); VisualVertex visualVertexB = vEdge.getVisualVertexB(); drawPath = vEdge.getGeneralPath(); fromvertexBounds = visualVertexA.getBounds(); tovertexBounds = visualVertexB.getBounds(); // Make sure to clear the GeneralPath() first. Otherwise, the edge's previous // path will be redrawn as well. drawPath.reset(); // Start the line from the center of the vEdgertex drawPath.moveTo( (float)fromvertexBounds.getCenterX(), (float)fromvertexBounds.getCenterY() ); drawPath.lineTo( (float)tovertexBounds.getCenterX(), (float)tovertexBounds.getCenterY() ); } public void addVertex(VisualVertex vVertex) { // Do nothing here } public void removeEdge(VisualEdge vEdge) { // Do nothing here } public void removeVertex(VisualVertex vVertex) { // Do nothing here } public void addEdge(VisualEdge vEdge) { // Do nothing here }}class TreeGridAdjuster implements Visitor { VisualGraph vGraph; Tree tree; Vertex parentNode; Grid grid; int insertedColumnX; boolean adjustToRight; /** * Log4J Category. The name of the category is the fully qualified name of the * enclosing class, which in this case is <tt>salvo.jesus.graph.visual.Grid</tt>. */ static Category logCategory; static { logCategory = Category.getInstance( Grid.class.getName()); } public TreeGridAdjuster( VisualGraph vGraph, Vertex parentNode, Grid grid, int insertedColumnX ) { this.vGraph = vGraph; this.tree = (Tree) this.vGraph.getGraph(); this.parentNode = parentNode; this.grid = grid; this.insertedColumnX = insertedColumnX; } public void adjust() { List children; try { children = this.tree.getChildren( parentNode ); } catch( GraphException ex ) { ex.printStackTrace(); return; } // Find the child that that is immediately to the right and to the left // of the inserted column VisualVertex immediateRightChild = null, immediateLeftChild = null; int immediateRightChildX = grid.getWidth(); int immediateLeftChildX = 0; for( int i = 0; i < children.size(); i++ ) { VisualVertex vVertex; vVertex = this.vGraph.getVisualVertex( (Vertex) children.get( i )); Point vertexPoint = grid.findVisualVertex( vVertex ); if( vertexPoint.x > insertedColumnX && ( immediateRightChild == null || vertexPoint.x < immediateRightChildX )) { immediateRightChild = vVertex; immediateRightChildX = vertexPoint.x; } if( vertexPoint.x < insertedColumnX && ( immediateLeftChild == null || vertexPoint.x > immediateLeftChildX )) { immediateLeftChild = vVertex; immediateLeftChildX = vertexPoint.x; } } // Move the granchild nodes to the right // whose parent is immediately to the right of the inserted colunm grid // but itself is to the left of the inserted column grid if( immediateRightChild != null ) { this.adjustToRight = true; this.visit( immediateRightChild.getVertex() ); } // Move the granchild nodes to the left // whose parent is immediately to the left of the inserted colunm grid // but itself is to the right of the inserted column grid if( immediateLeftChild != null ) { this.adjustToRight = false; this.visit( immediateLeftChild.getVertex() ); } } public boolean visit( Vertex vertexToVisit ) { List children; try { children = this.tree.getChildren( vertexToVisit ); } catch( GraphException ex ) { ex.printStackTrace(); return false; } int numberOfChildren = children.size(); VisualVertex vVertex; Point parentPoint = this.grid.findVisualVertex( this.vGraph.getVisualVertex( vertexToVisit )); Point childPoint; for( int i = 0; i < numberOfChildren; i++ ) { vVertex = this.vGraph.getVisualVertex( (Vertex) children.get( i )); childPoint = grid.findVisualVertex( vVertex ); // If the child is to the left of its parent and is to the left // of the inserted column but the parent is to the right of the inserted column, // move the child node to the right if( adjustToRight && childPoint.x < parentPoint.x ) { if( childPoint.x < this.insertedColumnX ) { logCategory.debug( "Moving " + vVertex + " from " + childPoint + " to (" + (childPoint.x + 1) + "," + (childPoint.y) ); this.grid.setGridPoint( childPoint.x + 1, childPoint.y, vVertex ); } this.visit( (Vertex) children.get(i) ); } // If the child is to the right of its parent and is to the right // of the inserted column but the parent is to the left of the inserted column, // move the child node to the left if( !adjustToRight && childPoint.x > parentPoint.x ) { if( childPoint.x > this.insertedColumnX ) { logCategory.debug( "Moving " + vVertex + " from " + childPoint + " to (" + (childPoint.x - 1) + "," + (childPoint.y) ); this.grid.setGridPoint( childPoint.x - 1, childPoint.y, vVertex ); } this.visit( (Vertex) children.get(i) ); } } return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -