1 /*
   2  * Copyright (c) 1997, 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  */
  25 package javax.swing;
  26 
  27 import java.awt.Component;
  28 import java.util.ArrayList;
  29 import java.util.Hashtable;
  30 import java.awt.Color;
  31 import java.awt.Graphics;
  32 import java.awt.Rectangle;
  33 import java.beans.JavaBean;
  34 import java.beans.BeanProperty;
  35 
  36 import sun.awt.SunToolkit;
  37 
  38 import javax.accessibility.*;
  39 
  40 /**
  41  * <code>JLayeredPane</code> adds depth to a JFC/Swing container,
  42  * allowing components to overlap each other when needed.
  43  * An <code>Integer</code> object specifies each component's depth in the
  44  * container, where higher-numbered components sit &quot;on top&quot; of other
  45  * components.
  46  * For task-oriented documentation and examples of using layered panes see
  47  * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html">How to Use a Layered Pane</a>,
  48  * a section in <em>The Java Tutorial</em>.
  49  *
  50  * <TABLE STYLE="FLOAT:RIGHT" BORDER="0" SUMMARY="layout">
  51  * <TR>
  52  *   <TD style="text-align:center">
  53  *     <P STYLE="TEXT-ALIGN:CENTER"><IMG SRC="doc-files/JLayeredPane-1.gif"
  54  *     alt="The following text describes this image."
  55  *     WIDTH="269" HEIGHT="264" STYLE="FLOAT:BOTTOM; BORDER=0">
  56  *   </TD>
  57  * </TR>
  58  * </TABLE>
  59  * For convenience, <code>JLayeredPane</code> divides the depth-range
  60  * into several different layers. Putting a component into one of those
  61  * layers makes it easy to ensure that components overlap properly,
  62  * without having to worry about specifying numbers for specific depths:
  63  * <DL>
  64  *    <DT>DEFAULT_LAYER</DT>
  65  *         <DD>The standard layer, where most components go. This the bottommost
  66  *         layer.
  67  *    <DT>PALETTE_LAYER</DT>
  68  *         <DD>The palette layer sits over the default layer. Useful for floating
  69  *         toolbars and palettes, so they can be positioned above other components.
  70  *    <DT>MODAL_LAYER</DT>
  71  *         <DD>The layer used for modal dialogs. They will appear on top of any
  72  *         toolbars, palettes, or standard components in the container.
  73  *    <DT>POPUP_LAYER</DT>
  74  *         <DD>The popup layer displays above dialogs. That way, the popup windows
  75  *         associated with combo boxes, tooltips, and other help text will appear
  76  *         above the component, palette, or dialog that generated them.
  77  *    <DT>DRAG_LAYER</DT>
  78  *         <DD>When dragging a component, reassigning it to the drag layer ensures
  79  *         that it is positioned over every other component in the container. When
  80  *         finished dragging, it can be reassigned to its normal layer.
  81  * </DL>
  82  * The <code>JLayeredPane</code> methods <code>moveToFront(Component)</code>,
  83  * <code>moveToBack(Component)</code> and <code>setPosition</code> can be used
  84  * to reposition a component within its layer. The <code>setLayer</code> method
  85  * can also be used to change the component's current layer.
  86  *
  87  * <h2>Details</h2>
  88  * <code>JLayeredPane</code> manages its list of children like
  89  * <code>Container</code>, but allows for the definition of a several
  90  * layers within itself. Children in the same layer are managed exactly
  91  * like the normal <code>Container</code> object,
  92  * with the added feature that when children components overlap, children
  93  * in higher layers display above the children in lower layers.
  94  * <p>
  95  * Each layer is a distinct integer number. The layer attribute can be set
  96  * on a <code>Component</code> by passing an <code>Integer</code>
  97  * object during the add call.<br> For example:
  98  * <PRE>
  99  *     layeredPane.add(child, JLayeredPane.DEFAULT_LAYER);
 100  * or
 101  *     layeredPane.add(child, Integer.valueOf.valueOf(10));
 102  * </PRE>
 103  * The layer attribute can also be set on a Component by calling<PRE>
 104  *     layeredPaneParent.setLayer(child, 10)</PRE>
 105  * on the <code>JLayeredPane</code> that is the parent of component. The layer
 106  * should be set <i>before</i> adding the child to the parent.
 107  * <p>
 108  * Higher number layers display above lower number layers. So, using
 109  * numbers for the layers and letters for individual components, a
 110  * representative list order would look like this:<PRE>
 111  *       5a, 5b, 5c, 2a, 2b, 2c, 1a </PRE>
 112  * where the leftmost components are closest to the top of the display.
 113  * <p>
 114  * A component can be moved to the top or bottom position within its
 115  * layer by calling <code>moveToFront</code> or <code>moveToBack</code>.
 116  * <p>
 117  * The position of a component within a layer can also be specified directly.
 118  * Valid positions range from 0 up to one less than the number of
 119  * components in that layer. A value of -1 indicates the bottommost
 120  * position. A value of 0 indicates the topmost position. Unlike layer
 121  * numbers, higher position values are <i>lower</i> in the display.
 122  * <blockquote>
 123  * <b>Note:</b> This sequence (defined by java.awt.Container) is the reverse
 124  * of the layer numbering sequence. Usually though, you will use <code>moveToFront</code>,
 125  * <code>moveToBack</code>, and <code>setLayer</code>.
 126  * </blockquote>
 127  * Here are some examples using the method add(Component, layer, position):
 128  * Calling add(5x, 5, -1) results in:<PRE>
 129  *       5a, 5b, 5c, 5x, 2a, 2b, 2c, 1a </PRE>
 130  *
 131  * Calling add(5z, 5, 2) results in:<PRE>
 132  *       5a, 5b, 5z, 5c, 5x, 2a, 2b, 2c, 1a </PRE>
 133  *
 134  * Calling add(3a, 3, 7) results in:<PRE>
 135  *       5a, 5b, 5z, 5c, 5x, 3a, 2a, 2b, 2c, 1a </PRE>
 136  *
 137  * Using normal paint/event mechanics results in 1a appearing at the bottom
 138  * and 5a being above all other components.
 139  * <p>
 140  * <b>Note:</b> that these layers are simply a logical construct and LayoutManagers
 141  * will affect all child components of this container without regard for
 142  * layer settings.
 143  * <p>
 144  * <strong>Warning:</strong> Swing is not thread safe. For more
 145  * information see <a
 146  * href="package-summary.html#threading">Swing's Threading
 147  * Policy</a>.
 148  * <p>
 149  * <strong>Warning:</strong>
 150  * Serialized objects of this class will not be compatible with
 151  * future Swing releases. The current serialization support is
 152  * appropriate for short term storage or RMI between applications running
 153  * the same version of Swing.  As of 1.4, support for long term storage
 154  * of all JavaBeans&trade;
 155  * has been added to the <code>java.beans</code> package.
 156  * Please see {@link java.beans.XMLEncoder}.
 157  *
 158  * @author David Kloba
 159  * @since 1.2
 160  */
 161 @JavaBean(defaultProperty = "accessibleContext")
 162 @SuppressWarnings("serial")
 163 public class JLayeredPane extends JComponent implements Accessible {
 164     /// Watch the values in getObjectForLayer()
 165     /** Convenience object defining the Default layer. Equivalent to Integer.valueOf(0).*/
 166     public static final Integer DEFAULT_LAYER = 0;
 167     /** Convenience object defining the Palette layer. Equivalent to Integer.valueOf(100).*/
 168     public static final Integer PALETTE_LAYER = 100;
 169     /** Convenience object defining the Modal layer. Equivalent to Integer.valueOf(200).*/
 170     public static final Integer MODAL_LAYER = 200;
 171     /** Convenience object defining the Popup layer. Equivalent to Integer.valueOf(300).*/
 172     public static final Integer POPUP_LAYER = 300;
 173     /** Convenience object defining the Drag layer. Equivalent to Integer.valueOf(400).*/
 174     public static final Integer DRAG_LAYER = 400;
 175     /** Convenience object defining the Frame Content layer.
 176       * This layer is normally only use to position the contentPane and menuBar
 177       * components of JFrame.
 178       * Equivalent to Integer.valueOf(-30000).
 179       * @see JFrame
 180       */
 181     public static final Integer FRAME_CONTENT_LAYER = -30000;
 182 
 183     /** Bound property */
 184     public static final String LAYER_PROPERTY = "layeredContainerLayer";
 185     // Hashtable to store layer values for non-JComponent components
 186     private Hashtable<Component,Integer> componentToLayer;
 187     private boolean optimizedDrawingPossible = true;
 188 
 189 
 190 //////////////////////////////////////////////////////////////////////////////
 191 //// Container Override methods
 192 //////////////////////////////////////////////////////////////////////////////
 193     /** Create a new JLayeredPane */
 194     public JLayeredPane() {
 195         setLayout(null);
 196     }
 197 
 198     private void validateOptimizedDrawing() {
 199         boolean layeredComponentFound = false;
 200         synchronized(getTreeLock()) {
 201             Integer layer;
 202 
 203             for (Component c : getComponents()) {
 204                 layer = null;
 205 
 206                 if(SunToolkit.isInstanceOf(c, "javax.swing.JInternalFrame") ||
 207                        (c instanceof JComponent &&
 208                         (layer = (Integer)((JComponent)c).
 209                                      getClientProperty(LAYER_PROPERTY)) != null))
 210                 {
 211                     if(layer != null && layer.equals(FRAME_CONTENT_LAYER))
 212                         continue;
 213                     layeredComponentFound = true;
 214                     break;
 215                 }
 216             }
 217         }
 218 
 219         if(layeredComponentFound)
 220             optimizedDrawingPossible = false;
 221         else
 222             optimizedDrawingPossible = true;
 223     }
 224 
 225     protected void addImpl(Component comp, Object constraints, int index) {
 226         int layer;
 227         int pos;
 228 
 229         if(constraints instanceof Integer) {
 230             layer = ((Integer)constraints).intValue();
 231             setLayer(comp, layer);
 232         } else
 233             layer = getLayer(comp);
 234 
 235         pos = insertIndexForLayer(layer, index);
 236         super.addImpl(comp, constraints, pos);
 237         comp.validate();
 238         comp.repaint();
 239         validateOptimizedDrawing();
 240     }
 241 
 242     /**
 243      * Remove the indexed component from this pane.
 244      * This is the absolute index, ignoring layers.
 245      *
 246      * @param index  an int specifying the component to remove
 247      * @see #getIndexOf
 248      */
 249     public void remove(int index) {
 250         Component c = getComponent(index);
 251         super.remove(index);
 252         if (c != null && !(c instanceof JComponent)) {
 253             getComponentToLayer().remove(c);
 254         }
 255         validateOptimizedDrawing();
 256     }
 257 
 258     /**
 259      * Removes all the components from this container.
 260      *
 261      * @since 1.5
 262      */
 263     public void removeAll() {
 264         Component[] children = getComponents();
 265         Hashtable<Component, Integer> cToL = getComponentToLayer();
 266         for (int counter = children.length - 1; counter >= 0; counter--) {
 267             Component c = children[counter];
 268             if (c != null && !(c instanceof JComponent)) {
 269                 cToL.remove(c);
 270             }
 271         }
 272         super.removeAll();
 273     }
 274 
 275     /**
 276      * Returns false if components in the pane can overlap, which makes
 277      * optimized drawing impossible. Otherwise, returns true.
 278      *
 279      * @return false if components can overlap, else true
 280      * @see JComponent#isOptimizedDrawingEnabled
 281      */
 282     @BeanProperty(bound = false)
 283     public boolean isOptimizedDrawingEnabled() {
 284         return optimizedDrawingPossible;
 285     }
 286 
 287 
 288 //////////////////////////////////////////////////////////////////////////////
 289 //// New methods for managing layers
 290 //////////////////////////////////////////////////////////////////////////////
 291     /** Sets the layer property on a JComponent. This method does not cause
 292       * any side effects like setLayer() (painting, add/remove, etc).
 293       * Normally you should use the instance method setLayer(), in order to
 294       * get the desired side-effects (like repainting).
 295       *
 296       * @param c      the JComponent to move
 297       * @param layer  an int specifying the layer to move it to
 298       * @see #setLayer
 299       */
 300     public static void putLayer(JComponent c, int layer) {
 301         /// MAKE SURE THIS AND setLayer(Component c, int layer, int position)  are SYNCED
 302         c.putClientProperty(LAYER_PROPERTY, layer);
 303     }
 304 
 305     /** Gets the layer property for a JComponent, it
 306       * does not cause any side effects like setLayer(). (painting, add/remove, etc)
 307       * Normally you should use the instance method getLayer().
 308       *
 309       * @param c  the JComponent to check
 310       * @return   an int specifying the component's layer
 311       */
 312     public static int getLayer(JComponent c) {
 313         Integer i;
 314         if((i = (Integer)c.getClientProperty(LAYER_PROPERTY)) != null)
 315             return i.intValue();
 316         return DEFAULT_LAYER.intValue();
 317     }
 318 
 319     /** Convenience method that returns the first JLayeredPane which
 320       * contains the specified component. Note that all JFrames have a
 321       * JLayeredPane at their root, so any component in a JFrame will
 322       * have a JLayeredPane parent.
 323       *
 324       * @param c the Component to check
 325       * @return the JLayeredPane that contains the component, or
 326       *         null if no JLayeredPane is found in the component
 327       *         hierarchy
 328       * @see JFrame
 329       * @see JRootPane
 330       */
 331     public static JLayeredPane getLayeredPaneAbove(Component c) {
 332         if(c == null) return null;
 333 
 334         Component parent = c.getParent();
 335         while(parent != null && !(parent instanceof JLayeredPane))
 336             parent = parent.getParent();
 337         return (JLayeredPane)parent;
 338     }
 339 
 340     /** Sets the layer attribute on the specified component,
 341       * making it the bottommost component in that layer.
 342       * Should be called before adding to parent.
 343       *
 344       * @param c     the Component to set the layer for
 345       * @param layer an int specifying the layer to set, where
 346       *              lower numbers are closer to the bottom
 347       */
 348     public void setLayer(Component c, int layer)  {
 349         setLayer(c, layer, -1);
 350     }
 351 
 352     /** Sets the layer attribute for the specified component and
 353       * also sets its position within that layer.
 354       *
 355       * @param c         the Component to set the layer for
 356       * @param layer     an int specifying the layer to set, where
 357       *                  lower numbers are closer to the bottom
 358       * @param position  an int specifying the position within the
 359       *                  layer, where 0 is the topmost position and -1
 360       *                  is the bottommost position
 361       */
 362     public void setLayer(Component c, int layer, int position)  {
 363         Integer layerObj;
 364         layerObj = getObjectForLayer(layer);
 365 
 366         if(layer == getLayer(c) && position == getPosition(c)) {
 367                 repaint(c.getBounds());
 368             return;
 369         }
 370 
 371         /// MAKE SURE THIS AND putLayer(JComponent c, int layer) are SYNCED
 372         if(c instanceof JComponent)
 373             ((JComponent)c).putClientProperty(LAYER_PROPERTY, layerObj);
 374         else
 375             getComponentToLayer().put(c, layerObj);
 376 
 377         if(c.getParent() == null || c.getParent() != this) {
 378             repaint(c.getBounds());
 379             return;
 380         }
 381 
 382         int index = insertIndexForLayer(c, layer, position);
 383 
 384         setComponentZOrder(c, index);
 385         repaint(c.getBounds());
 386     }
 387 
 388     /**
 389      * Returns the layer attribute for the specified Component.
 390      *
 391      * @param c  the Component to check
 392      * @return an int specifying the component's current layer
 393      */
 394     public int getLayer(Component c) {
 395         Integer i;
 396         if(c instanceof JComponent)
 397             i = (Integer)((JComponent)c).getClientProperty(LAYER_PROPERTY);
 398         else
 399             i = getComponentToLayer().get(c);
 400 
 401         if(i == null)
 402             return DEFAULT_LAYER.intValue();
 403         return i.intValue();
 404     }
 405 
 406     /**
 407      * Returns the index of the specified Component.
 408      * This is the absolute index, ignoring layers.
 409      * Index numbers, like position numbers, have the topmost component
 410      * at index zero. Larger numbers are closer to the bottom.
 411      *
 412      * @param c  the Component to check
 413      * @return an int specifying the component's index
 414      */
 415     public int getIndexOf(Component c) {
 416         int i, count;
 417 
 418         count = getComponentCount();
 419         for(i = 0; i < count; i++) {
 420             if(c == getComponent(i))
 421                 return i;
 422         }
 423         return -1;
 424     }
 425     /**
 426      * Moves the component to the top of the components in its current layer
 427      * (position 0).
 428      *
 429      * @param c the Component to move
 430      * @see #setPosition(Component, int)
 431      */
 432     public void moveToFront(Component c) {
 433         setPosition(c, 0);
 434     }
 435 
 436     /**
 437      * Moves the component to the bottom of the components in its current layer
 438      * (position -1).
 439      *
 440      * @param c the Component to move
 441      * @see #setPosition(Component, int)
 442      */
 443     public void moveToBack(Component c) {
 444         setPosition(c, -1);
 445     }
 446 
 447     /**
 448      * Moves the component to <code>position</code> within its current layer,
 449      * where 0 is the topmost position within the layer and -1 is the bottommost
 450      * position.
 451      * <p>
 452      * <b>Note:</b> Position numbering is defined by java.awt.Container, and
 453      * is the opposite of layer numbering. Lower position numbers are closer
 454      * to the top (0 is topmost), and higher position numbers are closer to
 455      * the bottom.
 456      *
 457      * @param c         the Component to move
 458      * @param position  an int in the range -1..N-1, where N is the number of
 459      *                  components in the component's current layer
 460      */
 461     public void setPosition(Component c, int position) {
 462         setLayer(c, getLayer(c), position);
 463     }
 464 
 465     /**
 466      * Get the relative position of the component within its layer.
 467      *
 468      * @param c  the Component to check
 469      * @return an int giving the component's position, where 0 is the
 470      *         topmost position and the highest index value = the count
 471      *         count of components at that layer, minus 1
 472      *
 473      * @see #getComponentCountInLayer
 474      */
 475     public int getPosition(Component c) {
 476         int i, startLayer, curLayer, startLocation, pos = 0;
 477 
 478         getComponentCount();
 479         startLocation = getIndexOf(c);
 480 
 481         if(startLocation == -1)
 482             return -1;
 483 
 484         startLayer = getLayer(c);
 485         for(i = startLocation - 1; i >= 0; i--) {
 486             curLayer = getLayer(getComponent(i));
 487             if(curLayer == startLayer)
 488                 pos++;
 489             else
 490                 return pos;
 491         }
 492         return pos;
 493     }
 494 
 495     /** Returns the highest layer value from all current children.
 496       * Returns 0 if there are no children.
 497       *
 498       * @return an int indicating the layer of the topmost component in the
 499       *         pane, or zero if there are no children
 500       */
 501     public int highestLayer() {
 502         if(getComponentCount() > 0)
 503             return getLayer(getComponent(0));
 504         return 0;
 505     }
 506 
 507     /** Returns the lowest layer value from all current children.
 508       * Returns 0 if there are no children.
 509       *
 510       * @return an int indicating the layer of the bottommost component in the
 511       *         pane, or zero if there are no children
 512       */
 513     public int lowestLayer() {
 514         int count = getComponentCount();
 515         if(count > 0)
 516             return getLayer(getComponent(count-1));
 517         return 0;
 518     }
 519 
 520     /**
 521      * Returns the number of children currently in the specified layer.
 522      *
 523      * @param layer  an int specifying the layer to check
 524      * @return an int specifying the number of components in that layer
 525      */
 526     public int getComponentCountInLayer(int layer) {
 527         int i, count, curLayer;
 528         int layerCount = 0;
 529 
 530         count = getComponentCount();
 531         for(i = 0; i < count; i++) {
 532             curLayer = getLayer(getComponent(i));
 533             if(curLayer == layer) {
 534                 layerCount++;
 535             /// Short circut the counting when we have them all
 536             } else if(layerCount > 0 || curLayer < layer) {
 537                 break;
 538             }
 539         }
 540 
 541         return layerCount;
 542     }
 543 
 544     /**
 545      * Returns an array of the components in the specified layer.
 546      *
 547      * @param layer  an int specifying the layer to check
 548      * @return an array of Components contained in that layer
 549      */
 550     public Component[] getComponentsInLayer(int layer) {
 551         int i, count, curLayer;
 552         int layerCount = 0;
 553         Component[] results;
 554 
 555         results = new Component[getComponentCountInLayer(layer)];
 556         count = getComponentCount();
 557         for(i = 0; i < count; i++) {
 558             curLayer = getLayer(getComponent(i));
 559             if(curLayer == layer) {
 560                 results[layerCount++] = getComponent(i);
 561             /// Short circut the counting when we have them all
 562             } else if(layerCount > 0 || curLayer < layer) {
 563                 break;
 564             }
 565         }
 566 
 567         return results;
 568     }
 569 
 570     /**
 571      * Paints this JLayeredPane within the specified graphics context.
 572      *
 573      * @param g  the Graphics context within which to paint
 574      */
 575     public void paint(Graphics g) {
 576         if(isOpaque()) {
 577             Rectangle r = g.getClipBounds();
 578             Color c = getBackground();
 579             if(c == null)
 580                 c = Color.lightGray;
 581             g.setColor(c);
 582             if (r != null) {
 583                 g.fillRect(r.x, r.y, r.width, r.height);
 584             }
 585             else {
 586                 g.fillRect(0, 0, getWidth(), getHeight());
 587             }
 588         }
 589         super.paint(g);
 590     }
 591 
 592 //////////////////////////////////////////////////////////////////////////////
 593 //// Implementation Details
 594 //////////////////////////////////////////////////////////////////////////////
 595 
 596     /**
 597      * Returns the hashtable that maps components to layers.
 598      *
 599      * @return the Hashtable used to map components to their layers
 600      */
 601     protected Hashtable<Component,Integer> getComponentToLayer() {
 602         if(componentToLayer == null)
 603             componentToLayer = new Hashtable<Component,Integer>(4);
 604         return componentToLayer;
 605     }
 606 
 607     /**
 608      * Returns the Integer object associated with a specified layer.
 609      *
 610      * @param layer an int specifying the layer
 611      * @return an Integer object for that layer
 612      */
 613     protected Integer getObjectForLayer(int layer) {
 614         switch(layer) {
 615         case 0:
 616             return DEFAULT_LAYER;
 617         case 100:
 618             return PALETTE_LAYER;
 619         case 200:
 620             return MODAL_LAYER;
 621         case 300:
 622             return POPUP_LAYER;
 623         case 400:
 624             return DRAG_LAYER;
 625         default:
 626             return layer;
 627         }
 628     }
 629 
 630     /**
 631      * Primitive method that determines the proper location to
 632      * insert a new child based on layer and position requests.
 633      *
 634      * @param layer     an int specifying the layer
 635      * @param position  an int specifying the position within the layer
 636      * @return an int giving the (absolute) insertion-index
 637      *
 638      * @see #getIndexOf
 639      */
 640     protected int insertIndexForLayer(int layer, int position) {
 641         return insertIndexForLayer(null, layer, position);
 642     }
 643 
 644     /**
 645      * This method is an extended version of insertIndexForLayer()
 646      * to support setLayer which uses Container.setZOrder which does
 647      * not remove the component from the containment hierarchy though
 648      * we need to ignore it when calculating the insertion index.
 649      *
 650      * @param comp      component to ignore when determining index
 651      * @param layer     an int specifying the layer
 652      * @param position  an int specifying the position within the layer
 653      * @return an int giving the (absolute) insertion-index
 654      *
 655      * @see #getIndexOf
 656      */
 657     private int insertIndexForLayer(Component comp, int layer, int position) {
 658         int i, count, curLayer;
 659         int layerStart = -1;
 660         int layerEnd = -1;
 661         int componentCount = getComponentCount();
 662 
 663         ArrayList<Component> compList =
 664             new ArrayList<Component>(componentCount);
 665         for (int index = 0; index < componentCount; index++) {
 666             if (getComponent(index) != comp) {
 667                 compList.add(getComponent(index));
 668             }
 669         }
 670 
 671         count = compList.size();
 672         for (i = 0; i < count; i++) {
 673             curLayer = getLayer(compList.get(i));
 674             if (layerStart == -1 && curLayer == layer) {
 675                 layerStart = i;
 676             }
 677             if (curLayer < layer) {
 678                 if (i == 0) {
 679                     // layer is greater than any current layer
 680                     // [ ASSERT(layer > highestLayer()) ]
 681                     layerStart = 0;
 682                     layerEnd = 0;
 683                 } else {
 684                     layerEnd = i;
 685                 }
 686                 break;
 687             }
 688         }
 689 
 690         // layer requested is lower than any current layer
 691         // [ ASSERT(layer < lowestLayer()) ]
 692         // put it on the bottom of the stack
 693         if (layerStart == -1 && layerEnd == -1)
 694             return count;
 695 
 696         // In the case of a single layer entry handle the degenerative cases
 697         if (layerStart != -1 && layerEnd == -1)
 698             layerEnd = count;
 699 
 700         if (layerEnd != -1 && layerStart == -1)
 701             layerStart = layerEnd;
 702 
 703         // If we are adding to the bottom, return the last element
 704         if (position == -1)
 705             return layerEnd;
 706 
 707         // Otherwise make sure the requested position falls in the
 708         // proper range
 709         if (position > -1 && layerStart + position <= layerEnd)
 710             return layerStart + position;
 711 
 712         // Otherwise return the end of the layer
 713         return layerEnd;
 714     }
 715 
 716     /**
 717      * Returns a string representation of this JLayeredPane. This method
 718      * is intended to be used only for debugging purposes, and the
 719      * content and format of the returned string may vary between
 720      * implementations. The returned string may be empty but may not
 721      * be <code>null</code>.
 722      *
 723      * @return  a string representation of this JLayeredPane.
 724      */
 725     protected String paramString() {
 726         String optimizedDrawingPossibleString = (optimizedDrawingPossible ?
 727                                                  "true" : "false");
 728 
 729         return super.paramString() +
 730         ",optimizedDrawingPossible=" + optimizedDrawingPossibleString;
 731     }
 732 
 733 /////////////////
 734 // Accessibility support
 735 ////////////////
 736 
 737     /**
 738      * Gets the AccessibleContext associated with this JLayeredPane.
 739      * For layered panes, the AccessibleContext takes the form of an
 740      * AccessibleJLayeredPane.
 741      * A new AccessibleJLayeredPane instance is created if necessary.
 742      *
 743      * @return an AccessibleJLayeredPane that serves as the
 744      *         AccessibleContext of this JLayeredPane
 745      */
 746     @BeanProperty(bound = false)
 747     public AccessibleContext getAccessibleContext() {
 748         if (accessibleContext == null) {
 749             accessibleContext = new AccessibleJLayeredPane();
 750         }
 751         return accessibleContext;
 752     }
 753 
 754     /**
 755      * This class implements accessibility support for the
 756      * <code>JLayeredPane</code> class.  It provides an implementation of the
 757      * Java Accessibility API appropriate to layered pane user-interface
 758      * elements.
 759      * <p>
 760      * <strong>Warning:</strong>
 761      * Serialized objects of this class will not be compatible with
 762      * future Swing releases. The current serialization support is
 763      * appropriate for short term storage or RMI between applications running
 764      * the same version of Swing.  As of 1.4, support for long term storage
 765      * of all JavaBeans&trade;
 766      * has been added to the <code>java.beans</code> package.
 767      * Please see {@link java.beans.XMLEncoder}.
 768      */
 769     @SuppressWarnings("serial")
 770     protected class AccessibleJLayeredPane extends AccessibleJComponent {
 771 
 772         /**
 773          * Get the role of this object.
 774          *
 775          * @return an instance of AccessibleRole describing the role of the
 776          * object
 777          * @see AccessibleRole
 778          */
 779         public AccessibleRole getAccessibleRole() {
 780             return AccessibleRole.LAYERED_PANE;
 781         }
 782     }
 783 }