jdk/src/java.desktop/share/classes/java/awt/Container.java
changeset 30947 8dae2913daf4
parent 30945 20e72c2570e2
child 32865 f9cb6e427f9e
equal deleted inserted replaced
30946:0e1542fb9cb4 30947:8dae2913daf4
    42 import java.io.PrintWriter;
    42 import java.io.PrintWriter;
    43 
    43 
    44 import java.lang.ref.WeakReference;
    44 import java.lang.ref.WeakReference;
    45 import java.security.AccessController;
    45 import java.security.AccessController;
    46 
    46 
       
    47 import java.util.ArrayList;
    47 import java.util.EventListener;
    48 import java.util.EventListener;
    48 import java.util.HashSet;
    49 import java.util.HashSet;
    49 import java.util.Set;
    50 import java.util.Set;
    50 
    51 
    51 import javax.accessibility.*;
    52 import javax.accessibility.*;
    98     /**
    99     /**
    99      * The components in this container.
   100      * The components in this container.
   100      * @see #add
   101      * @see #add
   101      * @see #getComponents
   102      * @see #getComponents
   102      */
   103      */
   103     private java.util.List<Component> component = new java.util.ArrayList<Component>();
   104     private java.util.List<Component> component = new ArrayList<>();
   104 
   105 
   105     /**
   106     /**
   106      * Layout manager for this container.
   107      * Layout manager for this container.
   107      * @see #doLayout
   108      * @see #doLayout
   108      * @see #setLayout
   109      * @see #setLayout
  2566     @Deprecated
  2567     @Deprecated
  2567     public Component locate(int x, int y) {
  2568     public Component locate(int x, int y) {
  2568         if (!contains(x, y)) {
  2569         if (!contains(x, y)) {
  2569             return null;
  2570             return null;
  2570         }
  2571         }
       
  2572         Component lightweight = null;
  2571         synchronized (getTreeLock()) {
  2573         synchronized (getTreeLock()) {
  2572             // Two passes: see comment in sun.awt.SunGraphicsCallback
  2574             // Optimized version of two passes:
  2573             for (int i = 0; i < component.size(); i++) {
  2575             // see comment in sun.awt.SunGraphicsCallback
  2574                 Component comp = component.get(i);
  2576             for (final Component comp : component) {
  2575                 if (comp != null &&
  2577                 if (comp.contains(x - comp.x, y - comp.y)) {
  2576                     !(comp.peer instanceof LightweightPeer)) {
  2578                     if (!comp.isLightweight()) {
  2577                     if (comp.contains(x - comp.x, y - comp.y)) {
  2579                         // return heavyweight component as soon as possible
  2578                         return comp;
  2580                         return comp;
  2579                     }
  2581                     }
  2580                 }
  2582                     if (lightweight == null) {
  2581             }
  2583                         // save and return later the first lightweight component
  2582             for (int i = 0; i < component.size(); i++) {
  2584                         lightweight = comp;
  2583                 Component comp = component.get(i);
       
  2584                 if (comp != null &&
       
  2585                     comp.peer instanceof LightweightPeer) {
       
  2586                     if (comp.contains(x - comp.x, y - comp.y)) {
       
  2587                         return comp;
       
  2588                     }
  2585                     }
  2589                 }
  2586                 }
  2590             }
  2587             }
  2591         }
  2588         }
  2592         return this;
  2589         return lightweight != null ? lightweight : this;
  2593     }
  2590     }
  2594 
  2591 
  2595     /**
  2592     /**
  2596      * Gets the component that contains the specified point.
  2593      * Gets the component that contains the specified point.
  2597      * @param      p   the point.
  2594      * @param      p   the point.
  2691             }
  2688             }
  2692         }
  2689         }
  2693         return null;
  2690         return null;
  2694     }
  2691     }
  2695 
  2692 
  2696     final Component findComponentAtImpl(int x, int y, boolean ignoreEnabled){
  2693     final Component findComponentAtImpl(int x, int y, boolean ignoreEnabled) {
  2697         checkTreeLock();
  2694         // checkTreeLock(); commented for a performance reason
  2698 
  2695 
  2699         if (!(contains(x, y) && visible && (ignoreEnabled || enabled))) {
  2696         if (!(contains(x, y) && visible && (ignoreEnabled || enabled))) {
  2700             return null;
  2697             return null;
  2701         }
  2698         }
  2702 
  2699         Component lightweight = null;
  2703         // Two passes: see comment in sun.awt.SunGraphicsCallback
  2700         // Optimized version of two passes:
  2704         for (int i = 0; i < component.size(); i++) {
  2701         // see comment in sun.awt.SunGraphicsCallback
  2705             Component comp = component.get(i);
  2702         for (final Component comp : component) {
  2706             if (comp != null &&
  2703             final int x1 = x - comp.x;
  2707                 !(comp.peer instanceof LightweightPeer)) {
  2704             final int y1 = y - comp.y;
  2708                 if (comp instanceof Container) {
  2705             if (!comp.contains(x1, y1)) {
  2709                     comp = ((Container)comp).findComponentAtImpl(x - comp.x,
  2706                 continue; // fast path
  2710                                                                  y - comp.y,
  2707             }
  2711                                                                  ignoreEnabled);
  2708             if (!comp.isLightweight()) {
  2712                 } else {
  2709                 final Component child = getChildAt(comp, x1, y1, ignoreEnabled);
  2713                     comp = comp.getComponentAt(x - comp.x, y - comp.y);
  2710                 if (child != null) {
  2714                 }
  2711                     // return heavyweight component as soon as possible
  2715                 if (comp != null && comp.visible &&
  2712                     return child;
  2716                     (ignoreEnabled || comp.enabled))
  2713                 }
  2717                 {
  2714             } else {
  2718                     return comp;
  2715                 if (lightweight == null) {
  2719                 }
  2716                     // save and return later the first lightweight component
  2720             }
  2717                     lightweight = getChildAt(comp, x1, y1, ignoreEnabled);
  2721         }
  2718                 }
  2722         for (int i = 0; i < component.size(); i++) {
  2719             }
  2723             Component comp = component.get(i);
  2720         }
  2724             if (comp != null &&
  2721         return lightweight != null ? lightweight : this;
  2725                 comp.peer instanceof LightweightPeer) {
  2722     }
  2726                 if (comp instanceof Container) {
  2723 
  2727                     comp = ((Container)comp).findComponentAtImpl(x - comp.x,
  2724     /**
  2728                                                                  y - comp.y,
  2725      * Helper method for findComponentAtImpl. Finds a child component using
  2729                                                                  ignoreEnabled);
  2726      * findComponentAtImpl for Container and getComponentAt for Component.
  2730                 } else {
  2727      */
  2731                     comp = comp.getComponentAt(x - comp.x, y - comp.y);
  2728     private static Component getChildAt(Component comp, int x, int y,
  2732                 }
  2729                                         boolean ignoreEnabled) {
  2733                 if (comp != null && comp.visible &&
  2730         if (comp instanceof Container) {
  2734                     (ignoreEnabled || comp.enabled))
  2731             comp = ((Container) comp).findComponentAtImpl(x, y,
  2735                 {
  2732                                                           ignoreEnabled);
  2736                     return comp;
  2733         } else {
  2737                 }
  2734             comp = comp.getComponentAt(x, y);
  2738             }
  2735         }
  2739         }
  2736         if (comp != null && comp.visible &&
  2740 
  2737                 (ignoreEnabled || comp.enabled)) {
  2741         return this;
  2738             return comp;
       
  2739         }
       
  2740         return null;
  2742     }
  2741     }
  2743 
  2742 
  2744     /**
  2743     /**
  2745      * Locates the visible child component that contains the specified
  2744      * Locates the visible child component that contains the specified
  2746      * point.  The top-most child component is returned in the case
  2745      * point.  The top-most child component is returned in the case