Package de.FeatureModellingTool.GraphicalEditor

Source Code of de.FeatureModellingTool.GraphicalEditor.GroupConstraintFigure

package de.FeatureModellingTool.GraphicalEditor;

import research.AbstractFigure;
import research.NullHandle;
import research.RelativeLocator;
import research.connector.Connector;
import research.connector.BoxConnector;

import javax.swing.*;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.util.Vector;

/**
* Created by IntelliJ IDEA.
* User: saturn
* Date: 2004-5-14
* Time: 10:21:04
* To change this template use File | Settings | File Templates.
*/
public class GroupConstraintFigure extends AbstractFigure implements ConstraintFigure {

    protected static BasicStroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);

    protected static String GroupType = "GroupType";

    public final static String Multi = "Multi";
    public final static String Single = "Single";
    public final static String All = "All";

    protected static String PortPosition = "PortPosition";

    public final static Integer North = new Integer(SwingConstants.NORTH);
    public final static Integer South = new Integer(SwingConstants.SOUTH);
    public final static Integer West = new Integer(SwingConstants.WEST);
    public final static Integer East = new Integer(SwingConstants.EAST);

    protected Vector handles = null;

    public GroupConstraintFigure() {
        this(new Point(0, 0));
    }

    public GroupConstraintFigure(Point position) {
        Rectangle box = new Rectangle(position.x, position.y, 45, 30);
        setAttribute("bounds", box);
        setAttribute("frameColor", new Color(102, 102, 255));
        setAttribute(GroupType, Single);
        setAttribute(PortPosition, East);

        initConnectors();
    }

    protected void initConnectors() {
        connector = new Connector[1];

        Integer portPosition = (Integer) getAttribute(PortPosition);

        int value = portPosition.intValue();

        switch (value) {
            case SwingConstants.NORTH:
                connector[0] = new BoxConnector(this, BoxConnector.NORTH);
                break;
            case SwingConstants.SOUTH:
                connector[0] = new BoxConnector(this, BoxConnector.SOUTH);
                break;
            case SwingConstants.WEST:
                connector[0] = new BoxConnector(this, BoxConnector.WEST);
                break;
            case SwingConstants.EAST:
                connector[0] = new BoxConnector(this, BoxConnector.EAST);
                break;
        }
    }

    public Connector connectorAt(int x, int y) {
        return connector[0];
    }

    public Rectangle getDisplayBox() {
        Rectangle box = (Rectangle) getAttribute("bounds");
        return (Rectangle) box.clone();
    }

    public Rectangle getDisplayBox(Rectangle r) {
        if (r == null)
            r = new Rectangle();

        Rectangle box = (Rectangle) getAttribute("bounds");

        r.setBounds(box);
        return r;
    }


    protected void basicMoveBy(int x, int y) {
        Rectangle box = (Rectangle) getAttribute("bounds");
        box.translate(x, y);
        setAttribute("bounds", box);
    }

    public Vector handles() {

        if (handles == null) {
            handles = new Vector();
            handles.addElement(new NullHandle(this, RelativeLocator.northWest()));
            handles.addElement(new NullHandle(this, RelativeLocator.northEast()));
            handles.addElement(new NullHandle(this, RelativeLocator.southEast()));
            handles.addElement(new NullHandle(this, RelativeLocator.southWest()));
        }

        return (Vector) handles.clone();
    }

    public void basicDisplayBox(Point origin, Point corner) {
        Rectangle box = (Rectangle) getAttribute("bounds");
        box = new Rectangle(origin);
        box.add(corner);
        setAttribute("bounds", box);
    }

    public void drawFrame(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        Stroke oldStroke = g2d.getStroke();

        g2d.setStroke(stroke);

        Rectangle displayBox = getDisplayBox();

        Integer portPosition = (Integer) getAttribute(PortPosition);

        int value = portPosition.intValue();

        Rectangle ovalRect = null;

        Point port = null;
        Point connect = null;

        switch (value) {
            case SwingConstants.NORTH:
                ovalRect = new Rectangle(displayBox.x, displayBox.y + displayBox.height/3, displayBox.width, displayBox.height - displayBox.height/3);
                port = new Point(displayBox.x + displayBox.width/2, displayBox.y);
                connect = new Point(ovalRect.x + ovalRect.width/2, ovalRect.y);
                break;

            case SwingConstants.SOUTH:
                ovalRect = new Rectangle(displayBox.x, displayBox.y, displayBox.width, displayBox.height - displayBox.height/3);
                port = new Point(displayBox.x + displayBox.width/2, displayBox.y + displayBox.height);
                connect = new Point(ovalRect.x + ovalRect.width/2, ovalRect.y + ovalRect.height);
                break;

            case SwingConstants.WEST:
                ovalRect = new Rectangle(displayBox.x + displayBox.width/3, displayBox.y, displayBox.width - displayBox.width/3, displayBox.height);
                port = new Point(displayBox.x, displayBox.y + displayBox.height/2);
                connect = new Point(ovalRect.x, ovalRect.y + ovalRect.height/2);
                break;

            case SwingConstants.EAST:
                ovalRect = new Rectangle(displayBox.x, displayBox.y, displayBox.width - displayBox.width/3, displayBox.height);
                port = new Point(displayBox.x + displayBox.width, displayBox.y + displayBox.height/2);
                connect = new Point(ovalRect.x + ovalRect.width, ovalRect.y + ovalRect.height/2);
                break;
        }

        g.drawOval(ovalRect.x, ovalRect.y, ovalRect.width, ovalRect.height);
        g.drawLine(connect.x, connect.y, port.x, port.y);

        g.drawRect(port.x - 1, port.y - 1, 2, 2);

        FontRenderContext frc = g2d.getFontRenderContext();

        String title = null;

        String type = (String) getAttribute("GroupType");

        if (type.equals(Multi)) title = "M";
        else if (type.equals(Single)) title = "S";
        else if (type.equals(All)) title = "A";
        else title = "X";

        Color textColor = (Color) getAttribute("textColor");
        Color oldC = g.getColor();

        g.setColor(textColor);

        g2d.drawString(title, ovalRect.x + ovalRect.width*2/5, ovalRect.y + ovalRect.height - ovalRect.height/3);

        g.setColor(oldC);

        g2d.setStroke(oldStroke);
    }

}
TOP

Related Classes of de.FeatureModellingTool.GraphicalEditor.GroupConstraintFigure

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.