FileDocCategorySizeDatePackage
MouseDispatcher.javaAPI DocAndroid 1.5 API15293Wed May 06 22:41:54 BST 2009java.awt

MouseDispatcher

public class MouseDispatcher extends Object
author
Dmitry A. Durnev, Michael Danilov, Pavel Dolgov
version
$Revision$

Fields Summary
private static final int
clickDelta
private final long[]
lastPressTime
private final Point[]
lastPressPos
private final boolean[]
buttonPressed
private final int[]
clickCount
private Component
lastUnderPointer
private final Point
lastScreenPos
private Component
lastUnderMotion
private Point
lastLocalPos
private final java.awt.Dispatcher.MouseGrabManager
mouseGrabManager
private final Toolkit
toolkit
Constructors Summary
MouseDispatcher(java.awt.Dispatcher.MouseGrabManager mouseGrabManager, Toolkit toolkit)

        this.mouseGrabManager = mouseGrabManager;
        this.toolkit = toolkit;
    
Methods Summary
private voidcheckMouseEnterExit(int modifiers, long when)

// BEGIN android-changed: AWT components not supported
//        PointerInfo info = findComponentUnderPointer();
//        Component curUnderPointer =
//                propagateEvent(info, AWTEvent.MOUSE_EVENT_MASK,
//                               MouseListener.class, false).src;
//
//        if (curUnderPointer != lastUnderPointer) {
//            Point pos = info.position;
//            if ((lastUnderPointer != null) &&
//                 lastUnderPointer.isMouseExitedExpected()) {
//
//                Point exitPos = convertPoint(null, lastScreenPos.x,
//                                             lastScreenPos.y, lastUnderPointer);
//
//                postMouseEnterExit(MouseEvent.MOUSE_EXITED, modifiers, when,
//                                   exitPos.x, exitPos.y, lastUnderPointer);
//            }
//            setCursor(curUnderPointer);
//            if (curUnderPointer != null) {
//                postMouseEnterExit(MouseEvent.MOUSE_ENTERED, modifiers, when,
//                                   pos.x, pos.y, curUnderPointer);
//            }
//            lastUnderPointer = curUnderPointer;
//        }
// END android-changed
    
static java.awt.PointconvertPoint(java.awt.Component src, int x, int y, java.awt.Component dest)


              
        Point srcPoint = getAbsLocation(src);
        Point destPoint = getAbsLocation(dest);

        return new Point(x + (srcPoint.x - destPoint.x),
                         y + (srcPoint.y - destPoint.y));
    
static java.awt.PointconvertPoint(java.awt.Component src, java.awt.Point p, java.awt.Component dst)

        return convertPoint(src, p.x, p.y, dst);
    
java.awt.event.MouseWheelEventcreateWheelEvent(java.awt.Component src, org.apache.harmony.awt.wtk.NativeEvent event, java.awt.Point where)


        Integer scrollAmountProperty =
            (Integer)toolkit.getDesktopProperty("awt.wheelScrollingSize"); //$NON-NLS-1$
        int amount = 1;
        int type = MouseWheelEvent.WHEEL_UNIT_SCROLL;

        if (scrollAmountProperty != null) {
            amount = scrollAmountProperty.intValue();
            if (amount == -1) {
                type = MouseWheelEvent.WHEEL_BLOCK_SCROLL;
                amount = 1;
            }
        }
        return new MouseWheelEvent(src, event.getEventId(),
                event.getTime(), event.getInputModifiers(),
                where.x, where.y, 0, false, type, amount,
                event.getWheelRotation());
    
private booleandeltaExceeded(int index, java.awt.MouseDispatcher$PointerInfo info)

        final Point lastPos = lastPressPos[index];
        if (lastPos == null) {
            return true;
        }
        return ((Math.abs(lastPos.x - info.position.x) > clickDelta) ||
                (Math.abs(lastPos.y - info.position.y) > clickDelta));
    
booleandispatch(java.awt.Component src, org.apache.harmony.awt.wtk.NativeEvent event)

        int id = event.getEventId();

        lastScreenPos.setLocation(event.getScreenPos());
        checkMouseEnterExit(event.getInputModifiers(), event.getTime());

        if (id == MouseEvent.MOUSE_WHEEL) {
// BEGIN android-changed: AWT components not supported
//            dispatchWheelEvent(src, event);
// END android-changed
        } else if ((id != MouseEvent.MOUSE_ENTERED) &&
                   (id != MouseEvent.MOUSE_EXITED)) {
            PointerInfo info = new PointerInfo(src, event.getLocalPos());

            mouseGrabManager.preprocessEvent(event);
            findEventSource(info);
            if ((id == MouseEvent.MOUSE_PRESSED) ||
                (id == MouseEvent.MOUSE_RELEASED)) {

                dispatchButtonEvent(info, event);
            } else if ((id == MouseEvent.MOUSE_MOVED) ||
                       (id == MouseEvent.MOUSE_DRAGGED)) {

                dispatchMotionEvent(info, event);
            }
        }

        return false;
    
