⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 edgebase.java

📁 UML设计测试工具
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                                          tX + tWidth/2, tY - 4, e );
                        break;
                    case 2:
                        setCorrectPoints( sX + sWidth/3, sY + sHeight/2,
                                          tX + tWidth/2, tY + 4, e );
                        break;
                    case 3:
                        setCorrectPoints( sX - sWidth/3, sY - sHeight/2,
                                          tX - tWidth/2, tY - 4, e );
                        break;
                    case 4:
                        setCorrectPoints( sX - sWidth/3, sY + sHeight/2,
                                          tX - tWidth/2, tY + 4, e );
                        break;
                    default:
                        setCorrectPoints( sX + sWidth/3, sY - sHeight/2,
                                          tX + tWidth/2, tY - 4, e );
                        break;
                    }
                }
            }
            
            
            // to get a approiate separation of the links ...
            // ... use the width
            if ( line.intersectsLine( uLeftX, uLeftY, uRightX, uRightY )
                    || line.intersectsLine( lLeftX, lLeftY, lRightX, lRightY ) ) {
                // calculates the length of the space between the edges
                space = calculateSpaces( Math.min( sWidth, tWidth ), edges );
                // calculates the length of the space to the first edge
                // for the bigger rectangle
                projection = ( Math.max( sWidth, tWidth ) - space
                        * ( edges.size() - 1 ) ) / 2.0;
                
                double counter = 1.0;
                
                // y-coordinates of start and end point for all lines
                double sStartY = sY;
                double tStartY = tY;
                
                double sStartX = sX - sWidth / 2.0;
                double tStartX = tX - tWidth / 2.0;
                
                // calculates the different x-coordinates of start and end
                // point
                Iterator it = edges.iterator();
                while ( it.hasNext() ) {
                    EdgeBase e = (EdgeBase) it.next();
                    // addition of space or projection to get the
                    // wanted space between the edges
                    if ( counter == 1 ) {
                        if ( sWidth <= tWidth ) {
                            sStartX += space;
                            tStartX += projection;
                        } else {
                            sStartX += projection;
                            tStartX += space;
                        }
                    } else {
                        sStartX += space;
                        tStartX += space;
                    }
                    setCorrectPoints( sStartX, sStartY, tStartX, tStartY, e );   
                    updateNodeOnEdges();
                    counter++;
                }
            // ... use the height
            } else if ( line.intersectsLine( uLeftX, uLeftY, lLeftX, lLeftY )
                    || line.intersectsLine( uRightX, uRightY, lRightX,
                                            lRightY ) ) {
                // calculates the length of the space between the edges
                space = calculateSpaces( Math.min( sHeight, tHeight ), edges );
                // calculates the length of the space to the first edge
                // for the bigger rectangle
                projection = ( Math.max( sHeight, tHeight ) - space
                        * ( edges.size() - 1 ) ) / 2.0;
                
                double counter = 1.0;
                
                // x-coordinates of start and end point for all lines
                double sStartX = sX;
                double tStartX = tX;
                
                double sStartY = sY - sHeight / 2.0;
                double tStartY = tY - tHeight / 2.0;
                
                // calculates the different y-coordinates of start and end
                // point
                Iterator it = edges.iterator();
                while ( it.hasNext() ) {
                    EdgeBase e = (EdgeBase) it.next();
                    // addition of space or projection to get the
                    // wanted space between the edges
                    if ( counter == 1 ) {
                        if ( sHeight <= tHeight ) {
                            sStartY += space;
                            tStartY += projection;
                        } else {
                            sStartY += projection;
                            tStartY += space;
                        }
                    } else {
                        sStartY += space;
                        tStartY += space;
                    }
                    setCorrectPoints( sStartX, sStartY, tStartX, tStartY, e );   
                    updateNodeOnEdges();
                    counter++;
                }
            }
        }
    }
    
    /**
     * Determinds the correct source side and sets the specific points.
     * 
     * @param sX X-coordinate for the source point.
     * @param sY Y-coordinate for the source point.
     * @param tX X-coordinate for the target point.
     * @param tY Y-coordinate for the target point.
     * @param e The points of this EdgeBase will be set.
     */
    private void setCorrectPoints( double sX, double sY, double tX, double tY,
                                   EdgeBase e ) {
        if ( e.fSource.equals( fSource ) ) {
            e.setPoint( SOURCE, sX, sY );
            e.setPoint( TARGET, tX, tY );
        } else {
            e.setPoint( TARGET, sX, sY );
            e.setPoint( SOURCE, tX, tY );
        }
        e.updateNodeOnEdges();
    }
    
    /**
     * This method calculates the intersection point of the given line and 
     * one of the four lines of the nodes polygon.
     * 
     * @param node the source node.
     * @param sX the source x-coordinate of the line.
     * @param sY the source y-coordinate of the line.
     * @param tX the target x-coordinate of the line.
     * @param tY the target y-coordinate of the line.
     */
    Point2D.Double getIntersectionCoordinate( NodeBase node, int sX, int sY,
                                              int tX, int tY ) {
        Polygon polygon = node.dimension();
        
        int[] xpoints = polygon.xpoints;
        int[] ypoints = polygon.ypoints;
        
        // source corner points
        // corner 1  ---------------  corner 2              
        //           |             |
        //           |             |
        // corner 4  ---------------  corner 3
        //
        //                  /\ corner 1
        //       corner 4  /  \ 
        //                 \  /  corner 2
        //       corner 3   \/
        double x_corner1 = xpoints[0];
        double x_corner2 = xpoints[1];
        double x_corner3 = xpoints[2];
        double x_corner4 = xpoints[3];
        double y_corner1 = ypoints[0];
        double y_corner2 = ypoints[1];
        double y_corner3 = ypoints[2];
        double y_corner4 = ypoints[3];
        
        // line from source node to target node
        Line2D.Double line = new Line2D.Double( sX, sY, tX, tY );
        
        // getting the intersection coordinate 
        if ( line.intersectsLine( x_corner1, y_corner1, x_corner2, y_corner2 ) ) {
            // cuts the line between corner 1 and 2 of the node
            return intersectionPoint( sX, sY, tX, tY, 
                                      x_corner1, y_corner1, 
                                      x_corner2, y_corner2 );
        } else if ( line.intersectsLine( x_corner3, y_corner3, x_corner4, y_corner4 ) ) {
            // cuts the line between corner 3 and 4 of the node
            return intersectionPoint( sX, sY, tX, tY, 
                                      x_corner3, y_corner3, 
                                      x_corner4, y_corner4 );
        } else if ( line.intersectsLine( x_corner1, y_corner1, x_corner4, y_corner4 ) ) {
            // cuts the line between corner 1 and 4 of the node
            return intersectionPoint( sX, sY, tX, tY, 
                                      x_corner1, y_corner1, 
                                      x_corner4, y_corner4 );
        } else if ( line.intersectsLine( x_corner2, y_corner2, x_corner3, y_corner3 ) ) {
            // cuts the line between corner 2 and 3 of the node
            return intersectionPoint( sX, sY, tX, tY, 
                                      x_corner2, y_corner2, 
                                      x_corner3, y_corner3 );
        }
        
        // if no line is cut return the start point 
        // (both nodes lay on top of each other.
        return new Point2D.Double( sX, sY );
    }
    
    /** 
     * Calculates the intersection point of to lines.
     * 
     * @param sX source x-coordinate of the first line.
     * @param sY source y-coordinate of the first line.
     * @param tX target x-coordinate of the first line.
     * @param tY target y-coordinate of the first line.
     * @param x_corner1 source x-coordinate of the second line.
     * @param y_corner1 source y-coordinate of the second line.
     * @param x_corner2 target x-coordinate of the second line.
     * @param y_corner2 target y-coordinate of the second line.
     * @return the intersection point of both lines.
     */    
    private Point2D.Double intersectionPoint( int sX, int sY, int tX, int tY, 
                                              double x_corner1, double y_corner1, 
                                              double x_corner2, double y_corner2 ) {
        // getting the intersection coordinate by vector arithmetic
        // example for the top line:
        //   sx over sy + r * (tx - sx) over (ty - sy)
        // = x_corner1 over y_corner1 + v * (x_corner2 - x_corner1) over (y_corner2 - y_corner1)
        
        double numerator = 1.0;
        double denominator = 1.0;
        
        numerator = (tX - sX) * (sY - y_corner1) + (tY - sY) * (x_corner1 - sX);
        denominator = (y_corner2 - y_corner1) * (tX - sX) 
        - (x_corner2 - x_corner1) * (tY - sY); 
        
        double v = numerator / denominator;
        
        double intersection_X = x_corner1 + v * (x_corner2 - x_corner1);
        double intersection_Y = y_corner1 + v * (y_corner2 - y_corner1);
        
        return new Point2D.Double( intersection_X, intersection_Y );
    }
    
    /**
     * Saves placement information about this edge.
     * @param hidden If this edge should be hidden or not.
     * @return A XML representation of the layout information.
     */
    public String storePlacementInfo( boolean hidden ) {
        String xml = "";

        if ( this instanceof NodeEdge && fAssoc.associationEnds().size() > 2 ) {
            // xml tag will be written in NodeEdge itself. 
            // Will be called from DiamondNode.
            return xml;
        }
        
        xml = LayoutTags.EDGE_O;
        if ( this instanceof NodeEdge ) {
            if ( isLink() ) {
                xml += " type=\"NodeEdge\" kind=\"link\">" + LayoutTags.NL;
            } else {
                xml += " type=\"NodeEdge\" kind=\"association\">" + LayoutTags.NL;
            }
        } else if ( this instanceof BinaryEdge ) {
            if ( isLink() ) {
                xml += " type=\"BinaryEdge\" kind=\"link\">" + LayoutTags.NL;
            } else {
                xml += " type=\"BinaryEdge\" kind=\"association\">" + LayoutTags.NL;
            } 
        } else if ( this instanceof HalfEdge ) {
            if ( isLink() ) {
                xml += " type=\"HalfEdge\" kind=\"link\">" + LayoutTags.NL;
            } else { 
                xml += " type=\"HalfEdge\" kind=\"association\">" + LayoutTags.NL;
            }
        } else if ( this instanceof GeneralizationEdge ) { 
            xml += " type=\"Inheritance\">" + LayoutTags.NL;
        } else {
            xml += " type=\"EdgeBase\">" + LayoutTags.NL;
        } 
        
        xml += LayoutTags.INDENT + LayoutTags.SOURCE_O + fSource.name() 
               + LayoutTags.SOURCE_C + LayoutTags.NL;
        xml += LayoutTags.INDENT + LayoutTags.TARGET_O + fTarget.name() 
               + LayoutTags.TARGET_C + LayoutTags.NL;
        xml += LayoutTags.INDENT + LayoutTags.NAME_O + fEdgeName 
               + LayoutTags.NAME_C + LayoutTags.NL;

        
        if ( fSourceRolename != null ) {
            xml += fSourceRolename.storePlacementInfo( hidden ) + LayoutTags.NL;
        }
        if ( fTargetRolename != null ) {
            xml += fTargetRolename.storePlacementInfo( hidden ) + LayoutTags.NL;
        }
        if ( fSourceMultiplicity != null ) {
            xml += fSourceMultiplicity.storePlacementInfo( hidden ) + LayoutTags.NL;
        }
        if ( fTargetMultiplicity != null ) {
            xml += fTargetMultiplicity.storePlacementInfo( hidden ) + LayoutTags.NL;
        }
        if ( fAssocName != null ) {
            xml += fAssocName.storePlacementInfo( hidden ) + LayoutTags.NL;
        }
        if ( !fNodesOnEdge.isEmpty() ) {
            Iterator it = fNodesOnEdge.iterator();
            while ( it.hasNext() ) {
                NodeOnEdge n = (NodeOnEdge) it.next();
                xml += n.storePlacementInfo( hidden ) + LayoutTags.NL;
            }
        }
        xml += LayoutTags.INDENT + LayoutTags.HIDDEN_O + hidden 
               + LayoutTags.HIDDEN_C + LayoutTags.NL;
        
        xml += LayoutTags.EDGE_C + LayoutTags.NL;
        return xml;
    }
    
    /**
     * Sorts the nodes on this edge according to their id.
     */
    public void sortNodesOnEdge() {
        Collections.sort( fNodesOnEdge, new NodeOnEdgeComparator() );
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -