classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] FYI: Some fixes and optimizations for Container


From: Roman Kennke
Subject: [cp-patches] FYI: Some fixes and optimizations for Container
Date: Mon, 25 Jul 2005 16:20:58 +0200
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050317)

Hi,

I checked in some fixes and optimizations that I came upon while merging with Jamaica's AWT. Most importantly the Lightweightdispatcher has been reworked so that it is reentrant, and preferredSize is now working optimized, so that it only recalculates the preferredSize if the container is invalid.

2005-07-25  Roman Kennke  <address@hidden>

        * gnu/java/awt/AWTUtilities.java:
        Added methods from SwingUtilities so that AWT does not have to
        depend on Swing.
        * java/awt/Component.java:
        Reverted my DEFAULT_FONT patch from yesterday. This does not
        seem to work with the Gtk peers.
        * java/awt/Container.java
        (addImpl): Call addNotify() on the added child. Invalidate not
        only the container but also the added child. Repaint the
        container.
        (remove): Repaint the container.
        (invalidate): Also invalidate the LayoutManager.
        (invalidateTree): Call super.invalidate to invalidate the
        container itself. Also invalidate the LayoutManager.
        (setFont): Only set the font if the specified argument actually
        differs from the current font.
        (preferredSize): Optimized this method so the LayoutManager is
        only called if the layout is invalid. Otherwise we return the
        preferred size that has been stored during last
        validation/layout.
        (getAlignmentX): Despite common belief, this method does _not_
        call the LayoutManagers getAlignmentX in the JDK. So we also
        don't.
        (getAlignmentY): Despite common belief, this method does _not_
        call the LayoutManagers getAlignmentX in the JDK. So we also
        don't.
        (dispatchEventImpl): Let the dispatcher decide if it is enabled
        for the incoming event type.
        (eventTypeEnabled): Enables only container events for
        containers.
        (addNotifyContainerChildren): Coalesced two if statements into
        one.
        Enable events on the dispatcher for this container.
        (LightweightDispatcher): Made this class reentrant. Handle
        events enabling/disabling here.

/Roman
Index: gnu/java/awt/AWTUtilities.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/awt/AWTUtilities.java,v
retrieving revision 1.3
diff -u -r1.3 AWTUtilities.java
--- gnu/java/awt/AWTUtilities.java      2 Jul 2005 20:32:10 -0000       1.3
+++ gnu/java/awt/AWTUtilities.java      25 Jul 2005 13:45:05 -0000
@@ -1,5 +1,4 @@
-/* AWTUtilities.java -- Common utility methods for AWT and Swing.
-   Copyright (C) 2005  Free Software Foundation, Inc.
+/* Copyright (C) 2004 Free Software Foundation
 
 This file is part of GNU Classpath.
 
@@ -15,8 +14,8 @@
 
 You should have received a copy of the GNU General Public License
 along with GNU Classpath; see the file COPYING.  If not, write to the
-Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-02110-1301 USA.
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
 
 Linking this library statically or dynamically with other modules is
 making a combined work based on this library.  Thus, the terms and
@@ -34,20 +33,30 @@
 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 gnu.java.awt;
 
+import java.applet.Applet;
 import java.awt.Component;
 import java.awt.Container;
+import java.awt.Font;
+import java.awt.FontMetrics;
+import java.awt.Insets;
+import java.awt.Point;
+import java.awt.Rectangle;
+import java.awt.Toolkit;
+import java.awt.Window;
+import java.awt.event.MouseEvent;
 import java.util.AbstractSequentialList;
 import java.util.List;
 import java.util.ListIterator;
 import java.util.NoSuchElementException;
 import java.util.WeakHashMap;
+import java.lang.reflect.InvocationTargetException;
 
 /**
- * This class provides utility methods that are commonly used in AWT
- * (and Swing).
+ * This class mirrors the javax.swing.SwingUtilities class. It 
+ * provides commonly needed functionalities for AWT classes without
+ * the need to reference classes in the javax.swing package.
  */
 public class AWTUtilities
 {
@@ -317,5 +326,366 @@
       visibleChildren = (VisibleComponentList) o;
 
     return visibleChildren;
+  }
+
+  /**
+   * Calculates the portion of the base rectangle which is inside the
+   * insets.
+   *
+   * @param base The rectangle to apply the insets to
+   * @param insets The insets to apply to the base rectangle
+   * @param ret A rectangle to use for storing the return value, or
+   * <code>null</code>
+   *
+   * @return The calculated area inside the base rectangle and its insets,
+   * either stored in ret or a new Rectangle if ret is <code>null</code>
+   *
+   * @see #calculateInnerArea
+   */
+  public static Rectangle calculateInsetArea(Rectangle base, Insets insets,
+                                                                               
                                                                                
                 Rectangle ret)
+  {
+    if (ret == null)
+      ret = new Rectangle();
+    ret.setBounds(base.x + insets.left, base.y + insets.top,
+        base.width - (insets.left + insets.right),
+        base.height - (insets.top + insets.bottom));
+    return ret;
+  }
+
+  /**
+   * Calculates the bounds of a component in the component's own coordinate
+   * space. The result has the same height and width as the component's
+   * bounds, but its location is set to (0,0).
+   *
+   * @param aComponent The component to measure
+   *
+   * @return The component's bounds in its local coordinate space
+   */
+  public static Rectangle getLocalBounds(Component aComponent)
+  {
+    Rectangle bounds = aComponent.getBounds();
+    return new Rectangle(0, 0, bounds.width, bounds.height);
+  }
+
+  /**
+   * Returns the font metrics object for a given font. The metrics can be
+   * used to calculate crude bounding boxes and positioning information,
+   * for laying out components with textual elements.
+   *
+   * @param font The font to get metrics for
+   *
+   * @return The font's metrics
+   *
+   * @see java.awt.font.GlyphMetrics
+   */
+  public static FontMetrics getFontMetrics(Font font)
+  {
+    return Toolkit.getDefaultToolkit().getFontMetrics(font);
+  }
+
+  /**
+   * Returns the least ancestor of <code>comp</code> which has the
+   * specified name.
+   *
+   * @param name The name to search for
+   * @param comp The component to search the ancestors of
+   *
+   * @return The nearest ancestor of <code>comp</code> with the given
+   * name, or <code>null</code> if no such ancestor exists
+   *
+   * @see java.awt.Component#getName
+   * @see #getAncestorOfClass
+   */
+  public static Container getAncestorNamed(String name, Component comp)
+  {
+    while (comp != null && (comp.getName() != name))
+      comp = comp.getParent();
+    return (Container) comp;
+  }
+
+  /**
+   * Returns the least ancestor of <code>comp</code> which is an instance
+   * of the specified class.
+   *
+   * @param c The class to search for
+   * @param comp The component to search the ancestors of
+   *
+   * @return The nearest ancestor of <code>comp</code> which is an instance
+   * of the given class, or <code>null</code> if no such ancestor exists
+   *
+   * @see #getAncestorOfClass
+   * @see #windowForComponent
+   * @see 
+   * 
+   */
+  public static Container getAncestorOfClass(Class c, Component comp)
+  {
+    while (comp != null && (! c.isInstance(comp)))
+      comp = comp.getParent();
+    return (Container) comp;
+  }
+
+  /**
+   * Equivalent to calling <code>getAncestorOfClass(Window, comp)</code>.
+   *
+   * @param comp The component to search for an ancestor window 
+   *
+   * @return An ancestral window, or <code>null</code> if none exists
+   */
+  public static Window windowForComponent(Component comp)
+  {
+    return (Window) getAncestorOfClass(Window.class, comp);
+  }
+
+  /**
+   * Returns the "root" of the component tree containint <code>comp</code>
+   * The root is defined as either the <em>least</em> ancestor of
+   * <code>comp</code> which is a address@hidden Window}, or the 
<em>greatest</em>
+   * ancestor of <code>comp</code> which is a address@hidden Applet} if no 
address@hidden
+   * Window} ancestors are found.
+   *
+   * @param comp The component to search for a root
+   *
+   * @return The root of the component's tree, or <code>null</code>
+   */
+  public static Component getRoot(Component comp)
+  {
+    Applet app = null;
+    Window win = null;
+
+    while (comp != null)
+     {
+      if (win == null && comp instanceof Window)
+        win = (Window) comp;
+      else if (comp instanceof Applet)
+        app = (Applet) comp;
+      comp = comp.getParent();
+    }
+
+    if (win != null)
+      return win;
+    else
+      return app;
+  }
+
+  /**
+   * Return true if a descends from b, in other words if b is an
+   * ancestor of a.
+   *
+   * @param a The child to search the ancestry of
+   * @param b The potential ancestor to search for
+   *
+   * @return true if a is a descendent of b, false otherwise
+   */
+  public static boolean isDescendingFrom(Component a, Component b)
+  {
+    while (true)
+     {
+      if (a == null || b == null)
+        return false;
+      if (a == b)
+        return true;
+      a = a.getParent();
+    }
+  }
+
+  /**
+   * Returns the deepest descendent of parent which is both visible and
+   * contains the point <code>(x,y)</code>. Returns parent when either
+   * parent is not a container, or has no children which contain
+   * <code>(x,y)</code>. Returns <code>null</code> when either
+   * <code>(x,y)</code> is outside the bounds of parent, or parent is
+   * <code>null</code>.
+   * 
+   * @param parent The component to search the descendents of
+   * @param x Horizontal coordinate to search for
+   * @param y Vertical coordinate to search for
+   *
+   * @return A component containing <code>(x,y)</code>, or
+   * <code>null</code>
+   *
+   * @see java.awt.Container#findComponentAt
+   */
+  public static Component getDeepestComponentAt(Component parent, int x, int y)
+  {
+    if (parent == null || (! parent.contains(x, y)))
+      return null;
+
+    if (! (parent instanceof Container))
+      return parent;
+
+    Container c = (Container) parent;
+    return c.findComponentAt(x, y);
+  }
+
+  /**
+   * Converts a point from a component's local coordinate space to "screen"
+   * coordinates (such as the coordinate space mouse events are delivered
+   * in). This operation is equivalent to translating the point by the
+   * location of the component (which is the origin of its coordinate
+   * space).
+   *
+   * @param p The point to convert
+   * @param c The component which the point is expressed in terms of
+   *
+   * @see convertPointFromScreen
+   */
+  public static void convertPointToScreen(Point p, Component c)
+  {
+    Point c0 = c.getLocationOnScreen();
+    p.translate(c0.x, c0.y);
+  }
+
+  /**
+   * Converts a point from "screen" coordinates (such as the coordinate
+   * space mouse events are delivered in) to a component's local coordinate
+   * space. This operation is equivalent to translating the point by the
+   * negation of the component's location (which is the origin of its
+   * coordinate space).
+   *
+   * @param p The point to convert
+   * @param c The component which the point should be expressed in terms of
+   */
+  public static void convertPointFromScreen(Point p, Component c)
+  {
+    Point c0 = c.getLocationOnScreen();
+    p.translate(-c0.x, -c0.y);
+  }
+
+  /**
+   * Converts a point <code>(x,y)</code> from the coordinate space of one
+   * component to another. This is equivalent to converting the point from
+   * <code>source</code> space to screen space, then back from screen space
+   * to <code>destination</code> space. If exactly one of the two
+   * Components is <code>null</code>, it is taken to refer to the root
+   * ancestor of the other component. If both are <code>null</code>, no
+   * transformation is done.
+   *
+   * @param source The component which the point is expressed in terms of
+   * @param x Horizontal coordinate of point to transform
+   * @param y Vertical coordinate of point to transform
+   * @param destination The component which the return value will be
+   * expressed in terms of
+   *
+   * @return The point <code>(x,y)</code> converted from the coordinate space 
of the
+   * source component to the coordinate space of the destination component
+   *
+   * @see #convertPointToScreen
+   * @see #convertPointFromScreen
+   * @see #convertRectangle
+   * @see #getRoot
+   */
+  public static Point convertPoint(Component source, int x, int y,
+                                                                               
                                                         Component destination)
