1121 lines · 40.2 KB · Java
RawDownload
/* * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.swing;  import javax.swing.border.*; import java.awt.LayoutManager;import java.awt.Component;import java.awt.Container;import java.awt.Rectangle;import java.awt.Dimension;import java.awt.Insets;import java.io.Serializable;  /** * The layout manager used by <code>JScrollPane</code>. * <code>JScrollPaneLayout</code> is * responsible for nine components: a viewport, two scrollbars, * a row header, a column header, and four "corner" components. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing.  As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. * * @see JScrollPane * @see JViewport * * @author Hans Muller */public class ScrollPaneLayout    implements LayoutManager, ScrollPaneConstants, Serializable{     /**     * The scrollpane's viewport child.     * Default is an empty <code>JViewport</code>.     * @see JScrollPane#setViewport     */    protected JViewport viewport;      /**     * The scrollpane's vertical scrollbar child.     * Default is a <code>JScrollBar</code>.     * @see JScrollPane#setVerticalScrollBar     */    protected JScrollBar vsb;      /**     * The scrollpane's horizontal scrollbar child.     * Default is a <code>JScrollBar</code>.     * @see JScrollPane#setHorizontalScrollBar     */    protected JScrollBar hsb;      /**     * The row header child.  Default is <code>null</code>.     * @see JScrollPane#setRowHeader     */    protected JViewport rowHead;      /**     * The column header child.  Default is <code>null</code>.     * @see JScrollPane#setColumnHeader     */    protected JViewport colHead;      /**     * The component to display in the lower left corner.     * Default is <code>null</code>.     * @see JScrollPane#setCorner     */    protected Component lowerLeft;      /**     * The component to display in the lower right corner.     * Default is <code>null</code>.     * @see JScrollPane#setCorner     */    protected Component lowerRight;      /**     * The component to display in the upper left corner.     * Default is <code>null</code>.     * @see JScrollPane#setCorner     */    protected Component upperLeft;      /**     * The component to display in the upper right corner.     * Default is <code>null</code>.     * @see JScrollPane#setCorner     */    protected Component upperRight;      /**     * The display policy for the vertical scrollbar.     * The default is <code>ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED</code>.     * <p>     * This field is obsolete, please use the <code>JScrollPane</code> field instead.     *     * @see JScrollPane#setVerticalScrollBarPolicy     */    protected int vsbPolicy = VERTICAL_SCROLLBAR_AS_NEEDED;      /**     * The display policy for the horizontal scrollbar.     * The default is <code>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED</code>.     * <p>     * This field is obsolete, please use the <code>JScrollPane</code> field instead.     *     * @see JScrollPane#setHorizontalScrollBarPolicy     */    protected int hsbPolicy = HORIZONTAL_SCROLLBAR_AS_NEEDED;      /**     * This method is invoked after the ScrollPaneLayout is set as the     * LayoutManager of a <code>JScrollPane</code>.     * It initializes all of the internal fields that     * are ordinarily set by <code>addLayoutComponent</code>.  For example:     * <pre>     * ScrollPaneLayout mySPLayout = new ScrollPanelLayout() {     *     public void layoutContainer(Container p) {     *         super.layoutContainer(p);     *         // do some extra work here ...     *     }     * };     * scrollpane.setLayout(mySPLayout):     * </pre>     */    public void syncWithScrollPane(JScrollPane sp) {        viewport = sp.getViewport();        vsb = sp.getVerticalScrollBar();        hsb = sp.getHorizontalScrollBar();        rowHead = sp.getRowHeader();        colHead = sp.getColumnHeader();        lowerLeft = sp.getCorner(LOWER_LEFT_CORNER);        lowerRight = sp.getCorner(LOWER_RIGHT_CORNER);        upperLeft = sp.getCorner(UPPER_LEFT_CORNER);        upperRight = sp.getCorner(UPPER_RIGHT_CORNER);        vsbPolicy = sp.getVerticalScrollBarPolicy();        hsbPolicy = sp.getHorizontalScrollBarPolicy();    }      /**     * Removes an existing component.  When a new component, such as     * the left corner, or vertical scrollbar, is added, the old one,     * if it exists, must be removed.     * <p>     * This method returns <code>newC</code>. If <code>oldC</code> is     * not equal to <code>newC</code> and is non-<code>null</code>,     * it will be removed from its parent.     *     * @param oldC the <code>Component</code> to replace     * @param newC the <code>Component</code> to add     * @return the <code>newC</code>     */    protected Component addSingletonComponent(Component oldC, Component newC)    {        if ((oldC != null) && (oldC != newC)) {            oldC.getParent().remove(oldC);        }        return newC;    }      /**     * Adds the specified component to the layout. The layout is     * identified using one of:     * <ul>     * <li>ScrollPaneConstants.VIEWPORT     * <li>ScrollPaneConstants.VERTICAL_SCROLLBAR     * <li>ScrollPaneConstants.HORIZONTAL_SCROLLBAR     * <li>ScrollPaneConstants.ROW_HEADER     * <li>ScrollPaneConstants.COLUMN_HEADER     * <li>ScrollPaneConstants.LOWER_LEFT_CORNER     * <li>ScrollPaneConstants.LOWER_RIGHT_CORNER     * <li>ScrollPaneConstants.UPPER_LEFT_CORNER     * <li>ScrollPaneConstants.UPPER_RIGHT_CORNER     * </ul>     *     * @param s the component identifier     * @param c the the component to be added     * @exception IllegalArgumentException if <code>s</code> is an invalid key     */    public void addLayoutComponent(String s, Component c)    {        if (s.equals(VIEWPORT)) {            viewport = (JViewport)addSingletonComponent(viewport, c);        }        else if (s.equals(VERTICAL_SCROLLBAR)) {            vsb = (JScrollBar)addSingletonComponent(vsb, c);        }        else if (s.equals(HORIZONTAL_SCROLLBAR)) {            hsb = (JScrollBar)addSingletonComponent(hsb, c);        }        else if (s.equals(ROW_HEADER)) {            rowHead = (JViewport)addSingletonComponent(rowHead, c);        }        else if (s.equals(COLUMN_HEADER)) {            colHead = (JViewport)addSingletonComponent(colHead, c);        }        else if (s.equals(LOWER_LEFT_CORNER)) {            lowerLeft = addSingletonComponent(lowerLeft, c);        }        else if (s.equals(LOWER_RIGHT_CORNER)) {            lowerRight = addSingletonComponent(lowerRight, c);        }        else if (s.equals(UPPER_LEFT_CORNER)) {            upperLeft = addSingletonComponent(upperLeft, c);        }        else if (s.equals(UPPER_RIGHT_CORNER)) {            upperRight = addSingletonComponent(upperRight, c);        }        else {            throw new IllegalArgumentException("invalid layout key " + s);        }    }      /**     * Removes the specified component from the layout.     *     * @param c the component to remove     */    public void removeLayoutComponent(Component c)    {        if (c == viewport) {            viewport = null;        }        else if (c == vsb) {            vsb = null;        }        else if (c == hsb) {            hsb = null;        }        else if (c == rowHead) {            rowHead = null;        }        else if (c == colHead) {            colHead = null;        }        else if (c == lowerLeft) {            lowerLeft = null;        }        else if (c == lowerRight) {            lowerRight = null;        }        else if (c == upperLeft) {            upperLeft = null;        }        else if (c == upperRight) {            upperRight = null;        }    }      /**     * Returns the vertical scrollbar-display policy.     *     * @return an integer giving the display policy     * @see #setVerticalScrollBarPolicy     */    public int getVerticalScrollBarPolicy() {        return vsbPolicy;    }      /**     * Sets the vertical scrollbar-display policy. The options     * are:     * <ul>     * <li>ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED     * <li>ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER     * <li>ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS     * </ul>     * Note: Applications should use the <code>JScrollPane</code> version     * of this method.  It only exists for backwards compatibility     * with the Swing 1.0.2 (and earlier) versions of this class.     *     * @param x an integer giving the display policy     * @exception IllegalArgumentException if <code>x</code> is an invalid     *          vertical scroll bar policy, as listed above     */    public void setVerticalScrollBarPolicy(int x) {        switch (x) {        case VERTICAL_SCROLLBAR_AS_NEEDED:        case VERTICAL_SCROLLBAR_NEVER:        case VERTICAL_SCROLLBAR_ALWAYS:                vsbPolicy = x;                break;        default:            throw new IllegalArgumentException("invalid verticalScrollBarPolicy");        }    }      /**     * Returns the horizontal scrollbar-display policy.     *     * @return an integer giving the display policy     * @see #setHorizontalScrollBarPolicy     */    public int getHorizontalScrollBarPolicy() {        return hsbPolicy;    }     /**     * Sets the horizontal scrollbar-display policy.     * The options are:<ul>     * <li>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED     * <li>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER     * <li>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS     * </ul>     * Note: Applications should use the <code>JScrollPane</code> version     * of this method.  It only exists for backwards compatibility     * with the Swing 1.0.2 (and earlier) versions of this class.     *     * @param x an int giving the display policy     * @exception IllegalArgumentException if <code>x</code> is not a valid     *          horizontal scrollbar policy, as listed above     */    public void setHorizontalScrollBarPolicy(int x) {        switch (x) {        case HORIZONTAL_SCROLLBAR_AS_NEEDED:        case HORIZONTAL_SCROLLBAR_NEVER:        case HORIZONTAL_SCROLLBAR_ALWAYS:                hsbPolicy = x;                break;        default:            throw new IllegalArgumentException("invalid horizontalScrollBarPolicy");        }    }      /**     * Returns the <code>JViewport</code> object that displays the     * scrollable contents.     * @return the <code>JViewport</code> object that displays the scrollable contents     * @see JScrollPane#getViewport     */    public JViewport getViewport() {        return viewport;    }      /**     * Returns the <code>JScrollBar</code> object that handles horizontal scrolling.     * @return the <code>JScrollBar</code> object that handles horizontal scrolling     * @see JScrollPane#getHorizontalScrollBar     */    public JScrollBar getHorizontalScrollBar() {        return hsb;    }     /**     * Returns the <code>JScrollBar</code> object that handles vertical scrolling.     * @return the <code>JScrollBar</code> object that handles vertical scrolling     * @see JScrollPane#getVerticalScrollBar     */    public JScrollBar getVerticalScrollBar() {        return vsb;    }      /**     * Returns the <code>JViewport</code> object that is the row header.     * @return the <code>JViewport</code> object that is the row header     * @see JScrollPane#getRowHeader     */    public JViewport getRowHeader() {        return rowHead;    }      /**     * Returns the <code>JViewport</code> object that is the column header.     * @return the <code>JViewport</code> object that is the column header     * @see JScrollPane#getColumnHeader     */    public JViewport getColumnHeader() {        return colHead;    }      /**     * Returns the <code>Component</code> at the specified corner.     * @param key the <code>String</code> specifying the corner     * @return the <code>Component</code> at the specified corner, as defined in     *         {@link ScrollPaneConstants}; if <code>key</code> is not one of the     *          four corners, <code>null</code> is returned     * @see JScrollPane#getCorner     */    public Component getCorner(String key) {        if (key.equals(LOWER_LEFT_CORNER)) {            return lowerLeft;        }        else if (key.equals(LOWER_RIGHT_CORNER)) {            return lowerRight;        }        else if (key.equals(UPPER_LEFT_CORNER)) {            return upperLeft;        }        else if (key.equals(UPPER_RIGHT_CORNER)) {            return upperRight;        }        else {            return null;        }    }      /**     * The preferred size of a <code>ScrollPane</code> is the size of the insets,     * plus the preferred size of the viewport, plus the preferred size of     * the visible headers, plus the preferred size of the scrollbars     * that will appear given the current view and the current     * scrollbar displayPolicies.     * <p>Note that the rowHeader is calculated as part of the preferred width     * and the colHeader is calculated as part of the preferred size.     *     * @param parent the <code>Container</code> that will be laid out     * @return a <code>Dimension</code> object specifying the preferred size of the     *         viewport and any scrollbars     * @see ViewportLayout     * @see LayoutManager     */    public Dimension preferredLayoutSize(Container parent)    {        /* Sync the (now obsolete) policy fields with the         * JScrollPane.         */        JScrollPane scrollPane = (JScrollPane)parent;        vsbPolicy = scrollPane.getVerticalScrollBarPolicy();        hsbPolicy = scrollPane.getHorizontalScrollBarPolicy();         Insets insets = parent.getInsets();        int prefWidth = insets.left + insets.right;        int prefHeight = insets.top + insets.bottom;         /* Note that viewport.getViewSize() is equivalent to         * viewport.getView().getPreferredSize() modulo a null         * view or a view whose size was explicitly set.         */         Dimension extentSize = null;        Dimension viewSize = null;        Component view = null;         if (viewport != null) {            extentSize = viewport.getPreferredSize();            view = viewport.getView();            if (view != null) {                viewSize = view.getPreferredSize();            } else {                viewSize = new Dimension(0, 0);            }        } 

Lines 1–500 of 1121.