/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* A Basic L&F implementation of ProgressBarUI.
*
* @author Michael C. Albers
* @author Kathy Walrath
*/
private int cachedPercent;
// The "selectionForeground" is the color of the text when it is painted
// over a filled area of the progress bar. The "selectionBackground"
// is for the text over the unfilled progress bar area.
/**
* The current state of the indeterminate animation's cycle.
* 0, the initial value, means paint the first frame.
* When the progress bar is indeterminate and showing,
* the default animation thread updates this variable
* by invoking incrementAnimationIndex()
* every repaintInterval milliseconds.
*/
/**
* The number of frames per cycle. Under the default implementation,
* this depends on the cycleTime and repaintInterval. It
* must be an even number for the default painting algorithm. This
* value is set in the initIndeterminateValues method.
*/
/**
* Interval (in ms) between repaints of the indeterminate progress bar.
* The value of this method is set
* (every time the progress bar changes to indeterminate mode)
* using the
* "ProgressBar.repaintInterval" key in the defaults table.
*/
private int repaintInterval;
/**
* The number of milliseconds until the animation cycle repeats.
* The value of this method is set
* (every time the progress bar changes to indeterminate mode)
* using the
* "ProgressBar.cycleTime" key in the defaults table.
*/
//performance stuff
//make this false for
//performance tests
/**
* Used to hold the location and size of the bouncing box (returned
* by getBox) to be painted.
*
* @since 1.5
*/
/**
* The rectangle to be updated the next time the
* animation thread calls repaint. For bouncing-box
* animation this rect should include the union of
* the currently displayed box (which needs to be erased)
* and the box to be displayed next.
* This rectangle's values are set in
* the setAnimationIndex method.
*/
//cache
/** The component's painting area, not including the border. */
/** For bouncing-box animation, the change in position per frame. */
return new BasicProgressBarUI();
}
progressBar = (JProgressBar)c;
if (progressBar.isIndeterminate()) {
}
}
if (progressBar.isIndeterminate()) {
}
progressBar = null;
}
protected void installDefaults() {
"ProgressBar.background",
"ProgressBar.foreground",
"ProgressBar.font");
}
protected void uninstallDefaults() {
}
protected void installListeners() {
//Listen for changes in the progress bar's data.
changeListener = getHandler();
//Listen for changes between determinate and indeterminate state.
}
}
return handler;
}
/**
* Starts the animation thread, creating and initializing
* it if necessary. This method is invoked when an
* indeterminate progress bar should start animating.
* Reasons for this may include:
* <ul>
* <li>The progress bar is determinate and becomes displayable
* <li>The progress bar is displayable and becomes determinate
* <li>The progress bar is displayable and determinate and this
* UI is installed
* </ul>
* If you implement your own animation thread,
* you must override this method.
*
* @since 1.4
* @see #stopAnimationTimer
*/
protected void startAnimationTimer() {
}
}
/**
* Stops the animation thread.
* This method is invoked when the indeterminate
* animation should be stopped. Reasons for this may include:
* <ul>
* <li>The progress bar changes to determinate
* <li>The progress bar is no longer part of a displayable hierarchy
* <li>This UI in uninstalled
* </ul>
* If you implement your own animation thread,
* you must override this method.
*
* @since 1.4
* @see #startAnimationTimer
*/
protected void stopAnimationTimer() {
}
}
/**
* Removes all listeners installed by this object.
*/
protected void uninstallListeners() {
}
/**
* Returns the baseline.
*
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
* @see javax.swing.JComponent#getBaseline(int, int)
* @since 1.6
*/
if (progressBar.isStringPainted() &&
metrics.getLeading() -
}
return -1;
}
/**
* Returns an enum indicating how the baseline of the component
* changes as the size changes.
*
* @throws NullPointerException {@inheritDoc}
* @see javax.swing.JComponent#getBaseline(int, int)
* @since 1.6
*/
JComponent c) {
super.getBaselineResizeBehavior(c);
if (progressBar.isStringPainted() &&
}
}
// Many of the Basic*UI components have the following methods.
// This component does not have these methods because *ProgressBarUI
// is not a compound component and does not accept input.
//
// protected void installComponents()
// protected void uninstallComponents()
// protected void installKeyboardActions()
// protected void uninstallKeyboardActions()
"ProgressBar.horizontalSize");
}
return horizDim;
}
"ProgressBar.verticalSize");
}
return vertDim;
}
/**
* The "selectionForeground" is the color of the text when it is painted
* over a filled area of the progress bar.
*/
return selectionForeground;
}
/**
* The "selectionBackground" is the color of the text when it is painted
* over an unfilled area of the progress bar.
*/
return selectionBackground;
}
private int getCachedPercent() {
return cachedPercent;
}
this.cachedPercent = cachedPercent;
}
/**
* Returns the width (if HORIZONTAL) or height (if VERTICAL)
* progress bar. However, for text rendering simplification and
* aesthetic considerations, this function will return 1 when
* the progress string is being rendered.
*
* @return the value representing the spacing between cells
* @see #setCellLength
* @see JProgressBar#isStringPainted
*/
protected int getCellLength() {
if (progressBar.isStringPainted()) {
return 1;
} else {
return cellLength;
}
}
this.cellLength = cellLen;
}
/**
* progress bar. However, for text rendering simplification and
* aesthetic considerations, this function will return 0 when
* the progress string is being rendered.
*
* @return the value representing the spacing between cells
* @see #setCellSpacing
* @see JProgressBar#isStringPainted
*/
protected int getCellSpacing() {
if (progressBar.isStringPainted()) {
return 0;
} else {
return cellSpacing;
}
}
this.cellSpacing = cellSpace;
}
/**
* This determines the amount of the progress bar that should be filled
* based on the percent done gathered from the model. This is a common
* operation so it was abstracted out. It assumes that your progress bar
* is linear. That is, if you are making a circular progress indicator,
* you will want to override this method.
*/
int amountFull = 0;
} else {
}
}
return amountFull;
}
/**
* Delegates painting to one of two methods:
* paintDeterminate or paintIndeterminate.
*/
if (progressBar.isIndeterminate()) {
paintIndeterminate(g, c);
} else {
paintDeterminate(g, c);
}
}
/**
* Stores the position and size of
* the bouncing box that would be painted for the current animation index
* in <code>r</code> and returns <code>r</code>.
* Subclasses that add to the painting performed
* in this class's implementation of <code>paintIndeterminate</code> --
* to draw an outline around the bouncing box, for example --
* can use this method to get the location of the bouncing
* box that was just painted.
* By overriding this method,
* you have complete control over the size and position
* of the bouncing box,
* without having to reimplement <code>paintIndeterminate</code>.
*
* @param r the Rectangle instance to be modified;
* may be <code>null</code>
* @return <code>null</code> if no box should be drawn;
* otherwise, returns the passed-in rectangle
* (if non-null)
* or a new rectangle
*
* @see #setAnimationIndex
* @since 1.4
*/
int currentFrame = getAnimationIndex();
updateSizes();
}
r = getGenericBox(r);
if (r == null) {
return null;
}
if (middleFrame <= 0) {
return null;
}
//assert currentFrame >= 0 && currentFrame < numFrames
if (currentFrame < middleFrame) {
r.x = componentInnards.x
} else {
r.x = maxPosition
(currentFrame - middleFrame));
}
} else { //VERTICAL indeterminate progress bar
if (currentFrame < middleFrame) {
r.y = componentInnards.y
} else {
r.y = maxPosition
(currentFrame - middleFrame));
}
}
return r;
}
/**
* Updates delta, max position.
* Assumes componentInnards is correct (e.g. call after sizeChanged()).
*/
private void updateSizes() {
int length = 0;
- length;
} else { //VERTICAL progress bar
- length;
}
//If we're doing bouncing-box animation, update delta.
}
/**
* Assumes that the component innards, max position, etc. are up-to-date.
*/
if (r == null) {
r = new Rectangle();
}
if (r.width < 0) {
r = null;
} else {
r.y = componentInnards.y;
}
// end of HORIZONTAL
} else { //VERTICAL progress bar
if (r.height < 0) {
r = null;
} else {
r.x = componentInnards.x;
}
} // end of VERTICAL
return r;
}
/**
* Returns the length
* of the "bouncing box" to be painted.
* This method is invoked by the
* default implementation of <code>paintIndeterminate</code>
* to get the width (if the progress bar is horizontal)
* or height (if vertical) of the box.
* For example:
* <blockquote>
* <pre>
*boxRect.width = getBoxLength(componentInnards.width,
* componentInnards.height);
* </pre>
* </blockquote>
*
* @param availableLength the amount of space available
* for the bouncing box to move in;
* for a horizontal progress bar,
* for example,
* this should be
* the inside width of the progress bar
* (the component width minus borders)
* @param otherDimension for a horizontal progress bar, this should be
* the inside height of the progress bar; this
* value might be used to constrain or determine
* the return value
*
* @return the size of the box dimension being determined;
* must be no larger than <code>availableLength</code>
*
* @see javax.swing.SwingUtilities#calculateInnerArea
* @since 1.5
*/
}
/**
* All purpose paint method that should do the right thing for all
* linear bouncing-box progress bars.
* Override this if you are making another kind of
* progress bar.
*
* @see #paintDeterminate
*
* @since 1.4
*/
if (!(g instanceof Graphics2D)) {
return;
}
return;
}
// Paint the bouncing box.
}
// Deal with possible text painting
if (progressBar.isStringPainted()) {
}
else {
}
}
}
/**
* All purpose paint method that should do the right thing for almost
* all linear, determinate progress bars. By setting a few values in
* the defaults
* table, things should work just fine to paint your progress bar.
* Naturally, override this if you are making a circular or
* semi-circular progress bar.
*
* @see #paintIndeterminate
*
* @since 1.4
*/
if (!(g instanceof Graphics2D)) {
return;
}
return;
}
int cellLength = getCellLength();
int cellSpacing = getCellSpacing();
// amount of progress to draw
// draw the cells
// draw one big Rect because there is no space between cells
} else {
// draw each individual cell
}
if (BasicGraphicsUtils.isLeftToRight(c)) {
} else {
}
} else { // VERTICAL
// draw the cells
// draw one big Rect because there is no space between cells
} else {
// draw each individual cell
}
b.top + barRectHeight,
}
// Deal with possible text painting
if (progressBar.isStringPainted()) {
amountFull, b);
}
}
int amountFull, Insets b) {
if (progressBar.isIndeterminate()) {
} else {
}
}
else {
amountFull, b);
}
}
else {
if (progressBar.isIndeterminate()) {
} else {
amountFull, b);
}
}
}
/**
* Paints the progress string.
*
* @param g Graphics used for drawing.
* @param x x location of bounding box
* @param y y location of bounding box
* @param width width of bounding box
* @param height height of bounding box
* @param fillStart start location, in x or y depending on orientation,
* of the filled portion of the progress bar.
* @param amountFull size of the fill region, either width or height
* depending upon orientation.
* @param b Insets of the progress bar.
*/
if (!(g instanceof Graphics2D)) {
return;
}
renderLocation.x, renderLocation.y);
renderLocation.x, renderLocation.y);
} else { // VERTICAL
renderLocation.x, renderLocation.y);
renderLocation.x, renderLocation.y);
}
}
/**
* Designate the place where the progress string will be painted.
* This implementation places it at the center of the progress
* bar (in both x and y). Override this if you want to right,
* left, top, or bottom align the progress string or if you need
* to nudge it around for any reason.
*/
progressBar.getFont());
y + ((height +
fontSizer.getLeading() -
} else { // VERTICAL
}
}
progressBar.getFont());
// Ensure that the progress string will fit
if (progressBar.isStringPainted()) {
// I'm doing this for completeness.
}
// This uses both Height and Descent to be sure that
// there is more than enough room in the progress bar
// for everything.
// This does have a strange dependency on
// getStringPlacememnt() in a funny way.
}
}
} else {
// Ensure that the progress string will fit.
if (progressBar.isStringPainted()) {
}
// This is also for completeness.
}
}
}
return size;
}
/**
* The Minimum size for this component is 10. The rationale here
* is that there should be at least one pixel per 10 percent.
*/
} else {
}
return pref;
}
} else {
}
return pref;
}
/**
* Gets the index of the current animation frame.
*
* @since 1.4
*/
protected int getAnimationIndex() {
return animationIndex;
}
/**
* Returns the number of frames for the complete animation loop
* used by an indeterminate JProgessBar. The progress chunk will go
* from one end to the other and back during the entire loop. This
* visual behavior may be changed by subclasses in other Look and Feels.
*
* @return the number of frames
* @since 1.6
*/
protected final int getFrameCount() {
return numFrames;
}
/**
* Sets the index of the current animation frame
* to the specified value and requests that the
* progress bar be repainted.
* Subclasses that don't use the default painting code
* might need to override this method
* to change the way that the <code>repaint</code> method
* is invoked.
*
* @param newValue the new animation index; no checking
* is performed on its value
* @see #incrementAnimationIndex
*
* @since 1.4
*/
if (animationIndex != newValue) {
if (sizeChanged()) {
return;
}
//Get the previous box drawn.
//Update the frame number.
//Get the next box to draw.
if (nextPaintRect != null) {
}
}
} else { //animationIndex == newValue
return;
}
if (nextPaintRect != null) {
} else {
}
}
private boolean sizeChanged() {
return true;
}
}
/**
* Sets the index of the current animation frame,
* to the next valid value,
* which results in the progress bar being repainted.
* The next valid value is, by default,
* the current animation index plus one.
* If the new value would be too large,
* this method sets the index to 0.
* Subclasses might need to override this method
* to ensure that the index does not go over
* the number of frames needed for the particular
* progress bar instance.
* This method is invoked by the default animation thread
* every <em>X</em> milliseconds,
* where <em>X</em> is specified by the "ProgressBar.repaintInterval"
* UI default.
*
* @see #setAnimationIndex
* @since 1.4
*/
protected void incrementAnimationIndex() {
} else {
}
}
/**
* Returns the desired number of milliseconds between repaints.
* This value is meaningful
* only if the progress bar is in indeterminate mode.
* The repaint interval determines how often the
* default animation thread's timer is fired.
* It's also used by the default indeterminate progress bar
* painting code when determining
* how far to move the bouncing box per frame.
* The repaint interval is specified by
* the "ProgressBar.repaintInterval" UI default.
*
* @return the repaint interval, in milliseconds
*/
private int getRepaintInterval() {
return repaintInterval;
}
private int initRepaintInterval() {
this, "ProgressBar.repaintInterval", 50);
return repaintInterval;
}
/**
* Returns the number of milliseconds per animation cycle.
* This value is meaningful
* only if the progress bar is in indeterminate mode.
* The cycle time is used by the default indeterminate progress bar
* painting code when determining
* how far to move the bouncing box per frame.
* The cycle time is specified by
* the "ProgressBar.cycleTime" UI default
* and adjusted, if necessary,
* by the initIndeterminateDefaults method.
*
* @return the cycle time, in milliseconds
*/
private int getCycleTime() {
return cycleTime;
}
private int initCycleTime() {
"ProgressBar.cycleTime", 3000);
return cycleTime;
}
/** Initialize cycleTime, repaintInterval, numFrames, animationIndex. */
private void initIndeterminateDefaults() {
initRepaintInterval(); //initialize repaint interval
initCycleTime(); //initialize cycle length
// Make sure repaintInterval is reasonable.
if (repaintInterval <= 0) {
repaintInterval = 100;
}
// Make sure cycleTime is reasonable.
if (repaintInterval > cycleTime) {
} else {
// Force cycleTime to be a even multiple of repaintInterval.
((double)cycleTime)
/ ((double)repaintInterval*2));
}
}
/**
* Invoked by PropertyChangeHandler.
*
* NOTE: This might not be invoked until after the first
* paintIndeterminate call.
*/
private void initIndeterminateValues() {
//assert cycleTime/repaintInterval is a whole multiple of 2.
nextPaintRect = new Rectangle();
componentInnards = new Rectangle();
oldComponentInnards = new Rectangle();
// we only bother installing the HierarchyChangeListener if we
// are indeterminate
// start the animation thread if necessary
if (progressBar.isDisplayable()) {
}
}
/** Invoked by PropertyChangeHandler. */
private void cleanUpIndeterminateValues() {
// stop the animation thread if necessary
if (progressBar.isDisplayable()) {
}
maxPosition = 0;
delta = 0.0;
}
// Called from initIndeterminateValues to initialize the animation index.
// This assumes that numFrames is set to a correct value.
private void initAnimationIndex() {
// If this is a left-to-right progress bar,
// start at the first frame.
} else {
}
}
//
// Animation Thread
//
/**
* Implements an animation thread that invokes repaint
* at a fixed rate. If ADJUSTTIMER is true, this thread
* will continuously adjust the repaint interval to
* try to make the actual time between repaints match
* the requested rate.
*/
/**
* Creates a timer if one doesn't already exist,
* then starts the timer thread.
*/
lastCall = 0;
} else {
}
if (ADJUSTTIMER) {
timer.setRepeats(false);
timer.setCoalesce(false);
}
}
/**
* Stops the timer thread.
*/
private void stop() {
}
/**
* Reacts to the timer's action events.
*/
if (ADJUSTTIMER) {
//XXX maybe should cache this after a while
//actual = time - lastCall
//difference = actual - interval
//nextDelay = previousDelay - difference
// = previousDelay - (time - lastCall - interval)
int nextDelay = (int)(previousDelay
+ getRepaintInterval());
if (nextDelay < MINIMUM_DELAY) {
}
}
}
incrementAnimationIndex(); //paint next frame
}
}
/**
* This class should be treated as a "protected" inner class.
* Instantiate it only within subclasses of {@code BasicProgressBarUI}.
*/
// NOTE: This class exists only for backward compatability. All
// its functionality has been moved into Handler. If you need to add
// new functionality add it to the Handler, but make sure this
// class calls into the Handler.
getHandler().stateChanged(e);
}
}
// ChangeListener
int newPercent;
int oldPercent = getCachedPercent();
if (newRange > 0) {
} else {
newPercent = 0;
}
if (newPercent != oldPercent) {
}
}
// PropertyChangeListener
if ("indeterminate" == prop) {
if (progressBar.isIndeterminate()) {
} else {
//clean up
}
}
}
// we don't want the animation to keep running if we're not displayable
if (progressBar.isIndeterminate()) {
if (progressBar.isDisplayable()) {
} else {
}
}
}
}
}
}