Package de.FeatureModellingTool.GraphicalEditor

Source Code of de.FeatureModellingTool.GraphicalEditor.VPConstraintFigure

package de.FeatureModellingTool.GraphicalEditor;

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

import java.awt.*;
import java.util.Vector;

/**
* Created by IntelliJ IDEA.
* User: saturn
* Date: 2004-5-15
* Time: 14:54:46
* To change this template use File | Settings | File Templates.
*/
public class VPConstraintFigure extends AbstractFigure implements ConstraintFigure {
    protected static BasicStroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);

    private static final int TOP = 0;
    private static final int BOTTOM = 1;

    protected static String VPType = "VPType";

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


    protected transient Vector handles = null;

    protected final static int _width = 20;
    protected final static int  _height = 17;

    protected final static int _topLeg = 5;
    protected final static int _botLeg = 10;

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

    public VPConstraintFigure(Point position) {
        Rectangle box = new Rectangle(position.x, position.y, _width, _height + _topLeg + _botLeg);
        setAttribute("bounds", box);
        setAttribute("topLeg", new Integer(_topLeg));
        setAttribute("botLeg", new Integer(_topLeg));
        setAttribute("frameColor", new Color(102, 102, 255));
        setAttribute(VPType, Single);

        initConnectors();
    }

    protected void initConnectors() {
        connector = new Connector[2];
        connector[TOP] = new BoxConnector(this, BoxConnector.NORTH);
        connector[BOTTOM] = new BoxConnector(this, BoxConnector.SOUTH);
    }

    public Connector connectorAt(int x, int y) {
        Rectangle box = (Rectangle) getAttribute("bounds");
        int topLeg = getTopLeg();
        int centerY = box.y + box.height / 2;

        if (y <= centerY)
            return connector[TOP];
        else
            return connector[BOTTOM];
    }

    public Connector getStartConnector(){
        return connector[TOP];
    }

    public Connector getEndConnector(){
        return connector[BOTTOM];   
    }

    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();

        int x0 = displayBox.x + displayBox.width / 2;
        int y0 = displayBox.y;

        int x5 = displayBox.x + displayBox.width / 2;
        int y5 = displayBox.y + displayBox.height;

        displayBox.y += _topLeg;
        displayBox.height -= _topLeg + _botLeg;

        int x1 = displayBox.x + displayBox.width / 2;
        int y1 = displayBox.y;

        int x2 = displayBox.x;
        int y2 = displayBox.y + displayBox.height;

        int x3 = displayBox.x + displayBox.width;
        int y3 = y2;

        int x4 = x1;
        int y4 = y3;

        g2d.drawLine(x0, y0, x1, y1);
        g2d.drawLine(x1, y1, x2, y2);
        g2d.drawLine(x1, y1, x3, y3);
        g2d.drawLine(x2, y2, x3, y3);
        g2d.drawLine(x4, y4, x5, y5);

        g2d.drawRect(x0 - 1, y0 - 1, 2, 2);
        g2d.drawRect(x5 - 1, y5 - 1, 2, 2);

        String vpType = (String)getAttribute(VPType);

        displayBox.x += displayBox.width*3/8;
        displayBox.y += displayBox.height/2;
        displayBox.width = displayBox.width*2/8;
        displayBox.height = displayBox.height*3/8;

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

        g.setColor(textColor);

        if (vpType.equals(Single)){
            draw1(g, displayBox);
        } else if (vpType.equals(Multi)){
            drawV(g, displayBox);
        } else if (vpType.equals(All)){
            drawA(g, displayBox);
        }

        g.setColor(oldC);

        g2d.setStroke(oldStroke);
    }

    protected static void draw1(Graphics g, Rectangle r) {
        g.drawLine(r.x + r.width - r.width / 2, r.y, r.x + r.width - r.width / 2, r.y + r.height);
    }

    protected static void drawV(Graphics g, Rectangle r) {
        g.drawLine(r.x + 1      , r.y, r.x + r.width - r.width / 2, r.y + r.height);
        g.drawLine(r.x + r.width, r.y, r.x + r.width - r.width / 2, r.y + r.height);
    }

    protected static void drawA(Graphics g, Rectangle r) {
        g.drawLine(r.x + r.width - r.width / 2, r.y, r.x + 1      , r.y + r.height);
        g.drawLine(r.x + r.width - r.width / 2, r.y, r.x + r.width, r.y + r.height);
    }

    protected int getTopLeg(){
        Integer topLeg = (Integer) getAttribute("topLeg");
        if (topLeg == null){
            topLeg = new Integer(_topLeg);
            setAttribute("topLeg", topLeg);
        }
        return topLeg.intValue();
    }

    protected int getBotLeg(){
        Integer botLeg = (Integer) getAttribute("botLeg");
        if (botLeg == null){
            botLeg = new Integer(_botLeg);
            setAttribute("botLeg", botLeg);
        }
        return botLeg.intValue();
    }
}
TOP

Related Classes of de.FeatureModellingTool.GraphicalEditor.VPConstraintFigure

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.