Package research

Source Code of research.ElbowHandle

/*
* @(#)ElbowHandle.java
*
* Project:    JHotdraw - a GUI framework for technical drawings
*        http://www.jhotdraw.org
*        http://jhotdraw.sourceforge.net
* Copyright:  ? by the original author(s) and all contributors
* License:    Lesser GNU Public License (LGPL)
*        http://www.opensource.org/licenses/lgpl-license.html
*/

package research;

import research.connection.LineConnection;
import research.connection.ElbowConnection;
import research.connector.Connector;

import java.awt.*;
import java.awt.event.InputEvent;

import research.util.Geom;


/**
* A Handle to move an ElbowConnection left/right or up/down.
*
* @version <$CURRENT_VERSION$>
*/
public class ElbowHandle extends AbstractHandle {

    private int fSegment;
    private int fLastX, fLastY;      // previous mouse position

    public ElbowHandle(ElbowConnection owner, int segment) {
        super(owner);
        fSegment = segment;
    }

    public void invokeStart(int x, int y, DrawingView view) {
        fLastX = x;
        fLastY = y;
    }

    public void invokeStep(InputEvent inputEvent, int x, int y, int anchorX, int anchorY, DrawingView view) {
        ElbowConnection line = ownerConnection();

        if ((fSegment == 0) || (fSegment == line.pointCount() - 2)){
           return;
        } else {
            Point p1 = line.pointAt(fSegment);
            Point p2 = line.pointAt(fSegment + 1);
            int ddx = x - fLastX;
            int ddy = y - fLastY;

            boolean isStartVertical = true;

            int displayMode = ((Integer) line.getAttribute(ElbowConnection.DISPLAY_MODE)).intValue();

            if (displayMode == ElbowConnection.N_S_MODE) {
                isStartVertical = true;
            } else if (displayMode == ElbowConnection.W_E_MODE) {
                isStartVertical = false;
            } else {
                int direction = line.getStartConnector().getDirection();

                if (direction == Connector.NORTH_DIRECTION || direction == Connector.SOUTH_DIRECTION) {
                    isStartVertical = true;
                } else if (direction == Connector.WEST_DIRECTION || direction == Connector.EAST_DIRECTION) {
                    isStartVertical = false;
                } else if (direction == Connector.NONE_DIRECTION) {
                    isStartVertical = false;
                }
            }

            boolean isCurrentVertical = isStartVertical;

            if (fSegment % 2 == 1)
                isCurrentVertical = !isStartVertical;

            Point np1;
            Point np2;
            //if (isVertical(p1, p2)) {
            if (isCurrentVertical) {
                int cx = constrainX(p1.x + ddx);
                np1 = new Point(cx, p1.y);
                np2 = new Point(cx, p2.y);
            } else {
                int cy = constrainY(p1.y + ddy);
                np1 = new Point(p1.x, cy);
                np2 = new Point(p2.x, cy);
            }
            line.setPointAt(np1, fSegment);
            line.setPointAt(np2, fSegment + 1);
            fLastX = x;
            fLastY = y;
        }
    }

    public Point locate() {
        LineConnection line = ownerConnection();
        int segment = Math.min(fSegment, line.pointCount() - 2);
        Point p1 = line.pointAt(segment);
        Point p2 = line.pointAt(segment + 1);
        return new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
    }

    public void draw(Graphics g) {
        Rectangle r = displayBox();

        g.setColor(Color.yellow);
        g.fillOval(r.x, r.y, r.width, r.height);

        g.setColor(Color.black);
        g.drawOval(r.x, r.y, r.width, r.height);
    }

    private int constrainX(int x) {
        LineConnection line = ownerConnection();
        Figure startFigure = line.getStartConnector().owner();
        Figure endFigure = line.getEndConnector().owner();
        Rectangle start = startFigure.getDisplayBox();
        Rectangle end = endFigure.getDisplayBox();
        Insets i1 = startFigure.connectionInsets();
        Insets i2 = endFigure.connectionInsets();

        int r1x, r1width, r2x, r2width;
        r1x = start.x + i1.left;
        r1width = start.width - i1.left - i1.right - 1;

        r2x = end.x + i2.left;
        r2width = end.width - i2.left - i2.right - 1;

        if (fSegment == 0) {
            x = Geom.range(r1x, r1x + r1width, x);
        }
        if (fSegment == line.pointCount() - 2) {
            x = Geom.range(r2x, r2x + r2width, x);
        }
        return x;
    }

    private int constrainY(int y) {
        LineConnection line = ownerConnection();
        Figure startFigure = line.getStartConnector().owner();
        Figure endFigure = line.getEndConnector().owner();
        Rectangle start = startFigure.getDisplayBox();
        Rectangle end = endFigure.getDisplayBox();
        Insets i1 = startFigure.connectionInsets();
        Insets i2 = endFigure.connectionInsets();

        int r1y, r1height, r2y, r2height;
        r1y = start.y + i1.top;
        r1height = start.height - i1.top - i1.bottom - 1;
        r2y = end.y + i2.top;
        r2height = end.height - i2.top - i2.bottom - 1;

        if (fSegment == 0) {
            y = Geom.range(r1y, r1y + r1height, y);
        }
        if (fSegment == line.pointCount() - 2) {
            y = Geom.range(r2y, r2y + r2height, y);
        }
        return y;
    }

    private ElbowConnection ownerConnection() {
        return (ElbowConnection) owner();
    }
}
TOP

Related Classes of research.ElbowHandle

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.