ScrollPane.java revision 243
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows/*
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Copyright 1996-2008 Sun Microsystems, Inc. All Rights Reserved.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows *
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * This code is free software; you can redistribute it and/or modify it
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * under the terms of the GNU General Public License version 2 only, as
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * published by the Free Software Foundation. Sun designates this
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * particular file as subject to the "Classpath" exception as provided
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * by Sun in the LICENSE file that accompanied this code.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows *
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * This code is distributed in the hope that it will be useful, but WITHOUT
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * version 2 for more details (a copy is included in the LICENSE file that
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * accompanied this code).
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows *
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * You should have received a copy of the GNU General Public License version
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * 2 along with this work; if not, write to the Free Software Foundation,
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows *
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * CA 95054 USA or visit www.sun.com if you need additional information or
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * have any questions.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolowspackage java.awt;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolowsimport java.awt.peer.LightweightPeer;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolowsimport java.awt.peer.ScrollPanePeer;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolowsimport java.awt.event.*;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolowsimport javax.accessibility.*;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolowsimport sun.awt.ScrollPaneWheelScroller;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolowsimport sun.awt.SunToolkit;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolowsimport java.beans.ConstructorProperties;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolowsimport java.beans.Transient;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolowsimport java.io.ObjectInputStream;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolowsimport java.io.ObjectOutputStream;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolowsimport java.io.IOException;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows/**
2d2a52253051eb056ca017546a076c0f585283f2stephh * A container class which implements automatic horizontal and/or
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * vertical scrolling for a single child component. The display
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * policy for the scrollbars can be set to:
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * <OL>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * <LI>as needed: scrollbars created and shown only when needed by scrollpane
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * <LI>always: scrollbars created and always shown by the scrollpane
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * <LI>never: scrollbars never created or shown by the scrollpane
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * </OL>
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * <P>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * The state of the horizontal and vertical scrollbars is represented
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * by two <code>ScrollPaneAdjustable</code> objects (one for each
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * dimension) which implement the <code>Adjustable</code> interface.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * The API provides methods to access those objects such that the
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * attributes on the Adjustable object (such as unitIncrement, value,
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * etc.) can be manipulated.
2d2a52253051eb056ca017546a076c0f585283f2stephh * <P>
2d2a52253051eb056ca017546a076c0f585283f2stephh * Certain adjustable properties (minimum, maximum, blockIncrement,
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * and visibleAmount) are set internally by the scrollpane in accordance
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * with the geometry of the scrollpane and its child and these should
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * not be set by programs using the scrollpane.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * <P>
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * If the scrollbar display policy is defined as "never", then the
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * scrollpane can still be programmatically scrolled using the
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * setScrollPosition() method and the scrollpane will move and clip
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * the child's contents appropriately. This policy is useful if the
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * program needs to create and manage its own adjustable controls.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * <P>
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * The placement of the scrollbars is controlled by platform-specific
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * properties set by the user outside of the program.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * <P>
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * The initial size of this container is set to 100x100, but can
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * be reset using setSize().
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * <P>
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Scrolling with the wheel on a wheel-equipped mouse is enabled by default.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * This can be disabled using <code>setWheelScrollingEnabled</code>.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Wheel scrolling can be customized by setting the block and
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * unit increment of the horizontal and vertical Adjustables.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * For information on how mouse wheel events are dispatched, see
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * the class description for {@link MouseWheelEvent}.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * <P>
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Insets are used to define any space used by scrollbars and any
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * borders created by the scroll pane. getInsets() can be used
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * to get the current value for the insets. If the value of
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * scrollbarsAlwaysVisible is false, then the value of the insets
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * will change dynamically depending on whether the scrollbars are
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * currently visible or not.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows *
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @author Tom Ball
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @author Amy Fowler
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @author Tim Prinzing
2d2a52253051eb056ca017546a076c0f585283f2stephh */
2d2a52253051eb056ca017546a076c0f585283f2stephhpublic class ScrollPane extends Container implements Accessible {
2d2a52253051eb056ca017546a076c0f585283f2stephh
2d2a52253051eb056ca017546a076c0f585283f2stephh
2d2a52253051eb056ca017546a076c0f585283f2stephh /**
2d2a52253051eb056ca017546a076c0f585283f2stephh * Initialize JNI field and method IDs
2d2a52253051eb056ca017546a076c0f585283f2stephh */
2d2a52253051eb056ca017546a076c0f585283f2stephh private static native void initIDs();
2d2a52253051eb056ca017546a076c0f585283f2stephh
2d2a52253051eb056ca017546a076c0f585283f2stephh static {
2d2a52253051eb056ca017546a076c0f585283f2stephh /* ensure that the necessary native libraries are loaded */
2d2a52253051eb056ca017546a076c0f585283f2stephh Toolkit.loadLibraries();
2d2a52253051eb056ca017546a076c0f585283f2stephh if (!GraphicsEnvironment.isHeadless()) {
2d2a52253051eb056ca017546a076c0f585283f2stephh initIDs();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Specifies that horizontal/vertical scrollbar should be shown
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * only when the size of the child exceeds the size of the scrollpane
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * in the horizontal/vertical dimension.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows public static final int SCROLLBARS_AS_NEEDED = 0;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Specifies that horizontal/vertical scrollbars should always be
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * shown regardless of the respective sizes of the scrollpane and child.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows public static final int SCROLLBARS_ALWAYS = 1;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Specifies that horizontal/vertical scrollbars should never be shown
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * regardless of the respective sizes of the scrollpane and child.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows public static final int SCROLLBARS_NEVER = 2;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * There are 3 ways in which a scroll bar can be displayed.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * This integer will represent one of these 3 displays -
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * (SCROLLBARS_ALWAYS, SCROLLBARS_AS_NEEDED, SCROLLBARS_NEVER)
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows *
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @serial
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @see #getScrollbarDisplayPolicy
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows private int scrollbarDisplayPolicy;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * An adjustable vertical scrollbar.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * It is important to note that you must <em>NOT</em> call 3
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * <code>Adjustable</code> methods, namely:
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * <code>setMinimum()</code>, <code>setMaximum()</code>,
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * <code>setVisibleAmount()</code>.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows *
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @serial
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @see #getVAdjustable
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows private ScrollPaneAdjustable vAdjustable;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * An adjustable horizontal scrollbar.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * It is important to note that you must <em>NOT</em> call 3
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * <code>Adjustable</code> methods, namely:
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * <code>setMinimum()</code>, <code>setMaximum()</code>,
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * <code>setVisibleAmount()</code>.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows *
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @serial
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @see #getHAdjustable
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows private ScrollPaneAdjustable hAdjustable;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows private static final String base = "scrollpane";
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows private static int nameCounter = 0;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows private static final boolean defaultWheelScroll = true;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Indicates whether or not scrolling should take place when a
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * MouseWheelEvent is received.
654b400c387942fc00d4f6869539adbd7b25fbceJoshua M. Clulow *
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @serial
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @since 1.4
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows private boolean wheelScrollingEnabled = defaultWheelScroll;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /*
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * JDK 1.1 serialVersionUID
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows private static final long serialVersionUID = 7956609840827222915L;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Create a new scrollpane container with a scrollbar display
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * policy of "as needed".
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @throws HeadlessException if GraphicsEnvironment.isHeadless()
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * returns true
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @see java.awt.GraphicsEnvironment#isHeadless
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows public ScrollPane() throws HeadlessException {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows this(SCROLLBARS_AS_NEEDED);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Create a new scrollpane container.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @param scrollbarDisplayPolicy policy for when scrollbars should be shown
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @throws IllegalArgumentException if the specified scrollbar
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * display policy is invalid
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @throws HeadlessException if GraphicsEnvironment.isHeadless()
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * returns true
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @see java.awt.GraphicsEnvironment#isHeadless
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows @ConstructorProperties({"scrollbarDisplayPolicy"})
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows public ScrollPane(int scrollbarDisplayPolicy) throws HeadlessException {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows GraphicsEnvironment.checkHeadless();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows this.layoutMgr = null;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows this.width = 100;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows this.height = 100;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows switch (scrollbarDisplayPolicy) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows case SCROLLBARS_NEVER:
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows case SCROLLBARS_AS_NEEDED:
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows case SCROLLBARS_ALWAYS:
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows this.scrollbarDisplayPolicy = scrollbarDisplayPolicy;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows break;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows default:
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows throw new IllegalArgumentException("illegal scrollbar display policy");
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows vAdjustable = new ScrollPaneAdjustable(this, new PeerFixer(this),
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Adjustable.VERTICAL);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows hAdjustable = new ScrollPaneAdjustable(this, new PeerFixer(this),
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Adjustable.HORIZONTAL);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows setWheelScrollingEnabled(defaultWheelScroll);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Construct a name for this component. Called by getName() when the
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * name is null.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows String constructComponentName() {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows synchronized (ScrollPane.class) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows return base + nameCounter++;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows // The scrollpane won't work with a windowless child... it assumes
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows // it is moving a child window around so the windowless child is
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows // wrapped with a window.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows private void addToPanel(Component comp, Object constraints, int index) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Panel child = new Panel();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows child.setLayout(new BorderLayout());
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows child.add(comp);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows super.addImpl(child, constraints, index);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows validate();
2d2a52253051eb056ca017546a076c0f585283f2stephh }
2d2a52253051eb056ca017546a076c0f585283f2stephh
2d2a52253051eb056ca017546a076c0f585283f2stephh /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Adds the specified component to this scroll pane container.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * If the scroll pane has an existing child component, that
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * component is removed and the new one is added.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @param comp the component to be added
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @param constraints not applicable
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @param index position of child component (must be <= 0)
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows protected final void addImpl(Component comp, Object constraints, int index) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows synchronized (getTreeLock()) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows if (getComponentCount() > 0) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows remove(0);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows if (index > 0) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows throw new IllegalArgumentException("position greater than 0");
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows if (!SunToolkit.isLightweightOrUnknown(comp)) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows super.addImpl(comp, constraints, index);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows } else {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows addToPanel(comp, constraints, index);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Returns the display policy for the scrollbars.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @return the display policy for the scrollbars
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows public int getScrollbarDisplayPolicy() {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows return scrollbarDisplayPolicy;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Returns the current size of the scroll pane's view port.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @return the size of the view port in pixels
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows public Dimension getViewportSize() {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Insets i = getInsets();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows return new Dimension(width - i.right - i.left,
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows height - i.top - i.bottom);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Returns the height that would be occupied by a horizontal
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * scrollbar, which is independent of whether it is currently
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * displayed by the scroll pane or not.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @return the height of a horizontal scrollbar in pixels
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows public int getHScrollbarHeight() {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows int h = 0;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows if (scrollbarDisplayPolicy != SCROLLBARS_NEVER) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows ScrollPanePeer peer = (ScrollPanePeer)this.peer;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows if (peer != null) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows h = peer.getHScrollbarHeight();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows return h;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Returns the width that would be occupied by a vertical
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * scrollbar, which is independent of whether it is currently
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * displayed by the scroll pane or not.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @return the width of a vertical scrollbar in pixels
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows public int getVScrollbarWidth() {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows int w = 0;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows if (scrollbarDisplayPolicy != SCROLLBARS_NEVER) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows ScrollPanePeer peer = (ScrollPanePeer)this.peer;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows if (peer != null) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows w = peer.getVScrollbarWidth();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows return w;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
2d2a52253051eb056ca017546a076c0f585283f2stephh /**
2d2a52253051eb056ca017546a076c0f585283f2stephh * Returns the <code>ScrollPaneAdjustable</code> object which
2d2a52253051eb056ca017546a076c0f585283f2stephh * represents the state of the vertical scrollbar.
2d2a52253051eb056ca017546a076c0f585283f2stephh * The declared return type of this method is
2d2a52253051eb056ca017546a076c0f585283f2stephh * <code>Adjustable</code> to maintain backward compatibility.
2d2a52253051eb056ca017546a076c0f585283f2stephh * @see java.awt.ScrollPaneAdjustable
2d2a52253051eb056ca017546a076c0f585283f2stephh */
2d2a52253051eb056ca017546a076c0f585283f2stephh public Adjustable getVAdjustable() {
2d2a52253051eb056ca017546a076c0f585283f2stephh return vAdjustable;
2d2a52253051eb056ca017546a076c0f585283f2stephh }
2d2a52253051eb056ca017546a076c0f585283f2stephh
2d2a52253051eb056ca017546a076c0f585283f2stephh /**
2d2a52253051eb056ca017546a076c0f585283f2stephh * Returns the <code>ScrollPaneAdjustable</code> object which
2d2a52253051eb056ca017546a076c0f585283f2stephh * represents the state of the horizontal scrollbar.
2d2a52253051eb056ca017546a076c0f585283f2stephh * The declared return type of this method is
2d2a52253051eb056ca017546a076c0f585283f2stephh * <code>Adjustable</code> to maintain backward compatibility.
2d2a52253051eb056ca017546a076c0f585283f2stephh * @see java.awt.ScrollPaneAdjustable
2d2a52253051eb056ca017546a076c0f585283f2stephh */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows public Adjustable getHAdjustable() {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows return hAdjustable;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Scrolls to the specified position within the child component.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * A call to this method is only valid if the scroll pane contains
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * a child. Specifying a position outside of the legal scrolling bounds
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * of the child will scroll to the closest legal position.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Legal bounds are defined to be the rectangle:
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * x = 0, y = 0, width = (child width - view port width),
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * height = (child height - view port height).
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * This is a convenience method which interfaces with the Adjustable
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * objects which represent the state of the scrollbars.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @param x the x position to scroll to
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @param y the y position to scroll to
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @throws NullPointerException if the scrollpane does not contain
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * a child
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows public void setScrollPosition(int x, int y) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows synchronized (getTreeLock()) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows if (ncomponents <= 0) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows throw new NullPointerException("child is null");
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows hAdjustable.setValue(x);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows vAdjustable.setValue(y);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Scrolls to the specified position within the child component.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * A call to this method is only valid if the scroll pane contains
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * a child and the specified position is within legal scrolling bounds
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * of the child. Specifying a position outside of the legal scrolling
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * bounds of the child will scroll to the closest legal position.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Legal bounds are defined to be the rectangle:
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * x = 0, y = 0, width = (child width - view port width),
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * height = (child height - view port height).
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * This is a convenience method which interfaces with the Adjustable
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * objects which represent the state of the scrollbars.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @param p the Point representing the position to scroll to
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows public void setScrollPosition(Point p) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows setScrollPosition(p.x, p.y);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Returns the current x,y position within the child which is displayed
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * at the 0,0 location of the scrolled panel's view port.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * This is a convenience method which interfaces with the adjustable
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * objects which represent the state of the scrollbars.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @return the coordinate position for the current scroll position
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @throws NullPointerException if the scrollpane does not contain
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * a child
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows @Transient
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows public Point getScrollPosition() {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows if (ncomponents <= 0) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows throw new NullPointerException("child is null");
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows return new Point(hAdjustable.getValue(), vAdjustable.getValue());
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Sets the layout manager for this container. This method is
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * overridden to prevent the layout mgr from being set.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @param mgr the specified layout manager
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows public final void setLayout(LayoutManager mgr) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows throw new AWTError("ScrollPane controls layout");
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Lays out this container by resizing its child to its preferred size.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * If the new preferred size of the child causes the current scroll
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * position to be invalid, the scroll position is set to the closest
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * valid position.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows *
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @see Component#validate
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows public void doLayout() {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows layout();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Determine the size to allocate the child component.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * If the viewport area is bigger than the childs
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * preferred size then the child is allocated enough
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * to fill the viewport, otherwise the child is given
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * it's preferred size.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Dimension calculateChildSize() {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows //
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows // calculate the view size, accounting for border but not scrollbars
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows // - don't use right/bottom insets since they vary depending
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows // on whether or not scrollbars were displayed on last resize
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows //
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Dimension size = getSize();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Insets insets = getInsets();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows int viewWidth = size.width - insets.left*2;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows int viewHeight = size.height - insets.top*2;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows //
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows // determine whether or not horz or vert scrollbars will be displayed
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows //
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows boolean vbarOn;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows boolean hbarOn;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Component child = getComponent(0);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Dimension childSize = new Dimension(child.getPreferredSize());
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows if (scrollbarDisplayPolicy == SCROLLBARS_AS_NEEDED) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows vbarOn = childSize.height > viewHeight;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows hbarOn = childSize.width > viewWidth;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows } else if (scrollbarDisplayPolicy == SCROLLBARS_ALWAYS) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows vbarOn = hbarOn = true;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows } else { // SCROLLBARS_NEVER
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows vbarOn = hbarOn = false;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows //
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows // adjust predicted view size to account for scrollbars
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows //
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows int vbarWidth = getVScrollbarWidth();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows int hbarHeight = getHScrollbarHeight();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows if (vbarOn) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows viewWidth -= vbarWidth;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows if(hbarOn) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows viewHeight -= hbarHeight;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows //
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows // if child is smaller than view, size it up
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows //
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows if (childSize.width < viewWidth) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows childSize.width = viewWidth;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows if (childSize.height < viewHeight) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows childSize.height = viewHeight;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows return childSize;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @deprecated As of JDK version 1.1,
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * replaced by <code>doLayout()</code>.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows @Deprecated
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows public void layout() {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows if (ncomponents > 0) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Component c = getComponent(0);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Point p = getScrollPosition();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Dimension cs = calculateChildSize();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Dimension vs = getViewportSize();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Insets i = getInsets();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows c.reshape(i.left - p.x, i.top - p.y, cs.width, cs.height);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows ScrollPanePeer peer = (ScrollPanePeer)this.peer;
2d2a52253051eb056ca017546a076c0f585283f2stephh if (peer != null) {
2d2a52253051eb056ca017546a076c0f585283f2stephh peer.childResized(cs.width, cs.height);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows // update adjustables... the viewport size may have changed
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows // with the scrollbars coming or going so the viewport size
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows // is updated before the adjustables.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows vs = getViewportSize();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows hAdjustable.setSpan(0, cs.width, vs.width);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows vAdjustable.setSpan(0, cs.height, vs.height);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Prints the component in this scroll pane.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @param g the specified Graphics window
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @see Component#print
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * @see Component#printAll
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows public void printComponents(Graphics g) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows if (ncomponents > 0) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Component c = component[0];
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Point p = c.getLocation();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Dimension vs = getViewportSize();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Insets i = getInsets();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows Graphics cg = g.create();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows try {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows cg.clipRect(i.left, i.top, vs.width, vs.height);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows cg.translate(p.x, p.y);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows c.printAll(cg);
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows } finally {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows cg.dispose();
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows }
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows /**
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows * Creates the scroll pane's peer.
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows */
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows public void addNotify() {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows synchronized (getTreeLock()) {
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows int vAdjustableValue = 0;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows int hAdjustableValue = 0;
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows // Bug 4124460. Save the current adjustable values,
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows // so they can be restored after addnotify. Set the
749f21d359d8fbd020c974a1a5227316221bfc9cwesolows // adjustables to 0, to prevent crashes for possible
// negative values.
if (getComponentCount() > 0) {
vAdjustableValue = vAdjustable.getValue();
hAdjustableValue = hAdjustable.getValue();
vAdjustable.setValue(0);
hAdjustable.setValue(0);
}
if (peer == null)
peer = getToolkit().createScrollPane(this);
super.addNotify();
// Bug 4124460. Restore the adjustable values.
if (getComponentCount() > 0) {
vAdjustable.setValue(vAdjustableValue);
hAdjustable.setValue(hAdjustableValue);
}
}
}
/**
* Returns a string representing the state of this
* <code>ScrollPane</code>. This
* method is intended to be used only for debugging purposes, and the
* content and format of the returned string may vary between
* implementations. The returned string may be empty but may not be
* <code>null</code>.
*
* @return the parameter string of this scroll pane
*/
public String paramString() {
String sdpStr;
switch (scrollbarDisplayPolicy) {
case SCROLLBARS_AS_NEEDED:
sdpStr = "as-needed";
break;
case SCROLLBARS_ALWAYS:
sdpStr = "always";
break;
case SCROLLBARS_NEVER:
sdpStr = "never";
break;
default:
sdpStr = "invalid display policy";
}
Point p = ncomponents > 0? getScrollPosition() : new Point(0,0);
Insets i = getInsets();
return super.paramString()+",ScrollPosition=("+p.x+","+p.y+")"+
",Insets=("+i.top+","+i.left+","+i.bottom+","+i.right+")"+
",ScrollbarDisplayPolicy="+sdpStr+
",wheelScrollingEnabled="+isWheelScrollingEnabled();
}
void autoProcessMouseWheel(MouseWheelEvent e) {
processMouseWheelEvent(e);
}
/**
* Process mouse wheel events that are delivered to this
* <code>ScrollPane</code> by scrolling an appropriate amount.
* <p>Note that if the event parameter is <code>null</code>
* the behavior is unspecified and may result in an
* exception.
*
* @param e the mouse wheel event
* @since 1.4
*/
protected void processMouseWheelEvent(MouseWheelEvent e) {
if (isWheelScrollingEnabled()) {
ScrollPaneWheelScroller.handleWheelScrolling(this, e);
e.consume();
}
super.processMouseWheelEvent(e);
}
/**
* If wheel scrolling is enabled, we return true for MouseWheelEvents
* @since 1.4
*/
protected boolean eventTypeEnabled(int type) {
if (type == MouseEvent.MOUSE_WHEEL && isWheelScrollingEnabled()) {
return true;
}
else {
return super.eventTypeEnabled(type);
}
}
/**
* Enables/disables scrolling in response to movement of the mouse wheel.
* Wheel scrolling is enabled by default.
*
* @param handleWheel <code>true</code> if scrolling should be done
* automatically for a MouseWheelEvent,
* <code>false</code> otherwise.
* @see #isWheelScrollingEnabled
* @see java.awt.event.MouseWheelEvent
* @see java.awt.event.MouseWheelListener
* @since 1.4
*/
public void setWheelScrollingEnabled(boolean handleWheel) {
wheelScrollingEnabled = handleWheel;
}
/**
* Indicates whether or not scrolling will take place in response to
* the mouse wheel. Wheel scrolling is enabled by default.
*
* @see #setWheelScrollingEnabled(boolean)
* @since 1.4
*/
public boolean isWheelScrollingEnabled() {
return wheelScrollingEnabled;
}
/**
* Writes default serializable fields to stream.
*/
private void writeObject(ObjectOutputStream s) throws IOException {
// 4352819: We only need this degenerate writeObject to make
// it safe for future versions of this class to write optional
// data to the stream.
s.defaultWriteObject();
}
/**
* Reads default serializable fields to stream.
* @exception HeadlessException if
* <code>GraphicsEnvironment.isHeadless()</code> returns
* <code>true</code>
* @see java.awt.GraphicsEnvironment#isHeadless
*/
private void readObject(ObjectInputStream s)
throws ClassNotFoundException, IOException, HeadlessException
{
GraphicsEnvironment.checkHeadless();
// 4352819: Gotcha! Cannot use s.defaultReadObject here and
// then continue with reading optional data. Use GetField instead.
ObjectInputStream.GetField f = s.readFields();
// Old fields
scrollbarDisplayPolicy = f.get("scrollbarDisplayPolicy",
SCROLLBARS_AS_NEEDED);
hAdjustable = (ScrollPaneAdjustable)f.get("hAdjustable", null);
vAdjustable = (ScrollPaneAdjustable)f.get("vAdjustable", null);
// Since 1.4
wheelScrollingEnabled = f.get("wheelScrollingEnabled",
defaultWheelScroll);
// // Note to future maintainers
// if (f.defaulted("wheelScrollingEnabled")) {
// // We are reading pre-1.4 stream that doesn't have
// // optional data, not even the TC_ENDBLOCKDATA marker.
// // Reading anything after this point is unsafe as we will
// // read unrelated objects further down the stream (4352819).
// }
// else {
// // Reading data from 1.4 or later, it's ok to try to read
// // optional data as OptionalDataException with eof == true
// // will be correctly reported
// }
}
class PeerFixer implements AdjustmentListener, java.io.Serializable
{
private static final long serialVersionUID = 1043664721353696630L;
PeerFixer(ScrollPane scroller) {
this.scroller = scroller;
}
/**
* Invoked when the value of the adjustable has changed.
*/
public void adjustmentValueChanged(AdjustmentEvent e) {
Adjustable adj = e.getAdjustable();
int value = e.getValue();
ScrollPanePeer peer = (ScrollPanePeer) scroller.peer;
if (peer != null) {
peer.setValue(adj, value);
}
Component c = scroller.getComponent(0);
switch(adj.getOrientation()) {
case Adjustable.VERTICAL:
c.move(c.getLocation().x, -(value));
break;
case Adjustable.HORIZONTAL:
c.move(-(value), c.getLocation().y);
break;
default:
throw new IllegalArgumentException("Illegal adjustable orientation");
}
}
private ScrollPane scroller;
}
/////////////////
// Accessibility support
////////////////
/**
* Gets the AccessibleContext associated with this ScrollPane.
* For scroll panes, the AccessibleContext takes the form of an
* AccessibleAWTScrollPane.
* A new AccessibleAWTScrollPane instance is created if necessary.
*
* @return an AccessibleAWTScrollPane that serves as the
* AccessibleContext of this ScrollPane
* @since 1.3
*/
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleAWTScrollPane();
}
return accessibleContext;
}
/**
* This class implements accessibility support for the
* <code>ScrollPane</code> class. It provides an implementation of the
* Java Accessibility API appropriate to scroll pane user-interface
* elements.
* @since 1.3
*/
protected class AccessibleAWTScrollPane extends AccessibleAWTContainer
{
/*
* JDK 1.3 serialVersionUID
*/
private static final long serialVersionUID = 6100703663886637L;
/**
* Get the role of this object.
*
* @return an instance of AccessibleRole describing the role of the
* object
* @see AccessibleRole
*/
public AccessibleRole getAccessibleRole() {
return AccessibleRole.SCROLL_PANE;
}
} // class AccessibleAWTScrollPane
}
/*
* In JDK 1.1.1, the pkg private class java.awt.PeerFixer was moved to
* become an inner class of ScrollPane, which broke serialization
* for ScrollPane objects using JDK 1.1.
* Instead of moving it back out here, which would break all JDK 1.1.x
* releases, we keep PeerFixer in both places. Because of the scoping rules,
* the PeerFixer that is used in ScrollPane will be the one that is the
* inner class. This pkg private PeerFixer class below will only be used
* if the Java 2 platform is used to deserialize ScrollPane objects that were serialized
* using JDK1.1
*/
class PeerFixer implements AdjustmentListener, java.io.Serializable {
/*
* serialVersionUID
*/
private static final long serialVersionUID = 7051237413532574756L;
PeerFixer(ScrollPane scroller) {
this.scroller = scroller;
}
/**
* Invoked when the value of the adjustable has changed.
*/
public void adjustmentValueChanged(AdjustmentEvent e) {
Adjustable adj = e.getAdjustable();
int value = e.getValue();
ScrollPanePeer peer = (ScrollPanePeer) scroller.peer;
if (peer != null) {
peer.setValue(adj, value);
}
Component c = scroller.getComponent(0);
switch(adj.getOrientation()) {
case Adjustable.VERTICAL:
c.move(c.getLocation().x, -(value));
break;
case Adjustable.HORIZONTAL:
c.move(-(value), c.getLocation().y);
break;
default:
throw new IllegalArgumentException("Illegal adjustable orientation");
}
}
private ScrollPane scroller;
}