+  {
+    Point pt = new Point(x, y);
+
+    if (source == null && destination == null)
+      return pt;
+
+    if (source == null)
+      source = getRoot(destination);
+
+    if (destination == null)
+      destination = getRoot(source);
+
+    convertPointToScreen(pt, source);
+    convertPointFromScreen(pt, destination);
+
+    return pt;
+  }
+
+  
+  /**
+   * Converts a rectangle from the coordinate space of one component to
+   * another. This is equivalent to converting the rectangle from
+   * <code>source</code> space to screen space, then back from screen space
+   * to <code>destination</code> space. If exactly one of the two
+   * Components is <code>null</code>, it is taken to refer to the root
+   * ancestor of the other component. If both are <code>null</code>, no
+   * transformation is done.
+   *
+   * @param source The component which the rectangle is expressed in terms of
+   * @param rect The rectangle to convert
+   * @param destination The component which the return value will be
+   * expressed in terms of
+   *
+   * @return A new rectangle, equal in size to the input rectangle, but
+   * with its position converted from the coordinate space of the source
+   * component to the coordinate space of the destination component
+   *
+   * @see #convertPointToScreen
+   * @see #convertPointFromScreen
+   * @see #convertPoint
+   * @see #getRoot
+   */
+  public static Rectangle convertRectangle(Component source,
+                                                                               
                                                                                
         Rectangle rect,
+                                                                               
                                                                                
         Component destination)
