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

📄 binaryedge.java

📁 UML设计测试工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        double sWidth = fSource.getWidth();
        int maxWidth = Math.max( 30, Math.max( labelWidth, (int) sWidth/2 ));
        
        if ( maxWidth > LARGEST_WIDTH ) {
            maxWidth = LARGEST_WIDTH;
        }
        return maxWidth;
    }

    /**
     * Calculates the max height of the label on a reflexive edge.
     * @param fm The font metrics which is used to calculate the height.
     * @return The max height of the label.
     */
    private int maxHeight( FontMetrics fm ) {
        final int LARGEST_HEIGHT = 55;
        int labelHeight = fm.getHeight();
        double sHeight = fSource.getHeight();
        int maxHeight = Math.max( 30, Math.max( (int) (sHeight/3), labelHeight+4 ) );
        
        // is the height of the node equals or just a little bit bigger
        // as the labelHeight use the labelHeight.
        if ( sHeight <= labelHeight+6 ) {
            maxHeight = labelHeight+6;
        }
        if ( maxHeight > LARGEST_HEIGHT ) {
            maxHeight = LARGEST_HEIGHT;
        }
        return maxHeight;
    }
    
    /**
     * Draws a straightline edge between source and target node.
     */
    public void draw( Graphics g, FontMetrics fm ) {
        // edge is not drawn directly in this method, because this
        // way drawBinaryEdge can be reused for drawing the edge
        // for a link object.
        drawBinaryEdge( g, fm );
    }

    /**
     * Draws a straightline edge between source and target node.
     */
    void drawBinaryEdge( Graphics g, FontMetrics fm ) {
        if ( isSelected() ) {
            g.setColor( fOpt.getEDGE_SELECTED_COLOR() );
        } else {
            g.setColor( fOpt.getEDGE_COLOR() );
        }
        
        // draw the edge
        if ( isReflexive() ) {
            drawReflexiveEdge( g, fm );
        } else {
            drawEdge( g );

            // draw edge properties on the edge
            if ( isSelected() ) {
                g.setColor( fOpt.getEDGE_SELECTED_COLOR() );
            } else {
                g.setColor( fOpt.getEDGE_LABEL_COLOR() );    
            }

            if ( fOpt.isShowAssocNames() ) {
                fAssocName.draw( g, fm );
            }

            if ( fOpt.isShowRolenames() ) {
                fSourceRolename.draw( g, fm );
                fTargetRolename.draw( g, fm );
            }

            if ( fOpt.isShowMutliplicities() ) {
                fSourceMultiplicity.draw( g, fm );
                fTargetMultiplicity.draw( g, fm );
            }
        }
        g.setColor( fOpt.getEDGE_COLOR() );
    }

    /**
     * Draws the binary edge.
     * @param g The binary edge is drawn in this graphics object.
     */
    private void drawEdge( Graphics g ) {
        NodeOnEdge n1 = null;
        NodeOnEdge n2 = null;
        int source = MAggregationKind.NONE;
        int target = MAggregationKind.NONE;
        
        Iterator assocEndIt = fAssoc.associationEnds().iterator();
        while ( assocEndIt.hasNext() ) {
            MAssociationEnd assocEnd = (MAssociationEnd) assocEndIt.next();
            if ( fSource.cls() != null 
                 && fSource.cls().equals( assocEnd.cls() ) ) {
                source = assocEnd.aggregationKind();
            } else if ( fTarget.cls() != null 
                        && fTarget.cls().equals( assocEnd.cls() ) ) {
                target = assocEnd.aggregationKind();
            }
        }
        
        // draw all line segments
        if ( !fNodesOnEdge.isEmpty() ) {
            Iterator it = fNodesOnEdge.iterator();
            int counter = 0;
            if ( it.hasNext() ) {
                n1 = (NodeOnEdge) it.next();
                counter++;
            }
            while( it.hasNext() ) {
                n2 = (NodeOnEdge) it.next();
                counter++;
                // draw nodeOnEdge
                n2.draw( g, g.getFontMetrics() );
                
                try {
                    if ( source != MAggregationKind.NONE
                         && n1.getSpecialID() == SOURCE ) {
                        drawAssociationKind( g, n2, n1, source );
                    } else if ( target != MAggregationKind.NONE
                                && n2.getSpecialID() == TARGET ) {
                        drawAssociationKind( g, n1, n2, target );
                    } else {
                        DirectedEdgeFactory.drawAssociation( g, (int) n1.x(),
                                                             (int) n1.y(),
                                                             (int) n2.x(),
                                                             (int) n2.y() );
                    }
                    n1 = n2;
                } catch ( Exception e ) {
                    //ignore
                }
            }
        }
    }

    private void drawAssociationKind( Graphics g, NodeOnEdge n1, NodeOnEdge n2,
                                      int aggregationKind ) {
        // draw the last line segment, as an association, 
        // composition or aggregation
        try {
            // draw association
            switch ( aggregationKind ) {
            case MAggregationKind.NONE:
                DirectedEdgeFactory.drawAssociation( g, (int) n1.x(),
                                                     (int) n1.y(),
                                                     (int) n2.x(),
                                                     (int) n2.y() );
                break;
            case MAggregationKind.AGGREGATION:
                DirectedEdgeFactory.drawAggregation( g, (int) n1.x(),
                                                     (int) n1.y(),
                                                     (int) n2.x(),
                                                     (int) n2.y() );
                break;
            case MAggregationKind.COMPOSITION:
                DirectedEdgeFactory.drawComposition( g, (int) n1.x(),
                                                     (int) n1.y(),
                                                     (int) n2.x(),
                                                     (int) n2.y() );
                break;
            default:
                DirectedEdgeFactory.drawAssociation( g, (int) n1.x(),
                                                     (int) n1.y(),
                                                     (int) n2.x(),
                                                     (int) n2.y() );
                break;
            }
        } catch ( Exception ex ) {
            // ignore
        }
    }

    /**
     * @param g
     * @param fm
     */
    private void drawReflexiveEdge( Graphics g, FontMetrics fm ) {
        updateReflexiveNodes( fm );
        drawEdge( g );
        
        int maxWidth = maxWidth( fm );
        int maxHeight = maxHeight( fm );
        
        g.setColor( fOpt.getEDGE_LABEL_COLOR() );
        if ( fOpt.isShowAssocNames() ) {
            fAssocName.drawEdgePropertyOnReflexiveEdge( g, fm, maxWidth, 
                                                        maxHeight );
        }

        if ( fOpt.isShowRolenames() ) {
            int furthestX = fX1 + maxWidth;
            if ( fX1 < fSource.x() ) {
                furthestX = fX1 - maxWidth;
            }
            fSourceRolename.drawEdgePropertyOnReflexiveEdge( g, fm, maxHeight,
                                                             furthestX );
            fTargetRolename.drawEdgePropertyOnReflexiveEdge( g, fm, maxHeight,
                                                             furthestX );
        }
        
        if ( fOpt.isShowMutliplicities() ) {
            int furthestX = fX1 + maxWidth;
            if ( fX1 < fSource.x() ) {
                furthestX = fX1 - maxWidth;
            }
            fSourceMultiplicity.drawEdgePropertyOnReflexiveEdge( g, fm, 
                                                                 maxHeight,
                                                                 furthestX );
            fTargetMultiplicity.drawEdgePropertyOnReflexiveEdge( g, fm,
                                                                 maxHeight,
                                                                 furthestX );
        }
    }
}

⌨️ 快捷键说明

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