This gets rid of AWTUtilities.convertPoint() calls in
LightweightDispatcher. This is much more efficient (avoids allocations
of Point objects AND avoids double conversion to screen coordinates and
back, which is error prone anyway). It also fixes a problem that Lillian
had with one applet.

2006-06-08  Roman Kennke  <[EMAIL PROTECTED]>

        * java/awt/LightweightDispatcher.java
        (handleMouseEvent): Replaced calls to
AWTUtilities.convertPoint()
        with convertPointToChild(). This is more efficient and avoids
        problems with getLocationOnScreen().
        (findTarget): Check for component beeing showing() early.
        Simplified AWTUtilities.convertPoint() to a simple substraction
        operation.
        (convertPointToChild): New helper method.

/Roman

Index: java/awt/LightweightDispatcher.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/awt/LightweightDispatcher.java,v
retrieving revision 1.9
diff -u -1 -0 -r1.9 LightweightDispatcher.java
--- java/awt/LightweightDispatcher.java	19 May 2006 13:23:48 -0000	1.9
+++ java/awt/LightweightDispatcher.java	8 Jun 2006 16:05:23 -0000
@@ -31,22 +31,20 @@
 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;
 
-import gnu.java.awt.AWTUtilities;
-
 import java.awt.event.MouseEvent;
 import java.util.WeakHashMap;
 
 /**
  * Redispatches mouse events to lightweight components. The native peers know
  * nothing about the lightweight components and thus mouse events are always
  * targetted at Windows or heavyweight components. This class listenes directly
  * on the eventqueue and dispatches mouse events to lightweight components.
  *
  * @author Roman Kennke ([EMAIL PROTECTED])
@@ -140,65 +138,64 @@
     // 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 = AWTUtilities.convertPoint(window, loc.x, loc.y, parent);
+    loc = convertPointToChild(window, loc, parent);
     Component target = null;
     if (parent != null)
       {
-        target = findTarget(deepest.getParent(), loc);
+        target = findTarget(parent, loc);
         while (target == null && parent != null)
           {
             if (parent.getMouseListeners().length > 0
                 || parent.getMouseMotionListeners().length > 0)
               {
                 target = parent;
               }
             else
               parent = parent.getParent();
           }
       }
     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 = AWTUtilities.convertPoint(window, ev.getX(),
-                                                     ev.getY(), lastTarget);
+                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());
                 lastTarget.dispatchEvent(mouseExited);
               }
             
             // If a target exists dispatch the MOUSE_ENTERED event only if
             // there is currently no component from which a drag operation
             // started (dragTarget == null) or the target is that component
             // (dragTarget == target)
             // That way a user can click and hold on a button (putting it into
             // the armed state), move the cursor above other buttons without
             // affecting their rollover state and get back to the initial
             // button.
             if (target != null && (dragTarget == null || dragTarget == target))
               {
-                Point p = AWTUtilities.convertPoint(window, ev.getX(), ev.getY(),
-                                                    target);
+                Point p = convertPointToChild(window, ev.getPoint(), target);
                 MouseEvent mouseEntered =
                   new MouseEvent(target, MouseEvent.MOUSE_ENTERED, ev.getWhen(),
                                  ev.getModifiers(), p.x, p.y, ev.getClickCount(),
                                  ev.isPopupTrigger());
                 target.dispatchEvent(mouseEntered);
               }
           }
         
         switch (ev.getID())
         {
@@ -247,22 +244,23 @@
             break;
           default:
             // Only declare current target as the old value in all other
             // cases.
             lastTarget = target;
             break;
         }
 
         if (target != null)
           {
-            Point targetCoordinates =
-              AWTUtilities.convertPoint(window, ev.getX(), ev.getY(), target);
+            Point targetCoordinates = convertPointToChild(window,
+                                                          ev.getPoint(),
+                                                          target);
             int dx = targetCoordinates.x - ev.getX();
             int dy = targetCoordinates.y - ev.getY();
             ev.translatePoint(dx, dy);
             ev.setSource(target);
             target.dispatchEvent(ev);
             
             // We reset the event, so that the normal event dispatching is not
             // influenced by this modified event.
             ev.setSource(window);
             ev.translatePoint(-dx, -dy);
@@ -286,27 +284,56 @@
    *
    * @return the actual receiver of the mouse event, or null, if no such
    *         component has been found
    */
   private Component findTarget(Container c, Point loc)
   {
     Component[] children = c.getComponents();
     Component target = null;
     if (c != null)
       {
-        Point childLoc;
         for (int i = 0; i < children.length; i++)
           {
             Component child = children[i];
-            childLoc = AWTUtilities.convertPoint(c, loc.x, loc.y, child);
-            if (child.isShowing() && child.contains(childLoc)
-                && (child.getMouseListeners().length > 0 
-                    || child.getMouseMotionListeners().length > 0))
+            if (child.isShowing())
               {
-                target = child;
-                break;
+                if (child.contains(loc.x - child.getX(), loc.y - child.getY())
+                    && (child.getMouseListeners().length > 0 
+                        || child.getMouseMotionListeners().length > 0))
+                  {
+                    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;
+    int offY = 0;
+    Component comp = child;
+    while (comp != null && comp != parent)
+      {
+        offX += comp.getX();
+        offY += comp.getY();
+        comp = comp.getParent();
+      }
+    p.x -= offX;
+    p.y -= offY;
+    return p;
+  }
 }

Reply via email to