+  {
+    Point pt = convertPoint(source, rect.x, rect.y, destination);
+    return new Rectangle(pt.x, pt.y, rect.width, rect.height);
+  }
+
+  /**
+   * Convert a mouse event which refrers to one component to another.  This
+   * includes changing the mouse event's coordinate space, as well as the
+   * source property of the event. If <code>source</code> is
+   * <code>null</code>, it is taken to refer to <code>destination</code>'s
+   * root component. If <code>destination</code> is <code>null</code>, the
+   * new event will remain expressed in <code>source</code>'s coordinate
+   * system.
+   *
+   * @param source The component the mouse event currently refers to
+   * @param sourceEvent The mouse event to convert
+   * @param destination The component the new mouse event should refer to
+   *
+   * @return A new mouse event expressed in terms of the destination
+   * component's coordinate space, and with the destination component as
+   * its source
+   *
+   * @see #convertPoint
+   */
+  public static MouseEvent convertMouseEvent(Component source,
+                                                                               
                                                                                
                 MouseEvent sourceEvent,
+                                                                               
                                                                                
                 Component destination)
+  {
+    Point newpt = convertPoint(source, sourceEvent.getX(), sourceEvent.getY(),
+        destination);
+
+    return new MouseEvent(destination, sourceEvent.getID(),
+        sourceEvent.getWhen(), sourceEvent.getModifiers(),
+                               newpt.x, newpt.y, sourceEvent.getClickCount(),
+                               sourceEvent.isPopupTrigger(), 
sourceEvent.getButton());
+  }
+
+
+  /** 
+   * Calls address@hidden java.awt.EventQueue.invokeLater} with the
+   * specified address@hidden Runnable}. 
+   */
+  public static void invokeLater(Runnable doRun)
+  {
+    java.awt.EventQueue.invokeLater(doRun);
+  }
+
+  /** 
+   * Calls address@hidden java.awt.EventQueue.invokeAndWait} with the
+   * specified address@hidden Runnable}. 
+   */
+  public static void invokeAndWait(Runnable doRun)
+  throws InterruptedException,
+  InvocationTargetException
+  {
+    java.awt.EventQueue.invokeAndWait(doRun);
+  }
+
+  /** 
+   * Calls address@hidden java.awt.EventQueue.isEventDispatchThread}.
+   */
+  public static boolean isEventDispatchThread()
+  {
+    return java.awt.EventQueue.isDispatchThread();
   }
 }
Index: java/awt/Component.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/awt/Component.java,v
retrieving revision 1.64
diff -u -r1.64 Component.java
--- java/awt/Component.java     23 Jul 2005 15:29:48 -0000      1.64
+++ java/awt/Component.java     25 Jul 2005 13:45:05 -0000
@@ -211,13 +211,6 @@
    */
   static final Object treeLock = new String("AWT_TREE_LOCK");
 
