Back to index

moin  1.9.0~rc2
DragTracker.java
Go to the documentation of this file.
00001 /*
00002  * @(#)DragTracker.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.standard;
00007 
00008 import java.awt.*;
00009 import java.awt.event.MouseEvent;
00010 import java.util.*;
00011 import CH.ifa.draw.framework.*;
00012 
00019 public class DragTracker extends AbstractTool {
00020 
00021     private Figure  fAnchorFigure;
00022     private int     fLastX, fLastY;      // previous mouse position
00023     private boolean fMoved = false;
00024 
00025     public DragTracker(DrawingView view, Figure anchor) {
00026         super(view);
00027         fAnchorFigure = anchor;
00028     }
00029 
00030     public void mouseDown(MouseEvent e, int x, int y) {
00031         super.mouseDown(e, x, y);
00032         fLastX = x;
00033         fLastY = y;
00034 
00035         if (e.isShiftDown()) {
00036            view().toggleSelection(fAnchorFigure);
00037            fAnchorFigure = null;
00038         } else if (!view().selection().contains(fAnchorFigure)) {
00039             view().clearSelection();
00040             view().addToSelection(fAnchorFigure);
00041         }
00042     }
00043 
00044     public void mouseDrag(MouseEvent e, int x, int y) {
00045         super.mouseDrag(e, x, y);
00046         fMoved = (Math.abs(x - fAnchorX) > 4) || (Math.abs(y - fAnchorY) > 4);
00047 
00048         if (fMoved) {
00049             view().moveSelection(x - fLastX, y - fLastY);
00050         }
00051         fLastX = x;
00052         fLastY = y;
00053     }
00054 }