jdk/src/share/classes/javax/swing/JProgressBar.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 438 2ae294e4518c
child 1301 15e81207e1f2
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 438
diff changeset
     2
 * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.text.Format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.text.NumberFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.swing.plaf.ProgressBarUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * A component that visually displays the progress of some task.  As the task
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * progresses towards completion, the progress bar displays the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * task's percentage of completion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * This percentage is typically represented visually by a rectangle which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * starts out empty and gradually becomes filled in as the task progresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * In addition, the progress bar can display a textual representation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * percentage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * {@code JProgressBar} uses a {@code BoundedRangeModel} as its data model,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * with the {@code value} property representing the "current" state of the task,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * and the {@code minimum} and {@code maximum} properties representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * beginning and end points, respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * To indicate that a task of unknown length is executing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * you can put a progress bar into indeterminate mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * While the bar is in indeterminate mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * it animates constantly to show that work is occurring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * As soon as you can determine the task's length and amount of progress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * you should update the progress bar's value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * and switch it back to determinate mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Here is an example of creating a progress bar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * where <code>task</code> is an object (representing some piece of work)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * which returns information about the progress of the task:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *progressBar = new JProgressBar(0, task.getLengthOfTask());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *progressBar.setValue(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *progressBar.setStringPainted(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * Here is an example of querying the current state of the task, and using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * the returned value to update the progress bar:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *progressBar.setValue(task.getCurrent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * Here is an example of putting a progress bar into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * indeterminate mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * and then switching back to determinate mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * once the length of the task is known:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *progressBar = new JProgressBar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *<em>...//when the task of (initially) unknown length begins:</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *progressBar.setIndeterminate(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *<em>...//do some work; get length of task...</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *progressBar.setMaximum(newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *progressBar.setValue(newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *progressBar.setIndeterminate(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * For complete examples and further documentation see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html" target="_top">How to Monitor Progress</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * a section in <em>The Java Tutorial.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <strong>Warning:</strong> Swing is not thread safe. For more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * information see <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * href="package-summary.html#threading">Swing's Threading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * Policy</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * @see javax.swing.plaf.basic.BasicProgressBarUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * @see javax.swing.BoundedRangeModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * @see javax.swing.SwingWorker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *      attribute: isContainer false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *    description: A component that displays an integer value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * @author Michael C. Albers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * @author Kathy Walrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
public class JProgressBar extends JComponent implements SwingConstants, Accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @see #getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private static final String uiClassID = "ProgressBarUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Whether the progress bar is horizontal or vertical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * The default is <code>HORIZONTAL</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @see #setOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    protected int orientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Whether to display a border around the progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * The default is <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @see #setBorderPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    protected boolean paintBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * The object that holds the data for the progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @see #setModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    protected BoundedRangeModel model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * An optional string that can be displayed on the progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * The default is <code>null</code>. Setting this to a non-<code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * value does not imply that the string will be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * To display the string, {@code paintString} must be {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @see #setString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @see #setStringPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    protected String progressString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Whether to display a string of text on the progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * The default is <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Setting this to <code>true</code> causes a textual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * display of the progress to be rendered on the progress bar. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * the <code>progressString</code> is <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * the percentage of completion is displayed on the progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Otherwise, the <code>progressString</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * rendered on the progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @see #setStringPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @see #setString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    protected boolean paintString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * The default minimum for a progress bar is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    static final private int defaultMinimum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * The default maximum for a progress bar is 100.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    static final private int defaultMaximum = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * The default orientation for a progress bar is <code>HORIZONTAL</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    static final private int defaultOrientation = HORIZONTAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Only one <code>ChangeEvent</code> is needed per instance since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * event's only interesting property is the immutable source, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * is the progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * The event is lazily created the first time that an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * event notification is fired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @see #fireStateChanged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    protected transient ChangeEvent changeEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Listens for change events sent by the progress bar's model,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * redispatching them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * to change-event listeners registered upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * this progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @see #createChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    protected ChangeListener changeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Format used when displaying percent complete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    private transient Format format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Whether the progress bar is indeterminate (<code>true</code>) or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * normal (<code>false</code>); the default is <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @see #setIndeterminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private boolean indeterminate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Creates a horizontal progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * that displays a border but no progress string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * The initial and minimum values are 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * and the maximum is 100.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @see #setOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @see #setBorderPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @see #setStringPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @see #setString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @see #setIndeterminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public JProgressBar()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        this(defaultOrientation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Creates a progress bar with the specified orientation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * which can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * either {@code SwingConstants.VERTICAL} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * {@code SwingConstants.HORIZONTAL}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * By default, a border is painted but a progress string is not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * The initial and minimum values are 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * and the maximum is 100.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param orient  the desired orientation of the progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @throws IllegalArgumentException if {@code orient} is an illegal value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @see #setOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @see #setBorderPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @see #setStringPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @see #setString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @see #setIndeterminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public JProgressBar(int orient)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        this(orient, defaultMinimum, defaultMaximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Creates a horizontal progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * with the specified minimum and maximum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Sets the initial value of the progress bar to the specified minimum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * By default, a border is painted but a progress string is not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * The <code>BoundedRangeModel</code> that holds the progress bar's data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * handles any issues that may arise from improperly setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * minimum, initial, and maximum values on the progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * See the {@code BoundedRangeModel} documentation for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param min  the minimum value of the progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param max  the maximum value of the progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @see BoundedRangeModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @see #setOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @see #setBorderPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @see #setStringPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @see #setString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @see #setIndeterminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public JProgressBar(int min, int max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        this(defaultOrientation, min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Creates a progress bar using the specified orientation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * minimum, and maximum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * By default, a border is painted but a progress string is not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Sets the initial value of the progress bar to the specified minimum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * The <code>BoundedRangeModel</code> that holds the progress bar's data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * handles any issues that may arise from improperly setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * minimum, initial, and maximum values on the progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * See the {@code BoundedRangeModel} documentation for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @param orient  the desired orientation of the progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @param min  the minimum value of the progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @param max  the maximum value of the progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @throws IllegalArgumentException if {@code orient} is an illegal value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @see BoundedRangeModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @see #setOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @see #setBorderPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @see #setStringPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @see #setString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @see #setIndeterminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public JProgressBar(int orient, int min, int max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        // Creating the model this way is a bit simplistic, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        //  I believe that it is the the most common usage of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        //  component - it's what people will expect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        setModel(new DefaultBoundedRangeModel(min, 0, min, max));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        updateUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        setOrientation(orient);      // documented with set/getOrientation()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        setBorderPainted(true);      // documented with is/setBorderPainted()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        setStringPainted(false);     // see setStringPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        setString(null);             // see getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        setIndeterminate(false);     // see setIndeterminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Creates a horizontal progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * that uses the specified model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * to hold the progress bar's data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * By default, a border is painted but a progress string is not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param newModel  the data model for the progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @see #setOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @see #setBorderPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @see #setStringPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @see #setString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @see #setIndeterminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public JProgressBar(BoundedRangeModel newModel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        setModel(newModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        updateUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        setOrientation(defaultOrientation);  // see setOrientation()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        setBorderPainted(true);              // see setBorderPainted()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        setStringPainted(false);             // see setStringPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        setString(null);                     // see getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        setIndeterminate(false);             // see setIndeterminate
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Returns {@code SwingConstants.VERTICAL} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * {@code SwingConstants.HORIZONTAL}, depending on the orientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * of the progress bar. The default orientation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * {@code SwingConstants.HORIZONTAL}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @return <code>HORIZONTAL</code> or <code>VERTICAL</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @see #setOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public int getOrientation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        return orientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Sets the progress bar's orientation to <code>newOrientation</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * which must be {@code SwingConstants.VERTICAL} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * {@code SwingConstants.HORIZONTAL}. The default orientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * is {@code SwingConstants.HORIZONTAL}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @param  newOrientation  <code>HORIZONTAL</code> or <code>VERTICAL</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @exception      IllegalArgumentException    if <code>newOrientation</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *                                              is an illegal value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @see #getOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *    preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *  description: Set the progress bar's orientation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    public void setOrientation(int newOrientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (orientation != newOrientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            switch (newOrientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            case VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            case HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                int oldOrientation = orientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                orientation = newOrientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                firePropertyChange("orientation", oldOrientation, newOrientation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                if (accessibleContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    accessibleContext.firePropertyChange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                            AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                            ((oldOrientation == VERTICAL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                             ? AccessibleState.VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                             : AccessibleState.HORIZONTAL),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                            ((orientation == VERTICAL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                             ? AccessibleState.VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                             : AccessibleState.HORIZONTAL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                throw new IllegalArgumentException(newOrientation +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                                             " is not a legal orientation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Returns the value of the <code>stringPainted</code> property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @return the value of the <code>stringPainted</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @see    #setStringPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @see    #setString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    public boolean isStringPainted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        return paintString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * Sets the value of the <code>stringPainted</code> property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * which determines whether the progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * should render a progress string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * The default is <code>false</code>, meaning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * no string is painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * Some look and feels might not support progress strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * or might support them only when the progress bar is in determinate mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @param   b       <code>true</code> if the progress bar should render a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @see     #isStringPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @see     #setString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *  description: Whether the progress bar should render a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    public void setStringPainted(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        //PENDING: specify that string not painted when in indeterminate mode?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        //         or just leave that to the L&F?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        boolean oldValue = paintString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        paintString = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        firePropertyChange("stringPainted", oldValue, paintString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (paintString != oldValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Returns a {@code String} representation of the current progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * By default, this returns a simple percentage {@code String} based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * the value returned from {@code getPercentComplete}.  An example
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * would be the "42%".  You can change this by calling {@code setString}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @return the value of the progress string, or a simple percentage string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *         if the progress string is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @see    #setString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public String getString(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        if (progressString != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            return progressString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            if (format == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                format = NumberFormat.getPercentInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            return format.format(new Double(getPercentComplete()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * Sets the value of the progress string. By default,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * this string is <code>null</code>, implying the built-in behavior of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * using a simple percent string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * If you have provided a custom progress string and want to revert to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * the built-in behavior, set the string back to <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * The progress string is painted only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * the <code>isStringPainted</code> method returns <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @param  s       the value of the progress string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @see    #getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @see    #setStringPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @see    #isStringPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *  description: Specifies the progress string to paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    public void setString(String s){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        String oldValue = progressString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        progressString = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        firePropertyChange("string", oldValue, progressString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        if (progressString == null || oldValue == null || !progressString.equals(oldValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * Returns the percent complete for the progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Note that this number is between 0.0 and 1.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @return the percent complete for this progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    public double getPercentComplete() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        long span = model.getMaximum() - model.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        double currentValue = model.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        double pc = (currentValue - model.getMinimum()) / span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        return pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * Returns the <code>borderPainted</code> property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @return the value of the <code>borderPainted</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @see    #setBorderPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *  description: Does the progress bar paint its border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    public boolean isBorderPainted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        return paintBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * Sets the <code>borderPainted</code> property, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * <code>true</code> if the progress bar should paint its border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * The default value for this property is <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * Some look and feels might not implement painted borders;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * they will ignore this property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @param   b       <code>true</code> if the progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *                  should paint its border;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *                  otherwise, <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @see     #isBorderPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *  description: Whether the progress bar should paint its border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    public void setBorderPainted(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        boolean oldValue = paintBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        paintBorder = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        firePropertyChange("borderPainted", oldValue, paintBorder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        if (paintBorder != oldValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * Paints the progress bar's border if the <code>borderPainted</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * property is <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @param g  the <code>Graphics</code> context within which to paint the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @see #paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @see #setBorder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @see #isBorderPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @see #setBorderPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    protected void paintBorder(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        if (isBorderPainted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            super.paintBorder(g);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * Returns the look-and-feel object that renders this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @return the <code>ProgressBarUI</code> object that renders this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    public ProgressBarUI getUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return (ProgressBarUI)ui;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Sets the look-and-feel object that renders this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @param ui  a <code>ProgressBarUI</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *       hidden: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *  description: The UI object that implements the Component's LookAndFeel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    public void setUI(ProgressBarUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        super.setUI(ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * Resets the UI property to a value from the current look and feel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @see JComponent#updateUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        setUI((ProgressBarUI)UIManager.getUI(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * Returns the name of the look-and-feel class that renders this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @return the string "ProgressBarUI"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *        expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *   description: A string that specifies the name of the look-and-feel class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        return uiClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    /* We pass each Change event to the listeners with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * the progress bar as the event source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    private class ModelListener implements ChangeListener, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * Subclasses that want to handle change events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * from the model differently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * can override this to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * an instance of a custom <code>ChangeListener</code> implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * The default {@code ChangeListener} simply calls the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * {@code fireStateChanged} method to forward {@code ChangeEvent}s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * to the {@code ChangeListener}s that have been added directly to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @see #changeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @see #fireStateChanged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @see javax.swing.event.ChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @see javax.swing.BoundedRangeModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    protected ChangeListener createChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        return new ModelListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * Adds the specified <code>ChangeListener</code> to the progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @param l the <code>ChangeListener</code> to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    public void addChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        listenerList.add(ChangeListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * Removes a <code>ChangeListener</code> from the progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @param l the <code>ChangeListener</code> to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    public void removeChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        listenerList.remove(ChangeListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * Returns an array of all the <code>ChangeListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * to this progress bar with <code>addChangeListener</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @return all of the <code>ChangeListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    public ChangeListener[] getChangeListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        return (ChangeListener[])listenerList.getListeners(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                ChangeListener.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * Send a {@code ChangeEvent}, whose source is this {@code JProgressBar}, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * all {@code ChangeListener}s that have registered interest in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * {@code ChangeEvent}s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * This method is called each time a {@code ChangeEvent} is received from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * The event instance is created if necessary, and stored in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * {@code changeEvent}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @see #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    protected void fireStateChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            if (listeners[i]==ChangeListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                if (changeEvent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    changeEvent = new ChangeEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * Returns the data model used by this progress bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * @return the <code>BoundedRangeModel</code> currently in use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * @see #setModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * @see    BoundedRangeModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    public BoundedRangeModel getModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        return model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * Sets the data model used by the <code>JProgressBar</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * Note that the {@code BoundedRangeModel}'s {@code extent} is not used,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * and is set to {@code 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * @param  newModel the <code>BoundedRangeModel</code> to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *    expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * description: The data model used by the JProgressBar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    public void setModel(BoundedRangeModel newModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        // PENDING(???) setting the same model to multiple bars is broken; listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        BoundedRangeModel oldModel = getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        if (newModel != oldModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            if (oldModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                oldModel.removeChangeListener(changeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                changeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            model = newModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            if (newModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                changeListener = createChangeListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                newModel.addChangeListener(changeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            if (accessibleContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                accessibleContext.firePropertyChange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                        AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                        (oldModel== null
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   778
                         ? null : Integer.valueOf(oldModel.getValue())),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                        (newModel== null
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   780
                         ? null : Integer.valueOf(newModel.getValue())));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            if (model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                model.setExtent(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    /* All of the model methods are implemented by delegation. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * Returns the progress bar's current {@code value}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * from the <code>BoundedRangeModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * The value is always between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * minimum and maximum values, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * @return  the current value of the progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * @see     #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * @see     BoundedRangeModel#getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    public int getValue() { return getModel().getValue(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * Returns the progress bar's {@code minimum} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * from the <code>BoundedRangeModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * @return  the progress bar's minimum value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * @see     #setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * @see     BoundedRangeModel#getMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    public int getMinimum() { return getModel().getMinimum(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * Returns the progress bar's {@code maximum} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * from the <code>BoundedRangeModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @return  the progress bar's maximum value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @see     #setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @see     BoundedRangeModel#getMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    public int getMaximum() { return getModel().getMaximum(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * Sets the progress bar's current value to {@code n}.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * forwards the new value to the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * The data model (an instance of {@code BoundedRangeModel})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * handles any mathematical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * issues arising from assigning faulty values.  See the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * {@code BoundedRangeModel} documentation for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * If the new value is different from the previous value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * all change listeners are notified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * @param   n       the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * @see     #getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * @see     #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * @see     BoundedRangeModel#setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *    preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *  description: The progress bar's current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    public void setValue(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        BoundedRangeModel brm = getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        int oldValue = brm.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        brm.setValue(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        if (accessibleContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            accessibleContext.firePropertyChange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                    AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   853
                    Integer.valueOf(oldValue),
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   854
                    Integer.valueOf(brm.getValue()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * Sets the progress bar's minimum value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * (stored in the progress bar's data model) to <code>n</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * The data model (a <code>BoundedRangeModel</code> instance)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * handles any mathematical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * issues arising from assigning faulty values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * See the {@code BoundedRangeModel} documentation for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * If the minimum value is different from the previous minimum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * all change listeners are notified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * @param  n       the new minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * @see    #getMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * @see    #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * @see    BoundedRangeModel#setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     *  preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * description: The progress bar's minimum value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    public void setMinimum(int n) { getModel().setMinimum(n); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * Sets the progress bar's maximum value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * (stored in the progress bar's data model) to <code>n</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * The underlying <code>BoundedRangeModel</code> handles any mathematical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * issues arising from assigning faulty values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * See the {@code BoundedRangeModel} documentation for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * If the maximum value is different from the previous maximum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * all change listeners are notified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * @param  n       the new maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @see    #getMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @see    #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @see    BoundedRangeModel#setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     *    preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     *  description: The progress bar's maximum value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    public void setMaximum(int n) { getModel().setMaximum(n); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * Sets the <code>indeterminate</code> property of the progress bar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * which determines whether the progress bar is in determinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * or indeterminate mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * An indeterminate progress bar continuously displays animation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * indicating that an operation of unknown length is occurring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * By default, this property is <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * Some look and feels might not support indeterminate progress bars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * they will ignore this property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * See
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html" target="_top">How to Monitor Progress</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * for examples of using indeterminate progress bars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * @param newValue  <code>true</code> if the progress bar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     *                  should change to indeterminate mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     *                  <code>false</code> if it should revert to normal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * @see #isIndeterminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * @see javax.swing.plaf.basic.BasicProgressBarUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     *  description: Set whether the progress bar is indeterminate (true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     *               or normal (false).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    public void setIndeterminate(boolean newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        boolean oldValue = indeterminate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        indeterminate = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        firePropertyChange("indeterminate", oldValue, indeterminate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * Returns the value of the <code>indeterminate</code> property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * @return the value of the <code>indeterminate</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * @see    #setIndeterminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     *  description: Is the progress bar indeterminate (true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *               or normal (false)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    public boolean isIndeterminate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        return indeterminate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * See readObject() and writeObject() in JComponent for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * information about serialization in Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        if (getUIClassID().equals(uiClassID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            byte count = JComponent.getWriteObjCounter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            JComponent.setWriteObjCounter(this, --count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            if (count == 0 && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * Returns a string representation of this <code>JProgressBar</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * This method is intended to be used only for debugging purposes. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * implementations. The returned string may be empty but may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * @return  a string representation of this <code>JProgressBar</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        String orientationString = (orientation == HORIZONTAL ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                                    "HORIZONTAL" : "VERTICAL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        String paintBorderString = (paintBorder ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                                    "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        String progressStringString = (progressString != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                                       progressString : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        String paintStringString = (paintString ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                                    "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        String indeterminateString = (indeterminate ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                                    "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        return super.paramString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        ",orientation=" + orientationString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        ",paintBorder=" + paintBorderString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        ",paintString=" + paintStringString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        ",progressString=" + progressStringString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        ",indeterminateString=" + indeterminateString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * Gets the <code>AccessibleContext</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * <code>JProgressBar</code>. For progress bars, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * <code>AccessibleContext</code> takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * <code>AccessibleJProgressBar</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * A new <code>AccessibleJProgressBar</code> instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * @return an <code>AccessibleJProgressBar</code> that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     *         <code>AccessibleContext</code> of this <code>JProgressBar</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     *       expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     *  description: The AccessibleContext associated with this ProgressBar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            accessibleContext = new AccessibleJProgressBar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * <code>JProgressBar</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * Java Accessibility API appropriate to progress bar user-interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    protected class AccessibleJProgressBar extends AccessibleJComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        implements AccessibleValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
         * Gets the state set of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
         * @return an instance of AccessibleState containing the current state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
         * of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
         * @see AccessibleState
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        public AccessibleStateSet getAccessibleStateSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            AccessibleStateSet states = super.getAccessibleStateSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            if (getModel().getValueIsAdjusting()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                states.add(AccessibleState.BUSY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            if (getOrientation() == VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                states.add(AccessibleState.VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                states.add(AccessibleState.HORIZONTAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            return states;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
         * Gets the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
         * object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            return AccessibleRole.PROGRESS_BAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
         * Gets the <code>AccessibleValue</code> associated with this object.  In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
         * implementation of the Java Accessibility API for this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
         * returns this object, which is responsible for implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
         * <code>AccessibleValue</code> interface on behalf of itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
         * @return this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        public AccessibleValue getAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
         * Gets the accessible value of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
         * @return the current value of this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        public Number getCurrentAccessibleValue() {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1090
            return Integer.valueOf(getValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
         * Sets the value of this object as a <code>Number</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
         * @return <code>true</code> if the value was set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        public boolean setCurrentAccessibleValue(Number n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            // TIGER- 4422535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            if (n == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            setValue(n.intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
         * Gets the minimum accessible value of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
         * @return the minimum value of this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        public Number getMinimumAccessibleValue() {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1113
            return Integer.valueOf(getMinimum());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
         * Gets the maximum accessible value of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
         * @return the maximum value of this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        public Number getMaximumAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            // TIGER - 4422362
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1123
            return Integer.valueOf(model.getMaximum() - model.getExtent());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    } // AccessibleJProgressBar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
}