-  /**
-   * Preallocated default font returned by getFont() if no font was
-   * set explicitly.
-   */
-  //  private static final Font DEFAULT_FONT = new Font ("Dialog", Font.PLAIN, 
12);
-  private static final Font DEFAULT_FONT = null;
-
   // Serialized fields from the serialization spec.
 
   /**
@@ -1081,9 +1074,9 @@
 
     Component p = parent;
     if (p != null)
-      return p.getFont ();
+      return p.getFont();
     else
-      return DEFAULT_FONT;
+      return new Font("Dialog", Font.PLAIN, 12);
   }
 
   /**
Index: java/awt/Container.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/awt/Container.java,v
retrieving revision 1.54
diff -u -r1.54 Container.java
--- java/awt/Container.java     6 Jul 2005 08:15:17 -0000       1.54
+++ java/awt/Container.java     25 Jul 2005 13:45:05 -0000
@@ -59,7 +59,8 @@
 import java.util.Set;
 
 import javax.accessibility.Accessible;
-import javax.swing.SwingUtilities;
+
+import gnu.java.awt.AWTUtilities;
 
 /**
  * A generic window toolkit object that acts as a container for other objects.
@@ -338,6 +339,10 @@
         if (comp.parent != null)
           comp.parent.remove(comp);
         comp.parent = this;
+
+        // Notify the component that it has a new parent.
+        comp.addNotify();
+
         if (peer != null)
           {
             if (comp.isLightweight ())
@@ -348,7 +353,8 @@
              }
           }
 
-        invalidate();
+        // Invalidate the layout of the added component and its ancestors.
+        comp.invalidate();
 
         if (component == null)
           component = new Component[4]; // FIXME, better initial size?
@@ -380,6 +386,7 @@
               {
                 LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
                 lm2.addLayoutComponent(comp, constraints);
+                lm2.invalidateLayout(this);
               }
             else if (constraints instanceof String)
               layoutMgr.addLayoutComponent((String) constraints, comp);
@@ -395,6 +402,9 @@
                                                    comp);
             getToolkit().getSystemEventQueue().postEvent(ce);
           }
+
+        // Repaint this container.
+        repaint();
       }
   }
 
@@ -430,6 +440,9 @@
                                                    r);
             getToolkit().getSystemEventQueue().postEvent(ce);
           }
+
+        // Repaint this container.
+        repaint();
       }
   }
 
@@ -513,6 +526,11 @@
   public void invalidate()
   {
     super.invalidate();
+    if (layoutMgr != null && layoutMgr instanceof LayoutManager2)
+      {
+        LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
+        lm2.invalidateLayout(this);
+      }
   }
 
   /**
@@ -534,6 +552,7 @@
    */
   void invalidateTree()
   {
+    super.invalidate();  // Clean cached layout state.
     for (int i = 0; i < ncomponents; i++)
       {
         Component comp = component[i];
@@ -541,6 +560,12 @@
         if (comp instanceof Container)
           ((Container) comp).invalidateTree();
       }
+
+    if (layoutMgr != null && layoutMgr instanceof LayoutManager2)
+      {
+        LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
+        lm2.invalidateLayout(this);
+      }
   }
 
   /**
@@ -596,11 +621,15 @@
 
   public void setFont(Font f)
   {
-    super.setFont(f);
-    // FIXME: Although it might make more sense to invalidate only
-    // those children whose font == null, Sun invalidates all children.
-    // So we'll do the same.
-    invalidateTree();
+    if( (f != null && (font == null || !font.equals(f)))
+        || f == null)
+      {
+        super.setFont(f);
+        // FIXME: Although it might make more sense to invalidate only
+        // those children whose font == null, Sun invalidates all children.
+        // So we'll do the same.
+        invalidateTree();
+      }
   }
 
   /**
@@ -622,10 +651,21 @@
    */
   public Dimension preferredSize()
   {
-    if (layoutMgr != null)
-      return layoutMgr.preferredLayoutSize (this);
-    else
-      return super.preferredSize ();
+    synchronized(treeLock)
+      {  
+        if(valid && prefSize != null)
+          return new Dimension(prefSize);
+
+        if (layoutMgr != null)
+          {
+            Dimension layoutSize = layoutMgr.preferredLayoutSize (this);
+            if(valid)
+              prefSize = layoutSize;
+            return new Dimension(layoutSize);
+          }
+        else
+          return super.preferredSize ();
+      }
   }
 
   /**
@@ -678,13 +718,7 @@
    */
   public float getAlignmentX()
   {
-    if (layoutMgr instanceof LayoutManager2)
-      {
-        LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
-        return lm2.getLayoutAlignmentX(this);
-      }
-    else
-      return super.getAlignmentX();
+    return super.getAlignmentX();
   }
 
   /**
@@ -696,13 +730,7 @@
    */
   public float getAlignmentY()
   {
-    if (layoutMgr instanceof LayoutManager2)
-      {
-        LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
-        return lm2.getLayoutAlignmentY(this);
-      }
-    else
-      return super.getAlignmentY();
+    return super.getAlignmentY();
   }
 
   /**
@@ -1525,11 +1553,9 @@
   void dispatchEventImpl(AWTEvent e)
   {
     // Give lightweight dispatcher a chance to handle it.
-    if (eventTypeEnabled (e.id)
-        && dispatcher != null 
-        && dispatcher.handleEvent (e))
+    if (dispatcher != null && dispatcher.handleEvent (e))
       return;
-    
+
     if ((e.id <= ContainerEvent.CONTAINER_LAST
              && e.id >= ContainerEvent.CONTAINER_FIRST)
         && (containerListener != null
@@ -1539,6 +1565,26 @@
       super.dispatchEventImpl(e);
   }
 
+  /**
+   * Tests if this container has an interest in the given event id.
+   *
+   * @param eventId The event id to check.
+   *
+   * @return <code>true</code> if a listener for the event id exists or
+   *         if the eventMask is set for the event id.
+   *
+   * @see java.awt.Component#eventTypeEnabled(int)
+   */
+  boolean eventTypeEnabled(int eventId)
+  {
+    if(eventId <= ContainerEvent.CONTAINER_LAST 
+       && eventId >= ContainerEvent.CONTAINER_FIRST)
+      return containerListener != null
+        || (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0;
+      else 
+        return super.eventTypeEnabled(eventId);
+  }
+
   // This is used to implement Component.transferFocus.
   Component findNextFocusComponent(Component child)
   {
@@ -1603,12 +1649,11 @@
 
                 // If we're not lightweight, and we just got a lightweight
                 // child, we need a lightweight dispatcher to feed it events.
-                if (! this.isLightweight()) 
-                  {
-                    if (dispatcher == null)
-                      dispatcher = new LightweightDispatcher (this);
-                  }    
-         
+                if (!this.isLightweight() && dispatcher == null) 
+                  dispatcher = new LightweightDispatcher (this);
+
+                if (dispatcher != null)
+                  dispatcher.enableEvents(component[i].eventMask);
 
                enableEvents(component[i].eventMask);
                if (peer != null && !isLightweight ())
@@ -1862,7 +1907,6 @@
  * rather than mimic it exactly we write something which does "roughly
  * the same thing".
  */
-
 class LightweightDispatcher implements Serializable
 {
   private static final long serialVersionUID = 5184291520170872969L;
@@ -1870,10 +1914,8 @@
   private Cursor nativeCursor;
   private long eventMask;
   
-  private transient Component mouseEventTarget;
   private transient Component pressedComponent;
   private transient Component lastComponentEntered;
-  private transient Component tempComponent;
   private transient int pressCount;
   
   LightweightDispatcher(Container c)
@@ -1881,11 +1923,17 @@
     nativeContainer = c;
   }
 
-  void acquireComponentForMouseEvent(MouseEvent me)
+  void enableEvents(long l)
+  {
+    eventMask |= l;
+  }
+
+  Component acquireComponentForMouseEvent(MouseEvent me)
   {
     int x = me.getX ();
     int y = me.getY ();
 
+    Component mouseEventTarget = null;
     // Find the candidate which should receive this event.
     Component parent = nativeContainer;
     Component candidate = null;
@@ -1893,13 +1941,13 @@
     while (candidate == null && parent != null)
       {
         candidate =
-          SwingUtilities.getDeepestComponentAt(parent, p.x, p.y);
+          AWTUtilities.getDeepestComponentAt(parent, p.x, p.y);
         if (candidate == null || (candidate.eventMask & me.getID()) == 0)
-        {
-          candidate = null;
-          p = SwingUtilities.convertPoint(parent, p.x, p.y, parent.parent);
-          parent = parent.parent;
-        }
+          {
+            candidate = null;
+            p = AWTUtilities.convertPoint(parent, p.x, p.y, parent.parent);
+            parent = parent.parent;
+          }
       }
 
     // If the only candidate we found was the native container itself,
@@ -1915,25 +1963,24 @@
       {
         // Old candidate could have been removed from 
         // the nativeContainer so we check first.
-        if (SwingUtilities.isDescendingFrom(lastComponentEntered, 
nativeContainer))
-        {
-          Point tp = 
-            SwingUtilities.convertPoint(nativeContainer, 
-                                        x, y, lastComponentEntered);
-          MouseEvent exited = new MouseEvent (lastComponentEntered, 
-                                              MouseEvent.MOUSE_EXITED,
-                                              me.getWhen (), 
-                                              me.getModifiersEx (), 
-                                              tp.x, tp.y,
-                                              me.getClickCount (),
-                                              me.isPopupTrigger (),
-                                              me.getButton ());
-          tempComponent = lastComponentEntered;
-          lastComponentEntered = null;
-          tempComponent.dispatchEvent(exited);
-        }
+        if (AWTUtilities.isDescendingFrom(lastComponentEntered,
+                                          nativeContainer))
+          {
+            Point tp = AWTUtilities.convertPoint(nativeContainer, 
+                                                 x, y, lastComponentEntered);
+            MouseEvent exited = new MouseEvent (lastComponentEntered, 
+                                                MouseEvent.MOUSE_EXITED,
+                                                me.getWhen (), 
+                                                me.getModifiersEx (), 
+                                                tp.x, tp.y,
+                                                me.getClickCount (),
+                                                me.isPopupTrigger (),
+                                                me.getButton ());
+            lastComponentEntered.dispatchEvent (exited); 
+          }
         lastComponentEntered = null;
       }
+
     // If we have a candidate, maybe enter it.
     if (candidate != null)
       {
@@ -1942,10 +1989,10 @@
             && candidate.isShowing()
             && candidate != nativeContainer
             && candidate != lastComponentEntered)
-         {                     
+         {
             lastComponentEntered = mouseEventTarget;
-            Point cp = SwingUtilities.convertPoint(nativeContainer, 
-                                                   x, y, lastComponentEntered);
+            Point cp = AWTUtilities.convertPoint(nativeContainer, 
+                                                 x, y, lastComponentEntered);
             MouseEvent entered = new MouseEvent (lastComponentEntered, 
                                                  MouseEvent.MOUSE_ENTERED,
                                                  me.getWhen (), 
@@ -1958,17 +2005,38 @@
           }
       }
 
+    // Check which buttons where pressed except the last button that
+    // changed state.
+    int modifiers = me.getModifiersEx() & (MouseEvent.BUTTON1_DOWN_MASK
+                                           | MouseEvent.BUTTON2_DOWN_MASK
+                                           | MouseEvent.BUTTON3_DOWN_MASK);
+    switch(me.getButton())
+      {
+      case MouseEvent.BUTTON1:
+        modifiers &= ~MouseEvent.BUTTON1_DOWN_MASK;
+        break;
+      case MouseEvent.BUTTON2:
+        modifiers &= ~MouseEvent.BUTTON2_DOWN_MASK;
+        break;
+      case MouseEvent.BUTTON3:
+        modifiers &= ~MouseEvent.BUTTON3_DOWN_MASK;
+        break;
+      }
+
     if (me.getID() == MouseEvent.MOUSE_RELEASED
-        || me.getID() == MouseEvent.MOUSE_PRESSED && pressCount > 0
+        || me.getID() == MouseEvent.MOUSE_PRESSED && modifiers > 0
         || me.getID() == MouseEvent.MOUSE_DRAGGED)
-      // If any of the following events occur while a button is held down,
-      // they should be dispatched to the same component to which the
-      // original MOUSE_PRESSED event was dispatched:
-      //   - MOUSE_RELEASED
-      //   - MOUSE_PRESSED: another button pressed while the first is held down
-      //   - MOUSE_DRAGGED
-      if (SwingUtilities.isDescendingFrom(pressedComponent, nativeContainer))
-        mouseEventTarget = pressedComponent;
+      {
+        // If any of the following events occur while a button is held down,
+        // they should be dispatched to the same component to which the
+        // original MOUSE_PRESSED event was dispatched:
+        //   - MOUSE_RELEASED
+        //   - MOUSE_PRESSED: another button pressed while the first is held
+        //     down
+        //   - MOUSE_DRAGGED
+        if (AWTUtilities.isDescendingFrom(pressedComponent, nativeContainer))
+          mouseEventTarget = pressedComponent;
+      }
     else if (me.getID() == MouseEvent.MOUSE_CLICKED)
       {
         // Don't dispatch CLICKED events whose target is not the same as the
@@ -1978,6 +2046,7 @@
         else if (pressCount == 0)
           pressedComponent = null;
       }
+    return mouseEventTarget;
   }
 
   boolean handleEvent(AWTEvent e)
@@ -1986,41 +2055,42 @@
       {
         MouseEvent me = (MouseEvent) e;
 
-        acquireComponentForMouseEvent(me);
-       
+        // Make the LightWeightDispatcher reentrant. This is necessary when
+        // a lightweight component does its own modal event queue.
+        Component mouseEventTarget = acquireComponentForMouseEvent(me);
+
         // Avoid dispatching ENTERED and EXITED events twice.
         if (mouseEventTarget != null
             && mouseEventTarget.isShowing()
             && e.getID() != MouseEvent.MOUSE_ENTERED
             && e.getID() != MouseEvent.MOUSE_EXITED)
           {
-            MouseEvent newEvt = 
-              SwingUtilities.convertMouseEvent(nativeContainer, me, 
-                                               mouseEventTarget);
-            mouseEventTarget.dispatchEvent(newEvt);
-
             switch (e.getID())
               {
-                case MouseEvent.MOUSE_PRESSED:
-                  if (pressCount++ == 0)
-                    pressedComponent = mouseEventTarget;
-                  break;
-
-                case MouseEvent.MOUSE_RELEASED:
-                  // Clear our memory of the original PRESSED event, only if
-                  // we're not expecting a CLICKED event after this. If
-                  // there is a CLICKED event after this, it will do clean up.
-                  if (--pressCount == 0
-                      && mouseEventTarget != pressedComponent)
-                    pressedComponent = null;
-                  break;
+              case MouseEvent.MOUSE_PRESSED:
+                if (pressCount++ == 0)
+                  pressedComponent = mouseEventTarget;
+                break;
+              case MouseEvent.MOUSE_RELEASED:
+                // Clear our memory of the original PRESSED event, only if
+                // we're not expecting a CLICKED event after this. If
+                // there is a CLICKED event after this, it will do clean up.
+                if (--pressCount == 0
+                    && mouseEventTarget != pressedComponent)
+                  pressedComponent = null;
+                break;
               }
-              if (newEvt.isConsumed())
-                e.consume();
+
+            MouseEvent newEvt =
+              AWTUtilities.convertMouseEvent(nativeContainer, me,
+                                             mouseEventTarget);
+            mouseEventTarget.dispatchEvent(newEvt);
+
+            if (newEvt.isConsumed())
+              e.consume();
           }
       }
-    
+
     return e.isConsumed();
   }
-
-} // class LightweightDispatcher
+}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]