I implemented the missing pieces for DnD autoscrolling. This is fairly straightforward. One ugliness is the use of the Swing timer here, but I don't see how we can efficiently do this with the java.util.Timer, since we have this actionPerformed() method in there. And the Swing timer is generally better suited for GUIish timing tasks.
2006-11-24 Roman Kennke <[EMAIL PROTECTED]> * java/awt/dnd/DropTarget.java (DropTargetAutoScroller.HYSTERESIS): New constant. (DropTargetAutoScroller.DELAY): New constant. (DropTargetAutoScroller.inner): New field. A cached Rectangle instance. (DropTargetAutoScroller.outer): New field. A cached Rectangle instance. (DropTargetAutoScroller.timer): New field. The actual timer. (DropTargetAutoScroller.DropTargetAutoScroller): Initialize timer. (DropTargetAutoScroller.actionPerformed): Implemented. (DropTargetAutoScroller.stop): Implemented. (DropTargetAutoScroller.updateLocation): Implemented. (clearAutoscroll): Stop the autoscroller before nullifying it. (createDropTargetAutoScroller): Don't set the field here, only return a new instance. (dragEnter): Only do something when active. Initialize auto scrolling. (dragExit): Only do something when active. Stop auto scrolling. (dragOver): Only do something when active. Update auto scrolling. (drop): Only do something when active. Update auto scrolling. (dropActionChanged): Only do something when active. Update auto scrolling. (initializeAutoScrolling): Check if component is an instance of Autoscroll, otherwise do nothing. (setActive): Disable autoscrolling when deactivating. (setComponent): When component is set to null, disable autoscrolling. /Roman
Index: java/awt/dnd/DropTarget.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/awt/dnd/DropTarget.java,v retrieving revision 1.16 diff -u -1 -5 -r1.16 DropTarget.java --- java/awt/dnd/DropTarget.java 2 Aug 2006 19:53:58 -0000 1.16 +++ java/awt/dnd/DropTarget.java 24 Nov 2006 10:19:39 -0000 @@ -26,86 +26,145 @@ As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this 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; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.peer.ComponentPeer; import java.awt.peer.LightweightPeer; import java.io.Serializable; import java.util.EventListener; import java.util.TooManyListenersException; +import javax.swing.Timer; + /** * @author Michael Koch * @since 1.2 */ public class DropTarget implements DropTargetListener, EventListener, Serializable { /** * Compatible with JDK 1.2+ */ private static final long serialVersionUID = -6283860791671019047L; 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); } } private Component component; private FlavorMap flavorMap; private int actions; private DropTargetPeer peer; private DropTargetContext dropTargetContext; private DropTargetListener dropTargetListener; private DropTarget.DropTargetAutoScroller autoscroller; private boolean active = true; /** * Creates a <code>DropTarget</code> object. * @@ -170,60 +229,64 @@ flavorMap = SystemFlavorMap.getDefaultFlavorMap(); else flavorMap = fm; setActive (b); if (c != null) c.setDropTarget(this); } /** * Sets the component associated with this drop target object. */ public void setComponent (Component c) { + if (component != null) + clearAutoscroll(); component = c; } /** * Returns the component associated with this drop target object. */ public Component getComponent () { return component; } /** * Sets the default actions. */ public void setDefaultActions (int ops) { actions = ops; } /** * Returns the default actions. */ public int getDefaultActions () { return actions; } public void setActive (boolean active) { this.active = active; + if (! active) + clearAutoscroll(); } public boolean isActive() { return active; } /** * Adds a new <code>DropTargetListener</code>. * * @exception TooManyListenersException Sun's JDK does not, despite * documentation, throw this exception here when you install an additional * <code>DropTargetListener</code>. So to be compatible, we do the same * thing. */ @@ -238,54 +301,71 @@ if (dropTargetListener != null) throw new TooManyListenersException(); dropTargetListener = dtl; } public void removeDropTargetListener(DropTargetListener dtl) { if (dropTargetListener != null) dropTargetListener = null; } 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); } public FlavorMap getFlavorMap() { return flavorMap; } public void setFlavorMap(FlavorMap fm) { flavorMap = fm; } public void addNotify(ComponentPeer p) @@ -320,37 +400,39 @@ return dropTargetContext; } protected DropTargetContext createDropTargetContext() { if (dropTargetContext == null) dropTargetContext = new DropTargetContext (this); return dropTargetContext; } 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) { if (autoscroller != null) autoscroller.updateLocation(dragCursorLocn); } protected void clearAutoscroll() { - autoscroller = null; + if (autoscroller != null) + { + autoscroller.stop(); + autoscroller = null; + } } } // class DropTarget