jdk/src/share/classes/javax/swing/JViewport.java
author darcy
Fri, 24 Jan 2014 07:16:53 -0800
changeset 22574 7f8ce0c8c20a
parent 21278 ef8a3a2a72f2
child 23006 7cb4567eb213
child 23280 df31f522531f
permissions -rw-r--r--
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes Reviewed-by: alexsch, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21278
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4261
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4261
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4261
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4261
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4261
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
20104
113755166f65 8023966: JViewPort.BLIT_SCROLL_MODE does not work in JLightweightFrame
pchelko
parents: 13234
diff changeset
    28
import sun.swing.JLightweightFrame;
113755166f65 8023966: JViewPort.BLIT_SCROLL_MODE does not work in JLightweightFrame
pchelko
parents: 13234
diff changeset
    29
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.peer.ComponentPeer;
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
    33
import java.beans.Transient;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.plaf.ViewportUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * The "viewport" or "porthole" through which you see the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * information. When you scroll, what moves is the viewport. It is like
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * peering through a camera's viewfinder. Moving the viewfinder upwards
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * brings new things into view at the top of the picture and loses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * things that were at the bottom.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * By default, <code>JViewport</code> is opaque. To change this, use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <code>setOpaque</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <b>NOTE:</b>We have implemented a faster scrolling algorithm that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * does not require a buffer to draw in. The algorithm works as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <ol><li>The view and parent view and checked to see if they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <code>JComponents</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * if they aren't, stop and repaint the whole viewport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <li>If the viewport is obscured by an ancestor, stop and repaint the whole
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * viewport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <li>Compute the region that will become visible, if it is as big as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * the viewport, stop and repaint the whole view region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <li>Obtain the ancestor <code>Window</code>'s graphics and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * do a <code>copyArea</code> on the scrolled region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <li>Message the view to repaint the newly visible region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <li>The next time paint is invoked on the viewport, if the clip region
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * is smaller than the viewport size a timer is kicked off to repaint the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * whole region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * In general this approach is much faster. Compared to the backing store
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * approach this avoids the overhead of maintaining an offscreen buffer and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * having to do two <code>copyArea</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * Compared to the non backing store case this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * approach will greatly reduce the painted region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * This approach can cause slower times than the backing store approach
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * when the viewport is obscured by another window, or partially offscreen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * When another window
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * obscures the viewport the copyArea will copy garbage and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * paint event will be generated by the system to inform us we need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * paint the newly exposed region. The only way to handle this is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * repaint the whole viewport, which can cause slower performance than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * backing store case. In most applications very rarely will the user be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * scrolling while the viewport is obscured by another window or offscreen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * so this optimization is usually worth the performance hit when obscured.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <strong>Warning:</strong> Swing is not thread safe. For more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * information see <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * href="package-summary.html#threading">Swing's Threading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * Policy</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20157
diff changeset
    97
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * @see JScrollPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21278
diff changeset
   105
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
public class JViewport extends JComponent implements Accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @see #getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @see #readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private static final String uiClassID = "ViewportUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /** Property used to indicate window blitting should not be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    static final Object EnableWindowBlit = "EnableWindowBlit";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * True when the viewport dimensions have been determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * The default is false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    protected boolean isViewSizeSet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * The last <code>viewPosition</code> that we've painted, so we know how
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * much of the backing store image is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    protected Point lastPaintPosition = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * True when this viewport is maintaining an offscreen image of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * contents, so that some scrolling can take place using fast "bit-blit"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * operations instead of by accessing the view object to construct the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * display.  The default is <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @deprecated As of Java 2 platform v1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @see #setScrollMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    protected boolean backingStore = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /** The view image used for a backing store. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    transient protected Image backingStoreImage = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * The <code>scrollUnderway</code> flag is used for components like
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <code>JList</code>.  When the downarrow key is pressed on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <code>JList</code> and the selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * cell is the last in the list, the <code>scrollpane</code> autoscrolls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Here, the old selected cell needs repainting and so we need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * a flag to make the viewport do the optimized painting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * only when there is an explicit call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <code>setViewPosition(Point)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * When <code>setBounds</code> is called through other routes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * the flag is off and the view repaints normally.  Another approach
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * would be to remove this from the <code>JViewport</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * class and have the <code>JList</code> manage this case by using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <code>setBackingStoreEnabled</code>.  The default is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    protected boolean scrollUnderway = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Listener that is notified each time the view changes size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private ComponentListener viewListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /* Only one <code>ChangeEvent</code> is needed per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <code>JViewport</code> instance since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * event's only (read-only) state is the source property.  The source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * of events generated here is always "this".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private transient ChangeEvent changeEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
      * Use <code>graphics.copyArea</code> to implement scrolling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
      * This is the fastest for most applications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
      * @see #setScrollMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
      * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public static final int BLIT_SCROLL_MODE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
      * Draws viewport contents into an offscreen image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
      * This was previously the default mode for <code>JTable</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
      * This mode may offer advantages over "blit mode"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
      * in some cases, but it requires a large chunk of extra RAM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
      * @see #setScrollMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
      * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public static final int BACKINGSTORE_SCROLL_MODE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
      * This mode uses the very simple method of redrawing the entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
      * contents of the scrollpane each time it is scrolled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
      * This was the default behavior in Swing 1.0 and Swing 1.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
      * Either of the other two options will provide better performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
      * in most cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
      * @see #setScrollMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
      * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public static final int SIMPLE_SCROLL_MODE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
      * @see #setScrollMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
      * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private int scrollMode = BLIT_SCROLL_MODE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    // Window blitting:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    // As mentioned in the javadoc when using windowBlit a paint event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    // will be generated by the system if copyArea copies a non-visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    // portion of the view (in other words, it copies garbage). We are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    // not guaranteed to receive the paint event before other mouse events,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    // so we can not be sure we haven't already copied garbage a bunch of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    // times to different parts of the view. For that reason when a blit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    // happens and the Component is obscured (the check for obscurity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    // is not supported on all platforms and is checked via ComponentPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    // methods) the ivar repaintAll is set to true. When paint is received
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    // if repaintAll is true (we previously did a blit) it is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    // false, and if the clip region is smaller than the viewport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    // waitingForRepaint is set to true and a timer is started. When
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    // the timer fires if waitingForRepaint is true, repaint is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    // In the mean time, if the view is asked to scroll and waitingForRepaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    // is true, a blit will not happen, instead the non-backing store case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    // of scrolling will happen, which will reset waitingForRepaint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    // waitingForRepaint is set to false in paint when the clip rect is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    // bigger (or equal) to the size of the viewport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    // A Timer is used instead of just a repaint as it appeared to offer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    // better performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * This is set to true in <code>setViewPosition</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * if doing a window blit and the viewport is obscured.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private transient boolean repaintAll;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * This is set to true in paint, if <code>repaintAll</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * is true and the clip rectangle does not match the bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * If true, and scrolling happens the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * repaint manager is not cleared which then allows for the repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * previously invoked to succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    private transient boolean waitingForRepaint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Instead of directly invoking repaint, a <code>Timer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * is started and when it fires, repaint is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    private transient Timer repaintTimer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Set to true in paintView when paint is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    private transient boolean inBlitPaint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Whether or not a valid view has been installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    private boolean hasHadValidView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
11980
da6a2d56e693 7107099: JScrollBar does not show up even if there are enough lebgth of textstring in textField
rupashka
parents: 9035
diff changeset
   269
    /**
da6a2d56e693 7107099: JScrollBar does not show up even if there are enough lebgth of textstring in textField
rupashka
parents: 9035
diff changeset
   270
     * When view is changed we have to synchronize scrollbar values
da6a2d56e693 7107099: JScrollBar does not show up even if there are enough lebgth of textstring in textField
rupashka
parents: 9035
diff changeset
   271
     * with viewport (see the BasicScrollPaneUI#syncScrollPaneWithViewport method).
da6a2d56e693 7107099: JScrollBar does not show up even if there are enough lebgth of textstring in textField
rupashka
parents: 9035
diff changeset
   272
     * This flag allows to invoke that method while ScrollPaneLayout#layoutContainer
da6a2d56e693 7107099: JScrollBar does not show up even if there are enough lebgth of textstring in textField
rupashka
parents: 9035
diff changeset
   273
     * is running.
da6a2d56e693 7107099: JScrollBar does not show up even if there are enough lebgth of textstring in textField
rupashka
parents: 9035
diff changeset
   274
     */
