1/*
2 * Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package java.awt;
26
27import java.awt.dnd.DropTarget;
28
29import java.awt.event.*;
30
31import java.awt.peer.ContainerPeer;
32import java.awt.peer.ComponentPeer;
33import java.awt.peer.LightweightPeer;
34
35import java.beans.PropertyChangeListener;
36
37import java.io.IOException;
38import java.io.ObjectInputStream;
39import java.io.ObjectOutputStream;
40import java.io.ObjectStreamField;
41import java.io.PrintStream;
42import java.io.PrintWriter;
43
44import java.lang.ref.WeakReference;
45import java.security.AccessController;
46
47import java.util.ArrayList;
48import java.util.EventListener;
49import java.util.HashSet;
50import java.util.Set;
51
52import javax.accessibility.*;
53
54import sun.util.logging.PlatformLogger;
55
56import sun.awt.AppContext;
57import sun.awt.AWTAccessor;
58import sun.awt.PeerEvent;
59import sun.awt.SunToolkit;
60
61import sun.awt.dnd.SunDropTargetEvent;
62
63import sun.java2d.pipe.Region;
64
65import sun.security.action.GetBooleanAction;
66
67/**
68 * A generic Abstract Window Toolkit(AWT) container object is a component
69 * that can contain other AWT components.
70 * <p>
71 * Components added to a container are tracked in a list.  The order
72 * of the list will define the components' front-to-back stacking order
73 * within the container.  If no index is specified when adding a
74 * component to a container, it will be added to the end of the list
75 * (and hence to the bottom of the stacking order).
76 * <p>
77 * <b>Note</b>: For details on the focus subsystem, see
78 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html">
79 * How to Use the Focus Subsystem</a>,
80 * a section in <em>The Java Tutorial</em>, and the
81 * <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
82 * for more information.
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 */
91public 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 ArrayList<>();
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
124     * value is null, this Container inherits its policy from its focus-cycle-
125     * root ancestor. If all such ancestors of this Container have null
126     * policies, then the current KeyboardFocusManager's default policy is
127     * used. If the value is non-null, this policy will be inherited by all
128     * focus-cycle-root children that have no keyboard-traversal policy of
129     * their own (as will, recursively, their focus-cycle-root children).
130     * <p>
131     * If this Container is not a focus cycle root, the value will be
132     * remembered, but will not be used or inherited by this or any other
133     * Containers until this Container is made a focus cycle root.
134     *
135     * @see #setFocusTraversalPolicy
136     * @see #getFocusTraversalPolicy
137     * @since 1.4
138     */
139    private transient FocusTraversalPolicy focusTraversalPolicy;
140
141    /**
142     * Indicates whether this Component is the root of a focus traversal cycle.
143     * Once focus enters a traversal cycle, typically it cannot leave it via
144     * focus traversal unless one of the up- or down-cycle keys is pressed.
145     * Normal traversal is limited to this Container, and all of this
146     * Container's descendants that are not descendants of inferior focus cycle
147     * roots.
148     *
149     * @see #setFocusCycleRoot
150     * @see #isFocusCycleRoot
151     * @since 1.4
152     */
153    private boolean focusCycleRoot = false;
154
155
156    /**
157     * Stores the value of focusTraversalPolicyProvider property.
158     * @since 1.5
159     * @see #setFocusTraversalPolicyProvider
160     */
161    private boolean focusTraversalPolicyProvider;
162
163    // keeps track of the threads that are printing this component
164    private transient Set<Thread> printingThreads;
165    // True if there is at least one thread that's printing this component
166    private transient boolean printing = false;
167
168    transient ContainerListener containerListener;
169
170    /* HierarchyListener and HierarchyBoundsListener support */
171    transient int listeningChildren;
172    transient int listeningBoundsChildren;
173    transient int descendantsCount;
174
175    /* Non-opaque window support -- see Window.setLayersOpaque */
176    transient Color preserveBackgroundColor = null;
177
178    /**
179     * JDK 1.1 serialVersionUID
180     */
181    private static final long serialVersionUID = 4613797578919906343L;
182
183    /**
184     * A constant which toggles one of the controllable behaviors
185     * of {@code getMouseEventTarget}. It is used to specify whether
186     * the method can return the Container on which it is originally called
187     * in case if none of its children are the current mouse event targets.
188     *
189     * @see #getMouseEventTarget(int, int, boolean)
190     */
191    static final boolean INCLUDE_SELF = true;
192
193    /**
194     * A constant which toggles one of the controllable behaviors
195     * of {@code getMouseEventTarget}. It is used to specify whether
196     * the method should search only lightweight components.
197     *
198     * @see #getMouseEventTarget(int, int, boolean)
199     */
200    static final boolean SEARCH_HEAVYWEIGHTS = true;
201
202    /*
203     * Number of HW or LW components in this container (including
204     * all descendant containers).
205     */
206    private transient int numOfHWComponents = 0;
207    private transient int numOfLWComponents = 0;
208
209    private static final PlatformLogger mixingLog = PlatformLogger.getLogger("java.awt.mixing.Container");
210
211    /**
212     * @serialField ncomponents                     int
213     *       The number of components in this container.
214     *       This value can be null.
215     * @serialField component                       Component[]
216     *       The components in this container.
217     * @serialField layoutMgr                       LayoutManager
218     *       Layout manager for this container.
219     * @serialField dispatcher                      LightweightDispatcher
220     *       Event router for lightweight components.  If this container
221     *       is native, this dispatcher takes care of forwarding and
222     *       retargeting the events to lightweight components contained
223     *       (if any).
224     * @serialField maxSize                         Dimension
225     *       Maximum size of this Container.
226     * @serialField focusCycleRoot                  boolean
227     *       Indicates whether this Component is the root of a focus traversal cycle.
228     *       Once focus enters a traversal cycle, typically it cannot leave it via
229     *       focus traversal unless one of the up- or down-cycle keys is pressed.
230     *       Normal traversal is limited to this Container, and all of this
231     *       Container's descendants that are not descendants of inferior focus cycle
232     *       roots.
233     * @serialField containerSerializedDataVersion  int
234     *       Container Serial Data Version.
235     * @serialField focusTraversalPolicyProvider    boolean
236     *       Stores the value of focusTraversalPolicyProvider property.
237     */
238    private static final ObjectStreamField[] serialPersistentFields = {
239        new ObjectStreamField("ncomponents", Integer.TYPE),
240        new ObjectStreamField("component", Component[].class),
241        new ObjectStreamField("layoutMgr", LayoutManager.class),
242        new ObjectStreamField("dispatcher", LightweightDispatcher.class),
243        new ObjectStreamField("maxSize", Dimension.class),
244        new ObjectStreamField("focusCycleRoot", Boolean.TYPE),
245        new ObjectStreamField("containerSerializedDataVersion", Integer.TYPE),
246        new ObjectStreamField("focusTraversalPolicyProvider", Boolean.TYPE),
247    };
248
249    static {
250        /* ensure that the necessary native libraries are loaded */
251        Toolkit.loadLibraries();
252        if (!GraphicsEnvironment.isHeadless()) {
253            initIDs();
254        }
255
256        AWTAccessor.setContainerAccessor(new AWTAccessor.ContainerAccessor() {
257            @Override
258            public void validateUnconditionally(Container cont) {
259                cont.validateUnconditionally();
260            }
261
262            @Override
263            public Component findComponentAt(Container cont, int x, int y,
264                    boolean ignoreEnabled) {
265                return cont.findComponentAt(x, y, ignoreEnabled);
266            }
267
268            @Override
269            public void startLWModal(Container cont) {
270                cont.startLWModal();
271            }
272
273            @Override
274            public void stopLWModal(Container cont) {
275                cont.stopLWModal();
276            }
277        });
278    }
279
280    /**
281     * Initialize JNI field and method IDs for fields that may be
282       called from C.
283     */
284    private static native void initIDs();
285
286    /**
287     * Constructs a new Container. Containers can be extended directly,
288     * but are lightweight in this case and must be contained by a parent
289     * somewhere higher up in the component tree that is native.
290     * (such as Frame for example).
291     */
292    public Container() {
293    }
294    @SuppressWarnings({"unchecked","rawtypes"})
295    void initializeFocusTraversalKeys() {
296        focusTraversalKeys = new Set[4];
297    }
298
299    /**
300     * Gets the number of components in this panel.
301     * <p>
302     * Note: This method should be called under AWT tree lock.
303     *
304     * @return    the number of components in this panel.
305     * @see       #getComponent
306     * @since     1.1
307     * @see Component#getTreeLock()
308     */
309    public int getComponentCount() {
310        return countComponents();
311    }
312
313    /**
314     * Returns the number of components in this container.
315     *
316     * @return the number of components in this container
317     * @deprecated As of JDK version 1.1,
318     * replaced by getComponentCount().
319     */
320    @Deprecated
321    public int countComponents() {
322        // This method is not synchronized under AWT tree lock.
323        // Instead, the calling code is responsible for the
324        // synchronization. See 6784816 for details.
325        return component.size();
326    }
327
328    /**
329     * Gets the nth component in this container.
330     * <p>
331     * Note: This method should be called under AWT tree lock.
332     *
333     * @param      n   the index of the component to get.
334     * @return     the n<sup>th</sup> component in this container.
335     * @exception  ArrayIndexOutOfBoundsException
336     *                 if the n<sup>th</sup> value does not exist.
337     * @see Component#getTreeLock()
338     */
339    public Component getComponent(int n) {
340        // This method is not synchronized under AWT tree lock.
341        // Instead, the calling code is responsible for the
342        // synchronization. See 6784816 for details.
343        try {
344            return component.get(n);
345        } catch (IndexOutOfBoundsException z) {
346            throw new ArrayIndexOutOfBoundsException("No such child: " + n);
347        }
348    }
349
350    /**
351     * Gets all the components in this container.
352     * <p>
353     * Note: This method should be called under AWT tree lock.
354     *
355     * @return    an array of all the components in this container.
356     * @see Component#getTreeLock()
357     */
358    public Component[] getComponents() {
359        // This method is not synchronized under AWT tree lock.
360        // Instead, the calling code is responsible for the
361        // synchronization. See 6784816 for details.
362        return getComponents_NoClientCode();
363    }
364
365    // NOTE: This method may be called by privileged threads.
366    //       This functionality is implemented in a package-private method
367    //       to insure that it cannot be overridden by client subclasses.
368    //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
369    final Component[] getComponents_NoClientCode() {
370        return component.toArray(EMPTY_ARRAY);
371    }
372
373    /*
374     * Wrapper for getComponents() method with a proper synchronization.
375     */
376    Component[] getComponentsSync() {
377        synchronized (getTreeLock()) {
378            return getComponents();
379        }
380    }
381
382    /**
383     * Determines the insets of this container, which indicate the size
384     * of the container's border.
385     * <p>
386     * A {@code Frame} object, for example, has a top inset that
387     * corresponds to the height of the frame's title bar.
388     * @return    the insets of this container.
389     * @see       Insets
390     * @see       LayoutManager
391     * @since     1.1
392     */
393    public Insets getInsets() {
394        return insets();
395    }
396
397    /**
398     * Returns the insets for this container.
399     *
400     * @deprecated As of JDK version 1.1,
401     * replaced by {@code getInsets()}.
402     * @return the insets for this container
403     */
404    @Deprecated
405    public Insets insets() {
406        ComponentPeer peer = this.peer;
407        if (peer instanceof ContainerPeer) {
408            ContainerPeer cpeer = (ContainerPeer)peer;
409            return (Insets)cpeer.getInsets().clone();
410        }
411        return new Insets(0, 0, 0, 0);
412    }
413
414    /**
415     * Appends the specified component to the end of this container.
416     * This is a convenience method for {@link #addImpl}.
417     * <p>
418     * This method changes layout-related information, and therefore,
419     * invalidates the component hierarchy. If the container has already been
420     * displayed, the hierarchy must be validated thereafter in order to
421     * display the added component.
422     *
423     * @param     comp   the component to be added
424     * @exception NullPointerException if {@code comp} is {@code null}
425     * @see #addImpl
426     * @see #invalidate
427     * @see #validate
428     * @see javax.swing.JComponent#revalidate()
429     * @return    the component argument
430     */
431    public Component add(Component comp) {
432        addImpl(comp, null, -1);
433        return comp;
434    }
435
436    /**
437     * Adds the specified component to this container.
438     * This is a convenience method for {@link #addImpl}.
439     * <p>
440     * This method is obsolete as of 1.1.  Please use the
441     * method {@code add(Component, Object)} instead.
442     * <p>
443     * This method changes layout-related information, and therefore,
444     * invalidates the component hierarchy. If the container has already been
445     * displayed, the hierarchy must be validated thereafter in order to
446     * display the added component.
447     *
448     * @param  name the name of the component to be added
449     * @param  comp the component to be added
450     * @return the component added
451     * @exception NullPointerException if {@code comp} is {@code null}
452     * @see #add(Component, Object)
453     * @see #invalidate
454     */
455    public Component add(String name, Component comp) {
456        addImpl(comp, name, -1);
457        return comp;
458    }
459
460    /**
461     * Adds the specified component to this container at the given
462     * position.
463     * This is a convenience method for {@link #addImpl}.
464     * <p>
465     * This method changes layout-related information, and therefore,
466     * invalidates the component hierarchy. If the container has already been
467     * displayed, the hierarchy must be validated thereafter in order to
468     * display the added component.
469     *
470     *
471     * @param     comp   the component to be added
472     * @param     index    the position at which to insert the component,
473     *                   or {@code -1} to append the component to the end
474     * @exception NullPointerException if {@code comp} is {@code null}
475     * @exception IllegalArgumentException if {@code index} is invalid (see
476     *            {@link #addImpl} for details)
477     * @return    the component {@code comp}
478     * @see #addImpl
479     * @see #remove
480     * @see #invalidate
481     * @see #validate
482     * @see javax.swing.JComponent#revalidate()
483     */
484    public Component add(Component comp, int index) {
485        addImpl(comp, null, index);
486        return comp;
487    }
488
489    /**
490     * Checks that the component
491     * isn't supposed to be added into itself.
492     */
493    private void checkAddToSelf(Component comp){
494        if (comp instanceof Container) {
495            for (Container cn = this; cn != null; cn=cn.parent) {
496                if (cn == comp) {
497                    throw new IllegalArgumentException("adding container's parent to itself");
498                }
499            }
500        }
501    }
502
503    /**
504     * Checks that the component is not a Window instance.
505     */
506    private void checkNotAWindow(Component comp){
507        if (comp instanceof Window) {
508            throw new IllegalArgumentException("adding a window to a container");
509        }
510    }
511
512    /**
513     * Checks that the component comp can be added to this container
514     * Checks :  index in bounds of container's size,
515     * comp is not one of this container's parents,
516     * and comp is not a window.
517     * Comp and container must be on the same GraphicsDevice.
518     * if comp is container, all sub-components must be on
519     * same GraphicsDevice.
520     *
521     * @since 1.5
522     */
523    private void checkAdding(Component comp, int index) {
524        checkTreeLock();
525
526        GraphicsConfiguration thisGC = getGraphicsConfiguration();
527
528        if (index > component.size() || index < 0) {
529            throw new IllegalArgumentException("illegal component position");
530        }
531        if (comp.parent == this) {
532            if (index == component.size()) {
533                throw new IllegalArgumentException("illegal component position " +
534                                                   index + " should be less than " + component.size());
535            }
536        }
537        checkAddToSelf(comp);
538        checkNotAWindow(comp);
539
540        Window thisTopLevel = getContainingWindow();
541        Window compTopLevel = comp.getContainingWindow();
542        if (thisTopLevel != compTopLevel) {
543            throw new IllegalArgumentException("component and container should be in the same top-level window");
544        }
545        if (thisGC != null) {
546            comp.checkGD(thisGC.getDevice().getIDstring());
547        }
548    }
549
550    /**
551     * Removes component comp from this container without making unnecessary changes
552     * and generating unnecessary events. This function intended to perform optimized
553     * remove, for example, if newParent and current parent are the same it just changes
554     * index without calling removeNotify.
555     * Note: Should be called while holding treeLock
556     * Returns whether removeNotify was invoked
557     * @since: 1.5
558     */
559    private boolean removeDelicately(Component comp, Container newParent, int newIndex) {
560        checkTreeLock();
561
562        int index = getComponentZOrder(comp);
563        boolean needRemoveNotify = isRemoveNotifyNeeded(comp, this, newParent);
564        if (needRemoveNotify) {
565            comp.removeNotify();
566        }
567        if (newParent != this) {
568            if (layoutMgr != null) {
569                layoutMgr.removeLayoutComponent(comp);
570            }
571            adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK,
572                                    -comp.numListening(AWTEvent.HIERARCHY_EVENT_MASK));
573            adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK,
574                                    -comp.numListening(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
575            adjustDescendants(-(comp.countHierarchyMembers()));
576
577            comp.parent = null;
578            if (needRemoveNotify) {
579                comp.setGraphicsConfiguration(null);
580            }
581            component.remove(index);
582
583            invalidateIfValid();
584        } else {
585            // We should remove component and then
586            // add it by the newIndex without newIndex decrement if even we shift components to the left
587            // after remove. Consult the rules below:
588            // 2->4: 012345 -> 013425, 2->5: 012345 -> 013452
589            // 4->2: 012345 -> 014235
590            component.remove(index);
591            component.add(newIndex, comp);
592        }
593        if (comp.parent == null) { // was actually removed
594            if (containerListener != null ||
595                (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 ||
596                Toolkit.enabledOnToolkit(AWTEvent.CONTAINER_EVENT_MASK)) {
597                ContainerEvent e = new ContainerEvent(this,
598                                                      ContainerEvent.COMPONENT_REMOVED,
599                                                      comp);
600                dispatchEvent(e);
601
602            }
603            comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, comp,
604                                       this, HierarchyEvent.PARENT_CHANGED,
605                                       Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
606            if (peer != null && layoutMgr == null && isVisible()) {
607                updateCursorImmediately();
608            }
609        }
610        return needRemoveNotify;
611    }
612
613    /**
614     * Checks whether this container can contain component which is focus owner.
615     * Verifies that container is enable and showing, and if it is focus cycle root
616     * its FTP allows component to be focus owner
617     * @since 1.5
618     */
619    boolean canContainFocusOwner(Component focusOwnerCandidate) {
620        if (!(isEnabled() && isDisplayable()
621              && isVisible() && isFocusable()))
622        {
623            return false;
624        }
625        if (isFocusCycleRoot()) {
626            FocusTraversalPolicy policy = getFocusTraversalPolicy();
627            if (policy instanceof DefaultFocusTraversalPolicy) {
628                if (!((DefaultFocusTraversalPolicy)policy).accept(focusOwnerCandidate)) {
629                    return false;
630                }
631            }
632        }
633        synchronized(getTreeLock()) {
634            if (parent != null) {
635                return parent.canContainFocusOwner(focusOwnerCandidate);
636            }
637        }
638        return true;
639    }
640
641    /**
642     * Checks whether or not this container has heavyweight children.
643     * Note: Should be called while holding tree lock
644     * @return true if there is at least one heavyweight children in a container, false otherwise
645     * @since 1.5
646     */
647    final boolean hasHeavyweightDescendants() {
648        checkTreeLock();
649        return numOfHWComponents > 0;
650    }
651
652    /**
653     * Checks whether or not this container has lightweight children.
654     * Note: Should be called while holding tree lock
655     * @return true if there is at least one lightweight children in a container, false otherwise
656     * @since 1.7
657     */
658    final boolean hasLightweightDescendants() {
659        checkTreeLock();
660        return numOfLWComponents > 0;
661    }
662
663    /**
664     * Returns closest heavyweight component to this container. If this container is heavyweight
665     * returns this.
666     * @since 1.5
667     */
668    Container getHeavyweightContainer() {
669        checkTreeLock();
670        if (peer != null && !(peer instanceof LightweightPeer)) {
671            return this;
672        } else {
673            return getNativeContainer();
674        }
675    }
676
677    /**
678     * Detects whether or not remove from current parent and adding to new parent requires call of
679     * removeNotify on the component. Since removeNotify destroys native window this might (not)
680     * be required. For example, if new container and old containers are the same we don't need to
681     * destroy native window.
682     * @since: 1.5
683     */
684    private static boolean isRemoveNotifyNeeded(Component comp, Container oldContainer, Container newContainer) {
685        if (oldContainer == null) { // Component didn't have parent - no removeNotify
686            return false;
687        }
688        if (comp.peer == null) { // Component didn't have peer - no removeNotify
689            return false;
690        }
691        if (newContainer.peer == null) {
692            // Component has peer but new Container doesn't - call removeNotify
693            return true;
694        }
695
696        // If component is lightweight non-Container or lightweight Container with all but heavyweight
697        // children there is no need to call remove notify
698        if (comp.isLightweight()) {
699            boolean isContainer = comp instanceof Container;
700
701            if (!isContainer || (isContainer && !((Container)comp).hasHeavyweightDescendants())) {
702                return false;
703            }
704        }
705
706        // If this point is reached, then the comp is either a HW or a LW container with HW descendants.
707
708        // All three components have peers, check for peer change
709        Container newNativeContainer = oldContainer.getHeavyweightContainer();
710        Container oldNativeContainer = newContainer.getHeavyweightContainer();
711        if (newNativeContainer != oldNativeContainer) {
712            // Native containers change - check whether or not current platform supports
713            // changing of widget hierarchy on native level without recreation.
714            // The current implementation forbids reparenting of LW containers with HW descendants
715            // into another native container w/o destroying the peers. Actually such an operation
716            // is quite rare. If we ever need to save the peers, we'll have to slightly change the
717            // addDelicately() method in order to handle such LW containers recursively, reparenting
718            // each HW descendant independently.
719            return !comp.peer.isReparentSupported();
720        } else {
721            return false;
722        }
723    }
724
725    /**
726     * Moves the specified component to the specified z-order index in
727     * the container. The z-order determines the order that components
728     * are painted; the component with the highest z-order paints first
729     * and the component with the lowest z-order paints last.
730     * Where components overlap, the component with the lower
731     * z-order paints over the component with the higher z-order.
732     * <p>
733     * If the component is a child of some other container, it is
734     * removed from that container before being added to this container.
735     * The important difference between this method and
736     * {@code java.awt.Container.add(Component, int)} is that this method
737     * doesn't call {@code removeNotify} on the component while
738     * removing it from its previous container unless necessary and when
739     * allowed by the underlying native windowing system. This way, if the
740     * component has the keyboard focus, it maintains the focus when
741     * moved to the new position.
742     * <p>
743     * This property is guaranteed to apply only to lightweight
744     * non-{@code Container} components.
745     * <p>
746     * This method changes layout-related information, and therefore,
747     * invalidates the component hierarchy.
748     * <p>
749     * <b>Note</b>: Not all platforms support changing the z-order of
750     * heavyweight components from one container into another without
751     * the call to {@code removeNotify}. There is no way to detect
752     * whether a platform supports this, so developers shouldn't make
753     * any assumptions.
754     *
755     * @param     comp the component to be moved
756     * @param     index the position in the container's list to
757     *            insert the component, where {@code getComponentCount()}
758     *            appends to the end
759     * @exception NullPointerException if {@code comp} is
760     *            {@code null}
761     * @exception IllegalArgumentException if {@code comp} is one of the
762     *            container's parents
763     * @exception IllegalArgumentException if {@code index} is not in
764     *            the range {@code [0, getComponentCount()]} for moving
765     *            between containers, or not in the range
766     *            {@code [0, getComponentCount()-1]} for moving inside
767     *            a container
768     * @exception IllegalArgumentException if adding a container to itself
769     * @exception IllegalArgumentException if adding a {@code Window}
770     *            to a container
771     * @see #getComponentZOrder(java.awt.Component)
772     * @see #invalidate
773     * @since 1.5
774     */
775    public void setComponentZOrder(Component comp, int index) {
776         synchronized (getTreeLock()) {
777             // Store parent because remove will clear it
778             Container curParent = comp.parent;
779             int oldZindex = getComponentZOrder(comp);
780
781             if (curParent == this && index == oldZindex) {
782                 return;
783             }
784             checkAdding(comp, index);
785
786             boolean peerRecreated = (curParent != null) ?
787                 curParent.removeDelicately(comp, this, index) : false;
788
789             addDelicately(comp, curParent, index);
790
791             // If the oldZindex == -1, the component gets inserted,
792             // rather than it changes its z-order.
793             if (!peerRecreated && oldZindex != -1) {
794                 // The new 'index' cannot be == -1.
795                 // It gets checked at the checkAdding() method.
796                 // Therefore both oldZIndex and index denote
797                 // some existing positions at this point and
798                 // this is actually a Z-order changing.
799                 comp.mixOnZOrderChanging(oldZindex, index);
800             }
801         }
802    }
803
804    /**
805     * Traverses the tree of components and reparents children heavyweight component
806     * to new heavyweight parent.
807     * @since 1.5
808     */
809    @SuppressWarnings("deprecation")
810    private void reparentTraverse(ContainerPeer parentPeer, Container child) {
811        checkTreeLock();
812
813        for (int i = 0; i < child.getComponentCount(); i++) {
814            Component comp = child.getComponent(i);
815            if (comp.isLightweight()) {
816                // If components is lightweight check if it is container
817                // If it is container it might contain heavyweight children we need to reparent
818                if (comp instanceof Container) {
819                    reparentTraverse(parentPeer, (Container)comp);
820                }
821            } else {
822                // Q: Need to update NativeInLightFixer?
823                comp.peer.reparent(parentPeer);
824            }
825        }
826    }
827
828    /**
829     * Reparents child component peer to this container peer.
830     * Container must be heavyweight.
831     * @since 1.5
832     */
833    @SuppressWarnings("deprecation")
834    private void reparentChild(Component comp) {
835        checkTreeLock();
836        if (comp == null) {
837            return;
838        }
839        if (comp.isLightweight()) {
840            // If component is lightweight container we need to reparent all its explicit  heavyweight children
841            if (comp instanceof Container) {
842                // Traverse component's tree till depth-first until encountering heavyweight component
843                reparentTraverse((ContainerPeer)peer, (Container)comp);
844            }
845        } else {
846            comp.peer.reparent((ContainerPeer) peer);
847        }
848    }
849
850    /**
851     * Adds component to this container. Tries to minimize side effects of this adding -
852     * doesn't call remove notify if it is not required.
853     * @since 1.5
854     */
855    private void addDelicately(Component comp, Container curParent, int index) {
856        checkTreeLock();
857
858        // Check if moving between containers
859        if (curParent != this) {
860            //index == -1 means add to the end.
861            if (index == -1) {
862                component.add(comp);
863            } else {
864                component.add(index, comp);
865            }
866            comp.parent = this;
867            comp.setGraphicsConfiguration(getGraphicsConfiguration());
868
869            adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK,
870                                    comp.numListening(AWTEvent.HIERARCHY_EVENT_MASK));
871            adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK,
872                                    comp.numListening(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
873            adjustDescendants(comp.countHierarchyMembers());
874        } else {
875            if (index < component.size()) {
876                component.set(index, comp);
877            }
878        }
879
880        invalidateIfValid();
881        if (peer != null) {
882            if (comp.peer == null) { // Remove notify was called or it didn't have peer - create new one
883                comp.addNotify();
884            } else { // Both container and child have peers, it means child peer should be reparented.
885                // In both cases we need to reparent native widgets.
886                Container newNativeContainer = getHeavyweightContainer();
887                Container oldNativeContainer = curParent.getHeavyweightContainer();
888                if (oldNativeContainer != newNativeContainer) {
889                    // Native container changed - need to reparent native widgets
890                    newNativeContainer.reparentChild(comp);
891                }
892                comp.updateZOrder();
893
894                if (!comp.isLightweight() && isLightweight()) {
895                    // If component is heavyweight and one of the containers is lightweight
896                    // the location of the component should be fixed.
897                    comp.relocateComponent();
898                }
899            }
900        }
901        if (curParent != this) {
902            /* Notify the layout manager of the added component. */
903            if (layoutMgr != null) {
904                if (layoutMgr instanceof LayoutManager2) {
905                    ((LayoutManager2)layoutMgr).addLayoutComponent(comp, null);
906                } else {
907                    layoutMgr.addLayoutComponent(null, comp);
908                }
909            }
910            if (containerListener != null ||
911                (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 ||
912                Toolkit.enabledOnToolkit(AWTEvent.CONTAINER_EVENT_MASK)) {
913                ContainerEvent e = new ContainerEvent(this,
914                                                      ContainerEvent.COMPONENT_ADDED,
915                                                      comp);
916                dispatchEvent(e);
917            }
918            comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, comp,
919                                       this, HierarchyEvent.PARENT_CHANGED,
920                                       Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
921
922            // If component is focus owner or parent container of focus owner check that after reparenting
923            // focus owner moved out if new container prohibit this kind of focus owner.
924            if (comp.isFocusOwner() && !comp.canBeFocusOwnerRecursively()) {
925                comp.transferFocus();
926            } else if (comp instanceof Container) {
927                Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
928                if (focusOwner != null && isParentOf(focusOwner) && !focusOwner.canBeFocusOwnerRecursively()) {
929                    focusOwner.transferFocus();
930                }
931            }
932        } else {
933            comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, comp,
934                                       this, HierarchyEvent.HIERARCHY_CHANGED,
935                                       Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
936        }
937
938        if (peer != null && layoutMgr == null && isVisible()) {
939            updateCursorImmediately();
940        }
941    }
942
943    /**
944     * Returns the z-order index of the component inside the container.
945     * The higher a component is in the z-order hierarchy, the lower
946     * its index.  The component with the lowest z-order index is
947     * painted last, above all other child components.
948     *
949     * @param comp the component being queried
950     * @return  the z-order index of the component; otherwise
951     *          returns -1 if the component is {@code null}
952     *          or doesn't belong to the container
953     * @see #setComponentZOrder(java.awt.Component, int)
954     * @since 1.5
955     */
956    public int getComponentZOrder(Component comp) {
957        if (comp == null) {
958            return -1;
959        }
960        synchronized(getTreeLock()) {
961            // Quick check - container should be immediate parent of the component
962            if (comp.parent != this) {
963                return -1;
964            }
965            return component.indexOf(comp);
966        }
967    }
968
969    /**
970     * Adds the specified component to the end of this container.
971     * Also notifies the layout manager to add the component to
972     * this container's layout using the specified constraints object.
973     * This is a convenience method for {@link #addImpl}.
974     * <p>
975     * This method changes layout-related information, and therefore,
976     * invalidates the component hierarchy. If the container has already been
977     * displayed, the hierarchy must be validated thereafter in order to
978     * display the added component.
979     *
980     *
981     * @param     comp the component to be added
982     * @param     constraints an object expressing
983     *                  layout constraints for this component
984     * @exception NullPointerException if {@code comp} is {@code null}
985     * @see #addImpl
986     * @see #invalidate
987     * @see #validate
988     * @see javax.swing.JComponent#revalidate()
989     * @see       LayoutManager
990     * @since     1.1
991     */
992    public void add(Component comp, Object constraints) {
993        addImpl(comp, constraints, -1);
994    }
995
996    /**
997     * Adds the specified component to this container with the specified
998     * constraints at the specified index.  Also notifies the layout
999     * manager to add the component to the this container's layout using
1000     * the specified constraints object.
1001     * This is a convenience method for {@link #addImpl}.
1002     * <p>
1003     * This method changes layout-related information, and therefore,
1004     * invalidates the component hierarchy. If the container has already been
1005     * displayed, the hierarchy must be validated thereafter in order to
1006     * display the added component.
1007     *
1008     *
1009     * @param comp the component to be added
1010     * @param constraints an object expressing layout constraints for this
1011     * @param index the position in the container's list at which to insert
1012     * the component; {@code -1} means insert at the end
1013     * component
1014     * @exception NullPointerException if {@code comp} is {@code null}
1015     * @exception IllegalArgumentException if {@code index} is invalid (see
1016     *            {@link #addImpl} for details)
1017     * @see #addImpl
1018     * @see #invalidate
1019     * @see #validate
1020     * @see javax.swing.JComponent#revalidate()
1021     * @see #remove
1022     * @see LayoutManager
1023     */
1024    public void add(Component comp, Object constraints, int index) {
1025       addImpl(comp, constraints, index);
1026    }
1027
1028    /**
1029     * Adds the specified component to this container at the specified
1030     * index. This method also notifies the layout manager to add
1031     * the component to this container's layout using the specified
1032     * constraints object via the {@code addLayoutComponent}
1033     * method.
1034     * <p>
1035     * The constraints are
1036     * defined by the particular layout manager being used.  For
1037     * example, the {@code BorderLayout} class defines five
1038     * constraints: {@code BorderLayout.NORTH},
1039     * {@code BorderLayout.SOUTH}, {@code BorderLayout.EAST},
1040     * {@code BorderLayout.WEST}, and {@code BorderLayout.CENTER}.
1041     * <p>
1042     * The {@code GridBagLayout} class requires a
1043     * {@code GridBagConstraints} object.  Failure to pass
1044     * the correct type of constraints object results in an
1045     * {@code IllegalArgumentException}.
1046     * <p>
1047     * If the current layout manager implements {@code LayoutManager2}, then
1048     * {@link LayoutManager2#addLayoutComponent(Component,Object)} is invoked on
1049     * it. If the current layout manager does not implement
1050     * {@code LayoutManager2}, and constraints is a {@code String}, then
1051     * {@link LayoutManager#addLayoutComponent(String,Component)} is invoked on it.
1052     * <p>
1053     * If the component is not an ancestor of this container and has a non-null
1054     * parent, it is removed from its current parent before it is added to this
1055     * container.
1056     * <p>
1057     * This is the method to override if a program needs to track
1058     * every add request to a container as all other add methods defer
1059     * to this one. An overriding method should
1060     * usually include a call to the superclass's version of the method:
1061     *
1062     * <blockquote>
1063     * {@code super.addImpl(comp, constraints, index)}
1064     * </blockquote>
1065     * <p>
1066     * This method changes layout-related information, and therefore,
1067     * invalidates the component hierarchy. If the container has already been
1068     * displayed, the hierarchy must be validated thereafter in order to
1069     * display the added component.
1070     *
1071     * @param     comp       the component to be added
1072     * @param     constraints an object expressing layout constraints
1073     *                 for this component
1074     * @param     index the position in the container's list at which to
1075     *                 insert the component, where {@code -1}
1076     *                 means append to the end
1077     * @exception IllegalArgumentException if {@code index} is invalid;
1078     *            if {@code comp} is a child of this container, the valid
1079     *            range is {@code [-1, getComponentCount()-1]}; if component is
1080     *            not a child of this container, the valid range is
1081     *            {@code [-1, getComponentCount()]}
1082     *
1083     * @exception IllegalArgumentException if {@code comp} is an ancestor of
1084     *                                     this container
1085     * @exception IllegalArgumentException if adding a window to a container
1086     * @exception NullPointerException if {@code comp} is {@code null}
1087     * @see       #add(Component)
1088     * @see       #add(Component, int)
1089     * @see       #add(Component, java.lang.Object)
1090     * @see #invalidate
1091     * @see       LayoutManager
1092     * @see       LayoutManager2
1093     * @since     1.1
1094     */
1095    protected void addImpl(Component comp, Object constraints, int index) {
1096        synchronized (getTreeLock()) {
1097            /* Check for correct arguments:  index in bounds,
1098             * comp cannot be one of this container's parents,
1099             * and comp cannot be a window.
1100             * comp and container must be on the same GraphicsDevice.
1101             * if comp is container, all sub-components must be on
1102             * same GraphicsDevice.
1103             */
1104            GraphicsConfiguration thisGC = this.getGraphicsConfiguration();
1105
1106            if (index > component.size() || (index < 0 && index != -1)) {
1107                throw new IllegalArgumentException(
1108                          "illegal component position");
1109            }
1110            checkAddToSelf(comp);
1111            checkNotAWindow(comp);
1112            /* Reparent the component and tidy up the tree's state. */
1113            if (comp.parent != null) {
1114                comp.parent.remove(comp);
1115                if (index > component.size()) {
1116                    throw new IllegalArgumentException("illegal component position");
1117                }
1118            }
1119            if (thisGC != null) {
1120                comp.checkGD(thisGC.getDevice().getIDstring());
1121            }
1122
1123
1124
1125            //index == -1 means add to the end.
1126            if (index == -1) {
1127                component.add(comp);
1128            } else {
1129                component.add(index, comp);
1130            }
1131            comp.parent = this;
1132            comp.setGraphicsConfiguration(thisGC);
1133
1134            adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK,
1135                comp.numListening(AWTEvent.HIERARCHY_EVENT_MASK));
1136            adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK,
1137                comp.numListening(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
1138            adjustDescendants(comp.countHierarchyMembers());
1139
1140            invalidateIfValid();
1141            if (peer != null) {
1142                comp.addNotify();
1143            }
1144
1145            /* Notify the layout manager of the added component. */
1146            if (layoutMgr != null) {
1147                if (layoutMgr instanceof LayoutManager2) {
1148                    ((LayoutManager2)layoutMgr).addLayoutComponent(comp, constraints);
1149                } else if (constraints instanceof String) {
1150                    layoutMgr.addLayoutComponent((String)constraints, comp);
1151                }
1152            }
1153            if (containerListener != null ||
1154                (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 ||
1155                Toolkit.enabledOnToolkit(AWTEvent.CONTAINER_EVENT_MASK)) {
1156                ContainerEvent e = new ContainerEvent(this,
1157                                     ContainerEvent.COMPONENT_ADDED,
1158                                     comp);
1159                dispatchEvent(e);
1160            }
1161
1162            comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, comp,
1163                                       this, HierarchyEvent.PARENT_CHANGED,
1164                                       Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
1165            if (peer != null && layoutMgr == null && isVisible()) {
1166                updateCursorImmediately();
1167            }
1168        }
1169    }
1170
1171    @Override
1172    boolean updateGraphicsData(GraphicsConfiguration gc) {
1173        checkTreeLock();
1174
1175        boolean ret = super.updateGraphicsData(gc);
1176
1177        for (Component comp : component) {
1178            if (comp != null) {
1179                ret |= comp.updateGraphicsData(gc);
1180            }
1181        }
1182        return ret;
1183    }
1184
1185    /**
1186     * Checks that all Components that this Container contains are on
1187     * the same GraphicsDevice as this Container.  If not, throws an
1188     * IllegalArgumentException.
1189     */
1190    void checkGD(String stringID) {
1191        for (Component comp : component) {
1192            if (comp != null) {
1193                comp.checkGD(stringID);
1194            }
1195        }
1196    }
1197
1198    /**
1199     * Removes the component, specified by {@code index},
1200     * from this container.
1201     * This method also notifies the layout manager to remove the
1202     * component from this container's layout via the
1203     * {@code removeLayoutComponent} method.
1204     * <p>
1205     * This method changes layout-related information, and therefore,
1206     * invalidates the component hierarchy. If the container has already been
1207     * displayed, the hierarchy must be validated thereafter in order to
1208     * reflect the changes.
1209     *
1210     *
1211     * @param     index   the index of the component to be removed
1212     * @throws ArrayIndexOutOfBoundsException if {@code index} is not in
1213     *         range {@code [0, getComponentCount()-1]}
1214     * @see #add
1215     * @see #invalidate
1216     * @see #validate
1217     * @see #getComponentCount
1218     * @since 1.1
1219     */
1220    public void remove(int index) {
1221        synchronized (getTreeLock()) {
1222            if (index < 0  || index >= component.size()) {
1223                throw new ArrayIndexOutOfBoundsException(index);
1224            }
1225            Component comp = component.get(index);
1226            if (peer != null) {
1227                comp.removeNotify();
1228            }
1229            if (layoutMgr != null) {
1230                layoutMgr.removeLayoutComponent(comp);
1231            }
1232
1233            adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK,
1234                -comp.numListening(AWTEvent.HIERARCHY_EVENT_MASK));
1235            adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK,
1236                -comp.numListening(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
1237            adjustDescendants(-(comp.countHierarchyMembers()));
1238
1239            comp.parent = null;
1240            component.remove(index);
1241            comp.setGraphicsConfiguration(null);
1242
1243            invalidateIfValid();
1244            if (containerListener != null ||
1245                (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 ||
1246                Toolkit.enabledOnToolkit(AWTEvent.CONTAINER_EVENT_MASK)) {
1247                ContainerEvent e = new ContainerEvent(this,
1248                                     ContainerEvent.COMPONENT_REMOVED,
1249                                     comp);
1250                dispatchEvent(e);
1251            }
1252
1253            comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, comp,
1254                                       this, HierarchyEvent.PARENT_CHANGED,
1255                                       Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
1256            if (peer != null && layoutMgr == null && isVisible()) {
1257                updateCursorImmediately();
1258            }
1259        }
1260    }
1261
1262    /**
1263     * Removes the specified component from this container.
1264     * This method also notifies the layout manager to remove the
1265     * component from this container's layout via the
1266     * {@code removeLayoutComponent} method.
1267     * <p>
1268     * This method changes layout-related information, and therefore,
1269     * invalidates the component hierarchy. If the container has already been
1270     * displayed, the hierarchy must be validated thereafter in order to
1271     * reflect the changes.
1272     *
1273     * @param comp the component to be removed
1274     * @throws NullPointerException if {@code comp} is {@code null}
1275     * @see #add
1276     * @see #invalidate
1277     * @see #validate
1278     * @see #remove(int)
1279     */
1280    public void remove(Component comp) {
1281        synchronized (getTreeLock()) {
1282            if (comp.parent == this)  {
1283                int index = component.indexOf(comp);
1284                if (index >= 0) {
1285                    remove(index);
1286                }
1287            }
1288        }
1289    }
1290
1291    /**
1292     * Removes all the components from this container.
1293     * This method also notifies the layout manager to remove the
1294     * components from this container's layout via the
1295     * {@code removeLayoutComponent} method.
1296     * <p>
1297     * This method changes layout-related information, and therefore,
1298     * invalidates the component hierarchy. If the container has already been
1299     * displayed, the hierarchy must be validated thereafter in order to
1300     * reflect the changes.
1301     *
1302     * @see #add
1303     * @see #remove
1304     * @see #invalidate
1305     */
1306    public void removeAll() {
1307        synchronized (getTreeLock()) {
1308            adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK,
1309                                    -listeningChildren);
1310            adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK,
1311                                    -listeningBoundsChildren);
1312            adjustDescendants(-descendantsCount);
1313
1314            while (!component.isEmpty()) {
1315                Component comp = component.remove(component.size()-1);
1316
1317                if (peer != null) {
1318                    comp.removeNotify();
1319                }
1320                if (layoutMgr != null) {
1321                    layoutMgr.removeLayoutComponent(comp);
1322                }
1323                comp.parent = null;
1324                comp.setGraphicsConfiguration(null);
1325                if (containerListener != null ||
1326                   (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 ||
1327                    Toolkit.enabledOnToolkit(AWTEvent.CONTAINER_EVENT_MASK)) {
1328                    ContainerEvent e = new ContainerEvent(this,
1329                                     ContainerEvent.COMPONENT_REMOVED,
1330                                     comp);
1331                    dispatchEvent(e);
1332                }
1333
1334                comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
1335                                           comp, this,
1336                                           HierarchyEvent.PARENT_CHANGED,
1337                                           Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
1338            }
1339            if (peer != null && layoutMgr == null && isVisible()) {
1340                updateCursorImmediately();
1341            }
1342            invalidateIfValid();
1343        }
1344    }
1345
1346    // Should only be called while holding tree lock
1347    int numListening(long mask) {
1348        int superListening = super.numListening(mask);
1349
1350        if (mask == AWTEvent.HIERARCHY_EVENT_MASK) {
1351            if (eventLog.isLoggable(PlatformLogger.Level.FINE)) {
1352                // Verify listeningChildren is correct
1353                int sum = 0;
1354                for (Component comp : component) {
1355                    sum += comp.numListening(mask);
1356                }
1357                if (listeningChildren != sum) {
1358                    eventLog.fine("Assertion (listeningChildren == sum) failed");
1359                }
1360            }
1361            return listeningChildren + superListening;
1362        } else if (mask == AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) {
1363            if (eventLog.isLoggable(PlatformLogger.Level.FINE)) {
1364                // Verify listeningBoundsChildren is correct
1365                int sum = 0;
1366                for (Component comp : component) {
1367                    sum += comp.numListening(mask);
1368                }
1369                if (listeningBoundsChildren != sum) {
1370                    eventLog.fine("Assertion (listeningBoundsChildren == sum) failed");
1371                }
1372            }
1373            return listeningBoundsChildren + superListening;
1374        } else {
1375            // assert false;
1376            if (eventLog.isLoggable(PlatformLogger.Level.FINE)) {
1377                eventLog.fine("This code must never be reached");
1378            }
1379            return superListening;
1380        }
1381    }
1382
1383    // Should only be called while holding tree lock
1384    void adjustListeningChildren(long mask, int num) {
1385        if (eventLog.isLoggable(PlatformLogger.Level.FINE)) {
1386            boolean toAssert = (mask == AWTEvent.HIERARCHY_EVENT_MASK ||
1387                                mask == AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK ||
1388                                mask == (AWTEvent.HIERARCHY_EVENT_MASK |
1389                                         AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
1390            if (!toAssert) {
1391                eventLog.fine("Assertion failed");
1392            }
1393        }
1394
1395        if (num == 0)
1396            return;
1397
1398        if ((mask & AWTEvent.HIERARCHY_EVENT_MASK) != 0) {
1399            listeningChildren += num;
1400        }
1401        if ((mask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0) {
1402            listeningBoundsChildren += num;
1403        }
1404
1405        adjustListeningChildrenOnParent(mask, num);
1406    }
1407
1408    // Should only be called while holding tree lock
1409    void adjustDescendants(int num) {
1410        if (num == 0)
1411            return;
1412
1413        descendantsCount += num;
1414        adjustDescendantsOnParent(num);
1415    }
1416
1417    // Should only be called while holding tree lock
1418    void adjustDescendantsOnParent(int num) {
1419        if (parent != null) {
1420            parent.adjustDescendants(num);
1421        }
1422    }
1423
1424    // Should only be called while holding tree lock
1425    int countHierarchyMembers() {
1426        if (log.isLoggable(PlatformLogger.Level.FINE)) {
1427            // Verify descendantsCount is correct
1428            int sum = 0;
1429            for (Component comp : component) {
1430                sum += comp.countHierarchyMembers();
1431            }
1432            if (descendantsCount != sum) {
1433                log.fine("Assertion (descendantsCount == sum) failed");
1434            }
1435        }
1436        return descendantsCount + 1;
1437    }
1438
1439    private int getListenersCount(int id, boolean enabledOnToolkit) {
1440        checkTreeLock();
1441        if (enabledOnToolkit) {
1442            return descendantsCount;
1443        }
1444        switch (id) {
1445          case HierarchyEvent.HIERARCHY_CHANGED:
1446            return listeningChildren;
1447          case HierarchyEvent.ANCESTOR_MOVED:
1448          case HierarchyEvent.ANCESTOR_RESIZED:
1449            return listeningBoundsChildren;
1450          default:
1451            return 0;
1452        }
1453    }
1454
1455    final int createHierarchyEvents(int id, Component changed,
1456        Container changedParent, long changeFlags, boolean enabledOnToolkit)
1457    {
1458        checkTreeLock();
1459        int listeners = getListenersCount(id, enabledOnToolkit);
1460
1461        for (int count = listeners, i = 0; count > 0; i++) {
1462            count -= component.get(i).createHierarchyEvents(id, changed,
1463                changedParent, changeFlags, enabledOnToolkit);
1464        }
1465        return listeners +
1466            super.createHierarchyEvents(id, changed, changedParent,
1467                                        changeFlags, enabledOnToolkit);
1468    }
1469
1470    final void createChildHierarchyEvents(int id, long changeFlags,
1471        boolean enabledOnToolkit)
1472    {
1473        checkTreeLock();
1474        if (component.isEmpty()) {
1475            return;
1476        }
1477        int listeners = getListenersCount(id, enabledOnToolkit);
1478
1479        for (int count = listeners, i = 0; count > 0; i++) {
1480            count -= component.get(i).createHierarchyEvents(id, this, parent,
1481                changeFlags, enabledOnToolkit);
1482        }
1483    }
1484
1485    /**
1486     * Gets the layout manager for this container.
1487     *
1488     * @see #doLayout
1489     * @see #setLayout
1490     * @return the current layout manager for this container
1491     */
1492    public LayoutManager getLayout() {
1493        return layoutMgr;
1494    }
1495
1496    /**
1497     * Sets the layout manager for this container.
1498     * <p>
1499     * This method changes layout-related information, and therefore,
1500     * invalidates the component hierarchy.
1501     *
1502     * @param mgr the specified layout manager
1503     * @see #doLayout
1504     * @see #getLayout
1505     * @see #invalidate
1506     */
1507    public void setLayout(LayoutManager mgr) {
1508        layoutMgr = mgr;
1509        invalidateIfValid();
1510    }
1511
1512    /**
1513     * Causes this container to lay out its components.  Most programs
1514     * should not call this method directly, but should invoke
1515     * the {@code validate} method instead.
1516     * @see LayoutManager#layoutContainer
1517     * @see #setLayout
1518     * @see #validate
1519     * @since 1.1
1520     */
1521    public void doLayout() {
1522        layout();
1523    }
1524
1525    /**
1526     * @deprecated As of JDK version 1.1,
1527     * replaced by {@code doLayout()}.
1528     */
1529    @Deprecated
1530    public void layout() {
1531        LayoutManager layoutMgr = this.layoutMgr;
1532        if (layoutMgr != null) {
1533            layoutMgr.layoutContainer(this);
1534        }
1535    }
1536
1537    /**
1538     * Indicates if this container is a <i>validate root</i>.
1539     * <p>
1540     * Layout-related changes, such as bounds of the validate root descendants,
1541     * do not affect the layout of the validate root parent. This peculiarity
1542     * enables the {@code invalidate()} method to stop invalidating the
1543     * component hierarchy when the method encounters a validate root. However,
1544     * to preserve backward compatibility this new optimized behavior is
1545     * enabled only when the {@code java.awt.smartInvalidate} system property
1546     * value is set to {@code true}.
1547     * <p>
1548     * If a component hierarchy contains validate roots and the new optimized
1549     * {@code invalidate()} behavior is enabled, the {@code validate()} method
1550     * must be invoked on the validate root of a previously invalidated
1551     * component to restore the validity of the hierarchy later. Otherwise,
1552     * calling the {@code validate()} method on the top-level container (such
1553     * as a {@code Frame} object) should be used to restore the validity of the
1554     * component hierarchy.
1555     * <p>
1556     * The {@code Window} class and the {@code Applet} class are the validate
1557     * roots in AWT.  Swing introduces more validate roots.
1558     *
1559     * @return whether this container is a validate root
1560     * @see #invalidate
1561     * @see java.awt.Component#invalidate
1562     * @see javax.swing.JComponent#isValidateRoot
1563     * @see javax.swing.JComponent#revalidate
1564     * @since 1.7
1565     */
1566    public boolean isValidateRoot() {
1567        return false;
1568    }
1569
1570    private static final boolean isJavaAwtSmartInvalidate;
1571    static {
1572        // Don't lazy-read because every app uses invalidate()
1573        isJavaAwtSmartInvalidate = AccessController.doPrivileged(
1574                new GetBooleanAction("java.awt.smartInvalidate"));
1575    }
1576
1577    /**
1578     * Invalidates the parent of the container unless the container
1579     * is a validate root.
1580     */
1581    @Override
1582    void invalidateParent() {
1583        if (!isJavaAwtSmartInvalidate || !isValidateRoot()) {
1584            super.invalidateParent();
1585        }
1586    }
1587
1588    /**
1589     * Invalidates the container.
1590     * <p>
1591     * If the {@code LayoutManager} installed on this container is an instance
1592     * of the {@code LayoutManager2} interface, then
1593     * the {@link LayoutManager2#invalidateLayout(Container)} method is invoked
1594     * on it supplying this {@code Container} as the argument.
1595     * <p>
1596     * Afterwards this method marks this container invalid, and invalidates its
1597     * ancestors. See the {@link Component#invalidate} method for more details.
1598     *
1599     * @see #validate
1600     * @see #layout
1601     * @see LayoutManager2
1602     */
1603    @Override
1604    public void invalidate() {
1605        LayoutManager layoutMgr = this.layoutMgr;
1606        if (layoutMgr instanceof LayoutManager2) {
1607            LayoutManager2 lm = (LayoutManager2) layoutMgr;
1608            lm.invalidateLayout(this);
1609        }
1610        super.invalidate();
1611    }
1612
1613    /**
1614     * Validates this container and all of its subcomponents.
1615     * <p>
1616     * Validating a container means laying out its subcomponents.
1617     * Layout-related changes, such as setting the bounds of a component, or
1618     * adding a component to the container, invalidate the container
1619     * automatically.  Note that the ancestors of the container may be
1620     * invalidated also (see {@link Component#invalidate} for details.)
1621     * Therefore, to restore the validity of the hierarchy, the {@code
1622     * validate()} method should be invoked on the top-most invalid
1623     * container of the hierarchy.
1624     * <p>
1625     * Validating the container may be a quite time-consuming operation. For
1626     * performance reasons a developer may postpone the validation of the
1627     * hierarchy till a set of layout-related operations completes, e.g. after
1628     * adding all the children to the container.
1629     * <p>
1630     * If this {@code Container} is not valid, this method invokes
1631     * the {@code validateTree} method and marks this {@code Container}
1632     * as valid. Otherwise, no action is performed.
1633     *
1634     * @see #add(java.awt.Component)
1635     * @see #invalidate
1636     * @see Container#isValidateRoot
1637     * @see javax.swing.JComponent#revalidate()
1638     * @see #validateTree
1639     */
1640    public void validate() {
1641        boolean updateCur = false;
1642        synchronized (getTreeLock()) {
1643            if ((!isValid() || descendUnconditionallyWhenValidating)
1644                    && peer != null)
1645            {
1646                ContainerPeer p = null;
1647                if (peer instanceof ContainerPeer) {
1648                    p = (ContainerPeer) peer;
1649                }
1650                if (p != null) {
1651                    p.beginValidate();
1652                }
1653                validateTree();
1654                if (p != null) {
1655                    p.endValidate();
1656                    // Avoid updating cursor if this is an internal call.
1657                    // See validateUnconditionally() for details.
1658                    if (!descendUnconditionallyWhenValidating) {
1659                        updateCur = isVisible();
1660                    }
1661                }
1662            }
1663        }
1664        if (updateCur) {
1665            updateCursorImmediately();
1666        }
1667    }
1668
1669    /**
1670     * Indicates whether valid containers should also traverse their
1671     * children and call the validateTree() method on them.
1672     *
1673     * Synchronization: TreeLock.
1674     *
1675     * The field is allowed to be static as long as the TreeLock itself is
1676     * static.
1677     *
1678     * @see #validateUnconditionally()
1679     */
1680    private static boolean descendUnconditionallyWhenValidating = false;
1681
1682    /**
1683     * Unconditionally validate the component hierarchy.
1684     */
1685    final void validateUnconditionally() {
1686        boolean updateCur = false;
1687        synchronized (getTreeLock()) {
1688            descendUnconditionallyWhenValidating = true;
1689
1690            validate();
1691            if (peer instanceof ContainerPeer) {
1692                updateCur = isVisible();
1693            }
1694
1695            descendUnconditionallyWhenValidating = false;
1696        }
1697        if (updateCur) {
1698            updateCursorImmediately();
1699        }
1700    }
1701
1702    /**
1703     * Recursively descends the container tree and recomputes the
1704     * layout for any subtrees marked as needing it (those marked as
1705     * invalid).  Synchronization should be provided by the method
1706     * that calls this one:  {@code validate}.
1707     *
1708     * @see #doLayout
1709     * @see #validate
1710     */
1711    protected void validateTree() {
1712        checkTreeLock();
1713        if (!isValid() || descendUnconditionallyWhenValidating) {
1714            if (peer instanceof ContainerPeer) {
1715                ((ContainerPeer)peer).beginLayout();
1716            }
1717            if (!isValid()) {
1718                doLayout();
1719            }
1720            for (int i = 0; i < component.size(); i++) {
1721                Component comp = component.get(i);
1722                if (   (comp instanceof Container)
1723                       && !(comp instanceof Window)
1724                       && (!comp.isValid() ||
1725                           descendUnconditionallyWhenValidating))
1726                {
1727                    ((Container)comp).validateTree();
1728                } else {
1729                    comp.validate();
1730                }
1731            }
1732            if (peer instanceof ContainerPeer) {
1733                ((ContainerPeer)peer).endLayout();
1734            }
1735        }
1736        super.validate();
1737    }
1738
1739    /**
1740     * Recursively descends the container tree and invalidates all
1741     * contained components.
1742     */
1743    void invalidateTree() {
1744        synchronized (getTreeLock()) {
1745            for (int i = 0; i < component.size(); i++) {
1746                Component comp = component.get(i);
1747                if (comp instanceof Container) {
1748                    ((Container)comp).invalidateTree();
1749                }
1750                else {
1751                    comp.invalidateIfValid();
1752                }
1753            }
1754            invalidateIfValid();
1755        }
1756    }
1757
1758    /**
1759     * Sets the font of this container.
1760     * <p>
1761     * This method changes layout-related information, and therefore,
1762     * invalidates the component hierarchy.
1763     *
1764     * @param f The font to become this container's font.
1765     * @see Component#getFont
1766     * @see #invalidate
1767     * @since 1.0
1768     */
1769    public void setFont(Font f) {
1770        boolean shouldinvalidate = false;
1771
1772        Font oldfont = getFont();
1773        super.setFont(f);
1774        Font newfont = getFont();
1775        if (newfont != oldfont && (oldfont == null ||
1776                                   !oldfont.equals(newfont))) {
1777            invalidateTree();
1778        }
1779    }
1780
1781    /**
1782     * Returns the preferred size of this container.  If the preferred size has
1783     * not been set explicitly by {@link Component#setPreferredSize(Dimension)}
1784     * and this {@code Container} has a {@code non-null} {@link LayoutManager},
1785     * then {@link LayoutManager#preferredLayoutSize(Container)}
1786     * is used to calculate the preferred size.
1787     *
1788     * <p>Note: some implementations may cache the value returned from the
1789     * {@code LayoutManager}.  Implementations that cache need not invoke
1790     * {@code preferredLayoutSize} on the {@code LayoutManager} every time
1791     * this method is invoked, rather the {@code LayoutManager} will only
1792     * be queried after the {@code Container} becomes invalid.
1793     *
1794     * @return    an instance of {@code Dimension} that represents
1795     *                the preferred size of this container.
1796     * @see       #getMinimumSize
1797     * @see       #getMaximumSize
1798     * @see       #getLayout
1799     * @see       LayoutManager#preferredLayoutSize(Container)
1800     * @see       Component#getPreferredSize
1801     */
1802    public Dimension getPreferredSize() {
1803        return preferredSize();
1804    }
1805
1806    /**
1807     * @deprecated As of JDK version 1.1,
1808     * replaced by {@code getPreferredSize()}.
1809     */
1810    @Deprecated
1811    public Dimension preferredSize() {
1812        /* Avoid grabbing the lock if a reasonable cached size value
1813         * is available.
1814         */
1815        Dimension dim = prefSize;
1816        if (dim == null || !(isPreferredSizeSet() || isValid())) {
1817            synchronized (getTreeLock()) {
1818                prefSize = (layoutMgr != null) ?
1819                    layoutMgr.preferredLayoutSize(this) :
1820                    super.preferredSize();
1821                dim = prefSize;
1822            }
1823        }
1824        if (dim != null){
1825            return new Dimension(dim);
1826        }
1827        else{
1828            return dim;
1829        }
1830    }
1831
1832    /**
1833     * Returns the minimum size of this container.  If the minimum size has
1834     * not been set explicitly by {@link Component#setMinimumSize(Dimension)}
1835     * and this {@code Container} has a {@code non-null} {@link LayoutManager},
1836     * then {@link LayoutManager#minimumLayoutSize(Container)}
1837     * is used to calculate the minimum size.
1838     *
1839     * <p>Note: some implementations may cache the value returned from the
1840     * {@code LayoutManager}.  Implementations that cache need not invoke
1841     * {@code minimumLayoutSize} on the {@code LayoutManager} every time
1842     * this method is invoked, rather the {@code LayoutManager} will only
1843     * be queried after the {@code Container} becomes invalid.
1844     *
1845     * @return    an instance of {@code Dimension} that represents
1846     *                the minimum size of this container.
1847     * @see       #getPreferredSize
1848     * @see       #getMaximumSize
1849     * @see       #getLayout
1850     * @see       LayoutManager#minimumLayoutSize(Container)
1851     * @see       Component#getMinimumSize
1852     * @since     1.1
1853     */
1854    public Dimension getMinimumSize() {
1855        return minimumSize();
1856    }
1857
1858    /**
1859     * @deprecated As of JDK version 1.1,
1860     * replaced by {@code getMinimumSize()}.
1861     */
1862    @Deprecated
1863    public Dimension minimumSize() {
1864        /* Avoid grabbing the lock if a reasonable cached size value
1865         * is available.
1866         */
1867        Dimension dim = minSize;
1868        if (dim == null || !(isMinimumSizeSet() || isValid())) {
1869            synchronized (getTreeLock()) {
1870                minSize = (layoutMgr != null) ?
1871                    layoutMgr.minimumLayoutSize(this) :
1872                    super.minimumSize();
1873                dim = minSize;
1874            }
1875        }
1876        if (dim != null){
1877            return new Dimension(dim);
1878        }
1879        else{
1880            return dim;
1881        }
1882    }
1883
1884    /**
1885     * Returns the maximum size of this container.  If the maximum size has
1886     * not been set explicitly by {@link Component#setMaximumSize(Dimension)}
1887     * and the {@link LayoutManager} installed on this {@code Container}
1888     * is an instance of {@link LayoutManager2}, then
1889     * {@link LayoutManager2#maximumLayoutSize(Container)}
1890     * is used to calculate the maximum size.
1891     *
1892     * <p>Note: some implementations may cache the value returned from the
1893     * {@code LayoutManager2}.  Implementations that cache need not invoke
1894     * {@code maximumLayoutSize} on the {@code LayoutManager2} every time
1895     * this method is invoked, rather the {@code LayoutManager2} will only
1896     * be queried after the {@code Container} becomes invalid.
1897     *
1898     * @return    an instance of {@code Dimension} that represents
1899     *                the maximum size of this container.
1900     * @see       #getPreferredSize
1901     * @see       #getMinimumSize
1902     * @see       #getLayout
1903     * @see       LayoutManager2#maximumLayoutSize(Container)
1904     * @see       Component#getMaximumSize
1905     */
1906    public Dimension getMaximumSize() {
1907        /* Avoid grabbing the lock if a reasonable cached size value
1908         * is available.
1909         */
1910        Dimension dim = maxSize;
1911        if (dim == null || !(isMaximumSizeSet() || isValid())) {
1912            synchronized (getTreeLock()) {
1913               if (layoutMgr instanceof LayoutManager2) {
1914                    LayoutManager2 lm = (LayoutManager2) layoutMgr;
1915                    maxSize = lm.maximumLayoutSize(this);
1916               } else {
1917                    maxSize = super.getMaximumSize();
1918               }
1919               dim = maxSize;
1920            }
1921        }
1922        if (dim != null){
1923            return new Dimension(dim);
1924        }
1925        else{
1926            return dim;
1927        }
1928    }
1929
1930    /**
1931     * Returns the alignment along the x axis.  This specifies how
1932     * the component would like to be aligned relative to other
1933     * components.  The value should be a number between 0 and 1
1934     * where 0 represents alignment along the origin, 1 is aligned
1935     * the furthest away from the origin, 0.5 is centered, etc.
1936     */
1937    public float getAlignmentX() {
1938        float xAlign;
1939        if (layoutMgr instanceof LayoutManager2) {
1940            synchronized (getTreeLock()) {
1941                LayoutManager2 lm = (LayoutManager2) layoutMgr;
1942                xAlign = lm.getLayoutAlignmentX(this);
1943            }
1944        } else {
1945            xAlign = super.getAlignmentX();
1946        }
1947        return xAlign;
1948    }
1949
1950    /**
1951     * Returns the alignment along the y axis.  This specifies how
1952     * the component would like to be aligned relative to other
1953     * components.  The value should be a number between 0 and 1
1954     * where 0 represents alignment along the origin, 1 is aligned
1955     * the furthest away from the origin, 0.5 is centered, etc.
1956     */
1957    public float getAlignmentY() {
1958        float yAlign;
1959        if (layoutMgr instanceof LayoutManager2) {
1960            synchronized (getTreeLock()) {
1961                LayoutManager2 lm = (LayoutManager2) layoutMgr;
1962                yAlign = lm.getLayoutAlignmentY(this);
1963            }
1964        } else {
1965            yAlign = super.getAlignmentY();
1966        }
1967        return yAlign;
1968    }
1969
1970    /**
1971     * Paints the container. This forwards the paint to any lightweight
1972     * components that are children of this container. If this method is
1973     * reimplemented, super.paint(g) should be called so that lightweight
1974     * components are properly rendered. If a child component is entirely
1975     * clipped by the current clipping setting in g, paint() will not be
1976     * forwarded to that child.
1977     *
1978     * @param g the specified Graphics window
1979     * @see   Component#update(Graphics)
1980     */
1981    public void paint(Graphics g) {
1982        if (isShowing()) {
1983            synchronized (getObjectLock()) {
1984                if (printing) {
1985                    if (printingThreads.contains(Thread.currentThread())) {
1986                        return;
1987                    }
1988                }
1989            }
1990
1991            // The container is showing on screen and
1992            // this paint() is not called from print().
1993            // Paint self and forward the paint to lightweight subcomponents.
1994
1995            // super.paint(); -- Don't bother, since it's a NOP.
1996
1997            GraphicsCallback.PaintCallback.getInstance().
1998                runComponents(getComponentsSync(), g, GraphicsCallback.LIGHTWEIGHTS);
1999        }
2000    }
2001
2002    /**
2003     * Updates the container.  This forwards the update to any lightweight
2004     * components that are children of this container.  If this method is
2005     * reimplemented, super.update(g) should be called so that lightweight
2006     * components are properly rendered.  If a child component is entirely
2007     * clipped by the current clipping setting in g, update() will not be
2008     * forwarded to that child.
2009     *
2010     * @param g the specified Graphics window
2011     * @see   Component#update(Graphics)
2012     */
2013    public void update(Graphics g) {
2014        if (isShowing()) {
2015            if (! (peer instanceof LightweightPeer)) {
2016                g.clearRect(0, 0, width, height);
2017            }
2018            paint(g);
2019        }
2020    }
2021
2022    /**
2023     * Prints the container. This forwards the print to any lightweight
2024     * components that are children of this container. If this method is
2025     * reimplemented, super.print(g) should be called so that lightweight
2026     * components are properly rendered. If a child component is entirely
2027     * clipped by the current clipping setting in g, print() will not be
2028     * forwarded to that child.
2029     *
2030     * @param g the specified Graphics window
2031     * @see   Component#update(Graphics)
2032     */
2033    public void print(Graphics g) {
2034        if (isShowing()) {
2035            Thread t = Thread.currentThread();
2036            try {
2037                synchronized (getObjectLock()) {
2038                    if (printingThreads == null) {
2039                        printingThreads = new HashSet<>();
2040                    }
2041                    printingThreads.add(t);
2042                    printing = true;
2043                }
2044                super.print(g);  // By default, Component.print() calls paint()
2045            } finally {
2046                synchronized (getObjectLock()) {
2047                    printingThreads.remove(t);
2048                    printing = !printingThreads.isEmpty();
2049                }
2050            }
2051
2052            GraphicsCallback.PrintCallback.getInstance().
2053                runComponents(getComponentsSync(), g, GraphicsCallback.LIGHTWEIGHTS);
2054        }
2055    }
2056
2057    /**
2058     * Paints each of the components in this container.
2059     * @param     g   the graphics context.
2060     * @see       Component#paint
2061     * @see       Component#paintAll
2062     */
2063    public void paintComponents(Graphics g) {
2064        if (isShowing()) {
2065            GraphicsCallback.PaintAllCallback.getInstance().
2066                runComponents(getComponentsSync(), g, GraphicsCallback.TWO_PASSES);
2067        }
2068    }
2069
2070    /**
2071     * Simulates the peer callbacks into java.awt for printing of
2072     * lightweight Containers.
2073     * @param     g   the graphics context to use for printing.
2074     * @see       Component#printAll
2075     * @see       #printComponents
2076     */
2077    void lightweightPaint(Graphics g) {
2078        super.lightweightPaint(g);
2079        paintHeavyweightComponents(g);
2080    }
2081
2082    /**
2083     * Prints all the heavyweight subcomponents.
2084     */
2085    void paintHeavyweightComponents(Graphics g) {
2086        if (isShowing()) {
2087            GraphicsCallback.PaintHeavyweightComponentsCallback.getInstance().
2088                runComponents(getComponentsSync(), g,
2089                              GraphicsCallback.LIGHTWEIGHTS | GraphicsCallback.HEAVYWEIGHTS);
2090        }
2091    }
2092
2093    /**
2094     * Prints each of the components in this container.
2095     * @param     g   the graphics context.
2096     * @see       Component#print
2097     * @see       Component#printAll
2098     */
2099    public void printComponents(Graphics g) {
2100        if (isShowing()) {
2101            GraphicsCallback.PrintAllCallback.getInstance().
2102                runComponents(getComponentsSync(), g, GraphicsCallback.TWO_PASSES);
2103        }
2104    }
2105
2106    /**
2107     * Simulates the peer callbacks into java.awt for printing of
2108     * lightweight Containers.
2109     * @param     g   the graphics context to use for printing.
2110     * @see       Component#printAll
2111     * @see       #printComponents
2112     */
2113    void lightweightPrint(Graphics g) {
2114        super.lightweightPrint(g);
2115        printHeavyweightComponents(g);
2116    }
2117
2118    /**
2119     * Prints all the heavyweight subcomponents.
2120     */
2121    void printHeavyweightComponents(Graphics g) {
2122        if (isShowing()) {
2123            GraphicsCallback.PrintHeavyweightComponentsCallback.getInstance().
2124                runComponents(getComponentsSync(), g,
2125                              GraphicsCallback.LIGHTWEIGHTS | GraphicsCallback.HEAVYWEIGHTS);
2126        }
2127    }
2128
2129    /**
2130     * Adds the specified container listener to receive container events
2131     * from this container.
2132     * If l is null, no exception is thrown and no action is performed.
2133     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
2134     * >AWT Threading Issues</a> for details on AWT's threading model.
2135     *
2136     * @param    l the container listener
2137     *
2138     * @see #removeContainerListener
2139     * @see #getContainerListeners
2140     */
2141    public synchronized void addContainerListener(ContainerListener l) {
2142        if (l == null) {
2143            return;
2144        }
2145        containerListener = AWTEventMulticaster.add(containerListener, l);
2146        newEventsOnly = true;
2147    }
2148
2149    /**
2150     * Removes the specified container listener so it no longer receives
2151     * container events from this container.
2152     * If l is null, no exception is thrown and no action is performed.
2153     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
2154     * >AWT Threading Issues</a> for details on AWT's threading model.
2155     *
2156     * @param   l the container listener
2157     *
2158     * @see #addContainerListener
2159     * @see #getContainerListeners
2160     */
2161    public synchronized void removeContainerListener(ContainerListener l) {
2162        if (l == null) {
2163            return;
2164        }
2165        containerListener = AWTEventMulticaster.remove(containerListener, l);
2166    }
2167
2168    /**
2169     * Returns an array of all the container listeners
2170     * registered on this container.
2171     *
2172     * @return all of this container's {@code ContainerListener}s
2173     *         or an empty array if no container
2174     *         listeners are currently registered
2175     *
2176     * @see #addContainerListener
2177     * @see #removeContainerListener
2178     * @since 1.4
2179     */
2180    public synchronized ContainerListener[] getContainerListeners() {
2181        return getListeners(ContainerListener.class);
2182    }
2183
2184    /**
2185     * Returns an array of all the objects currently registered
2186     * as <code><em>Foo</em>Listener</code>s
2187     * upon this {@code Container}.
2188     * <code><em>Foo</em>Listener</code>s are registered using the
2189     * <code>add<em>Foo</em>Listener</code> method.
2190     *
2191     * <p>
2192     * You can specify the {@code listenerType} argument
2193     * with a class literal, such as
2194     * <code><em>Foo</em>Listener.class</code>.
2195     * For example, you can query a
2196     * {@code Container c}
2197     * for its container listeners with the following code:
2198     *
2199     * <pre>ContainerListener[] cls = (ContainerListener[])(c.getListeners(ContainerListener.class));</pre>
2200     *
2201     * If no such listeners exist, this method returns an empty array.
2202     *
2203     * @param listenerType the type of listeners requested; this parameter
2204     *          should specify an interface that descends from
2205     *          {@code java.util.EventListener}
2206     * @return an array of all objects registered as
2207     *          <code><em>Foo</em>Listener</code>s on this container,
2208     *          or an empty array if no such listeners have been added
2209     * @exception ClassCastException if {@code listenerType}
2210     *          doesn't specify a class or interface that implements
2211     *          {@code java.util.EventListener}
2212     * @exception NullPointerException if {@code listenerType} is {@code null}
2213     *
2214     * @see #getContainerListeners
2215     *
2216     * @since 1.3
2217     */
2218    public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
2219        EventListener l = null;
2220        if  (listenerType == ContainerListener.class) {
2221            l = containerListener;
2222        } else {
2223            return super.getListeners(listenerType);
2224        }
2225        return AWTEventMulticaster.getListeners(l, listenerType);
2226    }
2227
2228    // REMIND: remove when filtering is done at lower level
2229    boolean eventEnabled(AWTEvent e) {
2230        int id = e.getID();
2231
2232        if (id == ContainerEvent.COMPONENT_ADDED ||
2233            id == ContainerEvent.COMPONENT_REMOVED) {
2234            if ((eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 ||
2235                containerListener != null) {
2236                return true;
2237            }
2238            return false;
2239        }
2240        return super.eventEnabled(e);
2241    }
2242
2243    /**
2244     * Processes events on this container. If the event is a
2245     * {@code ContainerEvent}, it invokes the
2246     * {@code processContainerEvent} method, else it invokes
2247     * its superclass's {@code processEvent}.
2248     * <p>Note that if the event parameter is {@code null}
2249     * the behavior is unspecified and may result in an
2250     * exception.
2251     *
2252     * @param e the event
2253     */
2254    protected void processEvent(AWTEvent e) {
2255        if (e instanceof ContainerEvent) {
2256            processContainerEvent((ContainerEvent)e);
2257            return;
2258        }
2259        super.processEvent(e);
2260    }
2261
2262    /**
2263     * Processes container events occurring on this container by
2264     * dispatching them to any registered ContainerListener objects.
2265     * NOTE: This method will not be called unless container events
2266     * are enabled for this component; this happens when one of the
2267     * following occurs:
2268     * <ul>
2269     * <li>A ContainerListener object is registered via
2270     *     {@code addContainerListener}
2271     * <li>Container events are enabled via {@code enableEvents}
2272     * </ul>
2273     * <p>Note that if the event parameter is {@code null}
2274     * the behavior is unspecified and may result in an
2275     * exception.
2276     *
2277     * @param e the container event
2278     * @see Component#enableEvents
2279     */
2280    protected void processContainerEvent(ContainerEvent e) {
2281        ContainerListener listener = containerListener;
2282        if (listener != null) {
2283            switch(e.getID()) {
2284              case ContainerEvent.COMPONENT_ADDED:
2285                listener.componentAdded(e);
2286                break;
2287              case ContainerEvent.COMPONENT_REMOVED:
2288                listener.componentRemoved(e);
2289                break;
2290            }
2291        }
2292    }
2293
2294    /*
2295     * Dispatches an event to this component or one of its sub components.
2296     * Create ANCESTOR_RESIZED and ANCESTOR_MOVED events in response to
2297     * COMPONENT_RESIZED and COMPONENT_MOVED events. We have to do this
2298     * here instead of in processComponentEvent because ComponentEvents
2299     * may not be enabled for this Container.
2300     * @param e the event
2301     */
2302    void dispatchEventImpl(AWTEvent e) {
2303        if ((dispatcher != null) && dispatcher.dispatchEvent(e)) {
2304            // event was sent to a lightweight component.  The
2305            // native-produced event sent to the native container
2306            // must be properly disposed of by the peer, so it
2307            // gets forwarded.  If the native host has been removed
2308            // as a result of the sending the lightweight event,
2309            // the peer reference will be null.
2310            e.consume();
2311            if (peer != null) {
2312                peer.handleEvent(e);
2313            }
2314            return;
2315        }
2316
2317        super.dispatchEventImpl(e);
2318
2319        synchronized (getTreeLock()) {
2320            switch (e.getID()) {
2321              case ComponentEvent.COMPONENT_RESIZED:
2322                createChildHierarchyEvents(HierarchyEvent.ANCESTOR_RESIZED, 0,
2323                                           Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
2324                break;
2325              case ComponentEvent.COMPONENT_MOVED:
2326                createChildHierarchyEvents(HierarchyEvent.ANCESTOR_MOVED, 0,
2327                                       Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
2328                break;
2329              default:
2330                break;
2331            }
2332        }
2333    }
2334
2335    /*
2336     * Dispatches an event to this component, without trying to forward
2337     * it to any subcomponents
2338     * @param e the event
2339     */
2340    void dispatchEventToSelf(AWTEvent e) {
2341        super.dispatchEventImpl(e);
2342    }
2343
2344    /**
2345     * Fetches the top-most (deepest) lightweight component that is interested
2346     * in receiving mouse events.
2347     */
2348    Component getMouseEventTarget(int x, int y, boolean includeSelf) {
2349        return getMouseEventTarget(x, y, includeSelf,
2350                                   MouseEventTargetFilter.FILTER,
2351                                   !SEARCH_HEAVYWEIGHTS);
2352    }
2353
2354    /**
2355     * Fetches the top-most (deepest) component to receive SunDropTargetEvents.
2356     */
2357    Component getDropTargetEventTarget(int x, int y, boolean includeSelf) {
2358        return getMouseEventTarget(x, y, includeSelf,
2359                                   DropTargetEventTargetFilter.FILTER,
2360                                   SEARCH_HEAVYWEIGHTS);
2361    }
2362
2363    /**
2364     * A private version of getMouseEventTarget which has two additional
2365     * controllable behaviors. This method searches for the top-most
2366     * descendant of this container that contains the given coordinates
2367     * and is accepted by the given filter. The search will be constrained to
2368     * lightweight descendants if the last argument is {@code false}.
2369     *
2370     * @param filter EventTargetFilter instance to determine whether the
2371     *        given component is a valid target for this event.
2372     * @param searchHeavyweights if {@code false}, the method
2373     *        will bypass heavyweight components during the search.
2374     */
2375    private Component getMouseEventTarget(int x, int y, boolean includeSelf,
2376                                          EventTargetFilter filter,
2377                                          boolean searchHeavyweights) {
2378        Component comp = null;
2379        if (searchHeavyweights) {
2380            comp = getMouseEventTargetImpl(x, y, includeSelf, filter,
2381                                           SEARCH_HEAVYWEIGHTS,
2382                                           searchHeavyweights);
2383        }
2384
2385        if (comp == null || comp == this) {
2386            comp = getMouseEventTargetImpl(x, y, includeSelf, filter,
2387                                           !SEARCH_HEAVYWEIGHTS,
2388                                           searchHeavyweights);
2389        }
2390
2391        return comp;
2392    }
2393
2394    /**
2395     * A private version of getMouseEventTarget which has three additional
2396     * controllable behaviors. This method searches for the top-most
2397     * descendant of this container that contains the given coordinates
2398     * and is accepted by the given filter. The search will be constrained to
2399     * descendants of only lightweight children or only heavyweight children
2400     * of this container depending on searchHeavyweightChildren. The search will
2401     * be constrained to only lightweight descendants of the searched children
2402     * of this container if searchHeavyweightDescendants is {@code false}.
2403     *
2404     * @param filter EventTargetFilter instance to determine whether the
2405     *        selected component is a valid target for this event.
2406     * @param searchHeavyweightChildren if {@code true}, the method
2407     *        will bypass immediate lightweight children during the search.
2408     *        If {@code false}, the methods will bypass immediate
2409     *        heavyweight children during the search.
2410     * @param searchHeavyweightDescendants if {@code false}, the method
2411     *        will bypass heavyweight descendants which are not immediate
2412     *        children during the search. If {@code true}, the method
2413     *        will traverse both lightweight and heavyweight descendants during
2414     *        the search.
2415     */
2416    private Component getMouseEventTargetImpl(int x, int y, boolean includeSelf,
2417                                         EventTargetFilter filter,
2418                                         boolean searchHeavyweightChildren,
2419                                         boolean searchHeavyweightDescendants) {
2420        synchronized (getTreeLock()) {
2421
2422            for (int i = 0; i < component.size(); i++) {
2423                Component comp = component.get(i);
2424                if (comp != null && comp.visible &&
2425                    ((!searchHeavyweightChildren &&
2426                      comp.peer instanceof LightweightPeer) ||
2427                     (searchHeavyweightChildren &&
2428                      !(comp.peer instanceof LightweightPeer))) &&
2429                    comp.contains(x - comp.x, y - comp.y)) {
2430
2431                    // found a component that intersects the point, see if there
2432                    // is a deeper possibility.
2433                    if (comp instanceof Container) {
2434                        Container child = (Container) comp;
2435                        Component deeper = child.getMouseEventTarget(
2436                                x - child.x,
2437                                y - child.y,
2438                                includeSelf,
2439                                filter,
2440                                searchHeavyweightDescendants);
2441                        if (deeper != null) {
2442                            return deeper;
2443                        }
2444                    } else {
2445                        if (filter.accept(comp)) {
2446                            // there isn't a deeper target, but this component
2447                            // is a target
2448                            return comp;
2449                        }
2450                    }
2451                }
2452            }
2453
2454            boolean isPeerOK;
2455            boolean isMouseOverMe;
2456
2457            isPeerOK = (peer instanceof LightweightPeer) || includeSelf;
2458            isMouseOverMe = contains(x,y);
2459
2460            // didn't find a child target, return this component if it's
2461            // a possible target
2462            if (isMouseOverMe && isPeerOK && filter.accept(this)) {
2463                return this;
2464            }
2465            // no possible target
2466            return null;
2467        }
2468    }
2469
2470    static interface EventTargetFilter {
2471        boolean accept(final Component comp);
2472    }
2473
2474    static class MouseEventTargetFilter implements EventTargetFilter {
2475        static final EventTargetFilter FILTER = new MouseEventTargetFilter();
2476
2477        private MouseEventTargetFilter() {}
2478
2479        public boolean accept(final Component comp) {
2480            return (comp.eventMask & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0
2481                || (comp.eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0
2482                || (comp.eventMask & AWTEvent.MOUSE_WHEEL_EVENT_MASK) != 0
2483                || comp.mouseListener != null
2484                || comp.mouseMotionListener != null
2485                || comp.mouseWheelListener != null;
2486        }
2487    }
2488
2489    static class DropTargetEventTargetFilter implements EventTargetFilter {
2490        static final EventTargetFilter FILTER = new DropTargetEventTargetFilter();
2491
2492        private DropTargetEventTargetFilter() {}
2493
2494        public boolean accept(final Component comp) {
2495            DropTarget dt = comp.getDropTarget();
2496            return dt != null && dt.isActive();
2497        }
2498    }
2499
2500    /**
2501     * This is called by lightweight components that want the containing
2502     * windowed parent to enable some kind of events on their behalf.
2503     * This is needed for events that are normally only dispatched to
2504     * windows to be accepted so that they can be forwarded downward to
2505     * the lightweight component that has enabled them.
2506     */
2507    void proxyEnableEvents(long events) {
2508        if (peer instanceof LightweightPeer) {
2509            // this container is lightweight.... continue sending it
2510            // upward.
2511            if (parent != null) {
2512                parent.proxyEnableEvents(events);
2513            }
2514        } else {
2515            // This is a native container, so it needs to host
2516            // one of it's children.  If this function is called before
2517            // a peer has been created we don't yet have a dispatcher
2518            // because it has not yet been determined if this instance
2519            // is lightweight.
2520            if (dispatcher != null) {
2521                dispatcher.enableEvents(events);
2522            }
2523        }
2524    }
2525
2526    /**
2527     * @deprecated As of JDK version 1.1,
2528     * replaced by {@code dispatchEvent(AWTEvent e)}
2529     */
2530    @Deprecated
2531    public void deliverEvent(Event e) {
2532        Component comp = getComponentAt(e.x, e.y);
2533        if ((comp != null) && (comp != this)) {
2534            e.translate(-comp.x, -comp.y);
2535            comp.deliverEvent(e);
2536        } else {
2537            postEvent(e);
2538        }
2539    }
2540
2541    /**
2542     * Locates the component that contains the x,y position.  The
2543     * top-most child component is returned in the case where there
2544     * is overlap in the components.  This is determined by finding
2545     * the component closest to the index 0 that claims to contain
2546     * the given point via Component.contains(), except that Components
2547     * which have native peers take precedence over those which do not
2548     * (i.e., lightweight Components).
2549     *
2550     * @param x the <i>x</i> coordinate
2551     * @param y the <i>y</i> coordinate
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)}.
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} 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}'s
2607     * coordinate space if the {@code Container} is under the mouse pointer,
2608     * otherwise returns {@code null}.
2609     * This method is similar to {@link Component#getMousePosition()} with the exception
2610     * that it can take the {@code Container}'s children into account.
2611     * If {@code allowChildren} is {@code false}, this method will return
2612     * a non-null value only if the mouse pointer is above the {@code Container}
2613     * directly, not above the part obscured by children.
2614     * If {@code allowChildren} is {@code true}, this method returns
2615     * a non-null value if the mouse pointer is above {@code Container} or any
2616     * of its descendants.
2617     *
2618     * @exception HeadlessException if GraphicsEnvironment.isHeadless() returns true
2619     * @param     allowChildren true if children should be taken into account
2620     * @see       Component#getMousePosition
2621     * @return    mouse coordinates relative to this {@code Component}, or null
2622     * @since     1.5
2623     */
2624    public Point getMousePosition(boolean allowChildren) throws HeadlessException {
2625        if (GraphicsEnvironment.isHeadless()) {
2626            throw new HeadlessException();
2627        }
2628        PointerInfo pi = java.security.AccessController.doPrivileged(
2629            new java.security.PrivilegedAction<PointerInfo>() {
2630                public PointerInfo run() {
2631                    return MouseInfo.getPointerInfo();
2632                }
2633            }
2634        );
2635        synchronized (getTreeLock()) {
2636            Component inTheSameWindow = findUnderMouseInWindow(pi);
2637            if (isSameOrAncestorOf(inTheSameWindow, allowChildren)) {
2638                return  pointRelativeToComponent(pi.getLocation());
2639            }
2640            return null;
2641        }
2642    }
2643
2644    boolean isSameOrAncestorOf(Component comp, boolean allowChildren) {
2645        return this == comp || (allowChildren && isParentOf(comp));
2646    }
2647
2648    /**
2649     * Locates the visible child component that contains the specified
2650     * position.  The top-most child component is returned in the case
2651     * where there is overlap in the components.  If the containing child
2652     * component is a Container, this method will continue searching for
2653     * the deepest nested child component.  Components which are not
2654     * visible are ignored during the search.<p>
2655     *
2656     * The findComponentAt method is different from getComponentAt in
2657     * that getComponentAt only searches the Container's immediate
2658     * children; if the containing component is a Container,
2659     * findComponentAt will search that child to find a nested component.
2660     *
2661     * @param x the <i>x</i> coordinate
2662     * @param y the <i>y</i> coordinate
2663     * @return null if the component does not contain the position.
2664     * If there is no child component at the requested point and the
2665     * point is within the bounds of the container the container itself
2666     * is returned.
2667     * @see Component#contains
2668     * @see #getComponentAt
2669     * @since 1.2
2670     */
2671    public Component findComponentAt(int x, int y) {
2672        return findComponentAt(x, y, true);
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.
2761     * @throws NullPointerException if {@code p} is {@code null}
2762     * @see Component#contains
2763     * @see #getComponentAt
2764     * @since 1.2
2765     */
2766    public Component findComponentAt(Point p) {
2767        return findComponentAt(p.x, p.y);
2768    }
2769
2770    /**
2771     * Makes this Container displayable by connecting it to
2772     * a native screen resource.  Making a container displayable will
2773     * cause all of its children to be made displayable.
2774     * This method is called internally by the toolkit and should
2775     * not be called directly by programs.
2776     * @see Component#isDisplayable
2777     * @see #removeNotify
2778     */
2779    public void addNotify() {
2780        synchronized (getTreeLock()) {
2781            // addNotify() on the children may cause proxy event enabling
2782            // on this instance, so we first call super.addNotify() and
2783            // possibly create an lightweight event dispatcher before calling
2784            // addNotify() on the children which may be lightweight.
2785            super.addNotify();
2786            if (! (peer instanceof LightweightPeer)) {
2787                dispatcher = new LightweightDispatcher(this);
2788            }
2789
2790            // We shouldn't use iterator because of the Swing menu
2791            // implementation specifics:
2792            // the menu is being assigned as a child to JLayeredPane
2793            // instead of particular component so always affect
2794            // collection of component if menu is becoming shown or hidden.
2795            for (int i = 0; i < component.size(); i++) {
2796                component.get(i).addNotify();
2797            }
2798        }
2799    }
2800
2801    /**
2802     * Makes this Container undisplayable by removing its connection
2803     * to its native screen resource.  Making a container undisplayable
2804     * will cause all of its children to be made undisplayable.
2805     * This method is called by the toolkit internally and should
2806     * not be called directly by programs.
2807     * @see Component#isDisplayable
2808     * @see #addNotify
2809     */
2810    public void removeNotify() {
2811        synchronized (getTreeLock()) {
2812            // We shouldn't use iterator because of the Swing menu
2813            // implementation specifics:
2814            // the menu is being assigned as a child to JLayeredPane
2815            // instead of particular component so always affect
2816            // collection of component if menu is becoming shown or hidden.
2817            for (int i = component.size()-1 ; i >= 0 ; i--) {
2818                Component comp = component.get(i);
2819                if (comp != null) {
2820                    // Fix for 6607170.
2821                    // We want to suppress focus change on disposal
2822                    // of the focused component. But because of focus
2823                    // is asynchronous, we should suppress focus change
2824                    // on every component in case it receives native focus
2825                    // in the process of disposal.
2826                    comp.setAutoFocusTransferOnDisposal(false);
2827                    comp.removeNotify();
2828                    comp.setAutoFocusTransferOnDisposal(true);
2829                 }
2830             }
2831            // If some of the children had focus before disposal then it still has.
2832            // Auto-transfer focus to the next (or previous) component if auto-transfer
2833            // is enabled.
2834            if (containsFocus() && KeyboardFocusManager.isAutoFocusTransferEnabledFor(this)) {
2835                if (!transferFocus(false)) {
2836                    transferFocusBackward(true);
2837                }
2838            }
2839            if ( dispatcher != null ) {
2840                dispatcher.dispose();
2841                dispatcher = null;
2842            }
2843            super.removeNotify();
2844        }
2845    }
2846
2847    /**
2848     * Checks if the component is contained in the component hierarchy of
2849     * this container.
2850     * @param c the component
2851     * @return     {@code true} if it is an ancestor;
2852     *             {@code false} otherwise.
2853     * @since      1.1
2854     */
2855    public boolean isAncestorOf(Component c) {
2856        Container p;
2857        if (c == null || ((p = c.getParent()) == null)) {
2858            return false;
2859        }
2860        while (p != null) {
2861            if (p == this) {
2862                return true;
2863            }
2864            p = p.getParent();
2865        }
2866        return false;
2867    }
2868
2869    /*
2870     * The following code was added to support modal JInternalFrames
2871     * Unfortunately this code has to be added here so that we can get access to
2872     * some private AWT classes like SequencedEvent.
2873     *
2874     * The native container of the LW component has this field set
2875     * to tell it that it should block Mouse events for all LW
2876     * children except for the modal component.
2877     *
2878     * In the case of nested Modal components, we store the previous
2879     * modal component in the new modal components value of modalComp;
2880     */
2881
2882    transient Component modalComp;
2883    transient AppContext modalAppContext;
2884
2885    private void startLWModal() {
2886        // Store the app context on which this component is being shown.
2887        // Event dispatch thread of this app context will be sleeping until
2888        // we wake it by any event from hideAndDisposeHandler().
2889        modalAppContext = AppContext.getAppContext();
2890
2891        // keep the KeyEvents from being dispatched
2892        // until the focus has been transferred
2893        long time = Toolkit.getEventQueue().getMostRecentKeyEventTime();
2894        Component predictedFocusOwner = (Component.isInstanceOf(this, "javax.swing.JInternalFrame")) ? ((javax.swing.JInternalFrame)(this)).getMostRecentFocusOwner() : null;
2895        if (predictedFocusOwner != null) {
2896            KeyboardFocusManager.getCurrentKeyboardFocusManager().
2897                enqueueKeyEvents(time, predictedFocusOwner);
2898        }
2899        // We have two mechanisms for blocking: 1. If we're on the
2900        // EventDispatchThread, start a new event pump. 2. If we're
2901        // on any other thread, call wait() on the treelock.
2902        final Container nativeContainer;
2903        synchronized (getTreeLock()) {
2904            nativeContainer = getHeavyweightContainer();
2905            if (nativeContainer.modalComp != null) {
2906                this.modalComp =  nativeContainer.modalComp;
2907                nativeContainer.modalComp = this;
2908                return;
2909            }
2910            else {
2911                nativeContainer.modalComp = this;
2912            }
2913        }
2914
2915        Runnable pumpEventsForHierarchy = () -> {
2916            EventDispatchThread dispatchThread = (EventDispatchThread)Thread.currentThread();
2917            dispatchThread.pumpEventsForHierarchy(() -> nativeContainer.modalComp != null,
2918                    Container.this);
2919        };
2920
2921        if (EventQueue.isDispatchThread()) {
2922            SequencedEvent currentSequencedEvent =
2923                KeyboardFocusManager.getCurrentKeyboardFocusManager().
2924                getCurrentSequencedEvent();
2925            if (currentSequencedEvent != null) {
2926                currentSequencedEvent.dispose();
2927            }
2928
2929            pumpEventsForHierarchy.run();
2930        } else {
2931            synchronized (getTreeLock()) {
2932                Toolkit.getEventQueue().
2933                    postEvent(new PeerEvent(this,
2934                                pumpEventsForHierarchy,
2935                                PeerEvent.PRIORITY_EVENT));
2936                while (nativeContainer.modalComp != null)
2937                {
2938                    try {
2939                        getTreeLock().wait();
2940                    } catch (InterruptedException e) {
2941                        break;
2942                    }
2943                }
2944            }
2945        }
2946        if (predictedFocusOwner != null) {
2947            KeyboardFocusManager.getCurrentKeyboardFocusManager().
2948                dequeueKeyEvents(time, predictedFocusOwner);
2949        }
2950    }
2951
2952    private void stopLWModal() {
2953        synchronized (getTreeLock()) {
2954            if (modalAppContext != null) {
2955                Container nativeContainer = getHeavyweightContainer();
2956                if(nativeContainer != null) {
2957                    if (this.modalComp !=  null) {
2958                        nativeContainer.modalComp = this.modalComp;
2959                        this.modalComp = null;
2960                        return;
2961                    }
2962                    else {
2963                        nativeContainer.modalComp = null;
2964                    }
2965                }
2966                // Wake up event dispatch thread on which the dialog was
2967                // initially shown
2968                SunToolkit.postEvent(modalAppContext,
2969                        new PeerEvent(this,
2970                                new WakingRunnable(),
2971                                PeerEvent.PRIORITY_EVENT));
2972            }
2973            EventQueue.invokeLater(new WakingRunnable());
2974            getTreeLock().notifyAll();
2975        }
2976    }
2977
2978    static final class WakingRunnable implements Runnable {
2979        public void run() {
2980        }
2981    }
2982
2983    /* End of JOptionPane support code */
2984
2985    /**
2986     * Returns a string representing the state of this {@code Container}.
2987     * This method is intended to be used only for debugging purposes, and the
2988     * content and format of the returned string may vary between
2989     * implementations. The returned string may be empty but may not be
2990     * {@code null}.
2991     *
2992     * @return    the parameter string of this container
2993     */
2994    protected String paramString() {
2995        String str = super.paramString();
2996        LayoutManager layoutMgr = this.layoutMgr;
2997        if (layoutMgr != null) {
2998            str += ",layout=" + layoutMgr.getClass().getName();
2999        }
3000        return str;
3001    }
3002
3003    /**
3004     * Prints a listing of this container to the specified output
3005     * stream. The listing starts at the specified indentation.
3006     * <p>
3007     * The immediate children of the container are printed with
3008     * an indentation of {@code indent+1}.  The children
3009     * of those children are printed at {@code indent+2}
3010     * and so on.
3011     *
3012     * @param    out      a print stream
3013     * @param    indent   the number of spaces to indent
3014     * @throws   NullPointerException if {@code out} is {@code null}
3015     * @see      Component#list(java.io.PrintStream, int)
3016     * @since    1.0
3017     */
3018    public void list(PrintStream out, int indent) {
3019        super.list(out, indent);
3020        synchronized(getTreeLock()) {
3021            for (int i = 0; i < component.size(); i++) {
3022                Component comp = component.get(i);
3023                if (comp != null) {
3024                    comp.list(out, indent+1);
3025                }
3026            }
3027        }
3028    }
3029
3030    /**
3031     * Prints out a list, starting at the specified indentation,
3032     * to the specified print writer.
3033     * <p>
3034     * The immediate children of the container are printed with
3035     * an indentation of {@code indent+1}.  The children
3036     * of those children are printed at {@code indent+2}
3037     * and so on.
3038     *
3039     * @param    out      a print writer
3040     * @param    indent   the number of spaces to indent
3041     * @throws   NullPointerException if {@code out} is {@code null}
3042     * @see      Component#list(java.io.PrintWriter, int)
3043     * @since    1.1
3044     */
3045    public void list(PrintWriter out, int indent) {
3046        super.list(out, indent);
3047        synchronized(getTreeLock()) {
3048            for (int i = 0; i < component.size(); i++) {
3049                Component comp = component.get(i);
3050                if (comp != null) {
3051                    comp.list(out, indent+1);
3052                }
3053            }
3054        }
3055    }
3056
3057    /**
3058     * Sets the focus traversal keys for a given traversal operation for this
3059     * Container.
3060     * <p>
3061     * The default values for a Container's focus traversal keys are
3062     * implementation-dependent. Sun recommends that all implementations for a
3063     * particular native platform use the same default values. The
3064     * recommendations for Windows and Unix are listed below. These
3065     * recommendations are used in the Sun AWT implementations.
3066     *
3067     * <table class="striped">
3068     * <caption>Recommended default values for a Container's focus traversal
3069     * keys</caption>
3070     * <thead>
3071     * <tr>
3072     *    <th>Identifier</th>
3073     *    <th>Meaning</th>
3074     *    <th>Default</th>
3075     * </tr>
3076     * </thead>
3077     * <tbody>
3078     * <tr>
3079     *    <td>KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS</td>
3080     *    <td>Normal forward keyboard traversal</td>
3081     *    <td>TAB on KEY_PRESSED, CTRL-TAB on KEY_PRESSED</td>
3082     * </tr>
3083     * <tr>
3084     *    <td>KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS</td>
3085     *    <td>Normal reverse keyboard traversal</td>
3086     *    <td>SHIFT-TAB on KEY_PRESSED, CTRL-SHIFT-TAB on KEY_PRESSED</td>
3087     * </tr>
3088     * <tr>
3089     *    <td>KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS</td>
3090     *    <td>Go up one focus traversal cycle</td>
3091     *    <td>none</td>
3092     * </tr>
3093     * <tr>
3094     *    <td>KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS</td>
3095     *    <td>Go down one focus traversal cycle</td>
3096     *    <td>none</td>
3097     * </tr>
3098     * </tbody>
3099     * </table>
3100     *
3101     * To disable a traversal key, use an empty Set; Collections.EMPTY_SET is
3102     * recommended.
3103     * <p>
3104     * Using the AWTKeyStroke API, client code can specify on which of two
3105     * specific KeyEvents, KEY_PRESSED or KEY_RELEASED, the focus traversal
3106     * operation will occur. Regardless of which KeyEvent is specified,
3107     * however, all KeyEvents related to the focus traversal key, including the
3108     * associated KEY_TYPED event, will be consumed, and will not be dispatched
3109     * to any Container. It is a runtime error to specify a KEY_TYPED event as
3110     * mapping to a focus traversal operation, or to map the same event to
3111     * multiple default focus traversal operations.
3112     * <p>
3113     * If a value of null is specified for the Set, this Container inherits the
3114     * Set from its parent. If all ancestors of this Container have null
3115     * specified for the Set, then the current KeyboardFocusManager's default
3116     * Set is used.
3117     * <p>
3118     * This method may throw a {@code ClassCastException} if any {@code Object}
3119     * in {@code keystrokes} is not an {@code AWTKeyStroke}.
3120     *
3121     * @param id one of KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
3122     *        KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
3123     *        KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
3124     *        KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
3125     * @param keystrokes the Set of AWTKeyStroke for the specified operation
3126     * @see #getFocusTraversalKeys
3127     * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
3128     * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
3129     * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
3130     * @see KeyboardFocusManager#DOWN_CYCLE_TRAVERSAL_KEYS
3131     * @throws IllegalArgumentException if id is not one of
3132     *         KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
3133     *         KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
3134     *         KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
3135     *         KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS, or if keystrokes
3136     *         contains null, or if any keystroke represents a KEY_TYPED event,
3137     *         or if any keystroke already maps to another focus traversal
3138     *         operation for this Container
3139     * @since 1.4
3140     */
3141    public void setFocusTraversalKeys(int id,
3142                                      Set<? extends AWTKeyStroke> keystrokes)
3143    {
3144        if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH) {
3145            throw new IllegalArgumentException("invalid focus traversal key identifier");
3146        }
3147
3148        // Don't call super.setFocusTraversalKey. The Component parameter check
3149        // does not allow DOWN_CYCLE_TRAVERSAL_KEYS, but we do.
3150        setFocusTraversalKeys_NoIDCheck(id, keystrokes);
3151    }
3152
3153    /**
3154     * Returns the Set of focus traversal keys for a given traversal operation
3155     * for this Container. (See
3156     * {@code setFocusTraversalKeys} for a full description of each key.)
3157     * <p>
3158     * If a Set of traversal keys has not been explicitly defined for this
3159     * Container, then this Container's parent's Set is returned. If no Set
3160     * has been explicitly defined for any of this Container's ancestors, then
3161     * the current KeyboardFocusManager's default Set is returned.
3162     *
3163     * @param id one of KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
3164     *        KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
3165     *        KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
3166     *        KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
3167     * @return the Set of AWTKeyStrokes for the specified operation. The Set
3168     *         will be unmodifiable, and may be empty. null will never be
3169     *         returned.
3170     * @see #setFocusTraversalKeys
3171     * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
3172     * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
3173     * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
3174     * @see KeyboardFocusManager#DOWN_CYCLE_TRAVERSAL_KEYS
3175     * @throws IllegalArgumentException if id is not one of
3176     *         KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
3177     *         KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
3178     *         KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
3179     *         KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
3180     * @since 1.4
3181     */
3182    public Set<AWTKeyStroke> getFocusTraversalKeys(int id) {
3183        if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH) {
3184            throw new IllegalArgumentException("invalid focus traversal key identifier");
3185        }
3186
3187        // Don't call super.getFocusTraversalKey. The Component parameter check
3188        // does not allow DOWN_CYCLE_TRAVERSAL_KEY, but we do.
3189        return getFocusTraversalKeys_NoIDCheck(id);
3190    }
3191
3192    /**
3193     * Returns whether the Set of focus traversal keys for the given focus
3194     * traversal operation has been explicitly defined for this Container. If
3195     * this method returns {@code false}, this Container is inheriting the
3196     * Set from an ancestor, or from the current KeyboardFocusManager.
3197     *
3198     * @param id one of KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
3199     *        KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
3200     *        KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
3201     *        KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
3202     * @return {@code true} if the Set of focus traversal keys for the
3203     *         given focus traversal operation has been explicitly defined for
3204     *         this Component; {@code false} otherwise.
3205     * @throws IllegalArgumentException if id is not one of
3206     *         KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
3207     *        KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
3208     *        KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
3209     *        KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
3210     * @since 1.4
3211     */
3212    public boolean areFocusTraversalKeysSet(int id) {
3213        if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH) {
3214            throw new IllegalArgumentException("invalid focus traversal key identifier");
3215        }
3216
3217        return (focusTraversalKeys != null && focusTraversalKeys[id] != null);
3218    }
3219
3220    /**
3221     * Returns whether the specified Container is the focus cycle root of this
3222     * Container's focus traversal cycle. Each focus traversal cycle has only
3223     * a single focus cycle root and each Container which is not a focus cycle
3224     * root belongs to only a single focus traversal cycle. Containers which
3225     * are focus cycle roots belong to two cycles: one rooted at the Container
3226     * itself, and one rooted at the Container's nearest focus-cycle-root
3227     * ancestor. This method will return {@code true} for both such
3228     * Containers in this case.
3229     *
3230     * @param container the Container to be tested
3231     * @return {@code true} if the specified Container is a focus-cycle-
3232     *         root of this Container; {@code false} otherwise
3233     * @see #isFocusCycleRoot()
3234     * @since 1.4
3235     */
3236    public boolean isFocusCycleRoot(Container container) {
3237        if (isFocusCycleRoot() && container == this) {
3238            return true;
3239        } else {
3240            return super.isFocusCycleRoot(container);
3241        }
3242    }
3243
3244    private Container findTraversalRoot() {
3245        // I potentially have two roots, myself and my root parent
3246        // If I am the current root, then use me
3247        // If none of my parents are roots, then use me
3248        // If my root parent is the current root, then use my root parent
3249        // If neither I nor my root parent is the current root, then
3250        // use my root parent (a guess)
3251
3252        Container currentFocusCycleRoot = KeyboardFocusManager.
3253            getCurrentKeyboardFocusManager().getCurrentFocusCycleRoot();
3254        Container root;
3255
3256        if (currentFocusCycleRoot == this) {
3257            root = this;
3258        } else {
3259            root = getFocusCycleRootAncestor();
3260            if (root == null) {
3261                root = this;
3262            }
3263        }
3264
3265        if (root != currentFocusCycleRoot) {
3266            KeyboardFocusManager.getCurrentKeyboardFocusManager().
3267                setGlobalCurrentFocusCycleRootPriv(root);
3268        }
3269        return root;
3270    }
3271
3272    final boolean containsFocus() {
3273        final Component focusOwner = KeyboardFocusManager.
3274            getCurrentKeyboardFocusManager().getFocusOwner();
3275        return isParentOf(focusOwner);
3276    }
3277
3278    /**
3279     * Check if this component is the child of this container or its children.
3280     * Note: this function acquires treeLock
3281     * Note: this function traverses children tree only in one Window.
3282     * @param comp a component in test, must not be null
3283     */
3284    private boolean isParentOf(Component comp) {
3285        synchronized(getTreeLock()) {
3286            while (comp != null && comp != this && !(comp instanceof Window)) {
3287                comp = comp.getParent();
3288            }
3289            return (comp == this);
3290        }
3291    }
3292
3293    void clearMostRecentFocusOwnerOnHide() {
3294        boolean reset = false;
3295        Window window = null;
3296
3297        synchronized (getTreeLock()) {
3298            window = getContainingWindow();
3299            if (window != null) {
3300                Component comp = KeyboardFocusManager.getMostRecentFocusOwner(window);
3301                reset = ((comp == this) || isParentOf(comp));
3302                // This synchronized should always be the second in a pair
3303                // (tree lock, KeyboardFocusManager.class)
3304                synchronized(KeyboardFocusManager.class) {
3305                    Component storedComp = window.getTemporaryLostComponent();
3306                    if (isParentOf(storedComp) || storedComp == this) {
3307                        window.setTemporaryLostComponent(null);
3308                    }
3309                }
3310            }
3311        }
3312
3313        if (reset) {
3314            KeyboardFocusManager.setMostRecentFocusOwner(window, null);
3315        }
3316    }
3317
3318    void clearCurrentFocusCycleRootOnHide() {
3319        KeyboardFocusManager kfm =
3320            KeyboardFocusManager.getCurrentKeyboardFocusManager();
3321        Container cont = kfm.getCurrentFocusCycleRoot();
3322
3323        if (cont == this || isParentOf(cont)) {
3324            kfm.setGlobalCurrentFocusCycleRootPriv(null);
3325        }
3326    }
3327
3328    final Container getTraversalRoot() {
3329        if (isFocusCycleRoot()) {
3330            return findTraversalRoot();
3331        }
3332
3333        return super.getTraversalRoot();
3334    }
3335
3336    /**
3337     * Sets the focus traversal policy that will manage keyboard traversal of
3338     * this Container's children, if this Container is a focus cycle root. If
3339     * the argument is null, this Container inherits its policy from its focus-
3340     * cycle-root ancestor. If the argument is non-null, this policy will be
3341     * inherited by all focus-cycle-root children that have no keyboard-
3342     * traversal policy of their own (as will, recursively, their focus-cycle-
3343     * root children).
3344     * <p>
3345     * If this Container is not a focus cycle root, the policy will be
3346     * remembered, but will not be used or inherited by this or any other
3347     * Containers until this Container is made a focus cycle root.
3348     *
3349     * @param policy the new focus traversal policy for this Container
3350     * @see #getFocusTraversalPolicy
3351     * @see #setFocusCycleRoot
3352     * @see #isFocusCycleRoot
3353     * @since 1.4
3354     */
3355    public void setFocusTraversalPolicy(FocusTraversalPolicy policy) {
3356        FocusTraversalPolicy oldPolicy;
3357        synchronized (this) {
3358            oldPolicy = this.focusTraversalPolicy;
3359            this.focusTraversalPolicy = policy;
3360        }
3361        firePropertyChange("focusTraversalPolicy", oldPolicy, policy);
3362    }
3363
3364    /**
3365     * Returns the focus traversal policy that will manage keyboard traversal
3366     * of this Container's children, or null if this Container is not a focus
3367     * cycle root. If no traversal policy has been explicitly set for this
3368     * Container, then this Container's focus-cycle-root ancestor's policy is
3369     * returned.
3370     *
3371     * @return this Container's focus traversal policy, or null if this
3372     *         Container is not a focus cycle root.
3373     * @see #setFocusTraversalPolicy
3374     * @see #setFocusCycleRoot
3375     * @see #isFocusCycleRoot
3376     * @since 1.4
3377     */
3378    public FocusTraversalPolicy getFocusTraversalPolicy() {
3379        if (!isFocusTraversalPolicyProvider() && !isFocusCycleRoot()) {
3380            return null;
3381        }
3382
3383        FocusTraversalPolicy policy = this.focusTraversalPolicy;
3384        if (policy != null) {
3385            return policy;
3386        }
3387
3388        Container rootAncestor = getFocusCycleRootAncestor();
3389        if (rootAncestor != null) {
3390            return rootAncestor.getFocusTraversalPolicy();
3391        } else {
3392            return KeyboardFocusManager.getCurrentKeyboardFocusManager().
3393                getDefaultFocusTraversalPolicy();
3394        }
3395    }
3396
3397    /**
3398     * Returns whether the focus traversal policy has been explicitly set for
3399     * this Container. If this method returns {@code false}, this
3400     * Container will inherit its focus traversal policy from an ancestor.
3401     *
3402     * @return {@code true} if the focus traversal policy has been
3403     *         explicitly set for this Container; {@code false} otherwise.
3404     * @since 1.4
3405     */
3406    public boolean isFocusTraversalPolicySet() {
3407        return (focusTraversalPolicy != null);
3408    }
3409
3410    /**
3411     * Sets whether this Container is the root of a focus traversal cycle. Once
3412     * focus enters a traversal cycle, typically it cannot leave it via focus
3413     * traversal unless one of the up- or down-cycle keys is pressed. Normal
3414     * traversal is limited to this Container, and all of this Container's
3415     * descendants that are not descendants of inferior focus cycle roots. Note
3416     * that a FocusTraversalPolicy may bend these restrictions, however. For
3417     * example, ContainerOrderFocusTraversalPolicy supports implicit down-cycle
3418     * traversal.
3419     * <p>
3420     * The alternative way to specify the traversal order of this Container's
3421     * children is to make this Container a
3422     * <a href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus traversal policy provider</a>.
3423     *
3424     * @param focusCycleRoot indicates whether this Container is the root of a
3425     *        focus traversal cycle
3426     * @see #isFocusCycleRoot()
3427     * @see #setFocusTraversalPolicy
3428     * @see #getFocusTraversalPolicy
3429     * @see ContainerOrderFocusTraversalPolicy
3430     * @see #setFocusTraversalPolicyProvider
3431     * @since 1.4
3432     */
3433    public void setFocusCycleRoot(boolean focusCycleRoot) {
3434        boolean oldFocusCycleRoot;
3435        synchronized (this) {
3436            oldFocusCycleRoot = this.focusCycleRoot;
3437            this.focusCycleRoot = focusCycleRoot;
3438        }
3439        firePropertyChange("focusCycleRoot", oldFocusCycleRoot,
3440                           focusCycleRoot);
3441    }
3442
3443    /**
3444     * Returns whether this Container is the root of a focus traversal cycle.
3445     * Once focus enters a traversal cycle, typically it cannot leave it via
3446     * focus traversal unless one of the up- or down-cycle keys is pressed.
3447     * Normal traversal is limited to this Container, and all of this
3448     * Container's descendants that are not descendants of inferior focus
3449     * cycle roots. Note that a FocusTraversalPolicy may bend these
3450     * restrictions, however. For example, ContainerOrderFocusTraversalPolicy
3451     * supports implicit down-cycle traversal.
3452     *
3453     * @return whether this Container is the root of a focus traversal cycle
3454     * @see #setFocusCycleRoot
3455     * @see #setFocusTraversalPolicy
3456     * @see #getFocusTraversalPolicy
3457     * @see ContainerOrderFocusTraversalPolicy
3458     * @since 1.4
3459     */
3460    public boolean isFocusCycleRoot() {
3461        return focusCycleRoot;
3462    }
3463
3464    /**
3465     * Sets whether this container will be used to provide focus
3466     * traversal policy. Container with this property as
3467     * {@code true} will be used to acquire focus traversal policy
3468     * instead of closest focus cycle root ancestor.
3469     * @param provider indicates whether this container will be used to
3470     *                provide focus traversal policy
3471     * @see #setFocusTraversalPolicy
3472     * @see #getFocusTraversalPolicy
3473     * @see #isFocusTraversalPolicyProvider
3474     * @since 1.5
3475     */
3476    public final void setFocusTraversalPolicyProvider(boolean provider) {
3477        boolean oldProvider;
3478        synchronized(this) {
3479            oldProvider = focusTraversalPolicyProvider;
3480            focusTraversalPolicyProvider = provider;
3481        }
3482        firePropertyChange("focusTraversalPolicyProvider", oldProvider, provider);
3483    }
3484
3485    /**
3486     * Returns whether this container provides focus traversal
3487     * policy. If this property is set to {@code true} then when
3488     * keyboard focus manager searches container hierarchy for focus
3489     * traversal policy and encounters this container before any other
3490     * container with this property as true or focus cycle roots then
3491     * its focus traversal policy will be used instead of focus cycle
3492     * root's policy.
3493     * @see #setFocusTraversalPolicy
3494     * @see #getFocusTraversalPolicy
3495     * @see #setFocusCycleRoot
3496     * @see #setFocusTraversalPolicyProvider
3497     * @return {@code true} if this container provides focus traversal
3498     *         policy, {@code false} otherwise
3499     * @since 1.5
3500     */
3501    public final boolean isFocusTraversalPolicyProvider() {
3502        return focusTraversalPolicyProvider;
3503    }
3504
3505    /**
3506     * Transfers the focus down one focus traversal cycle. If this Container is
3507     * a focus cycle root, then the focus owner is set to this Container's
3508     * default Component to focus, and the current focus cycle root is set to
3509     * this Container. If this Container is not a focus cycle root, then no
3510     * focus traversal operation occurs.
3511     *
3512     * @see       Component#requestFocus()
3513     * @see       #isFocusCycleRoot
3514     * @see       #setFocusCycleRoot
3515     * @since     1.4
3516     */
3517    public void transferFocusDownCycle() {
3518        if (isFocusCycleRoot()) {
3519            KeyboardFocusManager.getCurrentKeyboardFocusManager().
3520                setGlobalCurrentFocusCycleRootPriv(this);
3521            Component toFocus = getFocusTraversalPolicy().
3522                getDefaultComponent(this);
3523            if (toFocus != null) {
3524                toFocus.requestFocus(FocusEvent.Cause.TRAVERSAL_DOWN);
3525            }
3526        }
3527    }
3528
3529    void preProcessKeyEvent(KeyEvent e) {
3530        Container parent = this.parent;
3531        if (parent != null) {
3532            parent.preProcessKeyEvent(e);
3533        }
3534    }
3535
3536    void postProcessKeyEvent(KeyEvent e) {
3537        Container parent = this.parent;
3538        if (parent != null) {
3539            parent.postProcessKeyEvent(e);
3540        }
3541    }
3542
3543    boolean postsOldMouseEvents() {
3544        return true;
3545    }
3546
3547    /**
3548     * Sets the {@code ComponentOrientation} property of this container
3549     * and all components contained within it.
3550     * <p>
3551     * This method changes layout-related information, and therefore,
3552     * invalidates the component hierarchy.
3553     *
3554     * @param o the new component orientation of this container and
3555     *        the components contained within it.
3556     * @exception NullPointerException if {@code orientation} is null.
3557     * @see Component#setComponentOrientation
3558     * @see Component#getComponentOrientation
3559     * @see #invalidate
3560     * @since 1.4
3561     */
3562    public void applyComponentOrientation(ComponentOrientation o) {
3563        super.applyComponentOrientation(o);
3564        synchronized (getTreeLock()) {
3565            for (int i = 0; i < component.size(); i++) {
3566                Component comp = component.get(i);
3567                comp.applyComponentOrientation(o);
3568            }
3569        }
3570    }
3571
3572    /**
3573     * Adds a PropertyChangeListener to the listener list. The listener is
3574     * registered for all bound properties of this class, including the
3575     * following:
3576     * <ul>
3577     *    <li>this Container's font ("font")</li>
3578     *    <li>this Container's background color ("background")</li>
3579     *    <li>this Container's foreground color ("foreground")</li>
3580     *    <li>this Container's focusability ("focusable")</li>
3581     *    <li>this Container's focus traversal keys enabled state
3582     *        ("focusTraversalKeysEnabled")</li>
3583     *    <li>this Container's Set of FORWARD_TRAVERSAL_KEYS
3584     *        ("forwardFocusTraversalKeys")</li>
3585     *    <li>this Container's Set of BACKWARD_TRAVERSAL_KEYS
3586     *        ("backwardFocusTraversalKeys")</li>
3587     *    <li>this Container's Set of UP_CYCLE_TRAVERSAL_KEYS
3588     *        ("upCycleFocusTraversalKeys")</li>
3589     *    <li>this Container's Set of DOWN_CYCLE_TRAVERSAL_KEYS
3590     *        ("downCycleFocusTraversalKeys")</li>
3591     *    <li>this Container's focus traversal policy ("focusTraversalPolicy")
3592     *        </li>
3593     *    <li>this Container's focus-cycle-root state ("focusCycleRoot")</li>
3594     * </ul>
3595     * Note that if this Container is inheriting a bound property, then no
3596     * event will be fired in response to a change in the inherited property.
3597     * <p>
3598     * If listener is null, no exception is thrown and no action is performed.
3599     *
3600     * @param    listener  the PropertyChangeListener to be added
3601     *
3602     * @see Component#removePropertyChangeListener
3603     * @see #addPropertyChangeListener(java.lang.String,java.beans.PropertyChangeListener)
3604     */
3605    public void addPropertyChangeListener(PropertyChangeListener listener) {
3606        super.addPropertyChangeListener(listener);
3607    }
3608
3609    /**
3610     * Adds a PropertyChangeListener to the listener list for a specific
3611     * property. The specified property may be user-defined, or one of the
3612     * following defaults:
3613     * <ul>
3614     *    <li>this Container's font ("font")</li>
3615     *    <li>this Container's background color ("background")</li>
3616     *    <li>this Container's foreground color ("foreground")</li>
3617     *    <li>this Container's focusability ("focusable")</li>
3618     *    <li>this Container's focus traversal keys enabled state
3619     *        ("focusTraversalKeysEnabled")</li>
3620     *    <li>this Container's Set of FORWARD_TRAVERSAL_KEYS
3621     *        ("forwardFocusTraversalKeys")</li>
3622     *    <li>this Container's Set of BACKWARD_TRAVERSAL_KEYS
3623     *        ("backwardFocusTraversalKeys")</li>
3624     *    <li>this Container's Set of UP_CYCLE_TRAVERSAL_KEYS
3625     *        ("upCycleFocusTraversalKeys")</li>
3626     *    <li>this Container's Set of DOWN_CYCLE_TRAVERSAL_KEYS
3627     *        ("downCycleFocusTraversalKeys")</li>
3628     *    <li>this Container's focus traversal policy ("focusTraversalPolicy")
3629     *        </li>
3630     *    <li>this Container's focus-cycle-root state ("focusCycleRoot")</li>
3631     *    <li>this Container's focus-traversal-policy-provider state("focusTraversalPolicyProvider")</li>
3632     *    <li>this Container's focus-traversal-policy-provider state("focusTraversalPolicyProvider")</li>
3633     * </ul>
3634     * Note that if this Container is inheriting a bound property, then no
3635     * event will be fired in response to a change in the inherited property.
3636     * <p>
3637     * If listener is null, no exception is thrown and no action is performed.
3638     *
3639     * @param propertyName one of the property names listed above
3640     * @param listener the PropertyChangeListener to be added
3641     *
3642     * @see #addPropertyChangeListener(java.beans.PropertyChangeListener)
3643     * @see Component#removePropertyChangeListener
3644     */
3645    public void addPropertyChangeListener(String propertyName,
3646                                          PropertyChangeListener listener) {
3647        super.addPropertyChangeListener(propertyName, listener);
3648    }
3649
3650    // Serialization support. A Container is responsible for restoring the
3651    // parent fields of its component children.
3652
3653    /**
3654     * Container Serial Data Version.
3655     */
3656    private int containerSerializedDataVersion = 1;
3657
3658    /**
3659     * Serializes this {@code Container} to the specified
3660     * {@code ObjectOutputStream}.
3661     * <ul>
3662     *    <li>Writes default serializable fields to the stream.</li>
3663     *    <li>Writes a list of serializable ContainerListener(s) as optional
3664     *        data. The non-serializable ContainerListener(s) are detected and
3665     *        no attempt is made to serialize them.</li>
3666     *    <li>Write this Container's FocusTraversalPolicy if and only if it
3667     *        is Serializable; otherwise, {@code null} is written.</li>
3668     * </ul>
3669     *
3670     * @param s the {@code ObjectOutputStream} to write
3671     * @serialData {@code null} terminated sequence of 0 or more pairs;
3672     *   the pair consists of a {@code String} and {@code Object};
3673     *   the {@code String} indicates the type of object and
3674     *   is one of the following:
3675     *   {@code containerListenerK} indicating an
3676     *     {@code ContainerListener} object;
3677     *   the {@code Container}'s {@code FocusTraversalPolicy},
3678     *     or {@code null}
3679     *
3680     * @see AWTEventMulticaster#save(java.io.ObjectOutputStream, java.lang.String, java.util.EventListener)
3681     * @see Container#containerListenerK
3682     * @see #readObject(ObjectInputStream)
3683     */
3684    private void writeObject(ObjectOutputStream s) throws IOException {
3685        ObjectOutputStream.PutField f = s.putFields();
3686        f.put("ncomponents", component.size());
3687        f.put("component", component.toArray(EMPTY_ARRAY));
3688        f.put("layoutMgr", layoutMgr);
3689        f.put("dispatcher", dispatcher);
3690        f.put("maxSize", maxSize);
3691        f.put("focusCycleRoot", focusCycleRoot);
3692        f.put("containerSerializedDataVersion", containerSerializedDataVersion);
3693        f.put("focusTraversalPolicyProvider", focusTraversalPolicyProvider);
3694        s.writeFields();
3695
3696        AWTEventMulticaster.save(s, containerListenerK, containerListener);
3697        s.writeObject(null);
3698
3699        if (focusTraversalPolicy instanceof java.io.Serializable) {
3700            s.writeObject(focusTraversalPolicy);
3701        } else {
3702            s.writeObject(null);
3703        }
3704    }
3705
3706    /**
3707     * Deserializes this {@code Container} from the specified
3708     * {@code ObjectInputStream}.
3709     * <ul>
3710     *    <li>Reads default serializable fields from the stream.</li>
3711     *    <li>Reads a list of serializable ContainerListener(s) as optional
3712     *        data. If the list is null, no Listeners are installed.</li>
3713     *    <li>Reads this Container's FocusTraversalPolicy, which may be null,
3714     *        as optional data.</li>
3715     * </ul>
3716     *
3717     * @param s the {@code ObjectInputStream} to read
3718     * @serial
3719     * @see #addContainerListener
3720     * @see #writeObject(ObjectOutputStream)
3721     */
3722    private void readObject(ObjectInputStream s)
3723        throws ClassNotFoundException, IOException
3724    {
3725        ObjectInputStream.GetField f = s.readFields();
3726        Component [] tmpComponent = (Component[])f.get("component", EMPTY_ARRAY);
3727        int ncomponents = (Integer) f.get("ncomponents", 0);
3728        component = new java.util.ArrayList<Component>(ncomponents);
3729        for (int i = 0; i < ncomponents; ++i) {
3730            component.add(tmpComponent[i]);
3731        }
3732        layoutMgr = (LayoutManager)f.get("layoutMgr", null);
3733        dispatcher = (LightweightDispatcher)f.get("dispatcher", null);
3734        // Old stream. Doesn't contain maxSize among Component's fields.
3735        if (maxSize == null) {
3736            maxSize = (Dimension)f.get("maxSize", null);
3737        }
3738        focusCycleRoot = f.get("focusCycleRoot", false);
3739        containerSerializedDataVersion = f.get("containerSerializedDataVersion", 1);
3740        focusTraversalPolicyProvider = f.get("focusTraversalPolicyProvider", false);
3741        java.util.List<Component> component = this.component;
3742        for(Component comp : component) {
3743            comp.parent = this;
3744            adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK,
3745                                    comp.numListening(AWTEvent.HIERARCHY_EVENT_MASK));
3746            adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK,
3747                                    comp.numListening(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
3748            adjustDescendants(comp.countHierarchyMembers());
3749        }
3750
3751        Object keyOrNull;
3752        while(null != (keyOrNull = s.readObject())) {
3753            String key = ((String)keyOrNull).intern();
3754
3755            if (containerListenerK == key) {
3756                addContainerListener((ContainerListener)(s.readObject()));
3757            } else {
3758                // skip value for unrecognized key
3759                s.readObject();
3760            }
3761        }
3762
3763        try {
3764            Object policy = s.readObject();
3765            if (policy instanceof FocusTraversalPolicy) {
3766                focusTraversalPolicy = (FocusTraversalPolicy)policy;
3767            }
3768        } catch (java.io.OptionalDataException e) {
3769            // JDK 1.1/1.2/1.3 instances will not have this optional data.
3770            // e.eof will be true to indicate that there is no more data
3771            // available for this object. If e.eof is not true, throw the
3772            // exception as it might have been caused by reasons unrelated to
3773            // focusTraversalPolicy.
3774
3775            if (!e.eof) {
3776                throw e;
3777            }
3778        }
3779    }
3780
3781    /*
3782     * --- Accessibility Support ---
3783     */
3784
3785    /**
3786     * Inner class of Container used to provide default support for
3787     * accessibility.  This class is not meant to be used directly by
3788     * application developers, but is instead meant only to be
3789     * subclassed by container developers.
3790     * <p>
3791     * The class used to obtain the accessible role for this object,
3792     * as well as implementing many of the methods in the
3793     * AccessibleContainer interface.
3794     * @since 1.3
3795     */
3796    protected class AccessibleAWTContainer extends AccessibleAWTComponent {
3797
3798        /**
3799         * JDK1.3 serialVersionUID
3800         */
3801        private static final long serialVersionUID = 5081320404842566097L;
3802
3803        /**
3804         * Returns the number of accessible children in the object.  If all
3805         * of the children of this object implement {@code Accessible},
3806         * then this method should return the number of children of this object.
3807         *
3808         * @return the number of accessible children in the object
3809         */
3810        public int getAccessibleChildrenCount() {
3811            return Container.this.getAccessibleChildrenCount();
3812        }
3813
3814        /**
3815         * Returns the nth {@code Accessible} child of the object.
3816         *
3817         * @param i zero-based index of child
3818         * @return the nth {@code Accessible} child of the object
3819         */
3820        public Accessible getAccessibleChild(int i) {
3821            return Container.this.getAccessibleChild(i);
3822        }
3823
3824        /**
3825         * Returns the {@code Accessible} child, if one exists,
3826         * contained at the local coordinate {@code Point}.
3827         *
3828         * @param p the point defining the top-left corner of the
3829         *    {@code Accessible}, given in the coordinate space
3830         *    of the object's parent
3831         * @return the {@code Accessible}, if it exists,
3832         *    at the specified location; else {@code null}
3833         */
3834        public Accessible getAccessibleAt(Point p) {
3835            return Container.this.getAccessibleAt(p);
3836        }
3837
3838        /**
3839         * Number of PropertyChangeListener objects registered. It's used
3840         * to add/remove ContainerListener to track target Container's state.
3841         */
3842        private transient volatile int propertyListenersCount = 0;
3843
3844        /**
3845         * The handler to fire {@code PropertyChange}
3846         * when children are added or removed
3847         */
3848        protected ContainerListener accessibleContainerHandler = null;
3849
3850        /**
3851         * Fire {@code PropertyChange} listener, if one is registered,
3852         * when children are added or removed.
3853         * @since 1.3
3854         */
3855        protected class AccessibleContainerHandler
3856            implements ContainerListener {
3857            public void componentAdded(ContainerEvent e) {
3858                Component c = e.getChild();
3859                if (c != null && c instanceof Accessible) {
3860                    AccessibleAWTContainer.this.firePropertyChange(
3861                        AccessibleContext.ACCESSIBLE_CHILD_PROPERTY,
3862                        null, ((Accessible) c).getAccessibleContext());
3863                }
3864            }
3865            public void componentRemoved(ContainerEvent e) {
3866                Component c = e.getChild();
3867                if (c != null && c instanceof Accessible) {
3868                    AccessibleAWTContainer.this.firePropertyChange(
3869                        AccessibleContext.ACCESSIBLE_CHILD_PROPERTY,
3870                        ((Accessible) c).getAccessibleContext(), null);
3871                }
3872            }
3873        }
3874
3875        /**
3876         * Adds a PropertyChangeListener to the listener list.
3877         *
3878         * @param listener  the PropertyChangeListener to be added
3879         */
3880        public void addPropertyChangeListener(PropertyChangeListener listener) {
3881            if (accessibleContainerHandler == null) {
3882                accessibleContainerHandler = new AccessibleContainerHandler();
3883            }
3884            if (propertyListenersCount++ == 0) {
3885                Container.this.addContainerListener(accessibleContainerHandler);
3886            }
3887            super.addPropertyChangeListener(listener);
3888        }
3889
3890        /**
3891         * Remove a PropertyChangeListener from the listener list.
3892         * This removes a PropertyChangeListener that was registered
3893         * for all properties.
3894         *
3895         * @param listener the PropertyChangeListener to be removed
3896         */
3897        public void removePropertyChangeListener(PropertyChangeListener listener) {
3898            if (--propertyListenersCount == 0) {
3899                Container.this.removeContainerListener(accessibleContainerHandler);
3900            }
3901            super.removePropertyChangeListener(listener);
3902        }
3903
3904    } // inner class AccessibleAWTContainer
3905
3906    /**
3907     * Returns the {@code Accessible} child contained at the local
3908     * coordinate {@code Point}, if one exists.  Otherwise
3909     * returns {@code null}.
3910     *
3911     * @param p the point defining the top-left corner of the
3912     *    {@code Accessible}, given in the coordinate space
3913     *    of the object's parent
3914     * @return the {@code Accessible} at the specified location,
3915     *    if it exists; otherwise {@code null}
3916     */
3917    Accessible getAccessibleAt(Point p) {
3918        synchronized (getTreeLock()) {
3919            if (this instanceof Accessible) {
3920                Accessible a = (Accessible)this;
3921                AccessibleContext ac = a.getAccessibleContext();
3922                if (ac != null) {
3923                    AccessibleComponent acmp;
3924                    Point location;
3925                    int nchildren = ac.getAccessibleChildrenCount();
3926                    for (int i=0; i < nchildren; i++) {
3927                        a = ac.getAccessibleChild(i);
3928                        if ((a != null)) {
3929                            ac = a.getAccessibleContext();
3930                            if (ac != null) {
3931                                acmp = ac.getAccessibleComponent();
3932                                if ((acmp != null) && (acmp.isShowing())) {
3933                                    location = acmp.getLocation();
3934                                    Point np = new Point(p.x-location.x,
3935                                                         p.y-location.y);
3936                                    if (acmp.contains(np)){
3937                                        return a;
3938                                    }
3939                                }
3940                            }
3941                        }
3942                    }
3943                }
3944                return (Accessible)this;
3945            } else {
3946                Component ret = this;
3947                if (!this.contains(p.x,p.y)) {
3948                    ret = null;
3949                } else {
3950                    int ncomponents = this.getComponentCount();
3951                    for (int i=0; i < ncomponents; i++) {
3952                        Component comp = this.getComponent(i);
3953                        if ((comp != null) && comp.isShowing()) {
3954                            Point location = comp.getLocation();
3955                            if (comp.contains(p.x-location.x,p.y-location.y)) {
3956                                ret = comp;
3957                            }
3958                        }
3959                    }
3960                }
3961                if (ret instanceof Accessible) {
3962                    return (Accessible) ret;
3963                }
3964            }
3965            return null;
3966        }
3967    }
3968
3969    /**
3970     * Returns the number of accessible children in the object.  If all
3971     * of the children of this object implement {@code Accessible},
3972     * then this method should return the number of children of this object.
3973     *
3974     * @return the number of accessible children in the object
3975     */
3976    int getAccessibleChildrenCount() {
3977        synchronized (getTreeLock()) {
3978            int count = 0;
3979            Component[] children = this.getComponents();
3980            for (int i = 0; i < children.length; i++) {
3981                if (children[i] instanceof Accessible) {
3982                    count++;
3983                }
3984            }
3985            return count;
3986        }
3987    }
3988
3989    /**
3990     * Returns the nth {@code Accessible} child of the object.
3991     *
3992     * @param i zero-based index of child
3993     * @return the nth {@code Accessible} child of the object
3994     */
3995    Accessible getAccessibleChild(int i) {
3996        synchronized (getTreeLock()) {
3997            Component[] children = this.getComponents();
3998            int count = 0;
3999            for (int j = 0; j < children.length; j++) {
4000                if (children[j] instanceof Accessible) {
4001                    if (count == i) {
4002                        return (Accessible) children[j];
4003                    } else {
4004                        count++;
4005                    }
4006                }
4007            }
4008            return null;
4009        }
4010    }
4011
4012    // ************************** MIXING CODE *******************************
4013
4014    final void increaseComponentCount(Component c) {
4015        synchronized (getTreeLock()) {
4016            if (!c.isDisplayable()) {
4017                throw new IllegalStateException(
4018                    "Peer does not exist while invoking the increaseComponentCount() method"
4019                );
4020            }
4021
4022            int addHW = 0;
4023            int addLW = 0;
4024
4025            if (c instanceof Container) {
4026                addLW = ((Container)c).numOfLWComponents;
4027                addHW = ((Container)c).numOfHWComponents;
4028            }
4029            if (c.isLightweight()) {
4030                addLW++;
4031            } else {
4032                addHW++;
4033            }
4034
4035            for (Container cont = this; cont != null; cont = cont.getContainer()) {
4036                cont.numOfLWComponents += addLW;
4037                cont.numOfHWComponents += addHW;
4038            }
4039        }
4040    }
4041
4042    final void decreaseComponentCount(Component c) {
4043        synchronized (getTreeLock()) {
4044            if (!c.isDisplayable()) {
4045                throw new IllegalStateException(
4046                    "Peer does not exist while invoking the decreaseComponentCount() method"
4047                );
4048            }
4049
4050            int subHW = 0;
4051            int subLW = 0;
4052
4053            if (c instanceof Container) {
4054                subLW = ((Container)c).numOfLWComponents;
4055                subHW = ((Container)c).numOfHWComponents;
4056            }
4057            if (c.isLightweight()) {
4058                subLW++;
4059            } else {
4060                subHW++;
4061            }
4062
4063            for (Container cont = this; cont != null; cont = cont.getContainer()) {
4064                cont.numOfLWComponents -= subLW;
4065                cont.numOfHWComponents -= subHW;
4066            }
4067        }
4068    }
4069
4070    private int getTopmostComponentIndex() {
4071        checkTreeLock();
4072        if (getComponentCount() > 0) {
4073            return 0;
4074        }
4075        return -1;
4076    }
4077
4078    private int getBottommostComponentIndex() {
4079        checkTreeLock();
4080        if (getComponentCount() > 0) {
4081            return getComponentCount() - 1;
4082        }
4083        return -1;
4084    }
4085
4086    /*
4087     * This method is overriden to handle opaque children in non-opaque
4088     * containers.
4089     */
4090    @Override
4091    final Region getOpaqueShape() {
4092        checkTreeLock();
4093        if (isLightweight() && isNonOpaqueForMixing()
4094                && hasLightweightDescendants())
4095        {
4096            Region s = Region.EMPTY_REGION;
4097            for (int index = 0; index < getComponentCount(); index++) {
4098                Component c = getComponent(index);
4099                if (c.isLightweight() && c.isShowing()) {
4100                    s = s.getUnion(c.getOpaqueShape());
4101                }
4102            }
4103            return s.getIntersection(getNormalShape());
4104        }
4105        return super.getOpaqueShape();
4106    }
4107
4108    final void recursiveSubtractAndApplyShape(Region shape) {
4109        recursiveSubtractAndApplyShape(shape, getTopmostComponentIndex(), getBottommostComponentIndex());
4110    }
4111
4112    final void recursiveSubtractAndApplyShape(Region shape, int fromZorder) {
4113        recursiveSubtractAndApplyShape(shape, fromZorder, getBottommostComponentIndex());
4114    }
4115
4116    final void recursiveSubtractAndApplyShape(Region shape, int fromZorder, int toZorder) {
4117        checkTreeLock();
4118        if (mixingLog.isLoggable(PlatformLogger.Level.FINE)) {
4119            mixingLog.fine("this = " + this +
4120                "; shape=" + shape + "; fromZ=" + fromZorder + "; toZ=" + toZorder);
4121        }
4122        if (fromZorder == -1) {
4123            return;
4124        }
4125        if (shape.isEmpty()) {
4126            return;
4127        }
4128        // An invalid container with not-null layout should be ignored
4129        // by the mixing code, the container will be validated later
4130        // and the mixing code will be executed later.
4131        if (getLayout() != null && !isValid()) {
4132            return;
4133        }
4134        for (int index = fromZorder; index <= toZorder; index++) {
4135            Component comp = getComponent(index);
4136            if (!comp.isLightweight()) {
4137                comp.subtractAndApplyShape(shape);
4138            } else if (comp instanceof Container &&
4139                    ((Container)comp).hasHeavyweightDescendants() && comp.isShowing()) {
4140                ((Container)comp).recursiveSubtractAndApplyShape(shape);
4141            }
4142        }
4143    }
4144
4145    final void recursiveApplyCurrentShape() {
4146        recursiveApplyCurrentShape(getTopmostComponentIndex(), getBottommostComponentIndex());
4147    }
4148
4149    final void recursiveApplyCurrentShape(int fromZorder) {
4150        recursiveApplyCurrentShape(fromZorder, getBottommostComponentIndex());
4151    }
4152
4153    final void recursiveApplyCurrentShape(int fromZorder, int toZorder) {
4154        checkTreeLock();
4155        if (mixingLog.isLoggable(PlatformLogger.Level.FINE)) {
4156            mixingLog.fine("this = " + this +
4157                "; fromZ=" + fromZorder + "; toZ=" + toZorder);
4158        }
4159        if (fromZorder == -1) {
4160            return;
4161        }
4162        // An invalid container with not-null layout should be ignored
4163        // by the mixing code, the container will be validated later
4164        // and the mixing code will be executed later.
4165        if (getLayout() != null && !isValid()) {
4166            return;
4167        }
4168        for (int index = fromZorder; index <= toZorder; index++) {
4169            Component comp = getComponent(index);
4170            if (!comp.isLightweight()) {
4171                comp.applyCurrentShape();
4172            }
4173            if (comp instanceof Container &&
4174                    ((Container)comp).hasHeavyweightDescendants()) {
4175                ((Container)comp).recursiveApplyCurrentShape();
4176            }
4177        }
4178    }
4179
4180    @SuppressWarnings("deprecation")
4181    private void recursiveShowHeavyweightChildren() {
4182        if (!hasHeavyweightDescendants() || !isVisible()) {
4183            return;
4184        }
4185        for (int index = 0; index < getComponentCount(); index++) {
4186            Component comp = getComponent(index);
4187            if (comp.isLightweight()) {
4188                if  (comp instanceof Container) {
4189                    ((Container)comp).recursiveShowHeavyweightChildren();
4190                }
4191            } else {
4192                if (comp.isVisible()) {
4193                    ComponentPeer peer = comp.peer;
4194                    if (peer != null) {
4195                        peer.setVisible(true);
4196                    }
4197                }
4198            }
4199        }
4200    }
4201
4202    @SuppressWarnings("deprecation")
4203    private void recursiveHideHeavyweightChildren() {
4204        if (!hasHeavyweightDescendants()) {
4205            return;
4206        }
4207        for (int index = 0; index < getComponentCount(); index++) {
4208            Component comp = getComponent(index);
4209            if (comp.isLightweight()) {
4210                if  (comp instanceof Container) {
4211                    ((Container)comp).recursiveHideHeavyweightChildren();
4212                }
4213            } else {
4214                if (comp.isVisible()) {
4215                    ComponentPeer peer = comp.peer;
4216                    if (peer != null) {
4217                        peer.setVisible(false);
4218                    }
4219                }
4220            }
4221        }
4222    }
4223
4224    @SuppressWarnings("deprecation")
4225    private void recursiveRelocateHeavyweightChildren(Point origin) {
4226        for (int index = 0; index < getComponentCount(); index++) {
4227            Component comp = getComponent(index);
4228            if (comp.isLightweight()) {
4229                if  (comp instanceof Container &&
4230                        ((Container)comp).hasHeavyweightDescendants())
4231                {
4232                    final Point newOrigin = new Point(origin);
4233                    newOrigin.translate(comp.getX(), comp.getY());
4234                    ((Container)comp).recursiveRelocateHeavyweightChildren(newOrigin);
4235                }
4236            } else {
4237                ComponentPeer peer = comp.peer;
4238                if (peer != null) {
4239                    peer.setBounds(origin.x + comp.getX(), origin.y + comp.getY(),
4240                            comp.getWidth(), comp.getHeight(),
4241                            ComponentPeer.SET_LOCATION);
4242                }
4243            }
4244        }
4245    }
4246
4247    /**
4248     * Checks if the container and its direct lightweight containers are
4249     * visible.
4250     *
4251     * Consider the heavyweight container hides or shows the HW descendants
4252     * automatically. Therefore we care of LW containers' visibility only.
4253     *
4254     * This method MUST be invoked under the TreeLock.
4255     */
4256    final boolean isRecursivelyVisibleUpToHeavyweightContainer() {
4257        if (!isLightweight()) {
4258            return true;
4259        }
4260
4261        for (Container cont = this;
4262                cont != null && cont.isLightweight();
4263                cont = cont.getContainer())
4264        {
4265            if (!cont.isVisible()) {
4266                return false;
4267            }
4268        }
4269        return true;
4270    }
4271
4272    @Override
4273    void mixOnShowing() {
4274        synchronized (getTreeLock()) {
4275            if (mixingLog.isLoggable(PlatformLogger.Level.FINE)) {
4276                mixingLog.fine("this = " + this);
4277            }
4278
4279            boolean isLightweight = isLightweight();
4280
4281            if (isLightweight && isRecursivelyVisibleUpToHeavyweightContainer()) {
4282                recursiveShowHeavyweightChildren();
4283            }
4284
4285            if (!isMixingNeeded()) {
4286                return;
4287            }
4288
4289            if (!isLightweight || (isLightweight && hasHeavyweightDescendants())) {
4290                recursiveApplyCurrentShape();
4291            }
4292
4293            super.mixOnShowing();
4294        }
4295    }
4296
4297    @Override
4298    void mixOnHiding(boolean isLightweight) {
4299        synchronized (getTreeLock()) {
4300            if (mixingLog.isLoggable(PlatformLogger.Level.FINE)) {
4301                mixingLog.fine("this = " + this +
4302                        "; isLightweight=" + isLightweight);
4303            }
4304            if (isLightweight) {
4305                recursiveHideHeavyweightChildren();
4306            }
4307            super.mixOnHiding(isLightweight);
4308        }
4309    }
4310
4311    @Override
4312    void mixOnReshaping() {
4313        synchronized (getTreeLock()) {
4314            if (mixingLog.isLoggable(PlatformLogger.Level.FINE)) {
4315                mixingLog.fine("this = " + this);
4316            }
4317
4318            boolean isMixingNeeded = isMixingNeeded();
4319
4320            if (isLightweight() && hasHeavyweightDescendants()) {
4321                final Point origin = new Point(getX(), getY());
4322                for (Container cont = getContainer();
4323                        cont != null && cont.isLightweight();
4324                        cont = cont.getContainer())
4325                {
4326                    origin.translate(cont.getX(), cont.getY());
4327                }
4328
4329                recursiveRelocateHeavyweightChildren(origin);
4330
4331                if (!isMixingNeeded) {
4332                    return;
4333                }
4334
4335                recursiveApplyCurrentShape();
4336            }
4337
4338            if (!isMixingNeeded) {
4339                return;
4340            }
4341
4342            super.mixOnReshaping();
4343        }
4344    }
4345
4346    @Override
4347    void mixOnZOrderChanging(int oldZorder, int newZorder) {
4348        synchronized (getTreeLock()) {
4349            if (mixingLog.isLoggable(PlatformLogger.Level.FINE)) {
4350                mixingLog.fine("this = " + this +
4351                    "; oldZ=" + oldZorder + "; newZ=" + newZorder);
4352            }
4353
4354            if (!isMixingNeeded()) {
4355                return;
4356            }
4357
4358            boolean becameHigher = newZorder < oldZorder;
4359
4360            if (becameHigher && isLightweight() && hasHeavyweightDescendants()) {
4361                recursiveApplyCurrentShape();
4362            }
4363            super.mixOnZOrderChanging(oldZorder, newZorder);
4364        }
4365    }
4366
4367    @Override
4368    void mixOnValidating() {
4369        synchronized (getTreeLock()) {
4370            if (mixingLog.isLoggable(PlatformLogger.Level.FINE)) {
4371                mixingLog.fine("this = " + this);
4372            }
4373
4374            if (!isMixingNeeded()) {
4375                return;
4376            }
4377
4378            if (hasHeavyweightDescendants()) {
4379                recursiveApplyCurrentShape();
4380            }
4381
4382            if (isLightweight() && isNonOpaqueForMixing()) {
4383                subtractAndApplyShapeBelowMe();
4384            }
4385
4386            super.mixOnValidating();
4387        }
4388    }
4389
4390    // ****************** END OF MIXING CODE ********************************
4391}
4392
4393
4394/**
4395 * Class to manage the dispatching of MouseEvents to the lightweight descendants
4396 * and SunDropTargetEvents to both lightweight and heavyweight descendants
4397 * contained by a native container.
4398 *
4399 * NOTE: the class name is not appropriate anymore, but we cannot change it
4400 * because we must keep serialization compatibility.
4401 *
4402 * @author Timothy Prinzing
4403 */
4404class LightweightDispatcher implements java.io.Serializable, AWTEventListener {
4405
4406    /*
4407     * JDK 1.1 serialVersionUID
4408     */
4409    private static final long serialVersionUID = 5184291520170872969L;
4410    /*
4411     * Our own mouse event for when we're dragged over from another hw
4412     * container
4413     */
4414    private static final int  LWD_MOUSE_DRAGGED_OVER = 1500;
4415
4416    private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.LightweightDispatcher");
4417
4418    private static final int BUTTONS_DOWN_MASK;
4419
4420    static {
4421        int[] buttonsDownMask = AWTAccessor.getInputEventAccessor().
4422                getButtonDownMasks();
4423        int mask = 0;
4424        for (int buttonDownMask : buttonsDownMask) {
4425            mask |= buttonDownMask;
4426        }
4427        BUTTONS_DOWN_MASK = mask;
4428    }
4429
4430    LightweightDispatcher(Container nativeContainer) {
4431        this.nativeContainer = nativeContainer;
4432        mouseEventTarget = new WeakReference<>(null);
4433        targetLastEntered = new WeakReference<>(null);
4434        targetLastEnteredDT = new WeakReference<>(null);
4435        eventMask = 0;
4436    }
4437
4438    /*
4439     * Clean up any resources allocated when dispatcher was created;
4440     * should be called from Container.removeNotify
4441     */
4442    void dispose() {
4443        //System.out.println("Disposing lw dispatcher");
4444        stopListeningForOtherDrags();
4445        mouseEventTarget.clear();
4446        targetLastEntered.clear();
4447        targetLastEnteredDT.clear();
4448    }
4449
4450    /**
4451     * Enables events to subcomponents.
4452     */
4453    void enableEvents(long events) {
4454        eventMask |= events;
4455    }
4456
4457    /**
4458     * Dispatches an event to a sub-component if necessary, and
4459     * returns whether or not the event was forwarded to a
4460     * sub-component.
4461     *
4462     * @param e the event
4463     */
4464    boolean dispatchEvent(AWTEvent e) {
4465        boolean ret = false;
4466
4467        /*
4468         * Fix for BugTraq Id 4389284.
4469         * Dispatch SunDropTargetEvents regardless of eventMask value.
4470         * Do not update cursor on dispatching SunDropTargetEvents.
4471         */
4472        if (e instanceof SunDropTargetEvent) {
4473
4474            SunDropTargetEvent sdde = (SunDropTargetEvent) e;
4475            ret = processDropTargetEvent(sdde);
4476
4477        } else {
4478            if (e instanceof MouseEvent && (eventMask & MOUSE_MASK) != 0) {
4479                MouseEvent me = (MouseEvent) e;
4480                ret = processMouseEvent(me);
4481            }
4482
4483            if (e.getID() == MouseEvent.MOUSE_MOVED) {
4484                nativeContainer.updateCursorImmediately();
4485            }
4486        }
4487
4488        return ret;
4489    }
4490
4491    /* This method effectively returns whether or not a mouse button was down
4492     * just BEFORE the event happened.  A better method name might be
4493     * wasAMouseButtonDownBeforeThisEvent().
4494     */
4495    private boolean isMouseGrab(MouseEvent e) {
4496        int modifiers = e.getModifiersEx();
4497
4498        if (e.getID() == MouseEvent.MOUSE_PRESSED
4499                || e.getID() == MouseEvent.MOUSE_RELEASED) {
4500            modifiers ^= InputEvent.getMaskForButton(e.getButton());
4501        }
4502        /* modifiers now as just before event */
4503        return ((modifiers & BUTTONS_DOWN_MASK) != 0);
4504    }
4505
4506    /**
4507     * This method attempts to distribute a mouse event to a lightweight
4508     * component.  It tries to avoid doing any unnecessary probes down
4509     * into the component tree to minimize the overhead of determining
4510     * where to route the event, since mouse movement events tend to
4511     * come in large and frequent amounts.
4512     */
4513    private boolean processMouseEvent(MouseEvent e) {
4514        int id = e.getID();
4515        Component mouseOver =   // sensitive to mouse events
4516            nativeContainer.getMouseEventTarget(e.getX(), e.getY(),
4517                                                Container.INCLUDE_SELF);
4518
4519        trackMouseEnterExit(mouseOver, e);
4520
4521        Component met = mouseEventTarget.get();
4522        // 4508327 : MOUSE_CLICKED should only go to the recipient of
4523        // the accompanying MOUSE_PRESSED, so don't reset mouseEventTarget on a
4524        // MOUSE_CLICKED.
4525        if (!isMouseGrab(e) && id != MouseEvent.MOUSE_CLICKED) {
4526            met = (mouseOver != nativeContainer) ? mouseOver : null;
4527            mouseEventTarget = new WeakReference<>(met);
4528        }
4529
4530        if (met != null) {
4531            switch (id) {
4532                case MouseEvent.MOUSE_ENTERED:
4533                case MouseEvent.MOUSE_EXITED:
4534                    break;
4535                case MouseEvent.MOUSE_PRESSED:
4536                    retargetMouseEvent(met, id, e);
4537                    break;
4538                case MouseEvent.MOUSE_RELEASED:
4539                    retargetMouseEvent(met, id, e);
4540                    break;
4541                case MouseEvent.MOUSE_CLICKED:
4542                    // 4508327: MOUSE_CLICKED should never be dispatched to a Component
4543                    // other than that which received the MOUSE_PRESSED event.  If the
4544                    // mouse is now over a different Component, don't dispatch the event.
4545                    // The previous fix for a similar problem was associated with bug
4546                    // 4155217.
4547                    if (mouseOver == met) {
4548                        retargetMouseEvent(mouseOver, id, e);
4549                    }
4550                    break;
4551                case MouseEvent.MOUSE_MOVED:
4552                    retargetMouseEvent(met, id, e);
4553                    break;
4554                case MouseEvent.MOUSE_DRAGGED:
4555                    if (isMouseGrab(e)) {
4556                        retargetMouseEvent(met, id, e);
4557                    }
4558                    break;
4559                case MouseEvent.MOUSE_WHEEL:
4560                    // This may send it somewhere that doesn't have MouseWheelEvents
4561                    // enabled.  In this case, Component.dispatchEventImpl() will
4562                    // retarget the event to a parent that DOES have the events enabled.
4563                    if (eventLog.isLoggable(PlatformLogger.Level.FINEST) && (mouseOver != null)) {
4564                        eventLog.finest("retargeting mouse wheel to " +
4565                                mouseOver.getName() + ", " +
4566                                mouseOver.getClass());
4567                    }
4568                    retargetMouseEvent(mouseOver, id, e);
4569                    break;
4570            }
4571            //Consuming of wheel events is implemented in "retargetMouseEvent".
4572            if (id != MouseEvent.MOUSE_WHEEL) {
4573                e.consume();
4574            }
4575        }
4576        return e.isConsumed();
4577    }
4578
4579    private boolean processDropTargetEvent(SunDropTargetEvent e) {
4580        int id = e.getID();
4581        int x = e.getX();
4582        int y = e.getY();
4583
4584        /*
4585         * Fix for BugTraq ID 4395290.
4586         * It is possible that SunDropTargetEvent's Point is outside of the
4587         * native container bounds. In this case we truncate coordinates.
4588         */
4589        if (!nativeContainer.contains(x, y)) {
4590            final Dimension d = nativeContainer.getSize();
4591            if (d.width <= x) {
4592                x = d.width - 1;
4593            } else if (x < 0) {
4594                x = 0;
4595            }
4596            if (d.height <= y) {
4597                y = d.height - 1;
4598            } else if (y < 0) {
4599                y = 0;
4600            }
4601        }
4602        Component mouseOver =   // not necessarily sensitive to mouse events
4603            nativeContainer.getDropTargetEventTarget(x, y,
4604                                                     Container.INCLUDE_SELF);
4605        trackMouseEnterExit(mouseOver, e);
4606
4607        if (mouseOver != nativeContainer && mouseOver != null) {
4608            switch (id) {
4609            case SunDropTargetEvent.MOUSE_ENTERED:
4610            case SunDropTargetEvent.MOUSE_EXITED:
4611                break;
4612            default:
4613                retargetMouseEvent(mouseOver, id, e);
4614                e.consume();
4615                break;
4616            }
4617        }
4618        return e.isConsumed();
4619    }
4620
4621    /*
4622     * Generates dnd enter/exit events as mouse moves over lw components
4623     * @param targetOver       Target mouse is over (including native container)
4624     * @param e                SunDropTarget mouse event in native container
4625     */
4626    private void trackDropTargetEnterExit(Component targetOver, MouseEvent e) {
4627        int id = e.getID();
4628        if (id == MouseEvent.MOUSE_ENTERED && isMouseDTInNativeContainer) {
4629            // This can happen if a lightweight component which initiated the
4630            // drag has an associated drop target. MOUSE_ENTERED comes when the
4631            // mouse is in the native container already. To propagate this event
4632            // properly we should null out targetLastEntered.
4633            targetLastEnteredDT.clear();
4634        } else if (id == MouseEvent.MOUSE_ENTERED) {
4635            isMouseDTInNativeContainer = true;
4636        } else if (id == MouseEvent.MOUSE_EXITED) {
4637            isMouseDTInNativeContainer = false;
4638        }
4639        Component tle = retargetMouseEnterExit(targetOver, e,
4640                                                     targetLastEnteredDT.get(),
4641                                                     isMouseDTInNativeContainer);
4642        targetLastEnteredDT = new WeakReference<>(tle);
4643    }
4644
4645    /*
4646     * Generates enter/exit events as mouse moves over lw components
4647     * @param targetOver        Target mouse is over (including native container)
4648     * @param e                 Mouse event in native container
4649     */
4650    private void trackMouseEnterExit(Component targetOver, MouseEvent e) {
4651        if (e instanceof SunDropTargetEvent) {
4652            trackDropTargetEnterExit(targetOver, e);
4653            return;
4654        }
4655        int id = e.getID();
4656
4657        if ( id != MouseEvent.MOUSE_EXITED &&
4658             id != MouseEvent.MOUSE_DRAGGED &&
4659             id != LWD_MOUSE_DRAGGED_OVER &&
4660                !isMouseInNativeContainer) {
4661            // any event but an exit or drag means we're in the native container
4662            isMouseInNativeContainer = true;
4663            startListeningForOtherDrags();
4664        } else if (id == MouseEvent.MOUSE_EXITED) {
4665            isMouseInNativeContainer = false;
4666            stopListeningForOtherDrags();
4667        }
4668        Component tle = retargetMouseEnterExit(targetOver, e,
4669                                                   targetLastEntered.get(),
4670                                                   isMouseInNativeContainer);
4671        targetLastEntered = new WeakReference<>(tle);
4672    }
4673
4674    private Component retargetMouseEnterExit(Component targetOver, MouseEvent e,
4675                                             Component lastEntered,
4676                                             boolean inNativeContainer) {
4677        int id = e.getID();
4678        Component targetEnter = inNativeContainer ? targetOver : null;
4679
4680        if (lastEntered != targetEnter) {
4681            if (lastEntered != null) {
4682                retargetMouseEvent(lastEntered, MouseEvent.MOUSE_EXITED, e);
4683            }
4684            if (id == MouseEvent.MOUSE_EXITED) {
4685                // consume native exit event if we generate one
4686                e.consume();
4687            }
4688
4689            if (targetEnter != null) {
4690                retargetMouseEvent(targetEnter, MouseEvent.MOUSE_ENTERED, e);
4691            }
4692            if (id == MouseEvent.MOUSE_ENTERED) {
4693                // consume native enter event if we generate one
4694                e.consume();
4695            }
4696        }
4697        return targetEnter;
4698    }
4699
4700    /*
4701     * Listens to global mouse drag events so even drags originating
4702     * from other heavyweight containers will generate enter/exit
4703     * events in this container
4704     */
4705    private void startListeningForOtherDrags() {
4706        //System.out.println("Adding AWTEventListener");
4707        java.security.AccessController.doPrivileged(
4708            new java.security.PrivilegedAction<Object>() {
4709                public Object run() {
4710                    nativeContainer.getToolkit().addAWTEventListener(
4711                        LightweightDispatcher.this,
4712                        AWTEvent.MOUSE_EVENT_MASK |
4713                        AWTEvent.MOUSE_MOTION_EVENT_MASK);
4714                    return null;
4715                }
4716            }
4717        );
4718    }
4719
4720    private void stopListeningForOtherDrags() {
4721        //System.out.println("Removing AWTEventListener");
4722        java.security.AccessController.doPrivileged(
4723            new java.security.PrivilegedAction<Object>() {
4724                public Object run() {
4725                    nativeContainer.getToolkit().removeAWTEventListener(LightweightDispatcher.this);
4726                    return null;
4727                }
4728            }
4729        );
4730    }
4731
4732    /*
4733     * (Implementation of AWTEventListener)
4734     * Listen for drag events posted in other hw components so we can
4735     * track enter/exit regardless of where a drag originated
4736     */
4737    @SuppressWarnings("deprecation")
4738    public void eventDispatched(AWTEvent e) {
4739        boolean isForeignDrag = (e instanceof MouseEvent) &&
4740                                !(e instanceof SunDropTargetEvent) &&
4741                                (e.id == MouseEvent.MOUSE_DRAGGED) &&
4742                                (e.getSource() != nativeContainer);
4743
4744        if (!isForeignDrag) {
4745            // only interested in drags from other hw components
4746            return;
4747        }
4748
4749        MouseEvent      srcEvent = (MouseEvent)e;
4750        MouseEvent      me;
4751
4752        synchronized (nativeContainer.getTreeLock()) {
4753            Component srcComponent = srcEvent.getComponent();
4754
4755            // component may have disappeared since drag event posted
4756            // (i.e. Swing hierarchical menus)
4757            if ( !srcComponent.isShowing() ) {
4758                return;
4759            }
4760
4761            // see 5083555
4762            // check if srcComponent is in any modal blocked window
4763            Component c = nativeContainer;
4764            while ((c != null) && !(c instanceof Window)) {
4765                c = c.getParent_NoClientCode();
4766            }
4767            if ((c == null) || ((Window)c).isModalBlocked()) {
4768                return;
4769            }
4770
4771            //
4772            // create an internal 'dragged-over' event indicating
4773            // we are being dragged over from another hw component
4774            //
4775            me = new MouseEvent(nativeContainer,
4776                               LWD_MOUSE_DRAGGED_OVER,
4777                               srcEvent.getWhen(),
4778                               srcEvent.getModifiersEx() | srcEvent.getModifiers(),
4779                               srcEvent.getX(),
4780                               srcEvent.getY(),
4781                               srcEvent.getXOnScreen(),
4782                               srcEvent.getYOnScreen(),
4783                               srcEvent.getClickCount(),
4784                               srcEvent.isPopupTrigger(),
4785                               srcEvent.getButton());
4786            ((AWTEvent)srcEvent).copyPrivateDataInto(me);
4787            // translate coordinates to this native container
4788            final Point ptSrcOrigin = srcComponent.getLocationOnScreen();
4789
4790            if (AppContext.getAppContext() != nativeContainer.appContext) {
4791                final MouseEvent mouseEvent = me;
4792                Runnable r = new Runnable() {
4793                        public void run() {
4794                            if (!nativeContainer.isShowing() ) {
4795                                return;
4796                            }
4797
4798                            Point       ptDstOrigin = nativeContainer.getLocationOnScreen();
4799                            mouseEvent.translatePoint(ptSrcOrigin.x - ptDstOrigin.x,
4800                                              ptSrcOrigin.y - ptDstOrigin.y );
4801                            Component targetOver =
4802                                nativeContainer.getMouseEventTarget(mouseEvent.getX(),
4803                                                                    mouseEvent.getY(),
4804                                                                    Container.INCLUDE_SELF);
4805                            trackMouseEnterExit(targetOver, mouseEvent);
4806                        }
4807                    };
4808                SunToolkit.executeOnEventHandlerThread(nativeContainer, r);
4809                return;
4810            } else {
4811                if (!nativeContainer.isShowing() ) {
4812                    return;
4813                }
4814
4815                Point   ptDstOrigin = nativeContainer.getLocationOnScreen();
4816                me.translatePoint( ptSrcOrigin.x - ptDstOrigin.x, ptSrcOrigin.y - ptDstOrigin.y );
4817            }
4818        }
4819        //System.out.println("Track event: " + me);
4820        // feed the 'dragged-over' event directly to the enter/exit
4821        // code (not a real event so don't pass it to dispatchEvent)
4822        Component targetOver =
4823            nativeContainer.getMouseEventTarget(me.getX(), me.getY(),
4824                                                Container.INCLUDE_SELF);
4825        trackMouseEnterExit(targetOver, me);
4826    }
4827
4828    /**
4829     * Sends a mouse event to the current mouse event recipient using
4830     * the given event (sent to the windowed host) as a srcEvent.  If
4831     * the mouse event target is still in the component tree, the
4832     * coordinates of the event are translated to those of the target.
4833     * If the target has been removed, we don't bother to send the
4834     * message.
4835     */
4836    @SuppressWarnings("deprecation")
4837    void retargetMouseEvent(Component target, int id, MouseEvent e) {
4838        if (target == null) {
4839            return; // mouse is over another hw component or target is disabled
4840        }
4841
4842        int x = e.getX(), y = e.getY();
4843        Component component;
4844
4845        for(component = target;
4846            component != null && component != nativeContainer;
4847            component = component.getParent()) {
4848            x -= component.x;
4849            y -= component.y;
4850        }
4851        MouseEvent retargeted;
4852        if (component != null) {
4853            if (e instanceof SunDropTargetEvent) {
4854                retargeted = new SunDropTargetEvent(target,
4855                                                    id,
4856                                                    x,
4857                                                    y,
4858                                                    ((SunDropTargetEvent)e).getDispatcher());
4859            } else if (id == MouseEvent.MOUSE_WHEEL) {
4860                retargeted = new MouseWheelEvent(target,
4861                                      id,
4862                                       e.getWhen(),
4863                                       e.getModifiersEx() | e.getModifiers(),
4864                                       x,
4865                                       y,
4866                                       e.getXOnScreen(),
4867                                       e.getYOnScreen(),
4868                                       e.getClickCount(),
4869                                       e.isPopupTrigger(),
4870                                       ((MouseWheelEvent)e).getScrollType(),
4871                                       ((MouseWheelEvent)e).getScrollAmount(),
4872                                       ((MouseWheelEvent)e).getWheelRotation(),
4873                                       ((MouseWheelEvent)e).getPreciseWheelRotation());
4874            }
4875            else {
4876                retargeted = new MouseEvent(target,
4877                                            id,
4878                                            e.getWhen(),
4879                                            e.getModifiersEx() | e.getModifiers(),
4880                                            x,
4881                                            y,
4882                                            e.getXOnScreen(),
4883                                            e.getYOnScreen(),
4884                                            e.getClickCount(),
4885                                            e.isPopupTrigger(),
4886                                            e.getButton());
4887            }
4888
4889            ((AWTEvent)e).copyPrivateDataInto(retargeted);
4890
4891            if (target == nativeContainer) {
4892                // avoid recursively calling LightweightDispatcher...
4893                ((Container)target).dispatchEventToSelf(retargeted);
4894            } else {
4895                assert AppContext.getAppContext() == target.appContext;
4896
4897                if (nativeContainer.modalComp != null) {
4898                    if (((Container)nativeContainer.modalComp).isAncestorOf(target)) {
4899                        target.dispatchEvent(retargeted);
4900                    } else {
4901                        e.consume();
4902                    }
4903                } else {
4904                    target.dispatchEvent(retargeted);
4905                }
4906            }
4907            if (id == MouseEvent.MOUSE_WHEEL && retargeted.isConsumed()) {
4908                //An exception for wheel bubbling to the native system.
4909                //In "processMouseEvent" total event consuming for wheel events is skipped.
4910                //Protection from bubbling of Java-accepted wheel events.
4911                e.consume();
4912            }
4913        }
4914    }
4915
4916    // --- member variables -------------------------------
4917
4918    /**
4919     * The windowed container that might be hosting events for
4920     * subcomponents.
4921     */
4922    private Container nativeContainer;
4923
4924    /**
4925     * This variable is not used, but kept for serialization compatibility
4926     */
4927    private Component focus;
4928
4929    /**
4930     * The current subcomponent being hosted by this windowed
4931     * component that has events being forwarded to it.  If this
4932     * is null, there are currently no events being forwarded to
4933     * a subcomponent.
4934     */
4935    private transient WeakReference<Component> mouseEventTarget;
4936
4937    /**
4938     * The last component entered by the {@code MouseEvent}.
4939     */
4940    private transient  WeakReference<Component> targetLastEntered;
4941
4942    /**
4943     * The last component entered by the {@code SunDropTargetEvent}.
4944     */
4945    private transient  WeakReference<Component> targetLastEnteredDT;
4946
4947    /**
4948     * Is the mouse over the native container.
4949     */
4950    private transient boolean isMouseInNativeContainer = false;
4951
4952    /**
4953     * Is DnD over the native container.
4954     */
4955    private transient boolean isMouseDTInNativeContainer = false;
4956
4957    /**
4958     * This variable is not used, but kept for serialization compatibility
4959     */
4960    private Cursor nativeCursor;
4961
4962    /**
4963     * The event mask for contained lightweight components.  Lightweight
4964     * components need a windowed container to host window-related
4965     * events.  This separate mask indicates events that have been
4966     * requested by contained lightweight components without effecting
4967     * the mask of the windowed component itself.
4968     */
4969    private long eventMask;
4970
4971    /**
4972     * The kind of events routed to lightweight components from windowed
4973     * hosts.
4974     */
4975    private static final long PROXY_EVENT_MASK =
4976        AWTEvent.FOCUS_EVENT_MASK |
4977        AWTEvent.KEY_EVENT_MASK |
4978        AWTEvent.MOUSE_EVENT_MASK |
4979        AWTEvent.MOUSE_MOTION_EVENT_MASK |
4980        AWTEvent.MOUSE_WHEEL_EVENT_MASK;
4981
4982    private static final long MOUSE_MASK =
4983        AWTEvent.MOUSE_EVENT_MASK |
4984        AWTEvent.MOUSE_MOTION_EVENT_MASK |
4985        AWTEvent.MOUSE_WHEEL_EVENT_MASK;
4986}
4987