jlayeredpane.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 690 行 · 第 1/2 页

JAVA
690
字号
/* JLayeredPane.java --    Copyright (C) 2002, 2004 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package javax.swing;import java.awt.Color;import java.awt.Component;import java.awt.Container;import java.awt.Graphics;import java.awt.Rectangle;import java.util.Hashtable;import javax.accessibility.Accessible;import javax.accessibility.AccessibleContext;import javax.accessibility.AccessibleRole;/** * A container that adds depth to the usual <code>Container</code> semantics. * Each child component of a <code>Layered Pane</code> is placed within one * of several layers. <code>JLayeredPane</code> defines a set of standard * layers. The pre-defined sets are (in the order from button to top): * *  <dl> *    <dt>{@link #DEFAULT_LAYER}</dt> *    <dd>The layer where most of the normal components are placed. This *      is the bottommost layer.</dd> * *    <dt>{@link #PALETTE_LAYER}</dt> *    <dd>Palette windows are placed in this layer.</dd> * *    <dt>{@link #MODAL_LAYER}</dt> *    <dd>The layer where internal modal dialog windows are placed.</dd> * *    <dt>{@link #POPUP_LAYER}</dt> *    <dd>The layer for popup menus</dd> * *    <dt>{@link #DRAG_LAYER}</dt> *    <dd>Components that are beeing dragged are temporarily placed in *       this layer.</dd> *  </dl> * * <p>A child is in exactly one of these layers at any time, though there may * be other layers if someone creates them.</p> * * <p>You can add a component to a specific layer using the * {@link Container#add(Component, Object)} method. I.e. * <code>layeredPane.add(comp, JLayeredPane.MODAL_LAYER)</code> will add the * component <code>comp</code> to the modal layer of <code>layeredPane</code>. * </p> * * <p>To change the layer of a component that is already a child of * a <code>JLayeredPane</code>, use the {@link #setLayer(Component, int)}  * method.</p> * * <p>The purpose of this class is to translate this view of "layers" into a * contiguous array of components: the one held in our ancestor, * {@link java.awt.Container}.</p> * * <p>There is a precise set of words we will use to refer to numbers within * this class:</p> *  * <dl> * <dt>Component Index:</dt>  * <dd>An offset into the <code>component</code> array held in our ancestor, * {@link java.awt.Container}, from <code>[0 .. component.length)</code>. The drawing * rule with indices is that 0 is drawn last.</dd> * * <dt>Layer Number:</dt> * <dd>A general <code>int</code> specifying a layer within this component.  Negative * numbers are drawn first, then layer 0, then positive numbered layers, in * ascending order.</dd> * * <dt>Position:</dt>  * <dd>An offset into a layer's "logical drawing order". Layer position 0 * is drawn last. Layer position -1 is a synonym for the first layer * position (the logical "bottom").</dd> * </dl> * * <p><b>Note:</b> the layer numbering order is the <em>reverse</em> of the * component indexing and position order</p> * * @author Graydon Hoare (graydon@redhat.com) * @author Roman Kennke (kennke@aicas.com) */public class JLayeredPane extends JComponent implements Accessible{    /**   * Provides accessibility support for <code>JLayeredPane</code>.   */  protected class AccessibleJLayeredPane extends AccessibleJComponent  {    /**     * Creates a new instance of <code>AccessibleJLayeredPane</code>.     */    protected AccessibleJLayeredPane()    {      // Nothing to do here.    }    /**     * Returns the accessble role of <code>JLayeredPane</code>,     * {@link AccessibleRole#LAYERED_PANE}.      */    public AccessibleRole getAccessibleRole()    {      return AccessibleRole.LAYERED_PANE;    }  }  private static final long serialVersionUID = 5534920399324590459L;    public static final String LAYER_PROPERTY = "layeredContainerLayer";  public static final Integer FRAME_CONTENT_LAYER = new Integer (-30000);  public static final Integer DEFAULT_LAYER = new Integer (0);  public static final Integer PALETTE_LAYER = new Integer (100);  public static final Integer MODAL_LAYER   = new Integer (200);  public static final Integer POPUP_LAYER   = new Integer (300);  public static final Integer DRAG_LAYER    = new Integer (400);  private Hashtable componentToLayer;   // Component -> Layer Number (Integer)  public JLayeredPane()  {    componentToLayer = new Hashtable ();    setLayout(null);  }  /**    * Looks up the layer a child component is currently assigned to.   *   * If <code>c</code> is an instance of {@link JComponent}, then the layer   * is fetched from the client property with the key {@link #LAYER_PROPERTY}.   * Otherwise it is looked up in an internal hashtable that maps   * non-JComponent components to layers. If the components cannot be found   * in either way, the {@link #DEFAULT_LAYER} is returned.   *   * @param c the component to look up.   *   * @return the layer the component is currently assigned to; if the component   *         is not in this layered pane, then 0 (DEFAULT_LAYER) is returned   */  public int getLayer(Component c)  {    Integer layerObj;    if (c instanceof JComponent)      {        JComponent jc = (JComponent) c;        layerObj = (Integer) jc.getClientProperty(LAYER_PROPERTY);      }    else      layerObj = (Integer) componentToLayer.get(c);    if (layerObj == null)      layerObj = DEFAULT_LAYER;    return layerObj.intValue();  }  /**   * Looks up the layer in the client property with the key   * {@link #LAYER_PROPERTY} of <code>comp</code>. If no such property can be   * found, we return <code>0</code> ({@link #DEFAULT_LAYER}).   *    * @param comp the component for which the layer is looked up   *   * @return the layer of <code>comp</code> as stored in the corresponding   *         client property, or <code>0</code> if there is no such property   */  public static int getLayer(JComponent comp)  {    Integer layerObj = (Integer) comp.getClientProperty(LAYER_PROPERTY);    if (layerObj == null)      layerObj = DEFAULT_LAYER;    return layerObj.intValue();  }  /**   * Returns the first JLayeredPane that contains the Component   * <code>comp</code> or <code>null</code> if <code>comp</code> is   * not contained in a JLayeredPane.   *   * @param comp the component for which we are searching the JLayeredPane   *     ancestor   *   * @return the first JLayeredPane that contains the Component   *     <code>comp</code> or <code>null</code> if <code>comp</code> is   *     not contained in a JLayeredPane   */  public static JLayeredPane getLayeredPaneAbove(Component comp)  {    JLayeredPane lp = (JLayeredPane) SwingUtilities.getAncestorOfClass      (JLayeredPane.class, comp);    return lp;  }  /**   * Return the greatest layer number currently in use, in this container.   * This number may legally be positive <em>or</em> negative.   *   * @return the highest layer number   *   * @see #lowestLayer()   */  public int highestLayer()  {    Component[] components = getComponents();    int highest;    if (components.length == 0)      highest = 0;    else      {        highest = Integer.MIN_VALUE;        for (int i = 0; i < components.length; i++)          highest = Math.max(highest, getLayer(components[i]));      }    return highest;  }  /**   * Return the least layer number currently in use, in this container.   * This number may legally be positive <em>or</em> negative.   *   * @return the least layer number   *   * @see #highestLayer()   */  public int lowestLayer()  {    Component[] components = getComponents();    int lowest;    if (components.length == 0)      lowest = 0;    else      {        lowest = Integer.MAX_VALUE;        for (int i = 0; i < components.length; i++)          lowest = Math.max(lowest, getLayer(components[i]));      }    return lowest;  }  /**   * Moves a component to the "front" of its layer. The "front" is a   * synonym for position 0, which is also the last position drawn in each   * layer, so is usually the component which occludes the most other   * components in its layer.   *   * @param c the component to move to the front of its layer   *   * @see #moveToBack   */  public void moveToFront(Component c)  {    setPosition (c, 0);  }  /**   * <p>Moves a component to the "back" of its layer. The "back" is a   * synonym for position N-1 (also known as position -1), where N is the   * size of the layer.</p>   *   * <p>The "back" of a layer is the first position drawn, so the component at   * the "back" is usually the component which is occluded by the most   * other components in its layer.</p>   *   * @param c the component to move to the back of its layer.   *   * @see #moveToFront   */  public void moveToBack(Component c)  {    setPosition (c, -1);  }  /**   * Return the position of a component within its layer. Positions are assigned   * from the "front" (position 0) to the "back" (position N-1), and drawn from    * the back towards the front.   *   * @param c the component to get the position of   *   * @return the position of <code>c</code> within its layer or -1 if   *         <code>c</code> is not a child of this layered pane   *   * @see #setPosition   */  public int getPosition(Component c)  {    int pos = -1;    int index = getIndexOf(c);    Component[] components = getComponents();    int layer = getLayer(c);    if (index >= 0)      {        for (int i = index; i >= 0; --i)          {            if (layer == getLayer(components[i]))              pos++;            else              break;          }      }    return pos;  }  /**   * Change the position of a component within its layer. Positions are assigned

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?