da6a2d56e693 7107099: JScrollBar does not show up even if there are enough lebgth of textstring in textField
rupashka
parents: 9035
diff changeset
   275
    private boolean viewChanged;
da6a2d56e693 7107099: JScrollBar does not show up even if there are enough lebgth of textstring in textField
rupashka
parents: 9035
diff changeset
   276
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /** Creates a <code>JViewport</code>. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public JViewport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        setLayout(createLayoutManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        setOpaque(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        updateUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        setInheritsPopupMenu(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 20104
diff changeset
   289
     * Returns the L&amp;F object that renders this component.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @return a <code>ViewportUI</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public ViewportUI getUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return (ViewportUI)ui;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 20104
diff changeset
   300
     * Sets the L&amp;F object that renders this component.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 20104
diff changeset
   302
     * @param ui  the <code>ViewportUI</code> L&amp;F object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *       hidden: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *  description: The UI object that implements the Component's LookAndFeel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public void setUI(ViewportUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        super.setUI(ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * Resets the UI property to a value from the current look and feel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @see JComponent#updateUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        setUI((ViewportUI)UIManager.getUI(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 20104
diff changeset
   327
     * Returns a string that specifies the name of the L&amp;F class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * that renders this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @return the string "ViewportUI"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return uiClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Sets the <code>JViewport</code>'s one lightweight child,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * which can be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * (Since there is only one child which occupies the entire viewport,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * the <code>constraints</code> and <code>index</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * arguments are ignored.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @param child       the lightweight <code>child</code> of the viewport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @param constraints the <code>constraints</code> to be respected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @param index       the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @see #setView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    protected void addImpl(Component child, Object constraints, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
      setView(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Removes the <code>Viewport</code>s one lightweight child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @see #setView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public void remove(Component child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        child.removeComponentListener(viewListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        super.remove(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
20104
113755166f65 8023966: JViewPort.BLIT_SCROLL_MODE does not work in JLightweightFrame
pchelko
parents: 13234
diff changeset
   367
    @Override
113755166f65 8023966: JViewPort.BLIT_SCROLL_MODE does not work in JLightweightFrame
pchelko
parents: 13234
diff changeset
   368
    public void addNotify() {
113755166f65 8023966: JViewPort.BLIT_SCROLL_MODE does not work in JLightweightFrame
pchelko
parents: 13234
diff changeset
   369
        super.addNotify();
113755166f65 8023966: JViewPort.BLIT_SCROLL_MODE does not work in JLightweightFrame
pchelko
parents: 13234
diff changeset
   370
        // JLightweightFrame does not support BLIT_SCROLL_MODE, so it should be replaced
113755166f65 8023966: JViewPort.BLIT_SCROLL_MODE does not work in JLightweightFrame
pchelko
parents: 13234
diff changeset
   371
        Window rootWindow = SwingUtilities.getWindowAncestor(this);
113755166f65 8023966: JViewPort.BLIT_SCROLL_MODE does not work in JLightweightFrame
pchelko
parents: 13234
diff changeset
   372
        if (rootWindow instanceof JLightweightFrame
113755166f65 8023966: JViewPort.BLIT_SCROLL_MODE does not work in JLightweightFrame
pchelko
parents: 13234
diff changeset
   373
                && getScrollMode() == BLIT_SCROLL_MODE) {
113755166f65 8023966: JViewPort.BLIT_SCROLL_MODE does not work in JLightweightFrame
pchelko
parents: 13234
diff changeset
   374
            setScrollMode(BACKINGSTORE_SCROLL_MODE);
113755166f65 8023966: JViewPort.BLIT_SCROLL_MODE does not work in JLightweightFrame
pchelko
parents: 13234
diff changeset
   375
        }
113755166f65 8023966: JViewPort.BLIT_SCROLL_MODE does not work in JLightweightFrame
pchelko
parents: 13234
diff changeset
   376
    }
113755166f65 8023966: JViewPort.BLIT_SCROLL_MODE does not work in JLightweightFrame
pchelko
parents: 13234
diff changeset
   377
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Scrolls the view so that <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * within the view becomes visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * This attempts to validate the view before scrolling if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * view is currently not valid - <code>isValid</code> returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * To avoid excessive validation when the containment hierarchy is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * being created this will not validate if one of the ancestors does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * have a peer, or there is no validate root ancestor, or one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * ancestors is not a <code>Window</code> or <code>Applet</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Note that this method will not scroll outside of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * valid viewport; for example, if <code>contentRect</code> is larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * than the viewport, scrolling will be confined to the viewport's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param contentRect the <code>Rectangle</code> to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @see JComponent#isValidateRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @see java.awt.Component#isValid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @see java.awt.Component#getPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public void scrollRectToVisible(Rectangle contentRect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        Component view = getView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (view == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            if (!view.isValid()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                // If the view is not valid, validate. scrollRectToVisible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                // may fail if the view is not valid first, contentRect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                // could be bigger than invalid size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                validateView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 466
diff changeset
   412
            int dx, dy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            dx = positionAdjustment(getWidth(), contentRect.width, contentRect.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            dy = positionAdjustment(getHeight(), contentRect.height, contentRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            if (dx != 0 || dy != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                Point viewPosition = getViewPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                Dimension viewSize = view.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                int startX = viewPosition.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                int startY = viewPosition.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                Dimension extent = getExtentSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                viewPosition.x -= dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                viewPosition.y -= dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                // Only constrain the location if the view is valid. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                // the view isn't valid, it typically indicates the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                // isn't visible yet and most likely has a bogus size as will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                // we, and therefore we shouldn't constrain the scrolling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                if (view.isValid()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    if (getParent().getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                        if (viewPosition.x + extent.width > viewSize.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                            viewPosition.x = Math.max(0, viewSize.width - extent.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                        } else if (viewPosition.x < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                            viewPosition.x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        if (extent.width > viewSize.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                            viewPosition.x = viewSize.width - extent.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                            viewPosition.x = Math.max(0, Math.min(viewSize.width - extent.width, viewPosition.x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    if (viewPosition.y + extent.height > viewSize.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        viewPosition.y = Math.max(0, viewSize.height -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                                                  extent.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    else if (viewPosition.y < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        viewPosition.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                if (viewPosition.x != startX || viewPosition.y != startY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    setViewPosition(viewPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    // NOTE: How JViewport currently works with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    // backing store is not foolproof. The sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    // events when setViewPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    // (scrollRectToVisible) is called is to reset the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    // views bounds, which causes a repaint on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    // visible region and sets an ivar indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    // scrolling (scrollUnderway). When
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    // JViewport.paint is invoked if scrollUnderway is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    // true, the backing store is blitted.  This fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    // if between the time setViewPosition is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    // and paint is received another repaint is queued
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    // indicating part of the view is invalid. There
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    // is no way for JViewport to notice another
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   467
                    // repaint has occurred and it ends up blitting
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    // what is now a dirty region and the repaint is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    // never delivered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    // It just so happens JTable encounters this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    // behavior by way of scrollRectToVisible, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    // this reason scrollUnderway is set to false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    // here, which effectively disables the backing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    // store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    scrollUnderway = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * Ascends the <code>Viewport</code>'s parents stopping when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * a component is found that returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * <code>true</code> to <code>isValidateRoot</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * If all the <code>Component</code>'s  parents are visible,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * <code>validate</code> will then be invoked on it. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * <code>RepaintManager</code> is then invoked with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * <code>removeInvalidComponent</code>. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * is the synchronous version of a <code>revalidate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    private void validateView() {
4261
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 1301
diff changeset
   492
        Component validateRoot = SwingUtilities.getValidateRoot(this, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (validateRoot == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        // Validate the root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        validateRoot.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        // And let the RepaintManager it does not have to validate from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        // validateRoot anymore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        RepaintManager rm = RepaintManager.currentManager(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        if (rm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            rm.removeInvalidComponent((JComponent)validateRoot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     /*  Used by the scrollRectToVisible method to determine the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
      *  proper direction and amount to move by. The integer variables are named
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
      *  width, but this method is applicable to height also. The code assumes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
      *  parentWidth/childWidth are positive and childAt can be negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    private int positionAdjustment(int parentWidth, int childWidth, int childAt)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        //   +-----+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        //   | --- |     No Change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        //   +-----+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        if (childAt >= 0 && childWidth + childAt <= parentWidth)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        //   +-----+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        //  ---------   No Change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        //   +-----+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        if (childAt <= 0 && childWidth + childAt >= parentWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        //   +-----+          +-----+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        //   |   ----    ->   | ----|
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        //   +-----+          +-----+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        if (childAt > 0 && childWidth <= parentWidth)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            return -childAt + parentWidth - childWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        //   +-----+             +-----+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        //   |  --------  ->     |--------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        //   +-----+             +-----+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        if (childAt >= 0 && childWidth >= parentWidth)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            return -childAt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        //   +-----+          +-----+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        // ----    |     ->   |---- |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        //   +-----+          +-----+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        if (childAt <= 0 && childWidth <= parentWidth)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            return -childAt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        //   +-----+             +-----+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        //-------- |      ->   --------|
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        //   +-----+             +-----+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        if (childAt < 0 && childWidth >= parentWidth)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            return -childAt + parentWidth - childWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * The viewport "scrolls" its child (called the "view") by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * normal parent/child clipping (typically the view is moved in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * the opposite direction of the scroll).  A non-<code>null</code> border,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * or non-zero insets, isn't supported, to prevent the geometry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * of this component from becoming complex enough to inhibit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * subclassing.  To create a <code>JViewport</code> with a border,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * add it to a <code>JPanel</code> that has a border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * <p>Note:  If <code>border</code> is non-<code>null</code>, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * method will throw an exception as borders are not supported on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * a <code>JViewPort</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @param border the <code>Border</code> to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @exception IllegalArgumentException this method is not implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    public final void setBorder(Border border) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        if (border != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            throw new IllegalArgumentException("JViewport.setBorder() not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * Returns the insets (border) dimensions as (0,0,0,0), since borders
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * are not supported on a <code>JViewport</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   589
     * @return a <code>Rectangle</code> of zero dimension and zero origin
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @see #setBorder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    public final Insets getInsets() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        return new Insets(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * Returns an <code>Insets</code> object containing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * <code>JViewport</code>s inset values.  The passed-in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * <code>Insets</code> object will be reinitialized, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * all existing values within this object are overwritten.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @param insets the <code>Insets</code> object which can be reused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @return this viewports inset values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @see #getInsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *   expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    public final Insets getInsets(Insets insets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        insets.left = insets.top = insets.right = insets.bottom = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        return insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    private Graphics getBackingStoreGraphics(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        Graphics bsg = backingStoreImage.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        bsg.setColor(g.getColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        bsg.setFont(g.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        bsg.setClip(g.getClipBounds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        return bsg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    private void paintViaBackingStore(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        Graphics bsg = getBackingStoreGraphics(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            super.paint(bsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            g.drawImage(backingStoreImage, 0, 0, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            bsg.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    private void paintViaBackingStore(Graphics g, Rectangle oClip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        Graphics bsg = getBackingStoreGraphics(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            super.paint(bsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            g.setClip(oClip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            g.drawImage(backingStoreImage, 0, 0, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            bsg.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * The <code>JViewport</code> overrides the default implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * this method (in <code>JComponent</code>) to return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * This ensures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * that the drawing machinery will call the <code>Viewport</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * <code>paint</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * implementation rather than messaging the <code>JViewport</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * children directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * @return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    public boolean isOptimizedDrawingEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    /**
7005
5e6d24f4303b 6989617: Enable JComponent to control repaintings of its children
alexp
parents: 5506
diff changeset
   660
     * Returns true if scroll mode is a {@code BACKINGSTORE_SCROLL_MODE} to cause
5e6d24f4303b 6989617: Enable JComponent to control repaintings of its children
alexp
parents: 5506
diff changeset
   661
     * painting to originate from {@code JViewport}, or one of its
5e6d24f4303b 6989617: Enable JComponent to control repaintings of its children
alexp
parents: 5506
diff changeset
   662
     * ancestors. Otherwise returns {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *
7005
5e6d24f4303b 6989617: Enable JComponent to control repaintings of its children
alexp
parents: 5506
diff changeset
   664
     * @return true if if scroll mode is a {@code BACKINGSTORE_SCROLL_MODE}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @see JComponent#isPaintingOrigin()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     */
