< prev index next >

src/java.desktop/share/classes/java/awt/Container.java

Print this page




  27 import java.awt.dnd.DropTarget;
  28 
  29 import java.awt.event.*;
  30 
  31 import java.awt.peer.ContainerPeer;
  32 import java.awt.peer.ComponentPeer;
  33 import java.awt.peer.LightweightPeer;
  34 
  35 import java.beans.PropertyChangeListener;
  36 
  37 import java.io.IOException;
  38 import java.io.ObjectInputStream;
  39 import java.io.ObjectOutputStream;
  40 import java.io.ObjectStreamField;
  41 import java.io.PrintStream;
  42 import java.io.PrintWriter;
  43 
  44 import java.lang.ref.WeakReference;
  45 import java.security.AccessController;
  46 

  47 import java.util.EventListener;
  48 import java.util.HashSet;
  49 import java.util.Set;
  50 
  51 import javax.accessibility.*;
  52 
  53 import sun.util.logging.PlatformLogger;
  54 
  55 import sun.awt.AppContext;
  56 import sun.awt.AWTAccessor;
  57 import sun.awt.CausedFocusEvent;
  58 import sun.awt.PeerEvent;
  59 import sun.awt.SunToolkit;
  60 
  61 import sun.awt.dnd.SunDropTargetEvent;
  62 
  63 import sun.java2d.pipe.Region;
  64 
  65 import sun.security.action.GetBooleanAction;
  66 


  83  *
  84  * @author      Arthur van Hoff
  85  * @author      Sami Shaio
  86  * @see       #add(java.awt.Component, int)
  87  * @see       #getComponent(int)
  88  * @see       LayoutManager
  89  * @since     1.0
  90  */
  91 public class Container extends Component {
  92 
  93     private static final PlatformLogger log = PlatformLogger.getLogger("java.awt.Container");
  94     private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.Container");
  95 
  96     private static final Component[] EMPTY_ARRAY = new Component[0];
  97 
  98     /**
  99      * The components in this container.
 100      * @see #add
 101      * @see #getComponents
 102      */
 103     private java.util.List<Component> component = new java.util.ArrayList<Component>();
 104 
 105     /**
 106      * Layout manager for this container.
 107      * @see #doLayout
 108      * @see #setLayout
 109      * @see #getLayout
 110      */
 111     LayoutManager layoutMgr;
 112 
 113     /**
 114      * Event router for lightweight components.  If this container
 115      * is native, this dispatcher takes care of forwarding and
 116      * retargeting the events to lightweight components contained
 117      * (if any).
 118      */
 119     private LightweightDispatcher dispatcher;
 120 
 121     /**
 122      * The focus traversal policy that will manage keyboard traversal of this
 123      * Container's children, if this Container is a focus cycle root. If the


2551      * @return null if the component does not contain the position.
2552      * If there is no child component at the requested point and the
2553      * point is within the bounds of the container the container itself
2554      * is returned; otherwise the top-most child is returned.
2555      * @see Component#contains
2556      * @since 1.1
2557      */
2558     public Component getComponentAt(int x, int y) {
2559         return locate(x, y);
2560     }
2561 
2562     /**
2563      * @deprecated As of JDK version 1.1,
2564      * replaced by <code>getComponentAt(int, int)</code>.
2565      */
2566     @Deprecated
2567     public Component locate(int x, int y) {
2568         if (!contains(x, y)) {
2569             return null;
2570         }

2571         synchronized (getTreeLock()) {
2572             // Two passes: see comment in sun.awt.SunGraphicsCallback
2573             for (int i = 0; i < component.size(); i++) {
2574                 Component comp = component.get(i);
2575                 if (comp != null &&
2576                     !(comp.peer instanceof LightweightPeer)) {
2577                     if (comp.contains(x - comp.x, y - comp.y)) {


2578                         return comp;
2579                     }



2580                 }
2581             }
2582             for (int i = 0; i < component.size(); i++) {
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                     }
2589                 }
2590             }
2591         }
2592         return this;
2593     }
2594 
2595     /**
2596      * Gets the component that contains the specified point.
2597      * @param      p   the point.
2598      * @return     returns the component that contains the point,
2599      *                 or <code>null</code> if the component does
2600      *                 not contain the point.
2601      * @see        Component#contains
2602      * @since      1.1
2603      */
2604     public Component getComponentAt(Point p) {
2605         return getComponentAt(p.x, p.y);
2606     }
2607 
2608     /**
2609      * Returns the position of the mouse pointer in this <code>Container</code>'s
2610      * coordinate space if the <code>Container</code> is under the mouse pointer,
2611      * otherwise returns <code>null</code>.
2612      * This method is similar to {@link Component#getMousePosition()} with the exception


2676     }
2677 
2678     /**
2679      * Private version of findComponentAt which has a controllable
2680      * behavior. Setting 'ignoreEnabled' to 'false' bypasses disabled
2681      * Components during the search. This behavior is used by the
2682      * lightweight cursor support in sun.awt.GlobalCursorManager.
2683      *
2684      * The addition of this feature is temporary, pending the
2685      * adoption of new, public API which exports this feature.
2686      */
2687     final Component findComponentAt(int x, int y, boolean ignoreEnabled) {
2688         synchronized (getTreeLock()) {
2689             if (isRecursivelyVisible()){
2690                 return findComponentAtImpl(x, y, ignoreEnabled);
2691             }
2692         }
2693         return null;
2694     }
2695 
2696     final Component findComponentAtImpl(int x, int y, boolean ignoreEnabled){
2697         checkTreeLock();
2698 
2699         if (!(contains(x, y) && visible && (ignoreEnabled || enabled))) {
2700             return null;
2701         }
2702 
2703         // Two passes: see comment in sun.awt.SunGraphicsCallback
2704         for (int i = 0; i < component.size(); i++) {
2705             Component comp = component.get(i);
2706             if (comp != null &&
2707                 !(comp.peer instanceof LightweightPeer)) {
2708                 if (comp instanceof Container) {
2709                     comp = ((Container)comp).findComponentAtImpl(x - comp.x,
2710                                                                  y - comp.y,
2711                                                                  ignoreEnabled);





2712                 } else {
2713                     comp = comp.getComponentAt(x - comp.x, y - comp.y);


2714                 }
2715                 if (comp != null && comp.visible &&
2716                     (ignoreEnabled || comp.enabled))
2717                 {
2718                     return comp;
2719                 }
2720             }

2721         }
2722         for (int i = 0; i < component.size(); i++) {
2723             Component comp = component.get(i);
2724             if (comp != null &&
2725                 comp.peer instanceof LightweightPeer) {



2726                 if (comp instanceof Container) {
2727                     comp = ((Container)comp).findComponentAtImpl(x - comp.x,
2728                                                                  y - comp.y,
2729                                                                  ignoreEnabled);
2730                 } else {
2731                     comp = comp.getComponentAt(x - comp.x, y - comp.y);
2732                 }
2733                 if (comp != null && comp.visible &&
2734                     (ignoreEnabled || comp.enabled))
2735                 {
2736                     return comp;
2737                 }
2738             }
2739         }
2740 
2741         return this;
2742     }
2743 
2744     /**
2745      * Locates the visible child component that contains the specified
2746      * point.  The top-most child component is returned in the case
2747      * where there is overlap in the components.  If the containing child
2748      * component is a Container, this method will continue searching for
2749      * the deepest nested child component.  Components which are not
2750      * visible are ignored during the search.<p>
2751      *
2752      * The findComponentAt method is different from getComponentAt in
2753      * that getComponentAt only searches the Container's immediate
2754      * children; if the containing component is a Container,
2755      * findComponentAt will search that child to find a nested component.
2756      *
2757      * @param      p   the point.
2758      * @return null if the component does not contain the position.
2759      * If there is no child component at the requested point and the
2760      * point is within the bounds of the container the container itself
2761      * is returned.




  27 import java.awt.dnd.DropTarget;
  28 
  29 import java.awt.event.*;
  30 
  31 import java.awt.peer.ContainerPeer;
  32 import java.awt.peer.ComponentPeer;
  33 import java.awt.peer.LightweightPeer;
  34 
  35 import java.beans.PropertyChangeListener;
  36 
  37 import java.io.IOException;
  38 import java.io.ObjectInputStream;
  39 import java.io.ObjectOutputStream;
  40 import java.io.ObjectStreamField;
  41 import java.io.PrintStream;
  42 import java.io.PrintWriter;
  43 
  44 import java.lang.ref.WeakReference;
  45 import java.security.AccessController;
  46 
  47 import java.util.ArrayList;
  48 import java.util.EventListener;
  49 import java.util.HashSet;
  50 import java.util.Set;
  51 
  52 import javax.accessibility.*;
  53 
  54 import sun.util.logging.PlatformLogger;
  55 
  56 import sun.awt.AppContext;
  57 import sun.awt.AWTAccessor;
  58 import sun.awt.CausedFocusEvent;
  59 import sun.awt.PeerEvent;
  60 import sun.awt.SunToolkit;
  61 
  62 import sun.awt.dnd.SunDropTargetEvent;
  63 
  64 import sun.java2d.pipe.Region;
  65 
  66 import sun.security.action.GetBooleanAction;
  67 


  84  *
  85  * @author      Arthur van Hoff
  86  * @author      Sami Shaio
  87  * @see       #add(java.awt.Component, int)
  88  * @see       #getComponent(int)
  89  * @see       LayoutManager
  90  * @since     1.0
  91  */
  92 public class Container extends Component {
  93 
  94     private static final PlatformLogger log = PlatformLogger.getLogger("java.awt.Container");
  95     private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.Container");
  96 
  97     private static final Component[] EMPTY_ARRAY = new Component[0];
  98 
  99     /**
 100      * The components in this container.
 101      * @see #add
 102      * @see #getComponents
 103      */
 104     private java.util.List<Component> component = new ArrayList<>();
 105 
 106     /**
 107      * Layout manager for this container.
 108      * @see #doLayout
 109      * @see #setLayout
 110      * @see #getLayout
 111      */
 112     LayoutManager layoutMgr;
 113 
 114     /**
 115      * Event router for lightweight components.  If this container
 116      * is native, this dispatcher takes care of forwarding and
 117      * retargeting the events to lightweight components contained
 118      * (if any).
 119      */
 120     private LightweightDispatcher dispatcher;
 121 
 122     /**
 123      * The focus traversal policy that will manage keyboard traversal of this
 124      * Container's children, if this Container is a focus cycle root. If the


2552      * @return null if the component does not contain the position.
2553      * If there is no child component at the requested point and the
2554      * point is within the bounds of the container the container itself
2555      * is returned; otherwise the top-most child is returned.
2556      * @see Component#contains
2557      * @since 1.1
2558      */
2559     public Component getComponentAt(int x, int y) {
2560         return locate(x, y);
2561     }
2562 
2563     /**
2564      * @deprecated As of JDK version 1.1,
2565      * replaced by <code>getComponentAt(int, int)</code>.
2566      */
2567     @Deprecated
2568     public Component locate(int x, int y) {
2569         if (!contains(x, y)) {
2570             return null;
2571         }
2572         Component lightweight = null;
2573         synchronized (getTreeLock()) {
2574             // Optimized version of two passes:
2575             // see comment in sun.awt.SunGraphicsCallback
2576             for (final Component comp : component) {


2577                 if (comp.contains(x - comp.x, y - comp.y)) {
2578                     if (!comp.isLightweight()) {
2579                         // return heavyweight component as soon as possible
2580                         return comp;
2581                     }
2582                     if (lightweight == null) {
2583                         // save and return later the first lightweight component
2584                         lightweight = comp;
2585                     }
2586                 }






2587             }
2588         }
2589         return lightweight != null ? lightweight : this;


2590     }
2591 
2592     /**
2593      * Gets the component that contains the specified point.
2594      * @param      p   the point.
2595      * @return     returns the component that contains the point,
2596      *                 or <code>null</code> if the component does
2597      *                 not contain the point.
2598      * @see        Component#contains
2599      * @since      1.1
2600      */
2601     public Component getComponentAt(Point p) {
2602         return getComponentAt(p.x, p.y);
2603     }
2604 
2605     /**
2606      * Returns the position of the mouse pointer in this <code>Container</code>'s
2607      * coordinate space if the <code>Container</code> is under the mouse pointer,
2608      * otherwise returns <code>null</code>.
2609      * This method is similar to {@link Component#getMousePosition()} with the exception


2673     }
2674 
2675     /**
2676      * Private version of findComponentAt which has a controllable
2677      * behavior. Setting 'ignoreEnabled' to 'false' bypasses disabled
2678      * Components during the search. This behavior is used by the
2679      * lightweight cursor support in sun.awt.GlobalCursorManager.
2680      *
2681      * The addition of this feature is temporary, pending the
2682      * adoption of new, public API which exports this feature.
2683      */
2684     final Component findComponentAt(int x, int y, boolean ignoreEnabled) {
2685         synchronized (getTreeLock()) {
2686             if (isRecursivelyVisible()){
2687                 return findComponentAtImpl(x, y, ignoreEnabled);
2688             }
2689         }
2690         return null;
2691     }
2692 
2693     final Component findComponentAtImpl(int x, int y, boolean ignoreEnabled) {
2694         // checkTreeLock(); commented for a performance reason
2695 
2696         if (!(contains(x, y) && visible && (ignoreEnabled || enabled))) {
2697             return null;
2698         }
2699         Component lightweight = null;
2700         // Optimized version of two passes:
2701         // see comment in sun.awt.SunGraphicsCallback
2702         for (final Component comp : component) {
2703             final int x1 = x - comp.x;
2704             final int y1 = y - comp.y;
2705             if (!comp.contains(x1, y1)) {
2706                 continue; // fast path
2707             }
2708             if (!comp.isLightweight()) {
2709                 final Component child = getChildAt(comp, x1, y1, ignoreEnabled);
2710                 if (child != null) {
2711                     // return heavyweight component as soon as possible
2712                     return child;
2713                 }
2714             } else {
2715                 if (lightweight == null) {
2716                     // save and return later the first lightweight component
2717                     lightweight = getChildAt(comp, x1, y1, ignoreEnabled);
2718                 }




2719             }
2720         }
2721         return lightweight != null ? lightweight : this;
2722     }
2723 
2724     /**
2725      * Helper method for findComponentAtImpl. Finds a child component using
2726      * findComponentAtImpl for Container and getComponentAt for Component.
2727      */
2728     private static Component getChildAt(Component comp, int x, int y,
2729                                         boolean ignoreEnabled) {
2730         if (comp instanceof Container) {
2731             comp = ((Container) comp).findComponentAtImpl(x, y,

2732                                                           ignoreEnabled);
2733         } else {
2734             comp = comp.getComponentAt(x, y);
2735         }
2736         if (comp != null && comp.visible &&
2737                 (ignoreEnabled || comp.enabled)) {

2738             return comp;
2739         }
2740         return null;



2741     }
2742 
2743     /**
2744      * Locates the visible child component that contains the specified
2745      * point.  The top-most child component is returned in the case
2746      * where there is overlap in the components.  If the containing child
2747      * component is a Container, this method will continue searching for
2748      * the deepest nested child component.  Components which are not
2749      * visible are ignored during the search.<p>
2750      *
2751      * The findComponentAt method is different from getComponentAt in
2752      * that getComponentAt only searches the Container's immediate
2753      * children; if the containing component is a Container,
2754      * findComponentAt will search that child to find a nested component.
2755      *
2756      * @param      p   the point.
2757      * @return null if the component does not contain the position.
2758      * If there is no child component at the requested point and the
2759      * point is within the bounds of the container the container itself
2760      * is returned.


< prev index next >