Package research.tool

Source Code of research.tool.DragTracker

/*
* @(#)DragTracker.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.tool;

import research.Figure;
import research.FigureEnumeration;
import research.FigureEnumerator;
import research.TextHolder;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.Vector;

/**
* DragTracker implements the dragging of the clicked
* figure.
*
* @see SelectionTool
*
* @version <$CURRENT_VERSION$>
*/
public class DragTracker extends Tool {

    protected Figure anchorFigure;

    protected Point anchor = null;

    protected int fLastX, fLastY;      // previous mouse position
    protected boolean fMoved = false;

    protected transient double remainingX = 0;
    protected transient double remainingY = 0;

    public void setFigure(Figure anchorFigure) {
        this.anchorFigure = anchorFigure;
    }

    public void mouseDown(MouseEvent e, int x, int y) {
        super.mouseDown(e, x, y);

        anchor = e.getPoint();

        fLastX = x;
        fLastY = y;

        if (e.isShiftDown()) {
            drawingView.toggleSelection(anchorFigure);
            anchorFigure = null;

            Vector fs = drawingView.getSelection();

            Iterator iterator = fs.iterator();
            while (iterator.hasNext()) {
                Figure f = (Figure) iterator.next();

                if (f instanceof TextHolder) {
                    TextHolder th = (TextHolder) f;

                    if (th.isConnected()) {
                        Figure cf = th.getConnectedFigure();

                        if (fs.contains(cf)) {
                            drawingView.toggleSelection(f);
                        }
                    }

                }
            }

        } else if (!drawingView.isFigureSelected(anchorFigure)) {
            drawingView.setSelection(anchorFigure);
        }

        remainingX = 0;
        remainingY = 0;
    }

    public void mouseDrag(MouseEvent e, int x, int y) {
        super.mouseDrag(e, x, y);
        fMoved = (Math.abs(x - anchor.x) > 4) || (Math.abs(y - anchor.y) > 4);
        //fMoved = true;

        double scale = drawingView.getScale();

        double realDeltaX = (x - fLastX) / scale + remainingX;
        double realDeltaY = (y - fLastY) / scale + remainingY;

        int visualDeltaX = (int) realDeltaX;
        int visualDeltaY = (int) realDeltaY;

        if (fMoved) {
            FigureEnumeration figures = new FigureEnumerator(drawingView.getSelection());
            while (figures.hasMoreElements()) {
                figures.nextFigure().moveBy(visualDeltaX, visualDeltaY);
            }
        }


        fLastX = x;
        fLastY = y;

        remainingX = realDeltaX - visualDeltaX;
        remainingY = realDeltaY - visualDeltaY;
    }

}
TOP

Related Classes of research.tool.DragTracker

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.