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