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