jdk/src/share/classes/javax/swing/RepaintManager.java
author lana
Thu, 29 Jan 2009 09:25:47 -0800
changeset 1853 63c1129add13
parent 1639 a97859015238
parent 1832 0a885bee1070
child 2460 15e4d52f9ed4
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1639
a97859015238 6785258: Update copyright year
xdono
parents: 1301
diff changeset
     2
 * Copyright 1997-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.peer.ComponentPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.peer.ContainerPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.image.VolatileImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.applet.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.awt.DisplayChangedListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.java2d.SunGraphicsEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
1291
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
    43
import com.sun.java.swing.SwingUtilities3;
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
    44
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This class manages repaint requests, allowing the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * of repaints to be minimized, for example by collapsing multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * requests into a single repaint for members of a component tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * As of 1.6 <code>RepaintManager</code> handles repaint requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * for Swing's top level components (<code>JApplet</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <code>JWindow</code>, <code>JFrame</code> and <code>JDialog</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Any calls to <code>repaint</code> on one of these will call into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * appropriate <code>addDirtyRegion</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author Arnaud Weber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class RepaintManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Whether or not the RepaintManager should handle paint requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * for top levels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static final boolean HANDLE_TOP_LEVEL_PAINT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static final short BUFFER_STRATEGY_NOT_SPECIFIED = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static final short BUFFER_STRATEGY_SPECIFIED_ON = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final short BUFFER_STRATEGY_SPECIFIED_OFF = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final short BUFFER_STRATEGY_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Maps from GraphicsConfiguration to VolatileImage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private Map<GraphicsConfiguration,VolatileImage> volatileMap = new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                        HashMap<GraphicsConfiguration,VolatileImage>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // As of 1.6 Swing handles scheduling of paint events from native code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // That is, SwingPaintEventDispatcher is invoked on the toolkit thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // which in turn invokes nativeAddDirtyRegion.  Because this is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // from the native thread we can not invoke any public methods and so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // we introduce these added maps.  So, any time nativeAddDirtyRegion is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // invoked the region is added to hwDirtyComponents and a work request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // is scheduled.  When the work request is processed all entries in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // this map are pushed to the real map (dirtyComponents) and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // painted with the rest of the components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private Map<Container,Rectangle> hwDirtyComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private Map<Component,Rectangle> dirtyComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private Map<Component,Rectangle> tmpDirtyComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private java.util.List<Component> invalidComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // List of Runnables that need to be processed before painting from AWT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private java.util.List<Runnable> runnableList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    boolean   doubleBufferingEnabled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private Dimension doubleBufferMaxSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // Support for both the standard and volatile offscreen buffers exists to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // provide backwards compatibility for the [rare] programs which may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // calling getOffScreenBuffer() and not expecting to get a VolatileImage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    // Swing internally is migrating to use *only* the volatile image buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // Support for standard offscreen buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    DoubleBufferInfo standardDoubleBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Object responsible for hanlding core paint functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private PaintManager paintManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private static final Object repaintManagerKey = RepaintManager.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // Whether or not a VolatileImage should be used for double-buffered painting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static boolean volatileImageBufferEnabled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Value of the system property awt.nativeDoubleBuffering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private static boolean nativeDoubleBuffering;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    // The maximum number of times Swing will attempt to use the VolatileImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    // buffer during a paint operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private static final int VOLATILE_LOOP_MAX = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Number of <code>beginPaint</code> that have been invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private int paintDepth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Type of buffer strategy to use.  Will be one of the BUFFER_STRATEGY_
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * constants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private short bufferStrategyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // BufferStrategyPaintManager has the unique characteristic that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    // must deal with the buffer being lost while painting to it.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    // example, if we paint a component and show it and the buffer has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    // become lost we must repaint the whole window.  To deal with that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    // the PaintManager calls into repaintRoot, and if we're still in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    // the process of painting the repaintRoot field is set to the JRootPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    // and after the current JComponent.paintImmediately call finishes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    // paintImmediately will be invoked on the repaintRoot.  In this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    // way we don't try to show garbage to the screen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * True if we're in the process of painting the dirty regions.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * set to true in <code>paintDirtyRegions</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private boolean painting;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * If the PaintManager calls into repaintRoot during painting this field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * will be set to the root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private JComponent repaintRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * The Thread that has initiated painting.  If null it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * indicates painting is not currently in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private Thread paintThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Runnable used to process all repaint/revalidate requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private final ProcessingRunnable processingRunnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        volatileImageBufferEnabled = "true".equals(AccessController.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                doPrivileged(new GetPropertyAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                "swing.volatileImageBufferEnabled", "true")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        boolean headless = GraphicsEnvironment.isHeadless();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (volatileImageBufferEnabled && headless) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            volatileImageBufferEnabled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        nativeDoubleBuffering = "true".equals(AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    new GetPropertyAction("awt.nativeDoubleBuffering")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        String bs = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                          new GetPropertyAction("swing.bufferPerWindow"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (headless) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            BUFFER_STRATEGY_TYPE = BUFFER_STRATEGY_SPECIFIED_OFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        else if (bs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            BUFFER_STRATEGY_TYPE = BUFFER_STRATEGY_NOT_SPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        else if ("true".equals(bs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            BUFFER_STRATEGY_TYPE = BUFFER_STRATEGY_SPECIFIED_ON;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            BUFFER_STRATEGY_TYPE = BUFFER_STRATEGY_SPECIFIED_OFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        HANDLE_TOP_LEVEL_PAINT = "true".equals(AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
               new GetPropertyAction("swing.handleTopLevelPaint", "true")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        GraphicsEnvironment ge = GraphicsEnvironment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                getLocalGraphicsEnvironment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (ge instanceof SunGraphicsEnvironment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            ((SunGraphicsEnvironment)ge).addDisplayChangedListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    new DisplayChangedHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Return the RepaintManager for the calling thread given a Component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param c a Component -- unused in the default implementation, but could
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *          be used by an overridden version to return a different RepaintManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *          depending on the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @return the RepaintManager object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public static RepaintManager currentManager(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        // Note: DisplayChangedRunnable passes in null as the component, so if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        // component is ever used to determine the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // RepaintManager, DisplayChangedRunnable will need to be modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        // accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return currentManager(AppContext.getAppContext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Returns the RepaintManager for the specified AppContext.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * a RepaintManager has not been created for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * AppContext this will return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    static RepaintManager currentManager(AppContext appContext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        RepaintManager rm = (RepaintManager)appContext.get(repaintManagerKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (rm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            rm = new RepaintManager(BUFFER_STRATEGY_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            appContext.put(repaintManagerKey, rm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return rm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Return the RepaintManager for the calling thread given a JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    * Note: This method exists for backward binary compatibility with earlier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * versions of the Swing library. It simply returns the result returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * {@link #currentManager(Component)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param c a JComponent -- unused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @return the RepaintManager object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public static RepaintManager currentManager(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return currentManager((Component)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Set the RepaintManager that should be used for the calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * thread. <b>aRepaintManager</b> will become the current RepaintManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * for the calling thread's thread group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @param aRepaintManager  the RepaintManager object to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public static void setCurrentManager(RepaintManager aRepaintManager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (aRepaintManager != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            SwingUtilities.appContextPut(repaintManagerKey, aRepaintManager);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            SwingUtilities.appContextRemove(repaintManagerKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Create a new RepaintManager instance. You rarely call this constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * directly. To get the default RepaintManager, use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * RepaintManager.currentManager(JComponent) (normally "this").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public RepaintManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // Because we can't know what a subclass is doing with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // volatile image we immediately punt in subclasses.  If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        // poses a problem we'll need a more sophisticated detection algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // or API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        this(BUFFER_STRATEGY_SPECIFIED_OFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private RepaintManager(short bufferStrategyType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // If native doublebuffering is being used, do NOT use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // Swing doublebuffering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        doubleBufferingEnabled = !nativeDoubleBuffering;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            dirtyComponents = new IdentityHashMap<Component,Rectangle>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            tmpDirtyComponents = new IdentityHashMap<Component,Rectangle>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            this.bufferStrategyType = bufferStrategyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            hwDirtyComponents = new IdentityHashMap<Container,Rectangle>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        processingRunnable = new ProcessingRunnable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    private void displayChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        clearImages();
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
     * Mark the component as in need of layout and queue a runnable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * for the event dispatching thread that will validate the components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * first isValidateRoot() ancestor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @see JComponent#isValidateRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @see #removeInvalidComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public synchronized void addInvalidComponent(JComponent invalidComponent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    {
1291
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   308
        RepaintManager delegate = getDelegate(invalidComponent);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   309
        if (delegate != null) {
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   310
            delegate.addInvalidComponent(invalidComponent);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   311
            return;
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   312
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        Component validateRoot = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        /* Find the first JComponent ancestor of this component whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         * isValidateRoot() method returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        for(Component c = invalidComponent; c != null; c = c.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if ((c instanceof CellRendererPane) || (c.getPeer() == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if ((c instanceof JComponent) && (((JComponent)c).isValidateRoot())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                validateRoot = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        /* There's no validateRoot to apply validate to, so we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if (validateRoot == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        /* If the validateRoot and all of its ancestors aren't visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         * then we don't do anything.  While we're walking up the tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         * we find the root Window or Applet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        Component root = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        for(Component c = validateRoot; c != null; c = c.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            if (!c.isVisible() || (c.getPeer() == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            if ((c instanceof Window) || (c instanceof Applet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                root = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if (root == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        /* Lazily create the invalidateComponents vector and add the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
         * validateRoot if it's not there already.  If this validateRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
         * is already in the vector, we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (invalidComponents == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            invalidComponents = new ArrayList<Component>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            int n = invalidComponents.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            for(int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                if(validateRoot == invalidComponents.get(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        invalidComponents.add(validateRoot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        // Queue a Runnable to invoke paintDirtyRegions and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        // validateInvalidComponents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        scheduleProcessingRunnable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Remove a component from the list of invalid components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @see #addInvalidComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public synchronized void removeInvalidComponent(JComponent component) {
1291
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   383
        RepaintManager delegate = getDelegate(component);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   384
        if (delegate != null) {
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   385
            delegate.removeInvalidComponent(component);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   386
            return;
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   387
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if(invalidComponents != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            int index = invalidComponents.indexOf(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            if(index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                invalidComponents.remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Add a component in the list of components that should be refreshed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * If <i>c</i> already has a dirty region, the rectangle <i>(x,y,w,h)</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * will be unioned with the region that should be redrawn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @see JComponent#repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    private void addDirtyRegion0(Container c, int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        /* Special cases we don't have to bother with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if ((w <= 0) || (h <= 0) || (c == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if ((c.getWidth() <= 0) || (c.getHeight() <= 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (extendDirtyRegion(c, x, y, w, h)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            // Component was already marked as dirty, region has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            // extended, no need to continue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        /* Make sure that c and all it ancestors (up to an Applet or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
         * Window) are visible.  This loop has the same effect as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
         * checking c.isShowing() (and note that it's still possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
         * that c is completely obscured by an opaque ancestor in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
         * the specified rectangle).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        Component root = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        // Note: We can't synchronize around this, Frame.getExtendedState
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        // is synchronized so that if we were to synchronize around this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        // it could lead to the possibility of getting locks out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        // of order and deadlocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        for (Container p = c; p != null; p = p.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            if (!p.isVisible() || (p.getPeer() == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            if ((p instanceof Window) || (p instanceof Applet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                // Iconified frames are still visible!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                if (p instanceof Frame &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        (((Frame)p).getExtendedState() & Frame.ICONIFIED) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                                    Frame.ICONIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                root = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (root == null) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (extendDirtyRegion(c, x, y, w, h)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                // In between last check and this check another thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                // queued up runnable, can bail here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            dirtyComponents.put(c, new Rectangle(x, y, w, h));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        // Queue a Runnable to invoke paintDirtyRegions and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        // validateInvalidComponents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        scheduleProcessingRunnable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * Add a component in the list of components that should be refreshed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * If <i>c</i> already has a dirty region, the rectangle <i>(x,y,w,h)</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * will be unioned with the region that should be redrawn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @param c Component to repaint, null results in nothing happening.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @param x X coordinate of the region to repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @param y Y coordinate of the region to repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @param w Width of the region to repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @param h Height of the region to repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @see JComponent#repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public void addDirtyRegion(JComponent c, int x, int y, int w, int h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    {
1291
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   479
        RepaintManager delegate = getDelegate(c);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   480
        if (delegate != null) {
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   481
            delegate.addDirtyRegion(c, x, y, w, h);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   482
            return;
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   483
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        addDirtyRegion0(c, x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Adds <code>window</code> to the list of <code>Component</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * need to be repainted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @param window Window to repaint, null results in nothing happening.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @param x X coordinate of the region to repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @param y Y coordinate of the region to repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @param w Width of the region to repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @param h Height of the region to repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @see JFrame#repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @see JWindow#repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @see JDialog#repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public void addDirtyRegion(Window window, int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        addDirtyRegion0(window, x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * Adds <code>applet</code> to the list of <code>Component</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * need to be repainted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @param applet Applet to repaint, null results in nothing happening.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @param x X coordinate of the region to repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @param y Y coordinate of the region to repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @param w Width of the region to repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @param h Height of the region to repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @see JApplet#repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    public void addDirtyRegion(Applet applet, int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        addDirtyRegion0(applet, x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    void scheduleHeavyWeightPaints() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        Map<Container,Rectangle> hws;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            if (hwDirtyComponents.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            hws = hwDirtyComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            hwDirtyComponents =  new IdentityHashMap<Container,Rectangle>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        for (Container hw : hws.keySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            Rectangle dirty = hws.get(hw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            if (hw instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                addDirtyRegion((Window)hw, dirty.x, dirty.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                               dirty.width, dirty.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            else if (hw instanceof Applet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                addDirtyRegion((Applet)hw, dirty.x, dirty.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                               dirty.width, dirty.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            else { // SwingHeavyWeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                addDirtyRegion0(hw, dirty.x, dirty.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                                dirty.width, dirty.height);
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
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    // This is called from the toolkit thread when a native expose is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    // received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    void nativeAddDirtyRegion(AppContext appContext, Container c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                              int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        if (w > 0 && h > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                Rectangle dirty = hwDirtyComponents.get(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                if (dirty == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    hwDirtyComponents.put(c, new Rectangle(x, y, w, h));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    hwDirtyComponents.put(c, SwingUtilities.computeUnion(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                                              x, y, w, h, dirty));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            scheduleProcessingRunnable(appContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    // This is called from the toolkit thread when awt needs to run a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    // Runnable before we paint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    void nativeQueueSurfaceDataRunnable(AppContext appContext, Component c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                                        Runnable r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            if (runnableList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                runnableList = new LinkedList<Runnable>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            runnableList.add(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        scheduleProcessingRunnable(appContext);
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
     * Extends the dirty region for the specified component to include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * the new region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @return false if <code>c</code> is not yet marked dirty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    private synchronized boolean extendDirtyRegion(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        Component c, int x, int y, int w, int h) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1291
diff changeset
   592
        Rectangle r = dirtyComponents.get(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        if (r != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            // A non-null r implies c is already marked as dirty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            // and that the parent is valid. Therefore we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            // just union the rect and bail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            SwingUtilities.computeUnion(x, y, w, h, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /** Return the current dirty region for a component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *  Return an empty rectangle if the component is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *  dirty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    public Rectangle getDirtyRegion(JComponent aComponent) {
1291
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   608
        RepaintManager delegate = getDelegate(aComponent);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   609
        if (delegate != null) {
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   610
            return delegate.getDirtyRegion(aComponent);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   611
        }
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1291
diff changeset
   612
        Rectangle r;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        synchronized(this) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1291
diff changeset
   614
            r = dirtyComponents.get(aComponent);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        if(r == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            return new Rectangle(0,0,0,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            return new Rectangle(r);
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
     * Mark a component completely dirty. <b>aComponent</b> will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * completely painted during the next paintDirtyRegions() call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    public void markCompletelyDirty(JComponent aComponent) {
1291
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   627
        RepaintManager delegate = getDelegate(aComponent);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   628
        if (delegate != null) {
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   629
            delegate.markCompletelyDirty(aComponent);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   630
            return;
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   631
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        addDirtyRegion(aComponent,0,0,Integer.MAX_VALUE,Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * Mark a component completely clean. <b>aComponent</b> will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * get painted during the next paintDirtyRegions() call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    public void markCompletelyClean(JComponent aComponent) {
1291
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   640
        RepaintManager delegate = getDelegate(aComponent);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   641
        if (delegate != null) {
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   642
            delegate.markCompletelyClean(aComponent);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   643
            return;
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   644
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                dirtyComponents.remove(aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * Convenience method that returns true if <b>aComponent</b> will be completely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * painted during the next paintDirtyRegions(). If computing dirty regions is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * expensive for your component, use this method and avoid computing dirty region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * if it return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    public boolean isCompletelyDirty(JComponent aComponent) {
1291
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   657
        RepaintManager delegate = getDelegate(aComponent);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   658
        if (delegate != null) {
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   659
            return delegate.isCompletelyDirty(aComponent);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   660
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        Rectangle r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        r = getDirtyRegion(aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        if(r.width == Integer.MAX_VALUE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
           r.height == Integer.MAX_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            return false;
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
     * Validate all of the components that have been marked invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @see #addInvalidComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    public void validateInvalidComponents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        java.util.List<Component> ic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            if(invalidComponents == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            ic = invalidComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            invalidComponents = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        int n = ic.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        for(int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            ic.get(i).validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * This is invoked to process paint requests.  It's needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * for backward compatability in so far as RepaintManager would previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * not see paint requests for top levels, so, we have to make sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * a subclass correctly paints any dirty top levels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    private void prePaintDirtyRegions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        Map<Component,Rectangle> dirtyComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        java.util.List<Runnable> runnableList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            dirtyComponents = this.dirtyComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            runnableList = this.runnableList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            this.runnableList = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (runnableList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            for (Runnable runnable : runnableList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        paintDirtyRegions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        if (dirtyComponents.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            // This'll only happen if a subclass isn't correctly dealing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            // with toplevels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            paintDirtyRegions(dirtyComponents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * Paint all of the components that have been marked dirty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @see #addDirtyRegion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    public void paintDirtyRegions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        synchronized(this) {  // swap for thread safety
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            Map<Component,Rectangle> tmp = tmpDirtyComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            tmpDirtyComponents = dirtyComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            dirtyComponents = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            dirtyComponents.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        paintDirtyRegions(tmpDirtyComponents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    private void paintDirtyRegions(Map<Component,Rectangle>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                                   tmpDirtyComponents){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        int i, count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        java.util.List<Component> roots;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        Component dirtyComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        count = tmpDirtyComponents.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        Rectangle rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        int localBoundsX = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        int localBoundsY = 0;
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1291
diff changeset
   748
        int localBoundsH;
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1291
diff changeset
   749
        int localBoundsW;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        Enumeration keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        roots = new ArrayList<Component>(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        for (Component dirty : tmpDirtyComponents.keySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            collectDirtyComponents(tmpDirtyComponents, dirty, roots);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        count = roots.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        //        System.out.println("roots size is " + count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        painting = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            for(i=0 ; i < count ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                dirtyComponent = roots.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                rect = tmpDirtyComponents.get(dirtyComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                //            System.out.println("Should refresh :" + rect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                localBoundsH = dirtyComponent.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                localBoundsW = dirtyComponent.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                SwingUtilities.computeIntersection(localBoundsX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                                                   localBoundsY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                                                   localBoundsW,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                                                   localBoundsH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                                                   rect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                if (dirtyComponent instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                    ((JComponent)dirtyComponent).paintImmediately(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                        rect.x,rect.y,rect.width, rect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                else if (dirtyComponent.isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                    Graphics g = JComponent.safelyGetGraphics(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                            dirtyComponent, dirtyComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                    // If the Graphics goes away, it means someone disposed of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                    // the window, don't do anything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                    if (g != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                        g.setClip(rect.x, rect.y, rect.width, rect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                            dirtyComponent.paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                            g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                // If the repaintRoot has been set, service it now and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                // remove any components that are children of repaintRoot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                if (repaintRoot != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    adjustRoots(repaintRoot, roots, i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                    count = roots.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                    paintManager.isRepaintingRoot = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                    repaintRoot.paintImmediately(0, 0, repaintRoot.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                                                 repaintRoot.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    paintManager.isRepaintingRoot = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                    // Only service repaintRoot once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                    repaintRoot = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            painting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        tmpDirtyComponents.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * Removes any components from roots that are children of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    private void adjustRoots(JComponent root,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                             java.util.List<Component> roots, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        for (int i = roots.size() - 1; i >= index; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            Component c = roots.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            for(;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                if (c == root || c == null || !(c instanceof JComponent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                c = c.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            if (c == root) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                roots.remove(i);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    Rectangle tmp = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    void collectDirtyComponents(Map<Component,Rectangle> dirtyComponents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                                Component dirtyComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                                java.util.List<Component> roots) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        int dx, dy, rootDx, rootDy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        Component component, rootDirtyComponent,parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        Rectangle cBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        // Find the highest parent which is dirty.  When we get out of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        // rootDx and rootDy will contain the translation from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        // rootDirtyComponent's coordinate system to the coordinates of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        // original dirty component.  The tmp Rect is also used to compute the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        // visible portion of the dirtyRect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        component = rootDirtyComponent = dirtyComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        int x = dirtyComponent.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        int y = dirtyComponent.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        int w = dirtyComponent.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        int h = dirtyComponent.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        dx = rootDx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        dy = rootDy = 0;
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1291
diff changeset
   856
        tmp.setBounds(dirtyComponents.get(dirtyComponent));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        // System.out.println("Collect dirty component for bound " + tmp +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        //                                   "component bounds is " + cBounds);;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        SwingUtilities.computeIntersection(0,0,w,h,tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        if (tmp.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            // System.out.println("Empty 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        for(;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            if(!(component instanceof JComponent))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            parent = component.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            if(parent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            component = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            dx += x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            dy += y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            tmp.setLocation(tmp.x + x, tmp.y + y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            x = component.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            y = component.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            w = component.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            h = component.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            tmp = SwingUtilities.computeIntersection(0,0,w,h,tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            if (tmp.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                // System.out.println("Empty 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            if (dirtyComponents.get(component) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                rootDirtyComponent = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                rootDx = dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                rootDy = dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        if (dirtyComponent != rootDirtyComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            Rectangle r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            tmp.setLocation(tmp.x + rootDx - dx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                            tmp.y + rootDy - dy);
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1291
diff changeset
   903
            r = dirtyComponents.get(rootDirtyComponent);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            SwingUtilities.computeUnion(tmp.x,tmp.y,tmp.width,tmp.height,r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        // If we haven't seen this root before, then we need to add it to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        // list of root dirty Views.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        if (!roots.contains(rootDirtyComponent))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            roots.add(rootDirtyComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * Returns a string that displays and identifies this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * object's properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * @return a String representation of this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    public synchronized String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        if(dirtyComponents != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            sb.append("" + dirtyComponents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * Return the offscreen buffer that should be used as a double buffer with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * the component <code>c</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * By default there is a double buffer per RepaintManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * The buffer might be smaller than <code>(proposedWidth,proposedHeight)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * This happens when the maximum double buffer size as been set for the receiving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * repaint manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    public Image getOffscreenBuffer(Component c,int proposedWidth,int proposedHeight) {
1291
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   938
        RepaintManager delegate = getDelegate(c);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   939
        if (delegate != null) {
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   940
            return delegate.getOffscreenBuffer(c, proposedWidth, proposedHeight);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   941
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        return _getOffscreenBuffer(c, proposedWidth, proposedHeight);
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
   * Return a volatile offscreen buffer that should be used as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
   * double buffer with the specified component <code>c</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
   * The image returned will be an instance of VolatileImage, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
   * if a VolatileImage object could not be instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
   * This buffer might be smaller than <code>(proposedWidth,proposedHeight)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
   * This happens when the maximum double buffer size has been set for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
   * repaint manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
   * @see java.awt.image.VolatileImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
   * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    public Image getVolatileOffscreenBuffer(Component c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                                            int proposedWidth,int proposedHeight) {
1291
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   959
        RepaintManager delegate = getDelegate(c);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   960
        if (delegate != null) {
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   961
            return delegate.getVolatileOffscreenBuffer(c, proposedWidth,
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   962
                                                        proposedHeight);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
   963
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        GraphicsConfiguration config = c.getGraphicsConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        if (config == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            config = GraphicsEnvironment.getLocalGraphicsEnvironment().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                            getDefaultScreenDevice().getDefaultConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        Dimension maxSize = getDoubleBufferMaximumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        int width = proposedWidth < 1 ? 1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            (proposedWidth > maxSize.width? maxSize.width : proposedWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        int height = proposedHeight < 1 ? 1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            (proposedHeight > maxSize.height? maxSize.height : proposedHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        VolatileImage image = volatileMap.get(config);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        if (image == null || image.getWidth() < width ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                             image.getHeight() < height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            if (image != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                image.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            image = config.createCompatibleVolatileImage(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            volatileMap.put(config, image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    private Image _getOffscreenBuffer(Component c, int proposedWidth, int proposedHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        Dimension maxSize = getDoubleBufferMaximumSize();
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1291
diff changeset
   988
        DoubleBufferInfo doubleBuffer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        int width, height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        if (standardDoubleBuffer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            standardDoubleBuffer = new DoubleBufferInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        doubleBuffer = standardDoubleBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        width = proposedWidth < 1? 1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                  (proposedWidth > maxSize.width? maxSize.width : proposedWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        height = proposedHeight < 1? 1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                  (proposedHeight > maxSize.height? maxSize.height : proposedHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        if (doubleBuffer.needsReset || (doubleBuffer.image != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                                        (doubleBuffer.size.width < width ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                                         doubleBuffer.size.height < height))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            doubleBuffer.needsReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            if (doubleBuffer.image != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                doubleBuffer.image.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                doubleBuffer.image = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            width = Math.max(doubleBuffer.size.width, width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            height = Math.max(doubleBuffer.size.height, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        Image result = doubleBuffer.image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        if (doubleBuffer.image == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            result = c.createImage(width , height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            doubleBuffer.size = new Dimension(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            if (c instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                ((JComponent)c).setCreatedDoubleBuffer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                doubleBuffer.image = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            // JComponent will inform us when it is no longer valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            // (via removeNotify) we have no such hook to other components,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            // therefore we don't keep a ref to the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            // (indirectly through the Image) by stashing the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    /** Set the maximum double buffer size. **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    public void setDoubleBufferMaximumSize(Dimension d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        doubleBufferMaxSize = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        if (doubleBufferMaxSize == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            clearImages();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            clearImages(d.width, d.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    private void clearImages() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        clearImages(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    private void clearImages(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        if (standardDoubleBuffer != null && standardDoubleBuffer.image != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            if (standardDoubleBuffer.image.getWidth(null) > width ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                standardDoubleBuffer.image.getHeight(null) > height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                standardDoubleBuffer.image.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                standardDoubleBuffer.image = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        // Clear out the VolatileImages
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        Iterator gcs = volatileMap.keySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        while (gcs.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            GraphicsConfiguration gc = (GraphicsConfiguration)gcs.next();
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1291
diff changeset
  1057
            VolatileImage image = volatileMap.get(gc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            if (image.getWidth() > width || image.getHeight() > height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                image.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                gcs.remove();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * Returns the maximum double buffer size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * @return a Dimension object representing the maximum size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    public Dimension getDoubleBufferMaximumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        if (doubleBufferMaxSize == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                Rectangle virtualBounds = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                GraphicsEnvironment ge = GraphicsEnvironment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                                                 getLocalGraphicsEnvironment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                for (GraphicsDevice gd : ge.getScreenDevices()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                    GraphicsConfiguration gc = gd.getDefaultConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                    virtualBounds = virtualBounds.union(gc.getBounds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                doubleBufferMaxSize = new Dimension(virtualBounds.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                                                    virtualBounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            } catch (HeadlessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                doubleBufferMaxSize = new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        return doubleBufferMaxSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * Enables or disables double buffering in this RepaintManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * CAUTION: The default value for this property is set for optimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * paint performance on the given platform and it is not recommended
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * that programs modify this property directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * @param aFlag  true to activate double buffering
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * @see #isDoubleBufferingEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    public void setDoubleBufferingEnabled(boolean aFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        doubleBufferingEnabled = aFlag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        PaintManager paintManager = getPaintManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        if (!aFlag && paintManager.getClass() != PaintManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            setPaintManager(new PaintManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * Returns true if this RepaintManager is double buffered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * The default value for this property may vary from platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * to platform.  On platforms where native double buffering
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * is supported in the AWT, the default value will be <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * to avoid unnecessary buffering in Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * On platforms where native double buffering is not supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * the default value will be <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * @return true if this object is double buffered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    public boolean isDoubleBufferingEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        return doubleBufferingEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * This resets the double buffer. Actually, it marks the double buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * as invalid, the double buffer will then be recreated on the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * invocation of getOffscreenBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    void resetDoubleBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        if (standardDoubleBuffer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            standardDoubleBuffer.needsReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * This resets the volatile double buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    void resetVolatileDoubleBuffer(GraphicsConfiguration gc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        Image image = volatileMap.remove(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        if (image != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            image.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * Returns true if we should use the <code>Image</code> returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * from <code>getVolatileOffscreenBuffer</code> to do double buffering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    boolean useVolatileDoubleBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        return volatileImageBufferEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * Returns true if the current thread is the thread painting.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * will return false if no threads are painting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    private synchronized boolean isPaintingThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        return (Thread.currentThread() == paintThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    // Paint methods.  You very, VERY rarely need to invoke these.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    // They are invoked directly from JComponent's painting code and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    // when painting happens outside the normal flow: DefaultDesktopManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    // and JViewport.  If you end up needing these methods in other places be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    // careful that you don't get stuck in a paint loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * Paints a region of a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * @param paintingComponent Component to paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * @param bufferComponent Component to obtain buffer for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * @param g Graphics to paint to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * @param x X-coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * @param y Y-coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * @param w Width
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * @param h Height
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    void paint(JComponent paintingComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
               JComponent bufferComponent, Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
               int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        PaintManager paintManager = getPaintManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        if (!isPaintingThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            // We're painting to two threads at once.  PaintManager deals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            // with this a bit better than BufferStrategyPaintManager, use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            // it to avoid possible exceptions/corruption.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            if (paintManager.getClass() != PaintManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                paintManager = new PaintManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                paintManager.repaintManager = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        if (!paintManager.paint(paintingComponent, bufferComponent, g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                                x, y, w, h)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            g.setClip(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            paintingComponent.paintToOffscreen(g, x, y, w, h, x + w, y + h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * Does a copy area on the specified region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * @param clip Whether or not the copyArea needs to be clipped to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     *             Component's bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    void copyArea(JComponent c, Graphics g, int x, int y, int w, int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                  int deltaX, int deltaY, boolean clip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        getPaintManager().copyArea(c, g, x, y, w, h, deltaX, deltaY, clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * Invoked prior to any paint/copyArea method calls.  This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * be followed by an invocation of <code>endPaint</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * <b>WARNING</b>: Callers of this method need to wrap the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * in a <code>try/finally</code>, otherwise if an exception is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * during the course of painting the RepaintManager may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * be left in a state in which the screen is not updated, eg:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * repaintManager.beginPaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     *   repaintManager.paint(...);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     *   repaintManager.endPaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    void beginPaint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        boolean multiThreadedPaint = false;
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1291
diff changeset
  1225
        int paintDepth;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        Thread currentThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            paintDepth = this.paintDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            if (paintThread == null || currentThread == paintThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                paintThread = currentThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                this.paintDepth++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                multiThreadedPaint = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        if (!multiThreadedPaint && paintDepth == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            getPaintManager().beginPaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * Invoked after <code>beginPaint</code> has been invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    void endPaint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        if (isPaintingThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            PaintManager paintManager = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                if (--paintDepth == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                    paintManager = getPaintManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            if (paintManager != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                paintManager.endPaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                    paintThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        }
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
     * If possible this will show a previously rendered portion of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * a Component.  If successful, this will return true, otherwise false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * WARNING: This method is invoked from the native toolkit thread, be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * very careful as to what methods this invokes!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    boolean show(Container c, int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        return getPaintManager().show(c, x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * Invoked when the doubleBuffered or useTrueDoubleBuffering
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * properties of a JRootPane change.  This may come in on any thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    void doubleBufferingChanged(JRootPane rootPane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        getPaintManager().doubleBufferingChanged(rootPane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * Sets the <code>PaintManager</code> that is used to handle all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * double buffered painting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * @param paintManager The PaintManager to use.  Passing in null indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     *        the fallback PaintManager should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    void setPaintManager(PaintManager paintManager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        if (paintManager == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            paintManager = new PaintManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        PaintManager oldPaintManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
            oldPaintManager = this.paintManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            this.paintManager = paintManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            paintManager.repaintManager = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        if (oldPaintManager != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            oldPaintManager.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    private synchronized PaintManager getPaintManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        if (paintManager == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            PaintManager paintManager = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            if (doubleBufferingEnabled && !nativeDoubleBuffering) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                switch (bufferStrategyType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                case BUFFER_STRATEGY_NOT_SPECIFIED:
1832
0a885bee1070 6759311: RepaintManager casts Tookit to SunToolkit without instanceof check
rkennke
parents: 1301
diff changeset
  1308
                    Toolkit tk = Toolkit.getDefaultToolkit();
0a885bee1070 6759311: RepaintManager casts Tookit to SunToolkit without instanceof check
rkennke
parents: 1301
diff changeset
  1309
                    if (tk instanceof SunToolkit) {
0a885bee1070 6759311: RepaintManager casts Tookit to SunToolkit without instanceof check
rkennke
parents: 1301
diff changeset
  1310
                        SunToolkit stk = (SunToolkit) tk;
0a885bee1070 6759311: RepaintManager casts Tookit to SunToolkit without instanceof check
rkennke
parents: 1301
diff changeset
  1311
                        if (stk.useBufferPerWindow()) {
0a885bee1070 6759311: RepaintManager casts Tookit to SunToolkit without instanceof check
rkennke
parents: 1301
diff changeset
  1312
                            paintManager = new BufferStrategyPaintManager();
0a885bee1070 6759311: RepaintManager casts Tookit to SunToolkit without instanceof check
rkennke
parents: 1301
diff changeset
  1313
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                case BUFFER_STRATEGY_SPECIFIED_ON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                    paintManager = new BufferStrategyPaintManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            // null case handled in setPaintManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            setPaintManager(paintManager);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        return paintManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    private void scheduleProcessingRunnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        scheduleProcessingRunnable(AppContext.getAppContext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    private void scheduleProcessingRunnable(AppContext context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        if (processingRunnable.markPending()) {
1832
0a885bee1070 6759311: RepaintManager casts Tookit to SunToolkit without instanceof check
rkennke
parents: 1301
diff changeset
  1335
            Toolkit tk = Toolkit.getDefaultToolkit();
0a885bee1070 6759311: RepaintManager casts Tookit to SunToolkit without instanceof check
rkennke
parents: 1301
diff changeset
  1336
            if (tk instanceof SunToolkit) {
0a885bee1070 6759311: RepaintManager casts Tookit to SunToolkit without instanceof check
rkennke
parents: 1301
diff changeset
  1337
                SunToolkit.getSystemEventQueueImplPP(context).
0a885bee1070 6759311: RepaintManager casts Tookit to SunToolkit without instanceof check
rkennke
parents: 1301
diff changeset
  1338
                  postEvent(new InvocationEvent(Toolkit.getDefaultToolkit(),
0a885bee1070 6759311: RepaintManager casts Tookit to SunToolkit without instanceof check
rkennke
parents: 1301
diff changeset
  1339
                                                processingRunnable));
0a885bee1070 6759311: RepaintManager casts Tookit to SunToolkit without instanceof check
rkennke
parents: 1301
diff changeset
  1340
            } else {
0a885bee1070 6759311: RepaintManager casts Tookit to SunToolkit without instanceof check
rkennke
parents: 1301
diff changeset
  1341
                Toolkit.getDefaultToolkit().getSystemEventQueue().
0a885bee1070 6759311: RepaintManager casts Tookit to SunToolkit without instanceof check
rkennke
parents: 1301
diff changeset
  1342
                      postEvent(new InvocationEvent(Toolkit.getDefaultToolkit(),
0a885bee1070 6759311: RepaintManager casts Tookit to SunToolkit without instanceof check
rkennke
parents: 1301
diff changeset
  1343
                                                    processingRunnable));
0a885bee1070 6759311: RepaintManager casts Tookit to SunToolkit without instanceof check
rkennke
parents: 1301
diff changeset
  1344
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * PaintManager is used to handle all double buffered painting for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * Swing.  Subclasses should call back into the JComponent method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * <code>paintToOffscreen</code> to handle the actual painting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
    static class PaintManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
         * RepaintManager the PaintManager has been installed on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        protected RepaintManager repaintManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        boolean isRepaintingRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
         * Paints a region of a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
         * @param paintingComponent Component to paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
         * @param bufferComponent Component to obtain buffer for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
         * @param g Graphics to paint to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
         * @param x X-coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
         * @param y Y-coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
         * @param w Width
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
         * @param h Height
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
         * @return true if painting was successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        public boolean paint(JComponent paintingComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                             JComponent bufferComponent, Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                             int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            // First attempt to use VolatileImage buffer for performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            // If this fails (which should rarely occur), fallback to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            // standard Image buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            boolean paintCompleted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
            Image offscreen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            if (repaintManager.useVolatileDoubleBuffer() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                (offscreen = getValidImage(repaintManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                getVolatileOffscreenBuffer(bufferComponent, w, h))) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                VolatileImage vImage = (java.awt.image.VolatileImage)offscreen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                GraphicsConfiguration gc = bufferComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                                            getGraphicsConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                for (int i = 0; !paintCompleted &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                         i < RepaintManager.VOLATILE_LOOP_MAX; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                    if (vImage.validate(gc) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                                   VolatileImage.IMAGE_INCOMPATIBLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                        repaintManager.resetVolatileDoubleBuffer(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                        offscreen = repaintManager.getVolatileOffscreenBuffer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                            bufferComponent,w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                        vImage = (java.awt.image.VolatileImage)offscreen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                    paintDoubleBuffered(paintingComponent, vImage, g, x, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                                        w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                    paintCompleted = !vImage.contentsLost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            // VolatileImage painting loop failed, fallback to regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            // offscreen buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            if (!paintCompleted && (offscreen = getValidImage(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                      repaintManager.getOffscreenBuffer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                      bufferComponent, w, h))) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                paintDoubleBuffered(paintingComponent, offscreen, g, x, y, w,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                                    h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                paintCompleted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            return paintCompleted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
         * Does a copy area on the specified region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        public void copyArea(JComponent c, Graphics g, int x, int y, int w,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                             int h, int deltaX, int deltaY, boolean clip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            g.copyArea(x, y, w, h, deltaX, deltaY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
         * Invoked prior to any calls to paint or copyArea.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        public void beginPaint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
         * Invoked to indicate painting has been completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        public void endPaint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
         * Shows a region of a previously rendered component.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
         * will return true if successful, false otherwise.  The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
         * implementation returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        public boolean show(Container c, int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
         * Invoked when the doubleBuffered or useTrueDoubleBuffering
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
         * properties of a JRootPane change.  This may come in on any thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        public void doubleBufferingChanged(JRootPane rootPane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
         * Paints a portion of a component to an offscreen buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        protected void paintDoubleBuffered(JComponent c, Image image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                            Graphics g, int clipX, int clipY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                            int clipW, int clipH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            Graphics osg = image.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            int bw = Math.min(clipW, image.getWidth(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            int bh = Math.min(clipH, image.getHeight(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            int x,y,maxx,maxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                for(x = clipX, maxx = clipX+clipW; x < maxx ;  x += bw ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                    for(y=clipY, maxy = clipY + clipH; y < maxy ; y += bh) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                        osg.translate(-x, -y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                        osg.setClip(x,y,bw,bh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                        c.paintToOffscreen(osg, x, y, bw, bh, maxx, maxy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                        g.setClip(x, y, bw, bh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                        g.drawImage(image, x, y, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                        osg.translate(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
                osg.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
         * If <code>image</code> is non-null with a positive size it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
         * is returned, otherwise null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        private Image getValidImage(Image image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            if (image != null && image.getWidth(null) > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                                 image.getHeight(null) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
         * Schedules a repaint for the specified component.  This differs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
         * from <code>root.repaint</code> in that if the RepaintManager is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
         * currently processing paint requests it'll process this request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
         * with the current set of requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        protected void repaintRoot(JComponent root) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            assert (repaintManager.repaintRoot == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            if (repaintManager.painting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
                repaintManager.repaintRoot = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                root.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
            }
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
         * Returns true if the component being painted is the root component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
         * that was previously passed to <code>repaintRoot</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        protected boolean isRepaintingRoot() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            return isRepaintingRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
         * Cleans up any state.  After invoked the PaintManager will no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
         * longer be used anymore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        protected void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
    private class DoubleBufferInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        public Image image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        public Dimension size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        public boolean needsReset = false;
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
     * Listener installed to detect display changes. When display changes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * schedules a callback to notify all RepaintManagers of the display
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * changes. Only one DisplayChangedHandler is ever installed. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * singleton instance will schedule notification for all AppContexts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
    private static final class DisplayChangedHandler implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                                             DisplayChangedListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        public void displayChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            scheduleDisplayChanges();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        public void paletteChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        private void scheduleDisplayChanges() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
            // To avoid threading problems, we notify each RepaintManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
            // on the thread it was created on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            for (Object c : AppContext.getAppContexts()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                AppContext context = (AppContext) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                synchronized(context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                    if (!context.isDisposed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                        EventQueue eventQueue = (EventQueue)context.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                            AppContext.EVENT_QUEUE_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                        if (eventQueue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                            eventQueue.postEvent(new InvocationEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                                Toolkit.getDefaultToolkit(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                                new DisplayChangedRunnable()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
    private static final class DisplayChangedRunnable implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            RepaintManager.currentManager((JComponent)null).displayChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * Runnable used to process all repaint/revalidate requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
    private final class ProcessingRunnable implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        // If true, we're wainting on the EventQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        private boolean pending;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
         * Marks this processing runnable as pending. If this was not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
         * already marked as pending, true is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        public synchronized boolean markPending() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            if (!pending) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                pending = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                pending = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
            // First pass, flush any heavy paint events into real paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            // events.  If there are pending heavy weight requests this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            // result in q'ing this request up one more time.  As
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            // long as no other requests come in between now and the time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            // the second one is processed nothing will happen.  This is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            // ideal, but the logic needed to suppress the second request is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            // more headache than it's worth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
            scheduleHeavyWeightPaints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            // Do the actual validation and painting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            validateInvalidComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
            prePaintDirtyRegions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
    }
1291
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
  1607
    private RepaintManager getDelegate(Component c) {
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
  1608
        RepaintManager delegate = SwingUtilities3.getDelegateRepaintManager(c);
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
  1609
        if (this == delegate) {
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
  1610
            delegate = null;
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
  1611
        }
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
  1612
        return delegate;
e56898d6642d 6608456: need API to define RepaintManager per components hierarchy
idk
parents: 2
diff changeset
  1613
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
}