private voiddispatchButtonEvent(java.awt.MouseDispatcher$PointerInfo info, org.apache.harmony.awt.wtk.NativeEvent event)

        int button = event.getMouseButton();
        long time = event.getTime();
        int id = event.getEventId();
        int index = button - 1;
        boolean clickRequired = false;

        propagateEvent(info, AWTEvent.MOUSE_EVENT_MASK,
                       MouseListener.class, false);
        if (id == MouseEvent.MOUSE_PRESSED) {
            int clickInterval = toolkit.dispatcher.clickInterval;
            mouseGrabManager.onMousePressed(info.src);
            buttonPressed[index] = true;
            clickCount[index] = (!deltaExceeded(index, info) &&
                    ((time - lastPressTime[index]) <= clickInterval)) ?
                    clickCount[index] + 1 : 1;
            lastPressTime[index] = time;
            lastPressPos[index] = info.position;
        } else {
            mouseGrabManager.onMouseReleased(info.src);
            // set cursor back on synthetic mouse grab end:
// BEGIN android-changed: AWT components not supported
//            setCursor(findComponentUnderPointer().src);
// END android-changed
            if (buttonPressed[index]) {
                buttonPressed[index] = false;
                clickRequired = !deltaExceeded(index, info);
            } else {
                clickCount[index] = 0;
            }
        }
        if (info.src.isIndirectlyEnabled()) {
            final Point pos = info.position;
            final int mod = event.getInputModifiers();
            toolkit.getSystemEventQueueImpl().postEvent(
                            new MouseEvent(info.src, id, time, mod, pos.x,
                            pos.y, clickCount[index],
                            event.getTrigger(), button));
            if (clickRequired) {
                toolkit.getSystemEventQueueImpl().postEvent(
                            new MouseEvent(info.src,
                            MouseEvent.MOUSE_CLICKED,
                            time, mod, pos.x, pos.y,
                            clickCount[index], false,
                            button));
            }
        }
    
private voiddispatchMotionEvent(java.awt.MouseDispatcher$PointerInfo info, org.apache.harmony.awt.wtk.NativeEvent event)

        propagateEvent(info, AWTEvent.MOUSE_MOTION_EVENT_MASK,
                       MouseMotionListener.class, false);
        final Point pos = info.position;
        if ((lastUnderMotion != info.src) ||
            !lastLocalPos.equals(pos)) {

            lastUnderMotion = info.src;
            lastLocalPos = pos;

            if (info.src.isIndirectlyEnabled()) {
                toolkit.getSystemEventQueueImpl().postEvent(
                            new MouseEvent(info.src, event.getEventId(),
                            event.getTime(),
                            event.getInputModifiers(),
                            pos.x, pos.y, 0, false));
            }
        }
    
private voidfindEventSource(java.awt.MouseDispatcher$PointerInfo info)

        Component grabOwner = mouseGrabManager.getSyntheticGrabOwner();

        if (grabOwner != null && grabOwner.isShowing()) {
            info.position = convertPoint(info.src, info.position, grabOwner);
            info.src = grabOwner;
        } else {
            //???AWT: rise2TopLevel(info);
            //???AWT: fall2Child(info);
        }
    
private static java.awt.PointgetAbsLocation(java.awt.Component comp)

        Point location = new Point(0, 0);
// BEGIN android-changed: AWT components not supported
//        for (Component parent = comp; parent != null; parent = parent.parent) {
//            Point parentPos = (parent instanceof EmbeddedWindow ?
//                               parent.getNativeWindow().getScreenPos() :
//                               parent.getLocation());
//
//            location.translate(parentPos.x, parentPos.y);
//
//            if (parent instanceof Window) {
//                break;
//            }
//        }
// END android-changed

        return location;
    
java.awt.PointgetPointerPos()

        return lastScreenPos;
    
private voidpostMouseEnterExit(int id, int mod, long when, int x, int y, java.awt.Component comp)

        if (comp.isIndirectlyEnabled()) {
            toolkit.getSystemEventQueueImpl().postEvent(
                    new MouseEvent(comp, id, when, mod, x, y, 0, false));
            comp.setMouseExitedExpected(id == MouseEvent.MOUSE_ENTERED);
        } else {
            comp.setMouseExitedExpected(false);
        }
    
private java.awt.MouseDispatcher$PointerInfopropagateEvent(java.awt.MouseDispatcher$PointerInfo info, long mask, java.lang.Class type, boolean pierceHW)

        Component src = info.src;
        while ((src != null) &&
               (src.isLightweight() || pierceHW) &&
              !(src.isMouseEventEnabled(mask) ||
               (src.getListeners(type).length > 0))) {

            info.position.translate(src.x, src.y);
// BEGIN android-changed: AWT components not supported
//            src = src.parent;
// END android-changed
            info.src = src;
        }

        return info;
    
private voidsetCursor(java.awt.Component comp)

        if (comp == null) {
            return;
        }
        Component grabOwner = mouseGrabManager.getSyntheticGrabOwner();
        Component cursorComp = ((grabOwner != null) &&
                                 grabOwner.isShowing() ? grabOwner : comp);
        cursorComp.setCursor();