Skip to content

suggestion for improvement in ACLineData.java #9

Open
@SophiaLi20

Description

@SophiaLi20

we can improve the agileuml
/ACLineData.java code by addinf following features -
Remove Redundant Overloaded Methods
Use Constants for Magic Numbers
Improve Code Readability

the code is

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class ACLineData extends LineData {
    private static final int DASHED_LINE_OFFSET = 25;
    private static final int TEXT_PADDING = 5;
    private static final int ROLE_OFFSET = 8;

    private RectData myclass;

    private static final BasicStroke SOLID_STROKE = stroke; 

    public ACLineData(int xs, int ys, int xe, int ye, int lineCount, int type, RectData rd) {
        super(xs, ys, xe, ye, lineCount, type);
        this.myclass = rd;
    }

    @Override
    public Object clone() {
        int count = parseLabelToCount();
        ACLineData cloned = new ACLineData(xstart, ystart, xend, yend, count, linetype, myclass);
        cloned.setTransition(transition);
        cloned.setFlow(flow);
        cloned.setModelElement(modelElement);
        return cloned;
    }

    private int parseLabelToCount() {
        try {
            String countStr = label.substring(1, label.length() - 1);
            return Integer.parseInt(countStr);
        } catch (Exception e) {
            return 0;
        }
    }

    private void drawClass(Graphics2D g) {
        int midX = (xstart + xend) / 2;
        int midY = (ystart + yend) / 2;

        g.setStroke(dashed);
        g.drawLine(midX, midY, midX, midY + DASHED_LINE_OFFSET);
        g.setStroke(SOLID_STROKE);

        if (myclass != null) {
            myclass.changePosition(0, 0, midX - 10, midY + DASHED_LINE_OFFSET);
            myclass.drawData(g);
        }
    }

    @Override
    void drawData(Graphics2D g) {
        if (LineLength() > 5) {
            g.setColor(Color.BLACK);
            drawLine(g);
            drawArrow(g);
            drawLabels(g);
            if (myclass != null) {
                drawClass(g);
            }
        }
    }

    private void drawLine(Graphics2D g) {
        g.setStroke(linetype == 1 ? dashed : SOLID_STROKE);
        g.drawLine(xstart, ystart, xend, yend);
        g.setStroke(stroke);
    }

    private void drawArrow(Graphics2D g) {
        g.draw(new Line2D.Double(arrow1x, arrow1y, xend, yend));
        g.draw(new Line2D.Double(arrow2x, arrow2y, xend, yend));
    }

    private void drawLabels(Graphics2D g) {
        if (modelElement instanceof Association association) {
            String role1 = association.getRole1();
            String role2 = getRole2(association);
            drawString(g, role1, xstart + TEXT_PADDING, ystart - TEXT_PADDING);
            drawString(g, role2, (int) role2x, (int) role2y);

            String card1 = ModelElement.convertCard(association.getCard1());
            String card2 = ModelElement.convertCard(association.getCard2());
            drawString(g, card1, xstart + TEXT_PADDING, ystart + ROLE_OFFSET);
            drawString(g, card2, (int) card2x, (int) card2y);

            String associationName = association.getName();
            drawString(g, associationName, (xstart + xend) / 2, (ystart + yend) / 2);
        }
    }

    private String getRole2(Association association) {
        String role2 = association.getRole2();
        if (association.isOrdered()) role2 += " { ordered }";
        if (association.isFrozen()) role2 += " { frozen }";
        else if (association.isAddOnly()) role2 += " { addOnly }";
        if (association.isDerived()) role2 = "/" + role2;
        return role2;
    }

    private void drawString(Graphics g, String text, int x, int y) {
        if (text != null) {
            g.drawString(text, x, y);
        }
    }
}

This will mainly help in -

  • Readability: Cleaner, modular code.
  • Maintainability: Easier to modify or extend in the future.
  • Safety: Reduced null pointer and type safety issues.
  • Consistency: Unified logic for graphics operations

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions