This fixes a small issue that I had with LightweightDispatcher some time ago and makes some other things more elegant.

2006-09-18  Roman Kennke  <[EMAIL PROTECTED]>

        * java/awt/LightweightDispatcher.java
        (findTarget): Correctly translate child coordinates.
        Use Component.eventTypeEnabled() for checking if a component
        has a certain event enabled.
        (handleMouseEvent): Find the correct mouse event target.
        Use Component.eventTypeEnabled() for checking if a component
        has a certain event enabled.

/Roman
Index: java/awt/LightweightDispatcher.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/awt/LightweightDispatcher.java,v
retrieving revision 1.14
diff -u -1 -5 -r1.14 LightweightDispatcher.java
--- java/awt/LightweightDispatcher.java	25 Jul 2006 09:48:55 -0000	1.14
+++ java/awt/LightweightDispatcher.java	18 Sep 2006 13:44:56 -0000
@@ -134,78 +134,73 @@
   {
     Window window = (Window) ev.getSource();
     // Find the target for the mouse event. We first seach the deepest
     // component at the specified location. The we go up to its parent and
     // try to find a neighbor of the deepest component that is suitable as
     // mouse event target (it must be showing, at that location and have either
     // a MouseListener or MouseMotionListener installed). If no such component
     // is found, then we walk up the container hierarchy and find the next
     // container that has a MouseListener or MouseMotionListener installed.
     Component deepest = window.findComponentAt(ev.getX(), ev.getY());
     if (deepest == null)
       return false;
     Container parent = deepest.getParent();
     Point loc = ev.getPoint();
     loc = convertPointToChild(window, loc, parent);
-    Component target = null;
+    Component target = deepest;
     if (parent != null)
       {
-        target = findTarget(parent, loc);
-        while (target == null && parent != null)
+        Component newTarget = findTarget(parent, loc, ev.getID());
+        while (newTarget == null && parent != null)
           {
-            if (parent.mouseListener != null
-                || parent.mouseMotionListener != null
-                || (parent.eventMask
-                    & (AWTEvent.MOUSE_EVENT_MASK
-                        | AWTEvent.MOUSE_MOTION_EVENT_MASK)) != 0)
+            if (parent.eventTypeEnabled(ev.getID()))
               {
-                target = parent;
+                newTarget = parent;
               }
             else
               parent = parent.getParent();
           }
+        if (newTarget != null)
+          target = newTarget;
       }
     if (target == null || target.isLightweight())
       {
         // Dispatch additional MOUSE_EXITED and MOUSE_ENTERED if event target
         // is different from the last event target.
         if (target != lastTarget)
           {
             if (lastTarget != null)
               {
                 Point p1 = convertPointToChild(window, ev.getPoint(),
                                                lastTarget);
                 MouseEvent mouseExited =
                   new MouseEvent(lastTarget, MouseEvent.MOUSE_EXITED,
                                  ev.getWhen(), ev.getModifiers(), p1.x, p1.y,
                                  ev.getClickCount(), ev.isPopupTrigger());
-                //System.err.println("event: " + mouseExited);
                 lastTarget.dispatchEvent(mouseExited);
               }
             
             // If a target exists dispatch the MOUSE_ENTERED event.
             // Experimenting shows that the MOUSE_ENTERED is also dispatched
             // when the mouse is dragging.
             if (target != null)
               {
                 Point p = convertPointToChild(window, ev.getPoint(), target);
                 MouseEvent mouseEntered =
-                  new MouseEvent(target,
-                                 MouseEvent.MOUSE_ENTERED, ev.getWhen(),
+                  new MouseEvent(target, MouseEvent.MOUSE_ENTERED, ev.getWhen(),
                                  ev.getModifiers(), p.x, p.y, ev.getClickCount(),
                                  ev.isPopupTrigger());
-                //System.err.println("event: " + mouseEntered);
                 target.dispatchEvent(mouseEntered);
               }
           }
         
         switch (ev.getID())
         {
           case MouseEvent.MOUSE_PRESSED:
             // Handle the start of a drag operation or discard the event if
             // one is already in progress. This prevents focus changes with the
             // other mouse buttons when one is used for dragging.
             if (dragTarget == null)
               {
                 lastTarget = dragTarget = target;
                 
                 // Save the button that started the drag operation.
@@ -278,57 +273,56 @@
   }
 
   /**
    * Finds the actual target for a mouseevent, starting at <code>c</code>.
    * This searches through the children of the container and finds the first
    * one which is showing, at the location from the mouse event and has
    * a MouseListener or MouseMotionListener attached. If no such child component
    * is found, null is returned.
    *
    * @param c the container to search through
    * @param loc the mouse event point
    *
    * @return the actual receiver of the mouse event, or null, if no such
    *         component has been found
    */
-  private Component findTarget(Container c, Point loc)
+  private Component findTarget(Container c, Point loc, int id)
   {
     int numComponents = c.getComponentCount();
     Component target = null;
     if (c != null)
       {
+        int childX;
+        int childY;
         for (int i = 0; i < numComponents; i++)
           {
             Component child = c.getComponent(i);
             if (child.isShowing())
               {
-                if (child.contains(loc.x - child.getX(), loc.y - child.getY())
-                    && (child.mouseListener != null 
-                        || child.mouseMotionListener != null
-                        || (child.eventMask
-                            & (AWTEvent.MOUSE_EVENT_MASK
-                                | AWTEvent.MOUSE_MOTION_EVENT_MASK)) != 0))
+                childX = loc.x - child.getX();
+                childY = loc.y - child.getY();
+                if (child.contains(childX, childY)
+                    && child.eventTypeEnabled(id))
                   {
                     target = child;
                     break;
                   }
               }
           }
       }
     return target;
   }
-
   /**
    * Converts a point in the parent's coordinate system to a child coordinate
    * system. The resulting point is stored in the same Point object and
    * returned.
    *
    * @param parent the parent component
    * @param p the point
    * @param child the child component
    *
    * @return the translated point
    */
   private Point convertPointToChild(Component parent, Point p,
                                    Component child)
   {
     int offX = 0;

Reply via email to