/* * Copyright (c) 1997, 2011, 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 java.awt.*;import java.awt.event.*;import java.awt.image.VolatileImage;import java.awt.peer.ComponentPeer;import java.applet.Applet;import java.beans.Transient;import javax.swing.plaf.ViewportUI; import javax.swing.event.*;import javax.swing.border.*;import javax.accessibility.*; import java.io.Serializable; /** * The "viewport" or "porthole" through which you see the underlying * information. When you scroll, what moves is the viewport. It is like * peering through a camera's viewfinder. Moving the viewfinder upwards * brings new things into view at the top of the picture and loses * things that were at the bottom. * <p> * By default, <code>JViewport</code> is opaque. To change this, use the * <code>setOpaque</code> method. * <p> * <b>NOTE:</b>We have implemented a faster scrolling algorithm that * does not require a buffer to draw in. The algorithm works as follows: * <ol><li>The view and parent view and checked to see if they are * <code>JComponents</code>, * if they aren't, stop and repaint the whole viewport. * <li>If the viewport is obscured by an ancestor, stop and repaint the whole * viewport. * <li>Compute the region that will become visible, if it is as big as * the viewport, stop and repaint the whole view region. * <li>Obtain the ancestor <code>Window</code>'s graphics and * do a <code>copyArea</code> on the scrolled region. * <li>Message the view to repaint the newly visible region. * <li>The next time paint is invoked on the viewport, if the clip region * is smaller than the viewport size a timer is kicked off to repaint the * whole region. * </ol> * In general this approach is much faster. Compared to the backing store * approach this avoids the overhead of maintaining an offscreen buffer and * having to do two <code>copyArea</code>s. * Compared to the non backing store case this * approach will greatly reduce the painted region. * <p> * This approach can cause slower times than the backing store approach * when the viewport is obscured by another window, or partially offscreen. * When another window * obscures the viewport the copyArea will copy garbage and a * paint event will be generated by the system to inform us we need to * paint the newly exposed region. The only way to handle this is to * repaint the whole viewport, which can cause slower performance than the * backing store case. In most applications very rarely will the user be * scrolling while the viewport is obscured by another window or offscreen, * so this optimization is usually worth the performance hit when obscured. * <p> * <strong>Warning:</strong> Swing is not thread safe. For more * information see <a * href="package-summary.html#threading">Swing's Threading * Policy</a>. * <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}. * * @author Hans Muller * @author Philip Milne * @see JScrollPane */public class JViewport extends JComponent implements Accessible{ /** * @see #getUIClassID * @see #readObject */ private static final String uiClassID = "ViewportUI"; /** Property used to indicate window blitting should not be done. */ static final Object EnableWindowBlit = "EnableWindowBlit"; /** * True when the viewport dimensions have been determined. * The default is false. */ protected boolean isViewSizeSet = false; /** * The last <code>viewPosition</code> that we've painted, so we know how * much of the backing store image is valid. */ protected Point lastPaintPosition = null; /** * True when this viewport is maintaining an offscreen image of its * contents, so that some scrolling can take place using fast "bit-blit" * operations instead of by accessing the view object to construct the * display. The default is <code>false</code>. * * @deprecated As of Java 2 platform v1.3 * @see #setScrollMode */ @Deprecated protected boolean backingStore = false; /** The view image used for a backing store. */ transient protected Image backingStoreImage = null; /** * The <code>scrollUnderway</code> flag is used for components like * <code>JList</code>. When the downarrow key is pressed on a * <code>JList</code> and the selected * cell is the last in the list, the <code>scrollpane</code> autoscrolls. * Here, the old selected cell needs repainting and so we need * a flag to make the viewport do the optimized painting * only when there is an explicit call to * <code>setViewPosition(Point)</code>. * When <code>setBounds</code> is called through other routes, * the flag is off and the view repaints normally. Another approach * would be to remove this from the <code>JViewport</code> * class and have the <code>JList</code> manage this case by using * <code>setBackingStoreEnabled</code>. The default is * <code>false</code>. */ protected boolean scrollUnderway = false; /* * Listener that is notified each time the view changes size. */ private ComponentListener viewListener = null; /* Only one <code>ChangeEvent</code> is needed per * <code>JViewport</code> instance since the * event's only (read-only) state is the source property. The source * of events generated here is always "this". */ private transient ChangeEvent changeEvent = null; /** * Use <code>graphics.copyArea</code> to implement scrolling. * This is the fastest for most applications. * * @see #setScrollMode * @since 1.3 */ public static final int BLIT_SCROLL_MODE = 1; /** * Draws viewport contents into an offscreen image. * This was previously the default mode for <code>JTable</code>. * This mode may offer advantages over "blit mode" * in some cases, but it requires a large chunk of extra RAM. * * @see #setScrollMode * @since 1.3 */ public static final int BACKINGSTORE_SCROLL_MODE = 2; /** * This mode uses the very simple method of redrawing the entire * contents of the scrollpane each time it is scrolled. * This was the default behavior in Swing 1.0 and Swing 1.1. * Either of the other two options will provide better performance * in most cases. * * @see #setScrollMode * @since 1.3 */ public static final int SIMPLE_SCROLL_MODE = 0; /** * @see #setScrollMode * @since 1.3 */ private int scrollMode = BLIT_SCROLL_MODE; // // Window blitting: // // As mentioned in the javadoc when using windowBlit a paint event // will be generated by the system if copyArea copies a non-visible // portion of the view (in other words, it copies garbage). We are // not guaranteed to receive the paint event before other mouse events, // so we can not be sure we haven't already copied garbage a bunch of // times to different parts of the view. For that reason when a blit // happens and the Component is obscured (the check for obscurity // is not supported on all platforms and is checked via ComponentPeer // methods) the ivar repaintAll is set to true. When paint is received // if repaintAll is true (we previously did a blit) it is set to // false, and if the clip region is smaller than the viewport // waitingForRepaint is set to true and a timer is started. When // the timer fires if waitingForRepaint is true, repaint is invoked. // In the mean time, if the view is asked to scroll and waitingForRepaint // is true, a blit will not happen, instead the non-backing store case // of scrolling will happen, which will reset waitingForRepaint. // waitingForRepaint is set to false in paint when the clip rect is // bigger (or equal) to the size of the viewport. // A Timer is used instead of just a repaint as it appeared to offer // better performance. /** * This is set to true in <code>setViewPosition</code> * if doing a window blit and the viewport is obscured. */ private transient boolean repaintAll; /** * This is set to true in paint, if <code>repaintAll</code> * is true and the clip rectangle does not match the bounds. * If true, and scrolling happens the * repaint manager is not cleared which then allows for the repaint * previously invoked to succeed. */ private transient boolean waitingForRepaint; /** * Instead of directly invoking repaint, a <code>Timer</code> * is started and when it fires, repaint is invoked. */ private transient Timer repaintTimer; /** * Set to true in paintView when paint is invoked. */ private transient boolean inBlitPaint; /** * Whether or not a valid view has been installed. */ private boolean hasHadValidView; /** Creates a <code>JViewport</code>. */ public JViewport() { super(); setLayout(createLayoutManager()); setOpaque(true); updateUI(); setInheritsPopupMenu(true); } /** * Returns the L&F object that renders this component. * * @return a <code>ViewportUI</code> object * @since 1.3 */ public ViewportUI getUI() { return (ViewportUI)ui; } /** * Sets the L&F object that renders this component. * * @param ui the <code>ViewportUI</code> L&F object * @see UIDefaults#getUI * @beaninfo * bound: true * hidden: true * attribute: visualUpdate true * description: The UI object that implements the Component's LookAndFeel. * @since 1.3 */ public void setUI(ViewportUI ui) { super.setUI(ui); } /** * Resets the UI property to a value from the current look and feel. * * @see JComponent#updateUI */ public void updateUI() { setUI((ViewportUI)UIManager.getUI(this)); } /** * Returns a string that specifies the name of the L&F class * that renders this component. * * @return the string "ViewportUI" * * @see JComponent#getUIClassID * @see UIDefaults#getUI */ public String getUIClassID() { return uiClassID; } /** * Sets the <code>JViewport</code>'s one lightweight child, * which can be <code>null</code>. * (Since there is only one child which occupies the entire viewport, * the <code>constraints</code> and <code>index</code> * arguments are ignored.) * * @param child the lightweight <code>child</code> of the viewport * @param constraints the <code>constraints</code> to be respected * @param index the index * @see #setView */ protected void addImpl(Component child, Object constraints, int index) { setView(child); } /** * Removes the <code>Viewport</code>s one lightweight child. * * @see #setView */ public void remove(Component child) { child.removeComponentListener(viewListener); super.remove(child); } /** * Scrolls the view so that <code>Rectangle</code> * within the view becomes visible. * <p> * This attempts to validate the view before scrolling if the * view is currently not valid - <code>isValid</code> returns false. * To avoid excessive validation when the containment hierarchy is * being created this will not validate if one of the ancestors does not * have a peer, or there is no validate root ancestor, or one of the * ancestors is not a <code>Window</code> or <code>Applet</code>. * <p> * Note that this method will not scroll outside of the * valid viewport; for example, if <code>contentRect</code> is larger * than the viewport, scrolling will be confined to the viewport's * bounds. * * @param contentRect the <code>Rectangle</code> to display * @see JComponent#isValidateRoot * @see java.awt.Component#isValid * @see java.awt.Component#getPeer */ public void scrollRectToVisible(Rectangle contentRect) { Component view = getView(); if (view == null) { return; } else { if (!view.isValid()) { // If the view is not valid, validate. scrollRectToVisible // may fail if the view is not valid first, contentRect // could be bigger than invalid size. validateView(); } int dx, dy; dx = positionAdjustment(getWidth(), contentRect.width, contentRect.x); dy = positionAdjustment(getHeight(), contentRect.height, contentRect.y); if (dx != 0 || dy != 0) { Point viewPosition = getViewPosition(); Dimension viewSize = view.getSize(); int startX = viewPosition.x; int startY = viewPosition.y; Dimension extent = getExtentSize(); viewPosition.x -= dx; viewPosition.y -= dy; // Only constrain the location if the view is valid. If the // the view isn't valid, it typically indicates the view // isn't visible yet and most likely has a bogus size as will // we, and therefore we shouldn't constrain the scrolling if (view.isValid()) { if (getParent().getComponentOrientation().isLeftToRight()) { if (viewPosition.x + extent.width > viewSize.width) { viewPosition.x = Math.max(0, viewSize.width - extent.width); } else if (viewPosition.x < 0) { viewPosition.x = 0; } } else { if (extent.width > viewSize.width) { viewPosition.x = viewSize.width - extent.width; } else { viewPosition.x = Math.max(0, Math.min(viewSize.width - extent.width, viewPosition.x)); } } if (viewPosition.y + extent.height > viewSize.height) { viewPosition.y = Math.max(0, viewSize.height - extent.height); } else if (viewPosition.y < 0) { viewPosition.y = 0; } } if (viewPosition.x != startX || viewPosition.y != startY) { setViewPosition(viewPosition); // NOTE: How JViewport currently works with the // backing store is not foolproof. The sequence of // events when setViewPosition // (scrollRectToVisible) is called is to reset the // views bounds, which causes a repaint on the // visible region and sets an ivar indicating // scrolling (scrollUnderway). When // JViewport.paint is invoked if scrollUnderway is // true, the backing store is blitted. This fails // if between the time setViewPosition is invoked // and paint is received another repaint is queued // indicating part of the view is invalid. There // is no way for JViewport to notice another // repaint has occured and it ends up blitting // what is now a dirty region and the repaint is // never delivered. // It just so happens JTable encounters this // behavior by way of scrollRectToVisible, for // this reason scrollUnderway is set to false // here, which effectively disables the backing // store. scrollUnderway = false; } } } } /** * Ascends the <code>Viewport</code>'s parents stopping when * a component is found that returns * <code>true</code> to <code>isValidateRoot</code>. * If all the <code>Component</code>'s parents are visible, * <code>validate</code> will then be invoked on it. The * <code>RepaintManager</code> is then invoked with * <code>removeInvalidComponent</code>. This * is the synchronous version of a <code>revalidate</code>. */ private void validateView() { Component validateRoot = SwingUtilities.getValidateRoot(this, false); if (validateRoot == null) { return; } // Validate the root. validateRoot.validate(); // And let the RepaintManager it does not have to validate from // validateRoot anymore. RepaintManager rm = RepaintManager.currentManager(this); if (rm != null) { rm.removeInvalidComponent((JComponent)validateRoot); } } /* Used by the scrollRectToVisible method to determine the * proper direction and amount to move by. The integer variables are named * width, but this method is applicable to height also. The code assumes that * parentWidth/childWidth are positive and childAt can be negative. */ private int positionAdjustment(int parentWidth, int childWidth, int childAt) { // +-----+ // | --- | No Change // +-----+ if (childAt >= 0 && childWidth + childAt <= parentWidth) {Lines 1–500 of 1748. Load more
/* * Copyright (c) 1997, 2011, 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 java.awt.*; import java.awt.event.*; import java.awt.image.VolatileImage; import java.awt.peer.ComponentPeer; import java.applet.Applet; import java.beans.Transient; import javax.swing.plaf.ViewportUI; import javax.swing.event.*; import javax.swing.border.*; import javax.accessibility.*; import java.io.Serializable; /** * The "viewport" or "porthole" through which you see the underlying * information. When you scroll, what moves is the viewport. It is like * peering through a camera's viewfinder. Moving the viewfinder upwards * brings new things into view at the top of the picture and loses * things that were at the bottom. * <p> * By default, <code>JViewport</code> is opaque. To change this, use the * <code>setOpaque</code> method. * <p> * <b>NOTE:</b>We have implemented a faster scrolling algorithm that * does not require a buffer to draw in. The algorithm works as follows: * <ol><li>The view and parent view and checked to see if they are * <code>JComponents</code>, * if they aren't, stop and repaint the whole viewport. * <li>If the viewport is obscured by an ancestor, stop and repaint the whole * viewport. * <li>Compute the region that will become visible, if it is as big as * the viewport, stop and repaint the whole view region. * <li>Obtain the ancestor <code>Window</code>'s graphics and * do a <code>copyArea</code> on the scrolled region. * <li>Message the view to repaint the newly visible region. * <li>The next time paint is invoked on the viewport, if the clip region * is smaller than the viewport size a timer is kicked off to repaint the * whole region. * </ol> * In general this approach is much faster. Compared to the backing store * approach this avoids the overhead of maintaining an offscreen buffer and * having to do two <code>copyArea</code>s. * Compared to the non backing store case this * approach will greatly reduce the painted region. * <p> * This approach can cause slower times than the backing store approach * when the viewport is obscured by another window, or partially offscreen. * When another window * obscures the viewport the copyArea will copy garbage and a * paint event will be generated by the system to inform us we need to * paint the newly exposed region. The only way to handle this is to * repaint the whole viewport, which can cause slower performance than the * backing store case. In most applications very rarely will the user be * scrolling while the viewport is obscured by another window or offscreen, * so this optimization is usually worth the performance hit when obscured. * <p> * <strong>Warning:</strong> Swing is not thread safe. For more * information see <a * href="package-summary.html#threading">Swing's Threading * Policy</a>. * <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}. * * @author Hans Muller * @author Philip Milne * @see JScrollPane */ public class JViewport extends JComponent implements Accessible { /** * @see #getUIClassID * @see #readObject */ private static final String uiClassID = "ViewportUI"; /** Property used to indicate window blitting should not be done. */ static final Object EnableWindowBlit = "EnableWindowBlit"; /** * True when the viewport dimensions have been determined. * The default is false. */ protected boolean isViewSizeSet = false; /** * The last <code>viewPosition</code> that we've painted, so we know how * much of the backing store image is valid. */ protected Point lastPaintPosition = null; /** * True when this viewport is maintaining an offscreen image of its * contents, so that some scrolling can take place using fast "bit-blit" * operations instead of by accessing the view object to construct the * display. The default is <code>false</code>. * * @deprecated As of Java 2 platform v1.3 * @see #setScrollMode */ @Deprecated protected boolean backingStore = false; /** The view image used for a backing store. */ transient protected Image backingStoreImage = null; /** * The <code>scrollUnderway</code> flag is used for components like * <code>JList</code>. When the downarrow key is pressed on a * <code>JList</code> and the selected * cell is the last in the list, the <code>scrollpane</code> autoscrolls. * Here, the old selected cell needs repainting and so we need * a flag to make the viewport do the optimized painting * only when there is an explicit call to * <code>setViewPosition(Point)</code>. * When <code>setBounds</code> is called through other routes, * the flag is off and the view repaints normally. Another approach * would be to remove this from the <code>JViewport</code> * class and have the <code>JList</code> manage this case by using * <code>setBackingStoreEnabled</code>. The default is * <code>false</code>. */ protected boolean scrollUnderway = false; /* * Listener that is notified each time the view changes size. */ private ComponentListener viewListener = null; /* Only one <code>ChangeEvent</code> is needed per * <code>JViewport</code> instance since the * event's only (read-only) state is the source property. The source * of events generated here is always "this". */ private transient ChangeEvent changeEvent = null; /** * Use <code>graphics.copyArea</code> to implement scrolling. * This is the fastest for most applications. * * @see #setScrollMode * @since 1.3 */ public static final int BLIT_SCROLL_MODE = 1; /** * Draws viewport contents into an offscreen image. * This was previously the default mode for <code>JTable</code>. * This mode may offer advantages over "blit mode" * in some cases, but it requires a large chunk of extra RAM. * * @see #setScrollMode * @since 1.3 */ public static final int BACKINGSTORE_SCROLL_MODE = 2; /** * This mode uses the very simple method of redrawing the entire * contents of the scrollpane each time it is scrolled. * This was the default behavior in Swing 1.0 and Swing 1.1. * Either of the other two options will provide better performance * in most cases. * * @see #setScrollMode * @since 1.3 */ public static final int SIMPLE_SCROLL_MODE = 0; /** * @see #setScrollMode * @since 1.3 */ private int scrollMode = BLIT_SCROLL_MODE; // // Window blitting: // // As mentioned in the javadoc when using windowBlit a paint event // will be generated by the system if copyArea copies a non-visible // portion of the view (in other words, it copies garbage). We are // not guaranteed to receive the paint event before other mouse events, // so we can not be sure we haven't already copied garbage a bunch of // times to different parts of the view. For that reason when a blit // happens and the Component is obscured (the check for obscurity // is not supported on all platforms and is checked via ComponentPeer // methods) the ivar repaintAll is set to true. When paint is received // if repaintAll is true (we previously did a blit) it is set to // false, and if the clip region is smaller than the viewport // waitingForRepaint is set to true and a timer is started. When // the timer fires if waitingForRepaint is true, repaint is invoked. // In the mean time, if the view is asked to scroll and waitingForRepaint // is true, a blit will not happen, instead the non-backing store case // of scrolling will happen, which will reset waitingForRepaint. // waitingForRepaint is set to false in paint when the clip rect is // bigger (or equal) to the size of the viewport. // A Timer is used instead of just a repaint as it appeared to offer // better performance. /** * This is set to true in <code>setViewPosition</code> * if doing a window blit and the viewport is obscured. */ private transient boolean repaintAll; /** * This is set to true in paint, if <code>repaintAll</code> * is true and the clip rectangle does not match the bounds. * If true, and scrolling happens the * repaint manager is not cleared which then allows for the repaint * previously invoked to succeed. */ private transient boolean waitingForRepaint; /** * Instead of directly invoking repaint, a <code>Timer</code> * is started and when it fires, repaint is invoked. */ private transient Timer repaintTimer; /** * Set to true in paintView when paint is invoked. */ private transient boolean inBlitPaint; /** * Whether or not a valid view has been installed. */ private boolean hasHadValidView; /** Creates a <code>JViewport</code>. */ public JViewport() { super(); setLayout(createLayoutManager()); setOpaque(true); updateUI(); setInheritsPopupMenu(true); } /** * Returns the L&F object that renders this component. * * @return a <code>ViewportUI</code> object * @since 1.3 */ public ViewportUI getUI() { return (ViewportUI)ui; } /** * Sets the L&F object that renders this component. * * @param ui the <code>ViewportUI</code> L&F object * @see UIDefaults#getUI * @beaninfo * bound: true * hidden: true * attribute: visualUpdate true * description: The UI object that implements the Component's LookAndFeel. * @since 1.3 */ public void setUI(ViewportUI ui) { super.setUI(ui); } /** * Resets the UI property to a value from the current look and feel. * * @see JComponent#updateUI */ public void updateUI() { setUI((ViewportUI)UIManager.getUI(this)); } /** * Returns a string that specifies the name of the L&F class * that renders this component. * * @return the string "ViewportUI" * * @see JComponent#getUIClassID * @see UIDefaults#getUI */ public String getUIClassID() { return uiClassID; } /** * Sets the <code>JViewport</code>'s one lightweight child, * which can be <code>null</code>. * (Since there is only one child which occupies the entire viewport, * the <code>constraints</code> and <code>index</code> * arguments are ignored.) * * @param child the lightweight <code>child</code> of the viewport * @param constraints the <code>constraints</code> to be respected * @param index the index * @see #setView */ protected void addImpl(Component child, Object constraints, int index) { setView(child); } /** * Removes the <code>Viewport</code>s one lightweight child. * * @see #setView */ public void remove(Component child) { child.removeComponentListener(viewListener); super.remove(child); } /** * Scrolls the view so that <code>Rectangle</code> * within the view becomes visible. * <p> * This attempts to validate the view before scrolling if the * view is currently not valid - <code>isValid</code> returns false. * To avoid excessive validation when the containment hierarchy is * being created this will not validate if one of the ancestors does not * have a peer, or there is no validate root ancestor, or one of the * ancestors is not a <code>Window</code> or <code>Applet</code>. * <p> * Note that this method will not scroll outside of the * valid viewport; for example, if <code>contentRect</code> is larger * than the viewport, scrolling will be confined to the viewport's * bounds. * * @param contentRect the <code>Rectangle</code> to display * @see JComponent#isValidateRoot * @see java.awt.Component#isValid * @see java.awt.Component#getPeer */ public void scrollRectToVisible(Rectangle contentRect) { Component view = getView(); if (view == null) { return; } else { if (!view.isValid()) { // If the view is not valid, validate. scrollRectToVisible // may fail if the view is not valid first, contentRect // could be bigger than invalid size. validateView(); } int dx, dy; dx = positionAdjustment(getWidth(), contentRect.width, contentRect.x); dy = positionAdjustment(getHeight(), contentRect.height, contentRect.y); if (dx != 0 || dy != 0) { Point viewPosition = getViewPosition(); Dimension viewSize = view.getSize(); int startX = viewPosition.x; int startY = viewPosition.y; Dimension extent = getExtentSize(); viewPosition.x -= dx; viewPosition.y -= dy; // Only constrain the location if the view is valid. If the // the view isn't valid, it typically indicates the view // isn't visible yet and most likely has a bogus size as will // we, and therefore we shouldn't constrain the scrolling if (view.isValid()) { if (getParent().getComponentOrientation().isLeftToRight()) { if (viewPosition.x + extent.width > viewSize.width) { viewPosition.x = Math.max(0, viewSize.width - extent.width); } else if (viewPosition.x < 0) { viewPosition.x = 0; } } else { if (extent.width > viewSize.width) { viewPosition.x = viewSize.width - extent.width; } else { viewPosition.x = Math.max(0, Math.min(viewSize.width - extent.width, viewPosition.x)); } } if (viewPosition.y + extent.height > viewSize.height) { viewPosition.y = Math.max(0, viewSize.height - extent.height); } else if (viewPosition.y < 0) { viewPosition.y = 0; } } if (viewPosition.x != startX || viewPosition.y != startY) { setViewPosition(viewPosition); // NOTE: How JViewport currently works with the // backing store is not foolproof. The sequence of // events when setViewPosition // (scrollRectToVisible) is called is to reset the // views bounds, which causes a repaint on the // visible region and sets an ivar indicating // scrolling (scrollUnderway). When // JViewport.paint is invoked if scrollUnderway is // true, the backing store is blitted. This fails // if between the time setViewPosition is invoked // and paint is received another repaint is queued // indicating part of the view is invalid. There // is no way for JViewport to notice another // repaint has occured and it ends up blitting // what is now a dirty region and the repaint is // never delivered. // It just so happens JTable encounters this // behavior by way of scrollRectToVisible, for // this reason scrollUnderway is set to false // here, which effectively disables the backing // store. scrollUnderway = false; } } } } /** * Ascends the <code>Viewport</code>'s parents stopping when * a component is found that returns * <code>true</code> to <code>isValidateRoot</code>. * If all the <code>Component</code>'s parents are visible, * <code>validate</code> will then be invoked on it. The * <code>RepaintManager</code> is then invoked with * <code>removeInvalidComponent</code>. This * is the synchronous version of a <code>revalidate</code>. */ private void validateView() { Component validateRoot = SwingUtilities.getValidateRoot(this, false); if (validateRoot == null) { return; } // Validate the root. validateRoot.validate(); // And let the RepaintManager it does not have to validate from // validateRoot anymore. RepaintManager rm = RepaintManager.currentManager(this); if (rm != null) { rm.removeInvalidComponent((JComponent)validateRoot); } } /* Used by the scrollRectToVisible method to determine the * proper direction and amount to move by. The integer variables are named * width, but this method is applicable to height also. The code assumes that * parentWidth/childWidth are positive and childAt can be negative. */ private int positionAdjustment(int parentWidth, int childWidth, int childAt) { // +-----+ // | --- | No Change // +-----+ if (childAt >= 0 && childWidth + childAt <= parentWidth) {