jdk/src/share/classes/javax/swing/plaf/basic/BasicProgressBarUI.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 20169 d7fa6d7586c9
child 23010 6dadb192ad81
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
     2
 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.swing.DefaultLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 9230
diff changeset
    41
 * A Basic L&F implementation of ProgressBarUI.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Michael C. Albers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Kathy Walrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class BasicProgressBarUI extends ProgressBarUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private int cachedPercent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private int cellLength, cellSpacing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // The "selectionForeground" is the color of the text when it is painted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // over a filled area of the progress bar. The "selectionBackground"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // is for the text over the unfilled progress bar area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private Color selectionForeground, selectionBackground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private Animator animator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    protected JProgressBar progressBar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected ChangeListener changeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private Handler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * The current state of the indeterminate animation's cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * 0, the initial value, means paint the first frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * When the progress bar is indeterminate and showing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * the default animation thread updates this variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * by invoking incrementAnimationIndex()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * every repaintInterval milliseconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private int animationIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * The number of frames per cycle. Under the default implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * this depends on the cycleTime and repaintInterval.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * must be an even number for the default painting algorithm.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * value is set in the initIndeterminateValues method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private int numFrames;   //0 1|numFrames-1 ... numFrames/2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Interval (in ms) between repaints of the indeterminate progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * The value of this method is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * (every time the progress bar changes to indeterminate mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * "ProgressBar.repaintInterval" key in the defaults table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private int repaintInterval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * The number of milliseconds until the animation cycle repeats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * The value of this method is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * (every time the progress bar changes to indeterminate mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * "ProgressBar.cycleTime" key in the defaults table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private int cycleTime;  //must be repaintInterval*2*aPositiveInteger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    //performance stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private static boolean ADJUSTTIMER = true; //makes a BIG difference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                               //make this false for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                               //performance tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Used to hold the location and size of the bouncing box (returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * by getBox) to be painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    protected Rectangle boxRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * The rectangle to be updated the next time the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * animation thread calls repaint.  For bouncing-box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * animation this rect should include the union of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * the currently displayed box (which needs to be erased)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * and the box to be displayed next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * This rectangle's values are set in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * the setAnimationIndex method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private Rectangle nextPaintRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    //cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /** The component's painting area, not including the border. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private Rectangle componentInnards;    //the current painting area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private Rectangle oldComponentInnards; //used to see if the size changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /** For bouncing-box animation, the change in position per frame. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private double delta = 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private int maxPosition = 0; //maximum X (horiz) or Y box location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public static ComponentUI createUI(JComponent x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return new BasicProgressBarUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        progressBar = (JProgressBar)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        installDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (progressBar.isIndeterminate()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            initIndeterminateValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (progressBar.isIndeterminate()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            cleanUpIndeterminateValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        uninstallDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        progressBar = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        LookAndFeel.installProperty(progressBar, "opaque", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        LookAndFeel.installBorder(progressBar,"ProgressBar.border");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        LookAndFeel.installColorsAndFont(progressBar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                         "ProgressBar.background",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                         "ProgressBar.foreground",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                         "ProgressBar.font");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        cellLength = UIManager.getInt("ProgressBar.cellLength");
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   161
        if (cellLength == 0) cellLength = 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        cellSpacing = UIManager.getInt("ProgressBar.cellSpacing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        selectionForeground = UIManager.getColor("ProgressBar.selectionForeground");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        selectionBackground = UIManager.getColor("ProgressBar.selectionBackground");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    protected void uninstallDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        LookAndFeel.uninstallBorder(progressBar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        //Listen for changes in the progress bar's data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        changeListener = getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        progressBar.addChangeListener(changeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        //Listen for changes between determinate and indeterminate state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        progressBar.addPropertyChangeListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private Handler getHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Starts the animation thread, creating and initializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * it if necessary. This method is invoked when an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * indeterminate progress bar should start animating.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Reasons for this may include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *    <li>The progress bar is determinate and becomes displayable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *    <li>The progress bar is displayable and becomes determinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *    <li>The progress bar is displayable and determinate and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *        UI is installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * If you implement your own animation thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * you must override this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @see #stopAnimationTimer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    protected void startAnimationTimer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (animator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            animator = new Animator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        animator.start(getRepaintInterval());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Stops the animation thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * This method is invoked when the indeterminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * animation should be stopped. Reasons for this may include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *    <li>The progress bar changes to determinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *    <li>The progress bar is no longer part of a displayable hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *    <li>This UI in uninstalled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * If you implement your own animation thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * you must override this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @see #startAnimationTimer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    protected void stopAnimationTimer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (animator != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            animator.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Removes all listeners installed by this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        progressBar.removeChangeListener(changeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        progressBar.removePropertyChangeListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Returns the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public int getBaseline(JComponent c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        super.getBaseline(c, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (progressBar.isStringPainted() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            FontMetrics metrics = progressBar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    getFontMetrics(progressBar.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            Insets insets = progressBar.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            int y = insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            height = height - insets.top - insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            return y + (height + metrics.getAscent() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        metrics.getLeading() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        metrics.getDescent()) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return -1;
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
     * Returns an enum indicating how the baseline of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * changes as the size changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public Component.BaselineResizeBehavior getBaselineResizeBehavior(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        super.getBaselineResizeBehavior(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (progressBar.isStringPainted() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return Component.BaselineResizeBehavior.CENTER_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return Component.BaselineResizeBehavior.OTHER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    // Many of the Basic*UI components have the following methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    // This component does not have these methods because *ProgressBarUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    //  is not a compound component and does not accept input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    // protected void installComponents()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    // protected void uninstallComponents()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    // protected void installKeyboardActions()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    // protected void uninstallKeyboardActions()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    protected Dimension getPreferredInnerHorizontal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        Dimension horizDim = (Dimension)DefaultLookup.get(progressBar, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            "ProgressBar.horizontalSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (horizDim == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            horizDim = new Dimension(146, 12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return horizDim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    protected Dimension getPreferredInnerVertical() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        Dimension vertDim = (Dimension)DefaultLookup.get(progressBar, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            "ProgressBar.verticalSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (vertDim == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            vertDim = new Dimension(12, 146);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return vertDim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * The "selectionForeground" is the color of the text when it is painted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * over a filled area of the progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    protected Color getSelectionForeground() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return selectionForeground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * The "selectionBackground" is the color of the text when it is painted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * over an unfilled area of the progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    protected Color getSelectionBackground() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return selectionBackground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    private int getCachedPercent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return cachedPercent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    private void setCachedPercent(int cachedPercent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        this.cachedPercent = cachedPercent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Returns the width (if HORIZONTAL) or height (if VERTICAL)
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
   338
     * of each of the individual cells/units to be rendered in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * progress bar. However, for text rendering simplification and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * aesthetic considerations, this function will return 1 when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * the progress string is being rendered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @return the value representing the spacing between cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @see    #setCellLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @see    JProgressBar#isStringPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    protected int getCellLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (progressBar.isStringPainted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            return cellLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    protected void setCellLength(int cellLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        this.cellLength = cellLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Returns the spacing between each of the cells/units in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * progress bar. However, for text rendering simplification and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * aesthetic considerations, this function will return 0 when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * the progress string is being rendered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @return the value representing the spacing between cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @see    #setCellSpacing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @see    JProgressBar#isStringPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    protected int getCellSpacing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (progressBar.isStringPainted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            return cellSpacing;
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
    protected void setCellSpacing(int cellSpace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        this.cellSpacing = cellSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * This determines the amount of the progress bar that should be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * based on the percent done gathered from the model. This is a common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * operation so it was abstracted out. It assumes that your progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * is linear. That is, if you are making a circular progress indicator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * you will want to override this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    protected int getAmountFull(Insets b, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        int amountFull = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        BoundedRangeModel model = progressBar.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if ( (model.getMaximum() - model.getMinimum()) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                amountFull = (int)Math.round(width *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                             progressBar.getPercentComplete());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                amountFull = (int)Math.round(height *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                                             progressBar.getPercentComplete());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return amountFull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * Delegates painting to one of two methods:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * paintDeterminate or paintIndeterminate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (progressBar.isIndeterminate()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            paintIndeterminate(g, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            paintDeterminate(g, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Stores the position and size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * the bouncing box that would be painted for the current animation index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * in <code>r</code> and returns <code>r</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Subclasses that add to the painting performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * in this class's implementation of <code>paintIndeterminate</code> --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * to draw an outline around the bouncing box, for example --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * can use this method to get the location of the bouncing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * box that was just painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * By overriding this method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * you have complete control over the size and position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * of the bouncing box,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * without having to reimplement <code>paintIndeterminate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @param r  the Rectangle instance to be modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *           may be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @return   <code>null</code> if no box should be drawn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *           otherwise, returns the passed-in rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *           (if non-null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *           or a new rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @see #setAnimationIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    protected Rectangle getBox(Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        int currentFrame = getAnimationIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        int middleFrame = numFrames/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (sizeChanged() || delta == 0.0 || maxPosition == 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            updateSizes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        r = getGenericBox(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (middleFrame <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        //assert currentFrame >= 0 && currentFrame < numFrames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            if (currentFrame < middleFrame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                r.x = componentInnards.x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                      + (int)Math.round(delta * (double)currentFrame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                r.x = maxPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                      - (int)Math.round(delta *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                                        (currentFrame - middleFrame));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        } else { //VERTICAL indeterminate progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            if (currentFrame < middleFrame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                r.y = componentInnards.y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                      + (int)Math.round(delta * currentFrame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                r.y = maxPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                      - (int)Math.round(delta *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                                        (currentFrame - middleFrame));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * Updates delta, max position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * Assumes componentInnards is correct (e.g. call after sizeChanged()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    private void updateSizes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            length = getBoxLength(componentInnards.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                                  componentInnards.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            maxPosition = componentInnards.x + componentInnards.width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                          - length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        } else { //VERTICAL progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            length = getBoxLength(componentInnards.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                                  componentInnards.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            maxPosition = componentInnards.y + componentInnards.height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                          - length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        //If we're doing bouncing-box animation, update delta.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        delta = 2.0 * (double)maxPosition/(double)numFrames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Assumes that the component innards, max position, etc. are up-to-date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    private Rectangle getGenericBox(Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            r = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            r.width = getBoxLength(componentInnards.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                                   componentInnards.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            if (r.width < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                r = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                r.height = componentInnards.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                r.y = componentInnards.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
          // end of HORIZONTAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        } else { //VERTICAL progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            r.height = getBoxLength(componentInnards.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                                    componentInnards.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            if (r.height < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                r = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                r.width = componentInnards.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                r.x = componentInnards.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        } // end of VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * Returns the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * of the "bouncing box" to be painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * This method is invoked by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * default implementation of <code>paintIndeterminate</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * to get the width (if the progress bar is horizontal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * or height (if vertical) of the box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *boxRect.width = getBoxLength(componentInnards.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *                             componentInnards.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @param availableLength  the amount of space available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *                         for the bouncing box to move in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *                         for a horizontal progress bar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *                         for example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *                         this should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *                         the inside width of the progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *                         (the component width minus borders)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @param otherDimension   for a horizontal progress bar, this should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *                         the inside height of the progress bar; this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *                         value might be used to constrain or determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *                         the return value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @return the size of the box dimension being determined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *         must be no larger than <code>availableLength</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @see javax.swing.SwingUtilities#calculateInnerArea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    protected int getBoxLength(int availableLength, int otherDimension) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        return (int)Math.round(availableLength/6.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * All purpose paint method that should do the right thing for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * linear bouncing-box progress bars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * Override this if you are making another kind of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @see #paintDeterminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    protected void paintIndeterminate(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (!(g instanceof Graphics2D)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        Insets b = progressBar.getInsets(); // area for border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        int barRectWidth = progressBar.getWidth() - (b.right + b.left);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        if (barRectWidth <= 0 || barRectHeight <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        Graphics2D g2 = (Graphics2D)g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        // Paint the bouncing box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        boxRect = getBox(boxRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        if (boxRect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            g2.setColor(progressBar.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            g2.fillRect(boxRect.x, boxRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                       boxRect.width, boxRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        // Deal with possible text painting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        if (progressBar.isStringPainted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                paintString(g2, b.left, b.top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                            barRectWidth, barRectHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                            boxRect.x, boxRect.width, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                paintString(g2, b.left, b.top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                            barRectWidth, barRectHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                            boxRect.y, boxRect.height, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * All purpose paint method that should do the right thing for almost
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * all linear, determinate progress bars. By setting a few values in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * the defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * table, things should work just fine to paint your progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * Naturally, override this if you are making a circular or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * semi-circular progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @see #paintIndeterminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    protected void paintDeterminate(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        if (!(g instanceof Graphics2D)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        Insets b = progressBar.getInsets(); // area for border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        int barRectWidth = progressBar.getWidth() - (b.right + b.left);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        if (barRectWidth <= 0 || barRectHeight <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        int cellLength = getCellLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        int cellSpacing = getCellSpacing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        // amount of progress to draw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        int amountFull = getAmountFull(b, barRectWidth, barRectHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        Graphics2D g2 = (Graphics2D)g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        g2.setColor(progressBar.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            // draw the cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            if (cellSpacing == 0 && amountFull > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                // draw one big Rect because there is no space between cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                g2.setStroke(new BasicStroke((float)barRectHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                        BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                // draw each individual cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                g2.setStroke(new BasicStroke((float)barRectHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                        BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                        0.f, new float[] { cellLength, cellSpacing }, 0.f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            if (BasicGraphicsUtils.isLeftToRight(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                g2.drawLine(b.left, (barRectHeight/2) + b.top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                        amountFull + b.left, (barRectHeight/2) + b.top);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                g2.drawLine((barRectWidth + b.left),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                        (barRectHeight/2) + b.top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                        barRectWidth + b.left - amountFull,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                        (barRectHeight/2) + b.top);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        } else { // VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            // draw the cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            if (cellSpacing == 0 && amountFull > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                // draw one big Rect because there is no space between cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                g2.setStroke(new BasicStroke((float)barRectWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                        BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                // draw each individual cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                g2.setStroke(new BasicStroke((float)barRectWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                        BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                        0f, new float[] { cellLength, cellSpacing }, 0f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            g2.drawLine(barRectWidth/2 + b.left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                    b.top + barRectHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                    barRectWidth/2 + b.left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                    b.top + barRectHeight - amountFull);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        // Deal with possible text painting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        if (progressBar.isStringPainted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            paintString(g, b.left, b.top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                        barRectWidth, barRectHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                        amountFull, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    protected void paintString(Graphics g, int x, int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                               int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                               int amountFull, Insets b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            if (BasicGraphicsUtils.isLeftToRight(progressBar)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                if (progressBar.isIndeterminate()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    boxRect = getBox(boxRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    paintString(g, x, y, width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                            boxRect.x, boxRect.width, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    paintString(g, x, y, width, height, x, amountFull, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                paintString(g, x, y, width, height, x + width - amountFull,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                            amountFull, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            if (progressBar.isIndeterminate()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                boxRect = getBox(boxRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                paintString(g, x, y, width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                        boxRect.y, boxRect.height, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                paintString(g, x, y, width, height, y + height - amountFull,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                        amountFull, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * Paints the progress string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * @param g Graphics used for drawing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * @param x x location of bounding box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * @param y y location of bounding box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @param width width of bounding box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * @param height height of bounding box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @param fillStart start location, in x or y depending on orientation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *        of the filled portion of the progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @param amountFull size of the fill region, either width or height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     *        depending upon orientation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * @param b Insets of the progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    private void paintString(Graphics g, int x, int y, int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                             int fillStart, int amountFull, Insets b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        if (!(g instanceof Graphics2D)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        Graphics2D g2 = (Graphics2D)g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        String progressString = progressBar.getString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        g2.setFont(progressBar.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        Point renderLocation = getStringPlacement(g2, progressString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                                                  x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        Rectangle oldClip = g2.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            g2.setColor(getSelectionBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            SwingUtilities2.drawString(progressBar, g2, progressString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                                       renderLocation.x, renderLocation.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            g2.setColor(getSelectionForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            g2.clipRect(fillStart, y, amountFull, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            SwingUtilities2.drawString(progressBar, g2, progressString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                                       renderLocation.x, renderLocation.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        } else { // VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            g2.setColor(getSelectionBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            AffineTransform rotate =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                    AffineTransform.getRotateInstance(Math.PI/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            g2.setFont(progressBar.getFont().deriveFont(rotate));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            renderLocation = getStringPlacement(g2, progressString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                                                  x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            SwingUtilities2.drawString(progressBar, g2, progressString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                                       renderLocation.x, renderLocation.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            g2.setColor(getSelectionForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            g2.clipRect(x, fillStart, width, amountFull);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            SwingUtilities2.drawString(progressBar, g2, progressString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                                       renderLocation.x, renderLocation.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        g2.setClip(oldClip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * Designate the place where the progress string will be painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * This implementation places it at the center of the progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * bar (in both x and y). Override this if you want to right,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * left, top, or bottom align the progress string or if you need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * to nudge it around for any reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    protected Point getStringPlacement(Graphics g, String progressString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                                       int x,int y,int width,int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        FontMetrics fontSizer = SwingUtilities2.getFontMetrics(progressBar, g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                                            progressBar.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        int stringWidth = SwingUtilities2.stringWidth(progressBar, fontSizer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                                                      progressString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            return new Point(x + Math.round(width/2 - stringWidth/2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                             y + ((height +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                                 fontSizer.getAscent() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                                 fontSizer.getLeading() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                                 fontSizer.getDescent()) / 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        } else { // VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            return new Point(x + ((width - fontSizer.getAscent() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    fontSizer.getLeading() + fontSizer.getDescent()) / 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    y + Math.round(height/2 - stringWidth/2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    public Dimension getPreferredSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        Dimension       size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        Insets          border = progressBar.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        FontMetrics     fontSizer = progressBar.getFontMetrics(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                                                  progressBar.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            size = new Dimension(getPreferredInnerHorizontal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            // Ensure that the progress string will fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            if (progressBar.isStringPainted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                // I'm doing this for completeness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                String progString = progressBar.getString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                int stringWidth = SwingUtilities2.stringWidth(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                          progressBar, fontSizer, progString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                if (stringWidth > size.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    size.width = stringWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                // This uses both Height and Descent to be sure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                // there is more than enough room in the progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                // for everything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                // This does have a strange dependency on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                // getStringPlacememnt() in a funny way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                int stringHeight = fontSizer.getHeight() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                                   fontSizer.getDescent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                if (stringHeight > size.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                    size.height = stringHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            size = new Dimension(getPreferredInnerVertical());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            // Ensure that the progress string will fit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            if (progressBar.isStringPainted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                String progString = progressBar.getString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                int stringHeight = fontSizer.getHeight() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                        fontSizer.getDescent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                if (stringHeight > size.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                    size.width = stringHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                // This is also for completeness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                int stringWidth = SwingUtilities2.stringWidth(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                                       progressBar, fontSizer, progString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                if (stringWidth > size.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                    size.height = stringWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        size.width += border.left + border.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        size.height += border.top + border.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * The Minimum size for this component is 10. The rationale here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * is that there should be at least one pixel per 10 percent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    public Dimension getMinimumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        Dimension pref = getPreferredSize(progressBar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            pref.width = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            pref.height = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        return pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    public Dimension getMaximumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        Dimension pref = getPreferredSize(progressBar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            pref.width = Short.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            pref.height = Short.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        return pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * Gets the index of the current animation frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    protected int getAnimationIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        return animationIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * Returns the number of frames for the complete animation loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * used by an indeterminate JProgessBar. The progress chunk will go
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * from one end to the other and back during the entire loop. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * visual behavior may be changed by subclasses in other Look and Feels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * @return the number of frames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    protected final int getFrameCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        return numFrames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * Sets the index of the current animation frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * to the specified value and requests that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * progress bar be repainted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * Subclasses that don't use the default painting code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * might need to override this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * to change the way that the <code>repaint</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * @param newValue the new animation index; no checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     *                 is performed on its value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * @see #incrementAnimationIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    protected void setAnimationIndex(int newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        if (animationIndex != newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            if (sizeChanged()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                animationIndex = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                maxPosition = 0;  //needs to be recalculated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                delta = 0.0;      //needs to be recalculated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                progressBar.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            //Get the previous box drawn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            nextPaintRect = getBox(nextPaintRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            //Update the frame number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            animationIndex = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            //Get the next box to draw.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            if (nextPaintRect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                boxRect = getBox(boxRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                if (boxRect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                    nextPaintRect.add(boxRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        } else { //animationIndex == newValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        if (nextPaintRect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            progressBar.repaint(nextPaintRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            progressBar.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    private boolean sizeChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        if ((oldComponentInnards == null) || (componentInnards == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        oldComponentInnards.setRect(componentInnards);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        componentInnards = SwingUtilities.calculateInnerArea(progressBar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                                                             componentInnards);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        return !oldComponentInnards.equals(componentInnards);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * Sets the index of the current animation frame,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * to the next valid value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * which results in the progress bar being repainted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * The next valid value is, by default,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * the current animation index plus one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * If the new value would be too large,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * this method sets the index to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * Subclasses might need to override this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * to ensure that the index does not go over
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * the number of frames needed for the particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * progress bar instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * This method is invoked by the default animation thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * every <em>X</em> milliseconds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * where <em>X</em> is specified by the "ProgressBar.repaintInterval"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * UI default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * @see #setAnimationIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    protected void incrementAnimationIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        int newValue = getAnimationIndex() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        if (newValue < numFrames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            setAnimationIndex(newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            setAnimationIndex(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * Returns the desired number of milliseconds between repaints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * This value is meaningful
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * only if the progress bar is in indeterminate mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * The repaint interval determines how often the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * default animation thread's timer is fired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * It's also used by the default indeterminate progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * painting code when determining
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * how far to move the bouncing box per frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * The repaint interval is specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * the "ProgressBar.repaintInterval" UI default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * @return  the repaint interval, in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    private int getRepaintInterval() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        return repaintInterval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    private int initRepaintInterval() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        repaintInterval = DefaultLookup.getInt(progressBar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                this, "ProgressBar.repaintInterval", 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        return repaintInterval;
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
     * Returns the number of milliseconds per animation cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * This value is meaningful
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * only if the progress bar is in indeterminate mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * The cycle time is used by the default indeterminate progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * painting code when determining
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * how far to move the bouncing box per frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * The cycle time is specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * the "ProgressBar.cycleTime" UI default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * and adjusted, if necessary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * by the initIndeterminateDefaults method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * @return  the cycle time, in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    private int getCycleTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        return cycleTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    private int initCycleTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        cycleTime = DefaultLookup.getInt(progressBar, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                "ProgressBar.cycleTime", 3000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        return cycleTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    /** Initialize cycleTime, repaintInterval, numFrames, animationIndex. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    private void initIndeterminateDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        initRepaintInterval(); //initialize repaint interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        initCycleTime();       //initialize cycle length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        // Make sure repaintInterval is reasonable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        if (repaintInterval <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            repaintInterval = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        // Make sure cycleTime is reasonable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        if (repaintInterval > cycleTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            cycleTime = repaintInterval * 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            // Force cycleTime to be a even multiple of repaintInterval.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            int factor = (int)Math.ceil(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                                 ((double)cycleTime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                               / ((double)repaintInterval*2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            cycleTime = repaintInterval*factor*2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * Invoked by PropertyChangeHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     *  NOTE: This might not be invoked until after the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     *  paintIndeterminate call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    private void initIndeterminateValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        initIndeterminateDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        //assert cycleTime/repaintInterval is a whole multiple of 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        numFrames = cycleTime/repaintInterval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        initAnimationIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        boxRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        nextPaintRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        componentInnards = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        oldComponentInnards = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        // we only bother installing the HierarchyChangeListener if we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        // are indeterminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        progressBar.addHierarchyListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        // start the animation thread if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        if (progressBar.isDisplayable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            startAnimationTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    /** Invoked by PropertyChangeHandler. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    private void cleanUpIndeterminateValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        // stop the animation thread if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        if (progressBar.isDisplayable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            stopAnimationTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        cycleTime = repaintInterval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        numFrames = animationIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        maxPosition = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        delta = 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        boxRect = nextPaintRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        componentInnards = oldComponentInnards = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        progressBar.removeHierarchyListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    // Called from initIndeterminateValues to initialize the animation index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    // This assumes that numFrames is set to a correct value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    private void initAnimationIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        if ((progressBar.getOrientation() == JProgressBar.HORIZONTAL) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            (BasicGraphicsUtils.isLeftToRight(progressBar))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            // If this is a left-to-right progress bar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            // start at the first frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            setAnimationIndex(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            // If we go right-to-left or vertically, start at the right/bottom.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            setAnimationIndex(numFrames/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    // Animation Thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * Implements an animation thread that invokes repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * at a fixed rate.  If ADJUSTTIMER is true, this thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * will continuously adjust the repaint interval to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * try to make the actual time between repaints match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * the requested rate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    private class Animator implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        private Timer timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        private long previousDelay; //used to tune the repaint interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        private int interval; //the fixed repaint interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        private long lastCall; //the last time actionPerformed was called
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        private int MINIMUM_DELAY = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
         * Creates a timer if one doesn't already exist,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
         * then starts the timer thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        private void start(int interval) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            previousDelay = interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            lastCall = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            if (timer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                timer = new Timer(interval, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                timer.setDelay(interval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            if (ADJUSTTIMER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                timer.setRepeats(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                timer.setCoalesce(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            timer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
         * Stops the timer thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        private void stop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            timer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
         * Reacts to the timer's action events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            if (ADJUSTTIMER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                long time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                if (lastCall > 0) { //adjust nextDelay
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                //XXX maybe should cache this after a while
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                    //actual = time - lastCall
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                    //difference = actual - interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                    //nextDelay = previousDelay - difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                    //          = previousDelay - (time - lastCall - interval)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                   int nextDelay = (int)(previousDelay
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                                          - time + lastCall
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                                          + getRepaintInterval());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                    if (nextDelay < MINIMUM_DELAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                        nextDelay = MINIMUM_DELAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                    timer.setInitialDelay(nextDelay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                    previousDelay = nextDelay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                timer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                lastCall = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            incrementAnimationIndex(); //paint next frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * This class should be treated as a &quot;protected&quot; inner class.
9230
c5674561cb9e 6985329: 9 classes in swing.plaf contains words inappropriate for public spec - about some compiler bug
alexp
parents: 5506
diff changeset
  1215
     * Instantiate it only within subclasses of {@code BasicProgressBarUI}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    public class ChangeHandler implements ChangeListener {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  1218
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            getHandler().stateChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    private class Handler implements ChangeListener, PropertyChangeListener, HierarchyListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        // ChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            BoundedRangeModel model = progressBar.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            int newRange = model.getMaximum() - model.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            int newPercent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            int oldPercent = getCachedPercent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            if (newRange > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                newPercent = (int)((100 * (long)model.getValue()) / newRange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                newPercent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            if (newPercent != oldPercent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                setCachedPercent(newPercent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                progressBar.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        // PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            String prop = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            if ("indeterminate" == prop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                if (progressBar.isIndeterminate()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                    initIndeterminateValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                    //clean up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                    cleanUpIndeterminateValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                progressBar.repaint();
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
        // we don't want the animation to keep running if we're not displayable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        public void hierarchyChanged(HierarchyEvent he) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            if ((he.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                if (progressBar.isIndeterminate()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                    if (progressBar.isDisplayable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                        startAnimationTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                        stopAnimationTimer();
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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
}