Package research

Source Code of research.ElbowHandle2

package research;

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

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

import research.util.Geom;

/**
* Created by IntelliJ IDEA.
* User: guanly
* Date: 2004-6-22
* Time: 1:00:59
* To change this template use File | Settings | File Templates.
*/
public class ElbowHandle2 extends AbstractHandle {

    private int fSegment;
    private int fLastX, fLastY;      // previous mouse position
    private transient Point initStartOffset;
    private transient Point initEndOffset;

    public ElbowHandle2(ElbowConnection2 owner, int segment) {
        super(owner);
        fSegment = segment;
    }

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

        ElbowConnection2 line = ownerConnection();

        initStartOffset = (Point) line.getAttribute(ElbowConnection2.START_OFFSET);
        if (initStartOffset == null)
            initStartOffset = new Point(0, 0);
        else {
            initStartOffset = new Point(initStartOffset);
        }

        initEndOffset = (Point) line.getAttribute(ElbowConnection2.END_OFFSET);
        if (initEndOffset == null)
            initEndOffset = new Point(0, 0);
        else {
            initEndOffset = new Point(initEndOffset);
        }

    }

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

        if (fSegment == 0) {
            if ((inputEvent != null) && (inputEvent.isShiftDown())) {//������ʼ�˵����ӷ�ʽ(H <-> V)
                Point p1 = line.pointAt(fSegment);
                line.insertPointAt(new Point(p1), fSegment + 1);

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

                if (displayMode == ElbowConnection2.V_V_MODE) {
                    line.setAttribute(ElbowConnection2.DISPLAY_MODE, new Integer(ElbowConnection2.H_V_MODE));
                } else if (displayMode == ElbowConnection2.H_H_MODE) {
                    line.setAttribute(ElbowConnection2.DISPLAY_MODE, new Integer(ElbowConnection2.V_H_MODE));
                } else if (displayMode == ElbowConnection2.H_V_MODE) {
                    line.setAttribute(ElbowConnection2.DISPLAY_MODE, new Integer(ElbowConnection2.V_V_MODE));
                } else if (displayMode == ElbowConnection2.V_H_MODE) {
                    line.setAttribute(ElbowConnection2.DISPLAY_MODE, new Integer(ElbowConnection2.H_H_MODE));
                } else {
                    //should not go here
                    System.out.println("ElbowHandle2: should not go here");
                }

                fSegment = 1;
            } else if ((inputEvent != null) && (inputEvent.isControlDown())) {//�ı俪ʼ������������ӵ�ƫ��
                int displayMode = ((Integer) line.getAttribute(ElbowConnection2.DISPLAY_MODE)).intValue();
                Point startOffset = (Point) line.getAttribute(ElbowConnection2.START_OFFSET);
                if (startOffset == null)
                    startOffset = new Point(0, 0);

                if (displayMode == ElbowConnection2.V_V_MODE || displayMode == ElbowConnection2.V_H_MODE) {
                    startOffset.x = x - anchorX + initStartOffset.x;
                    line.setAttribute(ElbowConnection2.START_OFFSET, startOffset);
                    line.layoutConnection();
                } else if (displayMode == ElbowConnection2.H_H_MODE || displayMode == ElbowConnection2.H_V_MODE) {
                    startOffset.y = y - anchorY + initStartOffset.y;
                    line.setAttribute(ElbowConnection2.START_OFFSET, startOffset);
                    line.layoutConnection();
                } else {
                    //should not go here
                    System.out.println("ElbowHandle2: should not go here");
                }
            } else {//���ѿ�ʼ��
                Point p1 = line.pointAt(fSegment);
                Point p2 = line.pointAt(fSegment + 1);
                int _x = (p1.x + p2.x) / 2;
                int _y = (p1.y + p2.y) / 2;
                line.insertPointAt(new Point(_x, _y), fSegment + 1);
                line.insertPointAt(new Point(_x, _y), fSegment + 1);

                fSegment = 2;
            }
        } else if (fSegment == line.pointCount() - 2) {//����ĩ�˵����ӷ�ʽ(H <-> V)
            if ((inputEvent != null) && (inputEvent.isShiftDown())) {
                Point p2 = line.pointAt(fSegment + 1);
                line.insertPointAt(new Point(p2), fSegment + 1);

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

                if (displayMode == ElbowConnection2.V_V_MODE) {
                    line.setAttribute(ElbowConnection2.DISPLAY_MODE, new Integer(ElbowConnection2.V_H_MODE));
                } else if (displayMode == ElbowConnection2.H_H_MODE) {
                    line.setAttribute(ElbowConnection2.DISPLAY_MODE, new Integer(ElbowConnection2.H_V_MODE));
                } else if (displayMode == ElbowConnection2.H_V_MODE) {
                    line.setAttribute(ElbowConnection2.DISPLAY_MODE, new Integer(ElbowConnection2.H_H_MODE));
                } else if (displayMode == ElbowConnection2.V_H_MODE) {
                    line.setAttribute(ElbowConnection2.DISPLAY_MODE, new Integer(ElbowConnection2.V_V_MODE));
                } else {
                    //should not go here
                    System.out.println("ElbowHandle2: should not go here");
                }

            } else if ((inputEvent != null) && (inputEvent.isControlDown())) {//�ı�ĩ������������ӵ�ƫ��
                int displayMode = ((Integer) line.getAttribute(ElbowConnection2.DISPLAY_MODE)).intValue();
                Point endOffset = (Point) line.getAttribute(ElbowConnection2.END_OFFSET);
                if (endOffset == null)
                    endOffset = new Point(0, 0);

                if (displayMode == ElbowConnection2.V_V_MODE || displayMode == ElbowConnection2.H_V_MODE) {
                    endOffset.x = x - anchorX + initEndOffset.x;
                    line.setAttribute(ElbowConnection2.END_OFFSET, endOffset);
                    line.layoutConnection();
                } else if (displayMode == ElbowConnection2.V_H_MODE || displayMode == ElbowConnection2.H_H_MODE) {
                    endOffset.y = y - anchorY + initEndOffset.y;
                    line.setAttribute(ElbowConnection2.END_OFFSET, endOffset);
                    line.layoutConnection();
                } else {
                    //should not go here
                    System.out.println("ElbowHandle2: should not go here");
                }
            } else {//����ĩ��
                Point p1 = line.pointAt(fSegment);
                Point p2 = line.pointAt(fSegment + 1);
                int _x = (p1.x + p2.x) / 2;
                int _y = (p1.y + p2.y) / 2;
                line.insertPointAt(new Point(_x, _y), fSegment + 1);
                line.insertPointAt(new Point(_x, _y), fSegment + 1);
            }
        } else {
            Point p1 = line.pointAt(fSegment);
            Point p2 = line.pointAt(fSegment + 1);
            int ddx = x - fLastX;
            int ddy = y - fLastY;

            boolean _isCurrentVertical = isCurrentVertical();

            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 void invokeEnd(int x, int y, int anchorX, int anchorY, DrawingView view) {
        ElbowConnection2 line = ownerConnection();

        if (line.pointCount() <= 4) return;

        boolean _isCurrentVertical = isCurrentVertical();

        Point p1 = line.pointAt(fSegment);
        Point p2 = line.pointAt(fSegment + 1);

        boolean hasJointed = false;

        if (fSegment >= 2) {
            Point p0 = line.pointAt(fSegment - 1);
            Point ps = line.pointAt(fSegment - 2);

            if (_isCurrentVertical) {
                if (Math.abs(ps.x - p2.x) < 3) {
                    p2.x = ps.x;
                    line.setPointAt(p2, fSegment + 1);

                    line.removePointAt(fSegment - 1);
                    line.removePointAt(fSegment - 1);

                    hasJointed = true;
                }
            } else {
                if (Math.abs(ps.y - p2.y) < 3) {
                    p2.y = ps.y;
                    line.setPointAt(p2, fSegment + 1);

                    line.removePointAt(fSegment - 1);
                    line.removePointAt(fSegment - 1);

                    hasJointed = true;
                }
            }
        }

        if (!hasJointed && fSegment <= line.pointCount() - 4) {
            Point p3 = line.pointAt(fSegment + 2);
            Point pe = line.pointAt(fSegment + 3);

            if (_isCurrentVertical) {
                if (Math.abs(pe.x - p1.x) < 3) {
                    p1.x = pe.x;
                    line.setPointAt(p1, fSegment);

                    line.removePointAt(fSegment + 1);
                    line.removePointAt(fSegment + 1);

                    hasJointed = true;
                }
            } else {
                if (Math.abs(pe.y - p1.y) < 3) {
                    p1.y = pe.y;
                    line.setPointAt(p1, fSegment);

                    line.removePointAt(fSegment + 1);
                    line.removePointAt(fSegment + 1);

                    hasJointed = true;
                }
            }
        }

    }

    private boolean isCurrentVertical() {

        ElbowConnection2 line = ownerConnection();
        boolean _isStartVertical = true;

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

        if (displayMode == ElbowConnection2.V_V_MODE) {
            _isStartVertical = true;
        } else if (displayMode == ElbowConnection2.H_H_MODE) {
            _isStartVertical = false;
        } else if (displayMode == ElbowConnection2.H_V_MODE) {
            _isStartVertical = false;
        } else if (displayMode == ElbowConnection2.V_H_MODE) {
            _isStartVertical = true;
        } 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;

        return _isCurrentVertical;
    }

    /**
     * private boolean isVertical(Point p1, Point p2) {
     * return p1.x == p2.x;
     * }
     */

    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 ElbowConnection2 ownerConnection() {
        return (ElbowConnection2) owner();
    }
}
TOP

Related Classes of research.ElbowHandle2

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.