JAVA MouseEvent实现纪要

 

JAVA MouseEvent实现纪要

Jre1.7鼠标事件以MouseEvent类封装。共有如下8种:

/**

     *The"mouseclicked"event.This<code>MouseEvent</code>

     *occurswhenamousebuttonispressedandreleased.

     */

    publicstaticfinalintMOUSE_CLICKED = MOUSE_FIRST;

    /**

     *The"mousepressed"event.This<code>MouseEvent</code>

     *occurswhenamousebuttonispusheddown.

     */

    publicstaticfinalintMOUSE_PRESSED = 1 + MOUSE_FIRST; //Event.MOUSE_DOWN

    /**

     *The"mousereleased"event.This<code>MouseEvent</code>

     *occurswhenamousebuttonisletup.

     */

    publicstaticfinalintMOUSE_RELEASED = 2 + MOUSE_FIRST; //Event.MOUSE_UP

    /**

     *The"mousemoved"event.This<code>MouseEvent</code>

     *occurswhenthemousepositionchanges.

     */

    publicstaticfinalintMOUSE_MOVED = 3 + MOUSE_FIRST; //Event.MOUSE_MOVE

    /**

     *The"mouseentered"event.This<code>MouseEvent</code>

     *occurswhenthemousecursorenterstheunobscuredpartofcomponent's

     *geometry.

     */

    publicstaticfinalintMOUSE_ENTERED = 4 + MOUSE_FIRST; //Event.MOUSE_ENTER

    /**

     *The"mouseexited"event.This<code>MouseEvent</code>

     *occurswhenthemousecursorexitstheunobscuredpartofcomponent's

     *geometry.

     */

    publicstaticfinalintMOUSE_EXITED = 5 + MOUSE_FIRST; //Event.MOUSE_EXIT

    /**

     *The"mousedragged"event.This<code>MouseEvent</code>

     *occurswhenthemousepositionchangeswhileamousebuttonispressed.

     */

    publicstaticfinalintMOUSE_DRAGGED = 6 + MOUSE_FIRST; //Event.MOUSE_DRAG

    /**

     *The"mousewheel"event. Thisistheonly<code>MouseWheelEvent</code>.

     *Itoccurswhenamouseequippedwithawheelhasitswheelrotated.

     *@since1.4

     */

publicstaticfinalintMOUSE_WHEEL = 7 + MOUSE_FIRST;

鼠标事件由AWT-Windows线程从底层系统获取到并FlushEDT进行处理。EDT在处理时按一下步骤进行:

1. EventDispatchThread中执行SunDragSourceContextPeer.checkEvent。其功能含义为拖拽开始时将忽略掉当时还在Post Event Queue等待处理的单纯鼠标事件。虽然MouseEvent和拖拽的事件有着千丝万缕的联系,但是在拖拽过程中并不会影响MouseEvent的dispatch.

此前对java拖拽的实现分析已经知道,拖拽开始后AWT-loop到的底层事件将由一个DragSourcePeer在底层处理后直接包装成DragSourceEvent发布到EDT中进行处理,同时由一个DropTargetPeer在底层处理后包装成SunDropTargetEvent发布到EDT中进行处理。SunDropTargetEventMouseEvent的子类,而DragSourceEvent就不是。这里面的原因是因为拖拽过程中系统只有一个DragSource,因此DragSourceEventtarget一定是此DragSource组件,只要在拖拽开始时通过一个全局变量记录下来,在封装过程source=DragSourceEvent直接定位到组件即可,但是同一个拖拽过程却可能面临多个DropTarget,而且最关键的是就此底层事件底层系统只能给出对重量级组件的定位,因此对于SunDropTargetEvent需要经过类似MouseEventretarget过程确定目标组件,所以SunDropTargetEvent定义为MouseEvent的子类。当然SunDropTargetEvent在经过retarget后再形成具有准确目标组件的DropTargetEvent就和DragSourceEvent一样不再是MouseEvent的子类了。可见,SunDropTargetEvent可以看作是一个中间过渡事件,该类事件存在的主要意义就是借助EDTMouseEvent的统一retarget过程;也因而在EventQueue,EventDispatchThread类的处理中特别针对该事件有特殊处理。

正因为SunDropTargetEvent的存在,checkEvent这个方法的实现才是忽略单纯的鼠标事件,但不能忽略了SunDropTargetEvent

 

if (discardingMouseEvents && event instanceof MouseEvent) {

              MouseEvent mouseEvent = (MouseEvent) event;

              if (!(mouseEvent instanceof SunDropTargetEvent)) {

                   returnfalse;

              }

         }

     returntrue;

2. EventQueue((Component)src).dispatchEvent(event);而在ComponentdispatchEventImpl。如果该组件是一个Container,将进入Container. dispatchEventImpl,该方法将通过Container. LightweightDispatcher进行dispatchEvent,其主要功能就是要retarget及在之后针对Mouse_Move updateCursor.

Awt-windows loop到的Mouse Event只能是针对重量级组件的,如果本次点击是在某重量级组件比如JFRAME的一个JTEXTFIELD上点击,需要通过这个过程将此Mouse Event定位source=JTEXTFIELD;同时可能一个重量级组件的MouseMove对应其包含的两个轻量级组件的exitenter;所有这些具体retargetprocessMouseEvent中完成;完成逻辑主要根据鼠标事件的坐标和container的子组件记录,以及组件提供的publicboolean contains(int x, int y) 方法去寻找最顶层包含此坐标的子组件;除了找到该组件外,再根据一个targetLastEntered域变量比较是否发生了变化,如果是则要产生ExitEnter.

3. Retargeted Mouse Event将进入Component. dispatchEventImpl.

A.如果是SunDropTargetEvent将处理掉并返回。

B.MOUSE_WHEEL将由peer处理后可能被dispatchMouseWheelToAncestor,即交由该组件的合适的容器组件处理。

C.Allow the Toolkit to pass this to AWTEventListeners,即交给Toolkit注册listener处理。

D.newEventsOnly&& eventEnabled则交给该组件对应listener处理。

E.newEventsOnly will be false for a listenerless ScrollPane, but

MouseWheelEvents still need to be dispatched to it so scrolling

can be done. autoProcessMouseWheel方法提供了一个处理鼠标轮滚动的切入点,即如果scrollpane没有注册任何监听导致newEventsOnly =false,也可以实现autoProcessMouseWheel方法来实现通用的scroll。

你可能感兴趣的:(JAVA MouseEvent实现纪要)