jdk/src/share/classes/java/awt/Container.java
changeset 2463 c90a9d542c79
parent 2462 192552ca8292
child 2644 63360b4ca6c4
equal deleted inserted replaced
2462:192552ca8292 2463:c90a9d542c79
   268         focusTraversalKeys = new Set[4];
   268         focusTraversalKeys = new Set[4];
   269     }
   269     }
   270 
   270 
   271     /**
   271     /**
   272      * Gets the number of components in this panel.
   272      * Gets the number of components in this panel.
       
   273      * <p>
       
   274      * Note: This method should be called under AWT tree lock.
       
   275      *
   273      * @return    the number of components in this panel.
   276      * @return    the number of components in this panel.
   274      * @see       #getComponent
   277      * @see       #getComponent
   275      * @since     JDK1.1
   278      * @since     JDK1.1
       
   279      * @see Component#getTreeLock()
   276      */
   280      */
   277     public int getComponentCount() {
   281     public int getComponentCount() {
   278         return countComponents();
   282         return countComponents();
   279     }
   283     }
   280 
   284 
   282      * @deprecated As of JDK version 1.1,
   286      * @deprecated As of JDK version 1.1,
   283      * replaced by getComponentCount().
   287      * replaced by getComponentCount().
   284      */
   288      */
   285     @Deprecated
   289     @Deprecated
   286     public int countComponents() {
   290     public int countComponents() {
   287         synchronized (getTreeLock()) {
   291         // This method is not synchronized under AWT tree lock.
   288             return component.size();
   292         // Instead, the calling code is responsible for the
   289         }
   293         // synchronization. See 6784816 for details.
       
   294         return component.size();
   290     }
   295     }
   291 
   296 
   292     /**
   297     /**
   293      * Gets the nth component in this container.
   298      * Gets the nth component in this container.
       
   299      * <p>
       
   300      * Note: This method should be called under AWT tree lock.
       
   301      *
   294      * @param      n   the index of the component to get.
   302      * @param      n   the index of the component to get.
   295      * @return     the n<sup>th</sup> component in this container.
   303      * @return     the n<sup>th</sup> component in this container.
   296      * @exception  ArrayIndexOutOfBoundsException
   304      * @exception  ArrayIndexOutOfBoundsException
   297      *                 if the n<sup>th</sup> value does not exist.
   305      *                 if the n<sup>th</sup> value does not exist.
       
   306      * @see Component#getTreeLock()
   298      */
   307      */
   299     public Component getComponent(int n) {
   308     public Component getComponent(int n) {
   300         synchronized (getTreeLock()) {
   309         // This method is not synchronized under AWT tree lock.
   301             if ((n < 0) || (n >= component.size())) {
   310         // Instead, the calling code is responsible for the
   302                 throw new ArrayIndexOutOfBoundsException("No such child: " + n);
   311         // synchronization. See 6784816 for details.
   303             }
   312         try {
   304             return component.get(n);
   313             return component.get(n);
       
   314         } catch (IndexOutOfBoundsException z) {
       
   315             throw new ArrayIndexOutOfBoundsException("No such child: " + n);
   305         }
   316         }
   306     }
   317     }
   307 
   318 
   308     /**
   319     /**
   309      * Gets all the components in this container.
   320      * Gets all the components in this container.
       
   321      * <p>
       
   322      * Note: This method should be called under AWT tree lock.
       
   323      *
   310      * @return    an array of all the components in this container.
   324      * @return    an array of all the components in this container.
       
   325      * @see Component#getTreeLock()
   311      */
   326      */
   312     public Component[] getComponents() {
   327     public Component[] getComponents() {
       
   328         // This method is not synchronized under AWT tree lock.
       
   329         // Instead, the calling code is responsible for the
       
   330         // synchronization. See 6784816 for details.
   313         return getComponents_NoClientCode();
   331         return getComponents_NoClientCode();
   314     }
   332     }
       
   333 
   315     // NOTE: This method may be called by privileged threads.
   334     // NOTE: This method may be called by privileged threads.
   316     //       This functionality is implemented in a package-private method
   335     //       This functionality is implemented in a package-private method
   317     //       to insure that it cannot be overridden by client subclasses.
   336     //       to insure that it cannot be overridden by client subclasses.
   318     //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
   337     //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
   319     final Component[] getComponents_NoClientCode() {
   338     final Component[] getComponents_NoClientCode() {
       
   339         return component.toArray(EMPTY_ARRAY);
       
   340     }
       
   341 
       
   342     /*
       
   343      * Wrapper for getComponents() method with a proper synchronization.
       
   344      */
       
   345     Component[] getComponentsSync() {
   320         synchronized (getTreeLock()) {
   346         synchronized (getTreeLock()) {
   321             return component.toArray(EMPTY_ARRAY);
   347             return getComponents();
   322         }
   348         }
   323     } // getComponents_NoClientCode()
   349     }
   324 
   350 
   325     /**
   351     /**
   326      * Determines the insets of this container, which indicate the size
   352      * Determines the insets of this container, which indicate the size
   327      * of the container's border.
   353      * of the container's border.
   328      * <p>
   354      * <p>
  1026                 throw new IllegalArgumentException(
  1052                 throw new IllegalArgumentException(
  1027                           "illegal component position");
  1053                           "illegal component position");
  1028             }
  1054             }
  1029             checkAddToSelf(comp);
  1055             checkAddToSelf(comp);
  1030             checkNotAWindow(comp);
  1056             checkNotAWindow(comp);
  1031         if (thisGC != null) {
  1057             if (thisGC != null) {
  1032             comp.checkGD(thisGC.getDevice().getIDstring());
  1058                 comp.checkGD(thisGC.getDevice().getIDstring());
  1033         }
  1059             }
  1034 
  1060 
  1035             /* Reparent the component and tidy up the tree's state. */
  1061             /* Reparent the component and tidy up the tree's state. */
  1036             if (comp.parent != null) {
  1062             if (comp.parent != null) {
  1037                 comp.parent.remove(comp);
  1063                 comp.parent.remove(comp);
  1038                     if (index > component.size()) {
  1064                     if (index > component.size()) {
  1347         }
  1373         }
  1348         return descendantsCount + 1;
  1374         return descendantsCount + 1;
  1349     }
  1375     }
  1350 
  1376 
  1351     private int getListenersCount(int id, boolean enabledOnToolkit) {
  1377     private int getListenersCount(int id, boolean enabledOnToolkit) {
  1352         assert Thread.holdsLock(getTreeLock());
  1378         checkTreeLock();
  1353         if (enabledOnToolkit) {
  1379         if (enabledOnToolkit) {
  1354             return descendantsCount;
  1380             return descendantsCount;
  1355         }
  1381         }
  1356         switch (id) {
  1382         switch (id) {
  1357           case HierarchyEvent.HIERARCHY_CHANGED:
  1383           case HierarchyEvent.HIERARCHY_CHANGED:
  1365     }
  1391     }
  1366 
  1392 
  1367     final int createHierarchyEvents(int id, Component changed,
  1393     final int createHierarchyEvents(int id, Component changed,
  1368         Container changedParent, long changeFlags, boolean enabledOnToolkit)
  1394         Container changedParent, long changeFlags, boolean enabledOnToolkit)
  1369     {
  1395     {
  1370         assert Thread.holdsLock(getTreeLock());
  1396         checkTreeLock();
  1371         int listeners = getListenersCount(id, enabledOnToolkit);
  1397         int listeners = getListenersCount(id, enabledOnToolkit);
  1372 
  1398 
  1373         for (int count = listeners, i = 0; count > 0; i++) {
  1399         for (int count = listeners, i = 0; count > 0; i++) {
  1374             count -= component.get(i).createHierarchyEvents(id, changed,
  1400             count -= component.get(i).createHierarchyEvents(id, changed,
  1375                 changedParent, changeFlags, enabledOnToolkit);
  1401                 changedParent, changeFlags, enabledOnToolkit);
  1380     }
  1406     }
  1381 
  1407 
  1382     final void createChildHierarchyEvents(int id, long changeFlags,
  1408     final void createChildHierarchyEvents(int id, long changeFlags,
  1383         boolean enabledOnToolkit)
  1409         boolean enabledOnToolkit)
  1384     {
  1410     {
  1385         assert Thread.holdsLock(getTreeLock());
  1411         checkTreeLock();
  1386         if (component.isEmpty()) {
  1412         if (component.isEmpty()) {
  1387             return;
  1413             return;
  1388         }
  1414         }
  1389         int listeners = getListenersCount(id, enabledOnToolkit);
  1415         int listeners = getListenersCount(id, enabledOnToolkit);
  1390 
  1416 
  1515      *
  1541      *
  1516      * @see #doLayout
  1542      * @see #doLayout
  1517      * @see #validate
  1543      * @see #validate
  1518      */
  1544      */
  1519     protected void validateTree() {
  1545     protected void validateTree() {
       
  1546         checkTreeLock();
  1520         if (!isValid()) {
  1547         if (!isValid()) {
  1521             if (peer instanceof ContainerPeer) {
  1548             if (peer instanceof ContainerPeer) {
  1522                 ((ContainerPeer)peer).beginLayout();
  1549                 ((ContainerPeer)peer).beginLayout();
  1523             }
  1550             }
  1524             doLayout();
  1551             doLayout();
  1791             // Paint self and forward the paint to lightweight subcomponents.
  1818             // Paint self and forward the paint to lightweight subcomponents.
  1792 
  1819 
  1793             // super.paint(); -- Don't bother, since it's a NOP.
  1820             // super.paint(); -- Don't bother, since it's a NOP.
  1794 
  1821 
  1795             GraphicsCallback.PaintCallback.getInstance().
  1822             GraphicsCallback.PaintCallback.getInstance().
  1796                 runComponents(component.toArray(EMPTY_ARRAY), g, GraphicsCallback.LIGHTWEIGHTS);
  1823                 runComponents(getComponentsSync(), g, GraphicsCallback.LIGHTWEIGHTS);
  1797         }
  1824         }
  1798     }
  1825     }
  1799 
  1826 
  1800     /**
  1827     /**
  1801      * Updates the container.  This forwards the update to any lightweight
  1828      * Updates the container.  This forwards the update to any lightweight
  1846                     printing = !printingThreads.isEmpty();
  1873                     printing = !printingThreads.isEmpty();
  1847                 }
  1874                 }
  1848             }
  1875             }
  1849 
  1876 
  1850             GraphicsCallback.PrintCallback.getInstance().
  1877             GraphicsCallback.PrintCallback.getInstance().
  1851                 runComponents(component.toArray(EMPTY_ARRAY), g, GraphicsCallback.LIGHTWEIGHTS);
  1878                 runComponents(getComponentsSync(), g, GraphicsCallback.LIGHTWEIGHTS);
  1852         }
  1879         }
  1853     }
  1880     }
  1854 
  1881 
  1855     /**
  1882     /**
  1856      * Paints each of the components in this container.
  1883      * Paints each of the components in this container.
  1859      * @see       Component#paintAll
  1886      * @see       Component#paintAll
  1860      */
  1887      */
  1861     public void paintComponents(Graphics g) {
  1888     public void paintComponents(Graphics g) {
  1862         if (isShowing()) {
  1889         if (isShowing()) {
  1863             GraphicsCallback.PaintAllCallback.getInstance().
  1890             GraphicsCallback.PaintAllCallback.getInstance().
  1864                 runComponents(component.toArray(EMPTY_ARRAY), g, GraphicsCallback.TWO_PASSES);
  1891                 runComponents(getComponentsSync(), g, GraphicsCallback.TWO_PASSES);
  1865         }
  1892         }
  1866     }
  1893     }
  1867 
  1894 
  1868     /**
  1895     /**
  1869      * Simulates the peer callbacks into java.awt for printing of
  1896      * Simulates the peer callbacks into java.awt for printing of
  1881      * Prints all the heavyweight subcomponents.
  1908      * Prints all the heavyweight subcomponents.
  1882      */
  1909      */
  1883     void paintHeavyweightComponents(Graphics g) {
  1910     void paintHeavyweightComponents(Graphics g) {
  1884         if (isShowing()) {
  1911         if (isShowing()) {
  1885             GraphicsCallback.PaintHeavyweightComponentsCallback.getInstance().
  1912             GraphicsCallback.PaintHeavyweightComponentsCallback.getInstance().
  1886                 runComponents(component.toArray(EMPTY_ARRAY), g, GraphicsCallback.LIGHTWEIGHTS |
  1913                 runComponents(getComponentsSync(), g,
  1887                                             GraphicsCallback.HEAVYWEIGHTS);
  1914                               GraphicsCallback.LIGHTWEIGHTS | GraphicsCallback.HEAVYWEIGHTS);
  1888         }
  1915         }
  1889     }
  1916     }
  1890 
  1917 
  1891     /**
  1918     /**
  1892      * Prints each of the components in this container.
  1919      * Prints each of the components in this container.
  1895      * @see       Component#printAll
  1922      * @see       Component#printAll
  1896      */
  1923      */
  1897     public void printComponents(Graphics g) {
  1924     public void printComponents(Graphics g) {
  1898         if (isShowing()) {
  1925         if (isShowing()) {
  1899             GraphicsCallback.PrintAllCallback.getInstance().
  1926             GraphicsCallback.PrintAllCallback.getInstance().
  1900                 runComponents(component.toArray(EMPTY_ARRAY), g, GraphicsCallback.TWO_PASSES);
  1927                 runComponents(getComponentsSync(), g, GraphicsCallback.TWO_PASSES);
  1901         }
  1928         }
  1902     }
  1929     }
  1903 
  1930 
  1904     /**
  1931     /**
  1905      * Simulates the peer callbacks into java.awt for printing of
  1932      * Simulates the peer callbacks into java.awt for printing of
  1917      * Prints all the heavyweight subcomponents.
  1944      * Prints all the heavyweight subcomponents.
  1918      */
  1945      */
  1919     void printHeavyweightComponents(Graphics g) {
  1946     void printHeavyweightComponents(Graphics g) {
  1920         if (isShowing()) {
  1947         if (isShowing()) {
  1921             GraphicsCallback.PrintHeavyweightComponentsCallback.getInstance().
  1948             GraphicsCallback.PrintHeavyweightComponentsCallback.getInstance().
  1922                 runComponents(component.toArray(EMPTY_ARRAY), g, GraphicsCallback.LIGHTWEIGHTS |
  1949                 runComponents(getComponentsSync(), g,
  1923                                             GraphicsCallback.HEAVYWEIGHTS);
  1950                               GraphicsCallback.LIGHTWEIGHTS | GraphicsCallback.HEAVYWEIGHTS);
  1924         }
  1951         }
  1925     }
  1952     }
  1926 
  1953 
  1927     /**
  1954     /**
  1928      * Adds the specified container listener to receive container events
  1955      * Adds the specified container listener to receive container events
  2468      * @see Component#contains
  2495      * @see Component#contains
  2469      * @see #getComponentAt
  2496      * @see #getComponentAt
  2470      * @since 1.2
  2497      * @since 1.2
  2471      */
  2498      */
  2472     public Component findComponentAt(int x, int y) {
  2499     public Component findComponentAt(int x, int y) {
  2473         synchronized (getTreeLock()) {
  2500         return findComponentAt(x, y, true);
  2474             return findComponentAt(x, y, true);
       
  2475         }
       
  2476     }
  2501     }
  2477 
  2502 
  2478     /**
  2503     /**
  2479      * Private version of findComponentAt which has a controllable
  2504      * Private version of findComponentAt which has a controllable
  2480      * behavior. Setting 'ignoreEnabled' to 'false' bypasses disabled
  2505      * behavior. Setting 'ignoreEnabled' to 'false' bypasses disabled
  2483      * The cursor code calls this function directly via native code.
  2508      * The cursor code calls this function directly via native code.
  2484      *
  2509      *
  2485      * The addition of this feature is temporary, pending the
  2510      * The addition of this feature is temporary, pending the
  2486      * adoption of new, public API which exports this feature.
  2511      * adoption of new, public API which exports this feature.
  2487      */
  2512      */
  2488     final Component findComponentAt(int x, int y, boolean ignoreEnabled)
  2513     final Component findComponentAt(int x, int y, boolean ignoreEnabled) {
  2489     {
  2514         synchronized (getTreeLock()) {
  2490         if (isRecursivelyVisible()){
  2515             if (isRecursivelyVisible()){
  2491             return findComponentAtImpl(x, y, ignoreEnabled);
  2516                 return findComponentAtImpl(x, y, ignoreEnabled);
       
  2517             }
  2492         }
  2518         }
  2493         return null;
  2519         return null;
  2494     }
  2520     }
  2495 
  2521 
  2496     final Component findComponentAtImpl(int x, int y, boolean ignoreEnabled){
  2522     final Component findComponentAtImpl(int x, int y, boolean ignoreEnabled){
       
  2523         checkTreeLock();
       
  2524 
  2497         if (!(contains(x, y) && visible && (ignoreEnabled || enabled))) {
  2525         if (!(contains(x, y) && visible && (ignoreEnabled || enabled))) {
  2498             return null;
  2526             return null;
  2499         }
  2527         }
  2500 
  2528 
  2501         // Two passes: see comment in sun.awt.SunGraphicsCallback
  2529         // Two passes: see comment in sun.awt.SunGraphicsCallback
  2502         synchronized (getTreeLock()) {
  2530         for (int i = 0; i < component.size(); i++) {
  2503             for (int i = 0; i < component.size(); i++) {
  2531             Component comp = component.get(i);
  2504                 Component comp = component.get(i);
  2532             if (comp != null &&
  2505                 if (comp != null &&
  2533                 !(comp.peer instanceof LightweightPeer)) {
  2506                     !(comp.peer instanceof LightweightPeer)) {
  2534                 if (comp instanceof Container) {
  2507                     if (comp instanceof Container) {
  2535                     comp = ((Container)comp).findComponentAtImpl(x - comp.x,
  2508                         comp = ((Container)comp).findComponentAtImpl(x - comp.x,
  2536                                                                  y - comp.y,
  2509                                                                      y - comp.y,
  2537                                                                  ignoreEnabled);
  2510                                                                      ignoreEnabled);
  2538                 } else {
  2511                     } else {
  2539                     comp = comp.locate(x - comp.x, y - comp.y);
  2512                         comp = comp.locate(x - comp.x, y - comp.y);
  2540                 }
  2513                     }
  2541                 if (comp != null && comp.visible &&
  2514                     if (comp != null && comp.visible &&
  2542                     (ignoreEnabled || comp.enabled))
  2515                         (ignoreEnabled || comp.enabled))
  2543                 {
  2516                         {
  2544                     return comp;
  2517                             return comp;
  2545                 }
  2518                         }
  2546             }
  2519                 }
  2547         }
  2520             }
  2548         for (int i = 0; i < component.size(); i++) {
  2521             for (int i = 0; i < component.size(); i++) {
  2549             Component comp = component.get(i);
  2522                 Component comp = component.get(i);
  2550             if (comp != null &&
  2523                 if (comp != null &&
  2551                 comp.peer instanceof LightweightPeer) {
  2524                     comp.peer instanceof LightweightPeer) {
  2552                 if (comp instanceof Container) {
  2525                     if (comp instanceof Container) {
  2553                     comp = ((Container)comp).findComponentAtImpl(x - comp.x,
  2526                         comp = ((Container)comp).findComponentAtImpl(x - comp.x,
  2554                                                                  y - comp.y,
  2527                                                                      y - comp.y,
  2555                                                                  ignoreEnabled);
  2528                                                                      ignoreEnabled);
  2556                 } else {
  2529                     } else {
  2557                     comp = comp.locate(x - comp.x, y - comp.y);
  2530                         comp = comp.locate(x - comp.x, y - comp.y);
  2558                 }
  2531                     }
  2559                 if (comp != null && comp.visible &&
  2532                     if (comp != null && comp.visible &&
  2560                     (ignoreEnabled || comp.enabled))
  2533                         (ignoreEnabled || comp.enabled))
  2561                 {
  2534                         {
  2562                     return comp;
  2535                             return comp;
  2563                 }
  2536                         }
  2564             }
  2537                 }
  2565         }
  2538             }
  2566 
  2539         }
       
  2540         return this;
  2567         return this;
  2541     }
  2568     }
  2542 
  2569 
  2543     /**
  2570     /**
  2544      * Locates the visible child component that contains the specified
  2571      * Locates the visible child component that contains the specified
  3489      * @see #readObject(ObjectInputStream)
  3516      * @see #readObject(ObjectInputStream)
  3490      */
  3517      */
  3491     private void writeObject(ObjectOutputStream s) throws IOException {
  3518     private void writeObject(ObjectOutputStream s) throws IOException {
  3492         ObjectOutputStream.PutField f = s.putFields();
  3519         ObjectOutputStream.PutField f = s.putFields();
  3493         f.put("ncomponents", component.size());
  3520         f.put("ncomponents", component.size());
  3494         f.put("component", component.toArray(EMPTY_ARRAY));
  3521         f.put("component", getComponentsSync());
  3495         f.put("layoutMgr", layoutMgr);
  3522         f.put("layoutMgr", layoutMgr);
  3496         f.put("dispatcher", dispatcher);
  3523         f.put("dispatcher", dispatcher);
  3497         f.put("maxSize", maxSize);
  3524         f.put("maxSize", maxSize);
  3498         f.put("focusCycleRoot", focusCycleRoot);
  3525         f.put("focusCycleRoot", focusCycleRoot);
  3499         f.put("containerSerializedDataVersion", containerSerializedDataVersion);
  3526         f.put("containerSerializedDataVersion", containerSerializedDataVersion);