7005
5e6d24f4303b 6989617: Enable JComponent to control repaintings of its children
alexp
parents: 5506
diff changeset
   667
    protected boolean isPaintingOrigin() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 466
diff changeset
   668
        return scrollMode == BACKINGSTORE_SCROLL_MODE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * Only used by the paint method below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    private Point getViewLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        Component view = getView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            return view.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            return new Point(0,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * Depending on whether the <code>backingStore</code> is enabled,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * either paint the image through the backing store or paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * just the recently exposed part, using the backing store
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * to "blit" the remainder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * The term "blit" is the pronounced version of the PDP-10
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * BLT (BLock Transfer) instruction, which copied a block of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * bits. (In case you were curious.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @param g the <code>Graphics</code> context within which to paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    public void paint(Graphics g)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        int width = getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        int height = getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        if ((width <= 0) || (height <= 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        if (inBlitPaint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            // We invoked paint as part of copyArea cleanup, let it through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            super.paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        if (repaintAll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            repaintAll = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            Rectangle clipB = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            if (clipB.width < getWidth() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                clipB.height < getHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                waitingForRepaint = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                if (repaintTimer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    repaintTimer = createRepaintTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                repaintTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                repaintTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                // We really don't need to paint, a future repaint will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                // take care of it, but if we don't we get an ugly flicker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                if (repaintTimer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    repaintTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                waitingForRepaint = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        else if (waitingForRepaint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            // Need a complete repaint before resetting waitingForRepaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            Rectangle clipB = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            if (clipB.width >= getWidth() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                clipB.height >= getHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                waitingForRepaint = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                repaintTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        if (!backingStore || isBlitting() || getView() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            super.paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            lastPaintPosition = getViewLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        // If the view is smaller than the viewport and we are not opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        // (that is, we won't paint our background), we should set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        // clip. Otherwise, as the bounds of the view vary, we will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        // blit garbage into the exposed areas.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        Rectangle viewBounds = getView().getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        if (!isOpaque()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            g.clipRect(0, 0, viewBounds.width, viewBounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        if (backingStoreImage == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            // Backing store is enabled but this is the first call to paint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            // Create the backing store, paint it and then copy to g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            // The backing store image will be created with the size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            // the viewport. We must make sure the clip region is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            // same size, otherwise when scrolling the backing image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            // the region outside of the clipped region will not be painted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            // and result in empty areas.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            backingStoreImage = createImage(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            Rectangle clip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            if (clip.width != width || clip.height != height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                if (!isOpaque()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                    g.setClip(0, 0, Math.min(viewBounds.width, width),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                              Math.min(viewBounds.height, height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                    g.setClip(0, 0, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                paintViaBackingStore(g, clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                paintViaBackingStore(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            if (!scrollUnderway || lastPaintPosition.equals(getViewLocation())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                // No scrolling happened: repaint required area via backing store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                paintViaBackingStore(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                // The image was scrolled. Manipulate the backing store and flush it to g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                Point blitFrom = new Point();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                Point blitTo = new Point();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                Dimension blitSize = new Dimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                Rectangle blitPaint = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                Point newLocation = getViewLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                int dx = newLocation.x - lastPaintPosition.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                int dy = newLocation.y - lastPaintPosition.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                boolean canBlit = computeBlit(dx, dy, blitFrom, blitTo, blitSize, blitPaint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                if (!canBlit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                    // The image was either moved diagonally or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    // moved by more than the image size: paint normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                    paintViaBackingStore(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                    int bdx = blitTo.x - blitFrom.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                    int bdy = blitTo.y - blitFrom.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                    // Move the relevant part of the backing store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                    Rectangle clip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    // We don't want to inherit the clip region when copying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                    // bits, if it is inherited it will result in not moving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    // all of the image resulting in garbage appearing on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    // the screen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    g.setClip(0, 0, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    Graphics bsg = getBackingStoreGraphics(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                        bsg.copyArea(blitFrom.x, blitFrom.y, blitSize.width, blitSize.height, bdx, bdy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                        g.setClip(clip.x, clip.y, clip.width, clip.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                        // Paint the rest of the view; the part that has just been exposed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                        Rectangle r = viewBounds.intersection(blitPaint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                        bsg.setClip(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                        super.paint(bsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                        // Copy whole of the backing store to g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                        g.drawImage(backingStoreImage, 0, 0, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                        bsg.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        lastPaintPosition = getViewLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        scrollUnderway = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * Sets the bounds of this viewport.  If the viewport's width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * or height has changed, fire a <code>StateChanged</code> event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * @param x left edge of the origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * @param y top edge of the origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * @param w width in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * @param h height in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * @see JComponent#reshape(int, int, int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    public void reshape(int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        boolean sizeChanged = (getWidth() != w) || (getHeight() != h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        if (sizeChanged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            backingStoreImage = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        super.reshape(x, y, w, h);
11980
da6a2d56e693 7107099: JScrollBar does not show up even if there are enough lebgth of textstring in textField
rupashka
parents: 9035
diff changeset
   853
        if (sizeChanged || viewChanged) {
da6a2d56e693 7107099: JScrollBar does not show up even if there are enough lebgth of textstring in textField
rupashka
parents: 9035
diff changeset
   854
            viewChanged = false;
da6a2d56e693 7107099: JScrollBar does not show up even if there are enough lebgth of textstring in textField
rupashka
parents: 9035
diff changeset
   855
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
      * Used to control the method of scrolling the viewport contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
      * You may want to change this mode to get maximum performance for your
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
      * use case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
      * @param mode one of the following values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
      * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
      * <li> JViewport.BLIT_SCROLL_MODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
      * <li> JViewport.BACKINGSTORE_SCROLL_MODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
      * <li> JViewport.SIMPLE_SCROLL_MODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
      * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
      * @see #BLIT_SCROLL_MODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
      * @see #BACKINGSTORE_SCROLL_MODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
      * @see #SIMPLE_SCROLL_MODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
      * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
      *        bound: false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
      *  description: Method of moving contents for incremental scrolls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
      *         enum: BLIT_SCROLL_MODE JViewport.BLIT_SCROLL_MODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
      *               BACKINGSTORE_SCROLL_MODE JViewport.BACKINGSTORE_SCROLL_MODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
      *               SIMPLE_SCROLL_MODE JViewport.SIMPLE_SCROLL_MODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
      * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    public void setScrollMode(int mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        scrollMode = mode;
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 466
diff changeset
   888
        backingStore = mode == BACKINGSTORE_SCROLL_MODE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
      * Returns the current scrolling mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
      * @return the <code>scrollMode</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
      * @see #setScrollMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
      * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    public int getScrollMode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        return scrollMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * Returns <code>true</code> if this viewport is maintaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * an offscreen image of its contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * @return <code>true</code> if <code>scrollMode</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     *    <code>BACKINGSTORE_SCROLL_MODE</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * @deprecated As of Java 2 platform v1.3, replaced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     *             <code>getScrollMode()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    public boolean isBackingStoreEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        return scrollMode == BACKINGSTORE_SCROLL_MODE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * If true if this viewport will maintain an offscreen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * image of its contents.  The image is used to reduce the cost
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * of small one dimensional changes to the <code>viewPosition</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * Rather than repainting the entire viewport we use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * <code>Graphics.copyArea</code> to effect some of the scroll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * @param enabled if true, maintain an offscreen backing store
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * @deprecated As of Java 2 platform v1.3, replaced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *             <code>setScrollMode()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    public void setBackingStoreEnabled(boolean enabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        if (enabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            setScrollMode(BACKINGSTORE_SCROLL_MODE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            setScrollMode(BLIT_SCROLL_MODE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 466
diff changeset
   939
    private boolean isBlitting() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        Component view = getView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        return (scrollMode == BLIT_SCROLL_MODE) &&
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 466
diff changeset
   942
               (view instanceof JComponent) && view.isOpaque();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * Returns the <code>JViewport</code>'s one child or <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * @return the viewports child, or <code>null</code> if none exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * @see #setView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    public Component getView() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        return (getComponentCount() > 0) ? getComponent(0) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * Sets the <code>JViewport</code>'s one lightweight child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * (<code>view</code>), which can be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * @param view the viewport's new lightweight child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * @see #getView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    public void setView(Component view) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        /* Remove the viewport's existing children, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
         * Note that removeAll() isn't used here because it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
         * doesn't call remove() (which JViewport overrides).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        int n = getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        for(int i = n - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            remove(getComponent(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        isViewSizeSet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            super.addImpl(view, null, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            viewListener = createViewListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            view.addComponentListener(viewListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        if (hasHadValidView) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            // Only fire a change if a view has been installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        else if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            hasHadValidView = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
11980
da6a2d56e693 7107099: JScrollBar does not show up even if there are enough lebgth of textstring in textField
rupashka
parents: 9035
diff changeset
   992
        viewChanged = true;
da6a2d56e693 7107099: JScrollBar does not show up even if there are enough lebgth of textstring in textField
rupashka
parents: 9035
diff changeset
   993
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * If the view's size hasn't been explicitly set, return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * preferred size, otherwise return the view's current size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * If there is no view, return 0,0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * @return a <code>Dimension</code> object specifying the size of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    public Dimension getViewSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        Component view = getView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        if (view == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            return new Dimension(0,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        else if (isViewSizeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            return view.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            return view.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * Sets the size of the view.  A state changed event will be fired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * @param newSize a <code>Dimension</code> object specifying the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     *          size of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    public void setViewSize(Dimension newSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        Component view = getView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            Dimension oldSize = view.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            if (!newSize.equals(oldSize)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                // scrollUnderway will be true if this is invoked as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                // result of a validate and setViewPosition was previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                // invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                scrollUnderway = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                view.setSize(newSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                isViewSizeSet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * Returns the view coordinates that appear in the upper left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * hand corner of the viewport, or 0,0 if there's no view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * @return a <code>Point</code> object giving the upper left coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    public Point getViewPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        Component view = getView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            Point p = view.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            p.x = -p.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            p.y = -p.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            return new Point(0,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * Sets the view coordinates that appear in the upper left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * hand corner of the viewport, does nothing if there's no view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * @param p  a <code>Point</code> object giving the upper left coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    public void setViewPosition(Point p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        Component view = getView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        if (view == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        int oldX, oldY, x = p.x, y = p.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        /* Collect the old x,y values for the views location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
         * and do the song and dance to avoid allocating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
         * a Rectangle object if we don't have to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        if (view instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            JComponent c = (JComponent)view;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            oldX = c.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            oldY = c.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            Rectangle r = view.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            oldX = r.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            oldY = r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        /* The view scrolls in the opposite direction to mouse
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
         * movement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        int newX = -x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        int newY = -y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        if ((oldX != newX) || (oldY != newY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            if (!waitingForRepaint && isBlitting() && canUseWindowBlitter()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                RepaintManager rm = RepaintManager.currentManager(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                // The cast to JComponent will work, if view is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                // a JComponent, isBlitting will return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                JComponent jview = (JComponent)view;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                Rectangle dirty = rm.getDirtyRegion(jview);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                if (dirty == null || !dirty.contains(jview.getVisibleRect())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                    rm.beginPaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                        Graphics g = JComponent.safelyGetGraphics(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                        flushViewDirtyRegion(g, dirty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                        view.setLocation(newX, newY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                        g.setClip(0,0,getWidth(), Math.min(getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                                                           jview.getHeight()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                        // Repaint the complete component if the blit succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                        // and needsRepaintAfterBlit returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                        repaintAll = (windowBlitPaint(g) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                                      needsRepaintAfterBlit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                        g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                        rm.markCompletelyClean((JComponent)getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                        rm.markCompletelyClean(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                        rm.markCompletelyClean(jview);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                        rm.endPaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                    // The visible region is dirty, no point in doing copyArea
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                    view.setLocation(newX, newY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                    repaintAll = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                scrollUnderway = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                // This calls setBounds(), and then repaint().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                view.setLocation(newX, newY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                repaintAll = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            }
8524
f2fac94f2434 6826074: JScrollPane does not revalidate the component hierarchy after scrolling
alexp
parents: 7668
diff changeset
  1138
            // we must validate the hierarchy to not break the hw/lw mixing
f2fac94f2434 6826074: JScrollPane does not revalidate the component hierarchy after scrolling
alexp
parents: 7668
diff changeset
  1139
            revalidate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * Returns a rectangle whose origin is <code>getViewPosition</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * and size is <code>getExtentSize</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * This is the visible part of the view, in view coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * @return a <code>Rectangle</code> giving the visible part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     *          the view using view coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    public Rectangle getViewRect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        return new Rectangle(getViewPosition(), getExtentSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * Computes the parameters for a blit where the backing store image
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * currently contains <code>oldLoc</code> in the upper left hand corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * and we're scrolling to <code>newLoc</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * The parameters are modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * to return the values required for the blit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * @param dx  the horizontal delta
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * @param dy  the vertical delta
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * @param blitFrom the <code>Point</code> we're blitting from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * @param blitTo the <code>Point</code> we're blitting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * @param blitSize the <code>Dimension</code> of the area to blit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * @param blitPaint the area to blit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * @return  true if the parameters are modified and we're ready to blit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     *          false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    protected boolean computeBlit(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        int dx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        int dy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        Point blitFrom,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        Point blitTo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        Dimension blitSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        Rectangle blitPaint)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        int dxAbs = Math.abs(dx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        int dyAbs = Math.abs(dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        Dimension extentSize = getExtentSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        if ((dx == 0) && (dy != 0) && (dyAbs < extentSize.height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            if (dy < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                blitFrom.y = -dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                blitTo.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                blitPaint.y = extentSize.height + dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                blitFrom.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                blitTo.y = dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                blitPaint.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            blitPaint.x = blitFrom.x = blitTo.x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            blitSize.width = extentSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            blitSize.height = extentSize.height - dyAbs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            blitPaint.width = extentSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            blitPaint.height = dyAbs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        else if ((dy == 0) && (dx != 0) && (dxAbs < extentSize.width)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            if (dx < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                blitFrom.x = -dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                blitTo.x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                blitPaint.x = extentSize.width + dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                blitFrom.x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                blitTo.x = dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                blitPaint.x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            blitPaint.y = blitFrom.y = blitTo.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            blitSize.width = extentSize.width - dxAbs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            blitSize.height = extentSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            blitPaint.width = dxAbs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            blitPaint.height = extentSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * Returns the size of the visible part of the view in view coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * @return a <code>Dimension</code> object giving the size of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     */
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
  1243
    @Transient
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    public Dimension getExtentSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        return getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * Converts a size in pixel coordinates to view coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * Subclasses of viewport that support "logical coordinates"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * will override this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * @param size  a <code>Dimension</code> object using pixel coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * @return a <code>Dimension</code> object converted to view coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    public Dimension toViewCoordinates(Dimension size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        return new Dimension(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * Converts a point in pixel coordinates to view coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * Subclasses of viewport that support "logical coordinates"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * will override this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * @param p  a <code>Point</code> object using pixel coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * @return a <code>Point</code> object converted to view coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    public Point toViewCoordinates(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        return new Point(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * Sets the size of the visible part of the view using view coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * @param newExtent  a <code>Dimension</code> object specifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     *          the size of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    public void setExtentSize(Dimension newExtent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        Dimension oldExtent = getExtentSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        if (!newExtent.equals(oldExtent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
            setSize(newExtent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * A listener for the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20157
diff changeset
  1296
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21278
diff changeset
  1300
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    protected class ViewListener extends ComponentAdapter implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        public void componentResized(ComponentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * Creates a listener for the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * @return a <code>ViewListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    protected ViewListener createViewListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        return new ViewListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * Subclassers can override this to install a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * layout manager (or <code>null</code>) in the constructor.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * the <code>LayoutManager</code> to install on the <code>JViewport</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * @return a <code>LayoutManager</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    protected LayoutManager createLayoutManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        return ViewportLayout.SHARED_INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * Adds a <code>ChangeListener</code> to the list that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * notified each time the view's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * size, position, or the viewport's extent size has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * @param l the <code>ChangeListener</code> to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * @see #removeChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * @see #setViewPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * @see #setViewSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * @see #setExtentSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    public void addChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        listenerList.add(ChangeListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * Removes a <code>ChangeListener</code> from the list that's notified each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * time the views size, position, or the viewports extent size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * @param l the <code>ChangeListener</code> to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * @see #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    public void removeChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        listenerList.remove(ChangeListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * Returns an array of all the <code>ChangeListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * to this JViewport with addChangeListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * @return all of the <code>ChangeListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    public ChangeListener[] getChangeListeners() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 466
diff changeset
  1366
        return listenerList.getListeners(ChangeListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * Notifies all <code>ChangeListeners</code> when the views
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * size, position, or the viewports extent size has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * @see #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * @see #removeChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    protected void fireStateChanged()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            if (listeners[i] == ChangeListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                if (changeEvent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                    changeEvent = new ChangeEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                ((ChangeListener)listeners[i + 1]).stateChanged(changeEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * Always repaint in the parents coordinate system to make sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * only one paint is performed by the <code>RepaintManager</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * @param     tm   maximum time in milliseconds before update
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * @param     x    the <code>x</code> coordinate (pixels over from left)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * @param     y    the <code>y</code> coordinate (pixels down from top)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * @param     w    the width
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * @param     h   the height
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * @see       java.awt.Component#update(java.awt.Graphics)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    public void repaint(long tm, int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        Container parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        if(parent != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
            parent.repaint(tm,x+getX(),y+getY(),w,h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            super.repaint(tm,x,y,w,h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * Returns a string representation of this <code>JViewport</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     * is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * implementations. The returned string may be empty but may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     * @return  a string representation of this <code>JViewport</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        String isViewSizeSetString = (isViewSizeSet ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                                      "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        String lastPaintPositionString = (lastPaintPosition != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                                          lastPaintPosition.toString() : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        String scrollUnderwayString = (scrollUnderway ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                                       "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        return super.paramString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        ",isViewSizeSet=" + isViewSizeSetString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        ",lastPaintPosition=" + lastPaintPositionString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        ",scrollUnderway=" + scrollUnderwayString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    // Following is used when doBlit is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * Notifies listeners of a property change. This is subclassed to update
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * the <code>windowBlit</code> property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     * (The <code>putClientProperty</code> property is final).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * @param propertyName a string containing the property name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     * @param oldValue the old value of the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     * @param newValue  the new value of the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    protected void firePropertyChange(String propertyName, Object oldValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                                      Object newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        super.firePropertyChange(propertyName, oldValue, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        if (propertyName.equals(EnableWindowBlit)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            if (newValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                setScrollMode(BLIT_SCROLL_MODE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                setScrollMode(SIMPLE_SCROLL_MODE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     * Returns true if the component needs to be completely repainted after
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     * a blit and a paint is received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    private boolean needsRepaintAfterBlit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        // Find the first heavy weight ancestor. isObscured and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        // canDetermineObscurity are only appropriate for heavy weights.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        Component heavyParent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        while (heavyParent != null && heavyParent.isLightweight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            heavyParent = heavyParent.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        if (heavyParent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            ComponentPeer peer = heavyParent.getPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            if (peer != null && peer.canDetermineObscurity() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                                !peer.isObscured()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                // The peer says we aren't obscured, therefore we can assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                // that we won't later be messaged to paint a portion that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                // we tried to blit that wasn't valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                // It is certainly possible that when we blited we were
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                // obscured, and by the time this is invoked we aren't, but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                // chances of that happening are pretty slim.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
    private Timer createRepaintTimer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        Timer timer = new Timer(300, new ActionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            public void actionPerformed(ActionEvent ae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                // waitingForRepaint will be false if a paint came down
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                // with the complete clip rect, in which case we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                // have to cause a repaint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
                if (waitingForRepaint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                    repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        timer.setRepeats(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        return timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     * If the repaint manager has a dirty region for the view, the view is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     * asked to paint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     * @param g  the <code>Graphics</code> context within which to paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
    private void flushViewDirtyRegion(Graphics g, Rectangle dirty) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        JComponent view = (JComponent) getView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        if(dirty != null && dirty.width > 0 && dirty.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
            dirty.x += view.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
            dirty.y += view.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
            Rectangle clip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            if (clip == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                // Only happens in 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                g.setClip(0, 0, getWidth(), getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            g.clipRect(dirty.x, dirty.y, dirty.width, dirty.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            clip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            // Only paint the dirty region if it is visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            if (clip.width > 0 && clip.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                paintView(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * Used when blitting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * @param g  the <code>Graphics</code> context within which to paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * @return true if blitting succeeded; otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
    private boolean windowBlitPaint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        int width = getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        int height = getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        if ((width == 0) || (height == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        boolean retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        RepaintManager rm = RepaintManager.currentManager(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        JComponent view = (JComponent) getView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        if (lastPaintPosition == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            lastPaintPosition.equals(getViewLocation())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
            paintView(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            retValue = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
            // The image was scrolled. Manipulate the backing store and flush
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            // it to g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            Point blitFrom = new Point();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
            Point blitTo = new Point();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            Dimension blitSize = new Dimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
            Rectangle blitPaint = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
            Point newLocation = getViewLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            int dx = newLocation.x - lastPaintPosition.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
            int dy = newLocation.y - lastPaintPosition.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
            boolean canBlit = computeBlit(dx, dy, blitFrom, blitTo, blitSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                                          blitPaint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            if (!canBlit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                paintView(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                retValue = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                // Prepare the rest of the view; the part that has just been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                // exposed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                Rectangle r = view.getBounds().intersection(blitPaint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                r.x -= view.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                r.y -= view.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                blitDoubleBuffered(view, g, r.x, r.y, r.width, r.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                                   blitFrom.x, blitFrom.y, blitTo.x, blitTo.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                                   blitSize.width, blitSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                retValue = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        lastPaintPosition = getViewLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
    // NOTE: the code below uses paintForceDoubleBuffered for historical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
    // reasons.  If we're going to allow a blit we've already accounted for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    // everything that paintImmediately and _paintImmediately does, for that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
    // reason we call into paintForceDoubleBuffered to diregard whether or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
    // not setDoubleBuffered(true) was invoked on the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
    private void blitDoubleBuffered(JComponent view, Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                                    int clipX, int clipY, int clipW, int clipH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                                    int blitFromX, int blitFromY, int blitToX, int blitToY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                                    int blitW, int blitH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        // NOTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        //   blitFrom/blitTo are in JViewport coordinates system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        //     not the views coordinate space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        //   clip* are in the views coordinate space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        RepaintManager rm = RepaintManager.currentManager(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        int bdx = blitToX - blitFromX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        int bdy = blitToY - blitFromY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
13234
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 11980
diff changeset
  1604
        Composite oldComposite = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        // Shift the scrolled region
13234
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 11980
diff changeset
  1606
        if (g instanceof Graphics2D) {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 11980
diff changeset
  1607
            Graphics2D g2d = (Graphics2D) g;
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 11980
diff changeset
  1608
            oldComposite = g2d.getComposite();
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 11980
diff changeset
  1609
            g2d.setComposite(AlphaComposite.Src);
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 11980
diff changeset
  1610
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        rm.copyArea(this, g, blitFromX, blitFromY, blitW, blitH, bdx, bdy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                    false);
13234
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 11980
diff changeset
  1613
        if (oldComposite != null) {
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 11980
diff changeset
  1614
            ((Graphics2D) g).setComposite(oldComposite);
9437e5985d73 7124513: [macosx] Support NSTexturedBackgroundWindowMask/different titlebar styles to create unified toolbar
serb
parents: 11980
diff changeset
  1615
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        // Paint the newly exposed region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        int x = view.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        int y = view.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        g.translate(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
        g.setClip(clipX, clipY, clipW, clipH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        view.paintForceDoubleBuffered(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        g.translate(-x, -y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     * Called to paint the view, usually when <code>blitPaint</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     * can not blit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * @param g the <code>Graphics</code> context within which to paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
    private void paintView(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        Rectangle clip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        JComponent view = (JComponent)getView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
        if (view.getWidth() >= getWidth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
            // Graphics is relative to JViewport, need to map to view's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
            // coordinates space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
            int x = view.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
            int y = view.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            g.translate(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            g.setClip(clip.x - x, clip.y - y, clip.width, clip.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            view.paintForceDoubleBuffered(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            g.translate(-x, -y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            g.setClip(clip.x, clip.y, clip.width, clip.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            // To avoid any problems that may result from the viewport being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
            // bigger than the view we start painting from the viewport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                inBlitPaint = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                paintForceDoubleBuffered(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                inBlitPaint = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     * Returns true if the viewport is not obscured by one of its ancestors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     * or its ancestors children and if the viewport is showing. Blitting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * when the view isn't showing will work,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     * or rather <code>copyArea</code> will work,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     * but will not produce the expected behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
    private boolean canUseWindowBlitter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        if (!isShowing() || (!(getParent() instanceof JComponent) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                             !(getView() instanceof JComponent))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        if (isPainting()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
            // We're in the process of painting, don't blit. If we were
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
            // to blit we would draw on top of what we're already drawing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
            // so bail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        Rectangle dirtyRegion = RepaintManager.currentManager(this).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                                getDirtyRegion((JComponent)getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        if (dirtyRegion != null && dirtyRegion.width > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            dirtyRegion.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            // Part of the scrollpane needs to be repainted too, don't blit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        Rectangle clip = new Rectangle(0,0,getWidth(),getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
        Rectangle oldClip = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        Rectangle tmp2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
        Container parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        Component lastParent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        int x, y, w, h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        for(parent = this; parent != null && isLightweightComponent(parent); parent = parent.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
            x = parent.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            y = parent.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
            w = parent.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
            h = parent.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
            oldClip.setBounds(clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
            SwingUtilities.computeIntersection(0, 0, w, h, clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
            if(!clip.equals(oldClip))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            if(lastParent != null && parent instanceof JComponent &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
               !((JComponent)parent).isOptimizedDrawingEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                Component comps[] = parent.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                for(int i = comps.length - 1 ;i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                    if(comps[i] == lastParent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                        index = i - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                while(index >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                    tmp2 = comps[index].getBounds(tmp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                    if(tmp2.intersects(clip))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                    index--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
            clip.x += x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
            clip.y += y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
            lastParent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
        if (parent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
            // No Window parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     * Gets the AccessibleContext associated with this JViewport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     * For viewports, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     * AccessibleJViewport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     * A new AccessibleJViewport instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     * @return an AccessibleJViewport that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     *         AccessibleContext of this JViewport
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            accessibleContext = new AccessibleJViewport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * <code>JViewport</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * Java Accessibility API appropriate to viewport user-interface elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20157
diff changeset
  1766
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21278
diff changeset
  1770
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
    protected class AccessibleJViewport extends AccessibleJComponent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
         * @return an instance of AccessibleRole describing the role of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
         * the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
            return AccessibleRole.VIEWPORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
    } // inner class AccessibleJViewport
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
}