FileDocCategorySizeDatePackage
DragRecognitionSupport.javaAPI DocJava SE 5 API5280Fri Aug 26 14:58:06 BST 2005javax.swing.plaf.basic

DragRecognitionSupport

public class DragRecognitionSupport extends Object
Drag gesture recognition support for classes that have a TransferHandler. The gesture for a drag in this class is a mouse press followed by movement by DragSource.getDragThreshold() pixels. An instance of this class is maintained per AppContext, and the public static methods call into the appropriate instance.
author
Shannon Hickey
version
1.1 05/02/05

Fields Summary
private int
motionThreshold
private MouseEvent
dndArmedEvent
private JComponent
component
Constructors Summary
Methods Summary
private voidclearState()

        dndArmedEvent = null;
        component = null;
    
private static javax.swing.plaf.basic.DragRecognitionSupportgetDragRecognitionSupport()
Returns the DragRecognitionSupport for the caller's AppContext.

        DragRecognitionSupport support =
            (DragRecognitionSupport)AppContext.getAppContext().
                get(DragRecognitionSupport.class);

        if (support == null) {
            support = new DragRecognitionSupport();
            AppContext.getAppContext().put(DragRecognitionSupport.class, support);
        }

        return support;
    
private intmapDragOperationFromModifiers(java.awt.event.MouseEvent me, javax.swing.TransferHandler th)


        if (th == null || !SwingUtilities.isLeftMouseButton(me)) {
            return TransferHandler.NONE;
        }

        return SunDragSourceContextPeer.
            convertModifiersToDropAction(me.getModifiersEx(),
                                         th.getSourceActions(component));
    
public static booleanmouseDragged(java.awt.event.MouseEvent me, javax.swing.plaf.basic.DragRecognitionSupport$BeforeDrag bd)
Returns whether or not a drag gesture recognition is ongoing.

        return ((DragRecognitionSupport)getDragRecognitionSupport()).
            mouseDraggedImpl(me, bd);
    
private booleanmouseDraggedImpl(java.awt.event.MouseEvent me, javax.swing.plaf.basic.DragRecognitionSupport$BeforeDrag bd)
Returns whether or not a drag gesture recognition is ongoing.

        /* no recognition is in progress */
        if (dndArmedEvent == null) {
            return false;
        }

        /* component has changed unexpectedly, so bail */
        if (me.getSource() != component) {
            clearState();
            return false;
        }

        int dx = Math.abs(me.getX() - dndArmedEvent.getX());
        int dy = Math.abs(me.getY() - dndArmedEvent.getY());
        if ((dx > motionThreshold) || (dy > motionThreshold)) {
            TransferHandler th = component.getTransferHandler();
            int action = mapDragOperationFromModifiers(me, th);
            if (action != TransferHandler.NONE) {
                /* notify the BeforeDrag instance */
                if (bd != null) {
                    bd.dragStarting(dndArmedEvent);
                }
                th.exportAsDrag(component, dndArmedEvent, action);
                clearState();
            }
        }

        return true;
    
public static booleanmousePressed(java.awt.event.MouseEvent me)
Returns whether or not the event is potentially part of a drag sequence.

        return ((DragRecognitionSupport)getDragRecognitionSupport()).
            mousePressedImpl(me);
    
private booleanmousePressedImpl(java.awt.event.MouseEvent me)
Returns whether or not the event is potentially part of a drag sequence.

        component = (JComponent)me.getSource();

        if (mapDragOperationFromModifiers(me, component.getTransferHandler())
                != TransferHandler.NONE) {

            motionThreshold = DragSource.getDragThreshold();
            dndArmedEvent = me;
            return true;
        }

        clearState();
        return false;
    
public static java.awt.event.MouseEventmouseReleased(java.awt.event.MouseEvent me)
If a dnd recognition has been going on, return the MouseEvent that started the recognition. Otherwise, return null.

        return ((DragRecognitionSupport)getDragRecognitionSupport()).
            mouseReleasedImpl(me);
    
private java.awt.event.MouseEventmouseReleasedImpl(java.awt.event.MouseEvent me)
If a dnd recognition has been going on, return the MouseEvent that started the recognition. Otherwise, return null.

        /* no recognition has been going on */
        if (dndArmedEvent == null) {
            return null;
        }

        MouseEvent retEvent = null;

        if (me.getSource() == component) {
            retEvent = dndArmedEvent;
        } // else component has changed unexpectedly, so return null

        clearState();
        return retEvent;