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

Print this page




 530         if (comp.parent == this) {
 531             if (index == component.size()) {
 532                 throw new IllegalArgumentException("illegal component position " +
 533                                                    index + " should be less then " + component.size());
 534             }
 535         }
 536         checkAddToSelf(comp);
 537         checkNotAWindow(comp);
 538 
 539         Window thisTopLevel = getContainingWindow();
 540         Window compTopLevel = comp.getContainingWindow();
 541         if (thisTopLevel != compTopLevel) {
 542             throw new IllegalArgumentException("component and container should be in the same top-level window");
 543         }
 544         if (thisGC != null) {
 545             comp.checkGD(thisGC.getDevice().getIDstring());
 546         }
 547     }
 548 
 549     /**
 550      * Removes component comp from this container without making unneccessary changes
 551      * and generating unneccessary events. This function intended to perform optimized
 552      * remove, for example, if newParent and current parent are the same it just changes
 553      * index without calling removeNotify.
 554      * Note: Should be called while holding treeLock
 555      * Returns whether removeNotify was invoked
 556      * @since: 1.5
 557      */
 558     private boolean removeDelicately(Component comp, Container newParent, int newIndex) {
 559         checkTreeLock();
 560 
 561         int index = getComponentZOrder(comp);
 562         boolean needRemoveNotify = isRemoveNotifyNeeded(comp, this, newParent);
 563         if (needRemoveNotify) {
 564             comp.removeNotify();
 565         }
 566         if (newParent != this) {
 567             if (layoutMgr != null) {
 568                 layoutMgr.removeLayoutComponent(comp);
 569             }
 570             adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK,
 571                                     -comp.numListening(AWTEvent.HIERARCHY_EVENT_MASK));


1390 
1391         if (num == 0)
1392             return;
1393 
1394         if ((mask & AWTEvent.HIERARCHY_EVENT_MASK) != 0) {
1395             listeningChildren += num;
1396         }
1397         if ((mask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0) {
1398             listeningBoundsChildren += num;
1399         }
1400 
1401         adjustListeningChildrenOnParent(mask, num);
1402     }
1403 
1404     // Should only be called while holding tree lock
1405     void adjustDescendants(int num) {
1406         if (num == 0)
1407             return;
1408 
1409         descendantsCount += num;
1410         adjustDecendantsOnParent(num);
1411     }
1412 
1413     // Should only be called while holding tree lock
1414     void adjustDecendantsOnParent(int num) {
1415         if (parent != null) {
1416             parent.adjustDescendants(num);
1417         }
1418     }
1419 
1420     // Should only be called while holding tree lock
1421     int countHierarchyMembers() {
1422         if (log.isLoggable(PlatformLogger.Level.FINE)) {
1423             // Verify descendantsCount is correct
1424             int sum = 0;
1425             for (Component comp : component) {
1426                 sum += comp.countHierarchyMembers();
1427             }
1428             if (descendantsCount != sum) {
1429                 log.fine("Assertion (descendantsCount == sum) failed");
1430             }
1431         }
1432         return descendantsCount + 1;
1433     }
1434 


2321               case ComponentEvent.COMPONENT_MOVED:
2322                 createChildHierarchyEvents(HierarchyEvent.ANCESTOR_MOVED, 0,
2323                                        Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
2324                 break;
2325               default:
2326                 break;
2327             }
2328         }
2329     }
2330 
2331     /*
2332      * Dispatches an event to this component, without trying to forward
2333      * it to any subcomponents
2334      * @param e the event
2335      */
2336     void dispatchEventToSelf(AWTEvent e) {
2337         super.dispatchEventImpl(e);
2338     }
2339 
2340     /**
2341      * Fetchs the top-most (deepest) lightweight component that is interested
2342      * in receiving mouse events.
2343      */
2344     Component getMouseEventTarget(int x, int y, boolean includeSelf) {
2345         return getMouseEventTarget(x, y, includeSelf,
2346                                    MouseEventTargetFilter.FILTER,
2347                                    !SEARCH_HEAVYWEIGHTS);
2348     }
2349 
2350     /**
2351      * Fetches the top-most (deepest) component to receive SunDropTargetEvents.
2352      */
2353     Component getDropTargetEventTarget(int x, int y, boolean includeSelf) {
2354         return getMouseEventTarget(x, y, includeSelf,
2355                                    DropTargetEventTargetFilter.FILTER,
2356                                    SEARCH_HEAVYWEIGHTS);
2357     }
2358 
2359     /**
2360      * A private version of getMouseEventTarget which has two additional
2361      * controllable behaviors. This method searches for the top-most


2870      * some private AWT classes like SequencedEvent.
2871      *
2872      * The native container of the LW component has this field set
2873      * to tell it that it should block Mouse events for all LW
2874      * children except for the modal component.
2875      *
2876      * In the case of nested Modal components, we store the previous
2877      * modal component in the new modal components value of modalComp;
2878      */
2879 
2880     transient Component modalComp;
2881     transient AppContext modalAppContext;
2882 
2883     private void startLWModal() {
2884         // Store the app context on which this component is being shown.
2885         // Event dispatch thread of this app context will be sleeping until
2886         // we wake it by any event from hideAndDisposeHandler().
2887         modalAppContext = AppContext.getAppContext();
2888 
2889         // keep the KeyEvents from being dispatched
2890         // until the focus has been transfered
2891         long time = Toolkit.getEventQueue().getMostRecentKeyEventTime();
2892         Component predictedFocusOwner = (Component.isInstanceOf(this, "javax.swing.JInternalFrame")) ? ((javax.swing.JInternalFrame)(this)).getMostRecentFocusOwner() : null;
2893         if (predictedFocusOwner != null) {
2894             KeyboardFocusManager.getCurrentKeyboardFocusManager().
2895                 enqueueKeyEvents(time, predictedFocusOwner);
2896         }
2897         // We have two mechanisms for blocking: 1. If we're on the
2898         // EventDispatchThread, start a new event pump. 2. If we're
2899         // on any other thread, call wait() on the treelock.
2900         final Container nativeContainer;
2901         synchronized (getTreeLock()) {
2902             nativeContainer = getHeavyweightContainer();
2903             if (nativeContainer.modalComp != null) {
2904                 this.modalComp =  nativeContainer.modalComp;
2905                 nativeContainer.modalComp = this;
2906                 return;
2907             }
2908             else {
2909                 nativeContainer.modalComp = this;
2910             }


3656      */
3657     public void addPropertyChangeListener(String propertyName,
3658                                           PropertyChangeListener listener) {
3659         super.addPropertyChangeListener(propertyName, listener);
3660     }
3661 
3662     // Serialization support. A Container is responsible for restoring the
3663     // parent fields of its component children.
3664 
3665     /**
3666      * Container Serial Data Version.
3667      */
3668     private int containerSerializedDataVersion = 1;
3669 
3670     /**
3671      * Serializes this <code>Container</code> to the specified
3672      * <code>ObjectOutputStream</code>.
3673      * <ul>
3674      *    <li>Writes default serializable fields to the stream.</li>
3675      *    <li>Writes a list of serializable ContainerListener(s) as optional
3676      *        data. The non-serializable ContainerListner(s) are detected and
3677      *        no attempt is made to serialize them.</li>
3678      *    <li>Write this Container's FocusTraversalPolicy if and only if it
3679      *        is Serializable; otherwise, <code>null</code> is written.</li>
3680      * </ul>
3681      *
3682      * @param s the <code>ObjectOutputStream</code> to write
3683      * @serialData <code>null</code> terminated sequence of 0 or more pairs;
3684      *   the pair consists of a <code>String</code> and <code>Object</code>;
3685      *   the <code>String</code> indicates the type of object and
3686      *   is one of the following:
3687      *   <code>containerListenerK</code> indicating an
3688      *     <code>ContainerListener</code> object;
3689      *   the <code>Container</code>'s <code>FocusTraversalPolicy</code>,
3690      *     or <code>null</code>
3691      *
3692      * @see AWTEventMulticaster#save(java.io.ObjectOutputStream, java.lang.String, java.util.EventListener)
3693      * @see Container#containerListenerK
3694      * @see #readObject(ObjectInputStream)
3695      */
3696     private void writeObject(ObjectOutputStream s) throws IOException {




 530         if (comp.parent == this) {
 531             if (index == component.size()) {
 532                 throw new IllegalArgumentException("illegal component position " +
 533                                                    index + " should be less then " + component.size());
 534             }
 535         }
 536         checkAddToSelf(comp);
 537         checkNotAWindow(comp);
 538 
 539         Window thisTopLevel = getContainingWindow();
 540         Window compTopLevel = comp.getContainingWindow();
 541         if (thisTopLevel != compTopLevel) {
 542             throw new IllegalArgumentException("component and container should be in the same top-level window");
 543         }
 544         if (thisGC != null) {
 545             comp.checkGD(thisGC.getDevice().getIDstring());
 546         }
 547     }
 548 
 549     /**
 550      * Removes component comp from this container without making unnecessary changes
 551      * and generating unnecessary events. This function intended to perform optimized
 552      * remove, for example, if newParent and current parent are the same it just changes
 553      * index without calling removeNotify.
 554      * Note: Should be called while holding treeLock
 555      * Returns whether removeNotify was invoked
 556      * @since: 1.5
 557      */
 558     private boolean removeDelicately(Component comp, Container newParent, int newIndex) {
 559         checkTreeLock();
 560 
 561         int index = getComponentZOrder(comp);
 562         boolean needRemoveNotify = isRemoveNotifyNeeded(comp, this, newParent);
 563         if (needRemoveNotify) {
 564             comp.removeNotify();
 565         }
 566         if (newParent != this) {
 567             if (layoutMgr != null) {
 568                 layoutMgr.removeLayoutComponent(comp);
 569             }
 570             adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK,
 571                                     -comp.numListening(AWTEvent.HIERARCHY_EVENT_MASK));


1390 
1391         if (num == 0)
1392             return;
1393 
1394         if ((mask & AWTEvent.HIERARCHY_EVENT_MASK) != 0) {
1395             listeningChildren += num;
1396         }
1397         if ((mask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0) {
1398             listeningBoundsChildren += num;
1399         }
1400 
1401         adjustListeningChildrenOnParent(mask, num);
1402     }
1403 
1404     // Should only be called while holding tree lock
1405     void adjustDescendants(int num) {
1406         if (num == 0)
1407             return;
1408 
1409         descendantsCount += num;
1410         adjustDescendantsOnParent(num);
1411     }
1412 
1413     // Should only be called while holding tree lock
1414     void adjustDescendantsOnParent(int num) {
1415         if (parent != null) {
1416             parent.adjustDescendants(num);
1417         }
1418     }
1419 
1420     // Should only be called while holding tree lock
1421     int countHierarchyMembers() {
1422         if (log.isLoggable(PlatformLogger.Level.FINE)) {
1423             // Verify descendantsCount is correct
1424             int sum = 0;
1425             for (Component comp : component) {
1426                 sum += comp.countHierarchyMembers();
1427             }
1428             if (descendantsCount != sum) {
1429                 log.fine("Assertion (descendantsCount == sum) failed");
1430             }
1431         }
1432         return descendantsCount + 1;
1433     }
1434 


2321               case ComponentEvent.COMPONENT_MOVED:
2322                 createChildHierarchyEvents(HierarchyEvent.ANCESTOR_MOVED, 0,
2323                                        Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
2324                 break;
2325               default:
2326                 break;
2327             }
2328         }
2329     }
2330 
2331     /*
2332      * Dispatches an event to this component, without trying to forward
2333      * it to any subcomponents
2334      * @param e the event
2335      */
2336     void dispatchEventToSelf(AWTEvent e) {
2337         super.dispatchEventImpl(e);
2338     }
2339 
2340     /**
2341      * Fetches the top-most (deepest) lightweight component that is interested
2342      * in receiving mouse events.
2343      */
2344     Component getMouseEventTarget(int x, int y, boolean includeSelf) {
2345         return getMouseEventTarget(x, y, includeSelf,
2346                                    MouseEventTargetFilter.FILTER,
2347                                    !SEARCH_HEAVYWEIGHTS);
2348     }
2349 
2350     /**
2351      * Fetches the top-most (deepest) component to receive SunDropTargetEvents.
2352      */
2353     Component getDropTargetEventTarget(int x, int y, boolean includeSelf) {
2354         return getMouseEventTarget(x, y, includeSelf,
2355                                    DropTargetEventTargetFilter.FILTER,
2356                                    SEARCH_HEAVYWEIGHTS);
2357     }
2358 
2359     /**
2360      * A private version of getMouseEventTarget which has two additional
2361      * controllable behaviors. This method searches for the top-most


2870      * some private AWT classes like SequencedEvent.
2871      *
2872      * The native container of the LW component has this field set
2873      * to tell it that it should block Mouse events for all LW
2874      * children except for the modal component.
2875      *
2876      * In the case of nested Modal components, we store the previous
2877      * modal component in the new modal components value of modalComp;
2878      */
2879 
2880     transient Component modalComp;
2881     transient AppContext modalAppContext;
2882 
2883     private void startLWModal() {
2884         // Store the app context on which this component is being shown.
2885         // Event dispatch thread of this app context will be sleeping until
2886         // we wake it by any event from hideAndDisposeHandler().
2887         modalAppContext = AppContext.getAppContext();
2888 
2889         // keep the KeyEvents from being dispatched
2890         // until the focus has been transferred
2891         long time = Toolkit.getEventQueue().getMostRecentKeyEventTime();
2892         Component predictedFocusOwner = (Component.isInstanceOf(this, "javax.swing.JInternalFrame")) ? ((javax.swing.JInternalFrame)(this)).getMostRecentFocusOwner() : null;
2893         if (predictedFocusOwner != null) {
2894             KeyboardFocusManager.getCurrentKeyboardFocusManager().
2895                 enqueueKeyEvents(time, predictedFocusOwner);
2896         }
2897         // We have two mechanisms for blocking: 1. If we're on the
2898         // EventDispatchThread, start a new event pump. 2. If we're
2899         // on any other thread, call wait() on the treelock.
2900         final Container nativeContainer;
2901         synchronized (getTreeLock()) {
2902             nativeContainer = getHeavyweightContainer();
2903             if (nativeContainer.modalComp != null) {
2904                 this.modalComp =  nativeContainer.modalComp;
2905                 nativeContainer.modalComp = this;
2906                 return;
2907             }
2908             else {
2909                 nativeContainer.modalComp = this;
2910             }


3656      */
3657     public void addPropertyChangeListener(String propertyName,
3658                                           PropertyChangeListener listener) {
3659         super.addPropertyChangeListener(propertyName, listener);
3660     }
3661 
3662     // Serialization support. A Container is responsible for restoring the
3663     // parent fields of its component children.
3664 
3665     /**
3666      * Container Serial Data Version.
3667      */
3668     private int containerSerializedDataVersion = 1;
3669 
3670     /**
3671      * Serializes this <code>Container</code> to the specified
3672      * <code>ObjectOutputStream</code>.
3673      * <ul>
3674      *    <li>Writes default serializable fields to the stream.</li>
3675      *    <li>Writes a list of serializable ContainerListener(s) as optional
3676      *        data. The non-serializable ContainerListener(s) are detected and
3677      *        no attempt is made to serialize them.</li>
3678      *    <li>Write this Container's FocusTraversalPolicy if and only if it
3679      *        is Serializable; otherwise, <code>null</code> is written.</li>
3680      * </ul>
3681      *
3682      * @param s the <code>ObjectOutputStream</code> to write
3683      * @serialData <code>null</code> terminated sequence of 0 or more pairs;
3684      *   the pair consists of a <code>String</code> and <code>Object</code>;
3685      *   the <code>String</code> indicates the type of object and
3686      *   is one of the following:
3687      *   <code>containerListenerK</code> indicating an
3688      *     <code>ContainerListener</code> object;
3689      *   the <code>Container</code>'s <code>FocusTraversalPolicy</code>,
3690      *     or <code>null</code>
3691      *
3692      * @see AWTEventMulticaster#save(java.io.ObjectOutputStream, java.lang.String, java.util.EventListener)
3693      * @see Container#containerListenerK
3694      * @see #readObject(ObjectInputStream)
3695      */
3696     private void writeObject(ObjectOutputStream s) throws IOException {