diff options
Diffstat (limited to 'java/awt/dnd/DropTarget.java')
-rw-r--r-- | java/awt/dnd/DropTarget.java | 124 |
1 files changed, 103 insertions, 21 deletions
diff --git a/java/awt/dnd/DropTarget.java b/java/awt/dnd/DropTarget.java index 1e7b2c4cd..63be5ac04 100644 --- a/java/awt/dnd/DropTarget.java +++ b/java/awt/dnd/DropTarget.java @@ -38,12 +38,12 @@ exception statement from your version. */ package java.awt.dnd; -import gnu.classpath.NotImplementedException; - import java.awt.Component; import java.awt.GraphicsEnvironment; import java.awt.HeadlessException; +import java.awt.Insets; import java.awt.Point; +import java.awt.Rectangle; import java.awt.datatransfer.FlavorMap; import java.awt.datatransfer.SystemFlavorMap; import java.awt.dnd.peer.DropTargetPeer; @@ -55,6 +55,8 @@ import java.io.Serializable; import java.util.EventListener; import java.util.TooManyListenersException; +import javax.swing.Timer; + /** * @author Michael Koch * @since 1.2 @@ -70,30 +72,87 @@ public class DropTarget protected static class DropTargetAutoScroller implements ActionListener { + /** + * The threshold that keeps the autoscroller running. + */ + private static final int HYSTERESIS = 10; + + /** + * The initial timer delay. + */ + private static final int DELAY = 100; + private Component component; private Point point; - + + /** + * The timer that triggers autoscrolling. + */ + private Timer timer; + + /** + * The outer region of the scroller. This is the component's size. + */ + private Rectangle outer; + + /** + * The inner region of the scroller. This is the component size without + * the autoscroll insets. + */ + private Rectangle inner; + protected DropTargetAutoScroller (Component c, Point p) { component = c; point = p; + timer = new Timer(DELAY, this); + timer.setCoalesce(true); + timer.start(); } protected void updateLocation (Point newLocn) { + Point previous = point; point = newLocn; + if (Math.abs(point.x - previous.x) > HYSTERESIS + || Math.abs(point.y - previous.y) > HYSTERESIS) + { + if (timer.isRunning()) + timer.stop(); + } + else + { + if (! timer.isRunning()) + timer.start(); + } } protected void stop () - throws NotImplementedException { - // FIXME: implement this + timer.start(); } public void actionPerformed (ActionEvent e) - throws NotImplementedException { - // FIXME: implement this + Autoscroll autoScroll = (Autoscroll) component; + + // First synchronize the inner and outer rectangles. + Insets i = autoScroll.getAutoscrollInsets(); + int width = component.getWidth(); + int height = component.getHeight(); + if (width != outer.width || height != outer.height) + outer.setBounds(0, 0, width, height); + if (inner.x != i.left || inner.y != i.top) + inner.setLocation(i.left, i.top); + int inWidth = width - i.left - i.right; + int inHeight = height - i.top - i.bottom; + if (inWidth != inner.width || inHeight != inner.height) + inner.setSize(inWidth, inHeight); + + // Scroll if the outer rectangle contains the location, but the + // inner doesn't. + if (outer.contains(point) && ! inner.contains(point)) + autoScroll.autoscroll(point); } } @@ -182,6 +241,8 @@ public class DropTarget */ public void setComponent (Component c) { + if (component != null) + clearAutoscroll(); component = c; } @@ -212,6 +273,8 @@ public class DropTarget public void setActive (boolean active) { this.active = active; + if (! active) + clearAutoscroll(); } public boolean isActive() @@ -250,30 +313,47 @@ public class DropTarget public void dragEnter(DropTargetDragEvent dtde) { - if (dropTargetListener != null) - dropTargetListener.dragEnter(dtde); + if (active) + { + if (dropTargetListener != null) + dropTargetListener.dragEnter(dtde); + initializeAutoscrolling(dtde.getLocation()); + } } public void dragOver(DropTargetDragEvent dtde) { - if (dropTargetListener != null) - dropTargetListener.dragOver(dtde); + if (active) + { + if (dropTargetListener != null) + dropTargetListener.dragOver(dtde); + updateAutoscroll(dtde.getLocation()); + } } public void dropActionChanged(DropTargetDragEvent dtde) { - if (dropTargetListener != null) - dropTargetListener.dropActionChanged(dtde); + if (active) + { + if (dropTargetListener != null) + dropTargetListener.dropActionChanged(dtde); + updateAutoscroll(dtde.getLocation()); + } } public void dragExit(DropTargetEvent dte) { - if (dropTargetListener != null) - dropTargetListener.dragExit(dte); + if (active) + { + if (dropTargetListener != null) + dropTargetListener.dragExit(dte); + clearAutoscroll(); + } } public void drop(DropTargetDropEvent dtde) { + clearAutoscroll(); if (dropTargetListener != null) dropTargetListener.drop(dtde); } @@ -332,15 +412,13 @@ public class DropTarget protected DropTarget.DropTargetAutoScroller createDropTargetAutoScroller (Component c, Point p) { - if (autoscroller == null) - autoscroller = new DropTarget.DropTargetAutoScroller (c, p); - - return autoscroller; + return new DropTarget.DropTargetAutoScroller (c, p); } protected void initializeAutoscrolling(Point p) { - createDropTargetAutoScroller (component, p); + if (component instanceof Autoscroll) // Checks for null too. + autoscroller = createDropTargetAutoScroller (component, p); } protected void updateAutoscroll(Point dragCursorLocn) @@ -351,6 +429,10 @@ public class DropTarget protected void clearAutoscroll() { - autoscroller = null; + if (autoscroller != null) + { + autoscroller.stop(); + autoscroller = null; + } } } // class DropTarget |