javax.swing.plaf.basic
Class BasicProgressBarUI

java.lang.Object
  extended byjavax.swing.plaf.ComponentUI
      extended byjavax.swing.plaf.ProgressBarUI
          extended byjavax.swing.plaf.basic.BasicProgressBarUI
Direct Known Subclasses:
MetalProgressBarUI

public class BasicProgressBarUI
extends ProgressBarUI

A Basic L&F implementation of ProgressBarUI.

Author:
Michael C. Albers, Kathy Walrath

Nested Class Summary
private  class BasicProgressBarUI.Animator
          Implements an animation thread that invokes repaint at a fixed rate.
 class BasicProgressBarUI.ChangeHandler
          This inner class is marked "public" due to a compiler bug.
private  class BasicProgressBarUI.PropertyChangeHandler
          [PENDING: add doc here] [PENDING: make this static?]
 
Field Summary
private static boolean ADJUSTTIMER
           
private  int animationIndex
          The current state of the indeterminate animation's cycle. 0, the initial value, means paint the first frame.
private  BasicProgressBarUI.Animator animator
           
private static boolean BASICDEBUG
           
private  Rectangle boxRect
          Used to hold the location and size of the bouncing box (returned by getBox) to be painted.
private  int cachedPercent
           
private  int cellLength
           
private  int cellSpacing
           
protected  ChangeListener changeListener
           
private  Rectangle componentInnards
          The component's painting area, not including the border.
private  int cycleTime
          The number of milliseconds until the animation cycle repeats.
private static boolean DEBUGALL
           
private static boolean DEBUGTIMER
           
private  double delta
          For bouncing-box animation, the change in position per frame.
private  boolean isIndeterminate
           
private  long lastLoopTime
           
private static boolean LOGSTATS
           
private  int maxPosition
           
private  Rectangle nextPaintRect
          The rectangle to be updated the next time the animation thread calls repaint.
private  int numFrames
          The number of frames per cycle.
private  int numLoops
           
private  Rectangle oldComponentInnards
           
private static Dimension PREFERRED_INNER_HORIZONTAL
           
private static Dimension PREFERRED_INNER_VERTICAL
           
protected  JProgressBar progressBar
           
private  java.beans.PropertyChangeListener propertyListener
           
private  int repaintInterval
          Interval (in ms) between repaints of the indeterminate progress bar.
private  Color selectionBackground
           
private  Color selectionForeground
           
private  long startTime
           
 
Constructor Summary
BasicProgressBarUI()
           
 
Method Summary
private  void cleanUpIndeterminateValues()
          Invoked by PropertyChangeHandler after stopAnimationTimer().
static ComponentUI createUI(JComponent x)
          Returns an instance of the UI delegate for the specified component.
protected  int getAmountFull(Insets b, int width, int height)
          This determines the amount of the progress bar that should be filled based on the percent done gathered from the model.
protected  int getAnimationIndex()
          Gets the index of the current animation frame.
protected  Rectangle getBox(Rectangle r)
          Stores the position and size of the bouncing box that would be painted for the current animation index in r and returns r.
private  int getBoxLength(int availableLength, int otherDimension)
          Returns the length of the "bouncing box" to be painted.
private  int getCachedPercent()
           
protected  int getCellLength()
          Returns the width (if HORIZONTAL) or height (if VERTICAL) of each of the indivdual cells/units to be rendered in the progress bar.
protected  int getCellSpacing()
          Returns the spacing between each of the cells/units in the progress bar.
private  int getCycleTime()
          Returns the number of milliseconds per animation cycle.
private  Rectangle getGenericBox(Rectangle r)
          Assumes that the component innards, max position, etc. are up-to-date.
 Dimension getMaximumSize(JComponent c)
          Returns the specified component's maximum size appropriate for the look and feel.
 Dimension getMinimumSize(JComponent c)
          The Minimum size for this component is 10.
protected  Dimension getPreferredInnerHorizontal()
           
protected  Dimension getPreferredInnerVertical()
           
 Dimension getPreferredSize(JComponent c)
          Returns the specified component's preferred size appropriate for the look and feel.
private  int getRepaintInterval()
          Returns the desired number of milliseconds between repaints.
protected  Color getSelectionBackground()
          The "selectionBackground" is the color of the text when it is painted over an unfilled area of the progress bar.
protected  Color getSelectionForeground()
          The "selectionForeground" is the color of the text when it is painted over a filled area of the progress bar.
protected  Point getStringPlacement(Graphics g, String progressString, int x, int y, int width, int height)
          Designate the place where the progress string will be painted.
protected  void incrementAnimationIndex()
          Sets the index of the current animation frame, to the next valid value, which results in the progress bar being repainted.
private  void initAnimationIndex()
           
private  int initCycleTime()
           
private  void initIndeterminateDefaults()
          Initialize cycleTime, repaintInterval, numFrames, animationIndex.
private  void initIndeterminateValues()
          Invoked by PropertyChangeHandler before startAnimationTimer().
private  int initRepaintInterval()
           
protected  void installDefaults()
           
protected  void installListeners()
           
 void installUI(JComponent c)
          Configures the specified component appropriate for the look and feel.
 void paint(Graphics g, JComponent c)
          Delegates painting to one of two methods: paintDeterminate or paintIndeterminate.
protected  void paintDeterminate(Graphics g, JComponent c)
          All purpose paint method that should do the right thing for almost all linear, determinate progress bars.
protected  void paintIndeterminate(Graphics g, JComponent c)
          All purpose paint method that should do the right thing for all linear bouncing-box progress bars.
protected  void paintString(Graphics g, int x, int y, int width, int height, int amountFull, Insets b)
           
private  void paintString(Graphics g, int x, int y, int width, int height, int fillStart, int amountFull, Insets b)
          Paints the progress string.
protected  void setAnimationIndex(int newValue)
          Sets the index of the current animation frame to the specified value and requests that the progress bar be repainted.
private  void setCachedPercent(int cachedPercent)
           
protected  void setCellLength(int cellLen)
           
protected  void setCellSpacing(int cellSpace)
           
private  boolean sizeChanged()
           
protected  void startAnimationTimer()
          Starts the animation thread, creating and initializing it if necessary.
protected  void stopAnimationTimer()
          Stops the animation thread.
protected  void uninstallDefaults()
           
protected  void uninstallListeners()
          Removes all listeners installed by this object.
 void uninstallUI(JComponent c)
          Reverses configuration which was done on the specified component during installUI.
private  void updateSizes()
          Updates delta, max position.
 
Methods inherited from class javax.swing.plaf.ComponentUI
contains, getAccessibleChild, getAccessibleChildrenCount, update
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PREFERRED_INNER_HORIZONTAL

private static final Dimension PREFERRED_INNER_HORIZONTAL

PREFERRED_INNER_VERTICAL

private static final Dimension PREFERRED_INNER_VERTICAL

cachedPercent

private int cachedPercent

cellLength

private int cellLength

cellSpacing

private int cellSpacing

selectionForeground

private Color selectionForeground

selectionBackground

private Color selectionBackground

isIndeterminate

private boolean isIndeterminate

animator

private BasicProgressBarUI.Animator animator

propertyListener

private java.beans.PropertyChangeListener propertyListener

progressBar

protected JProgressBar progressBar

changeListener

protected ChangeListener changeListener

animationIndex

private int animationIndex
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.


numFrames

private int numFrames
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.


repaintInterval

private int repaintInterval
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.


cycleTime

private int cycleTime
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.


ADJUSTTIMER

private static boolean ADJUSTTIMER

DEBUGALL

private static boolean DEBUGALL

DEBUGTIMER

private static boolean DEBUGTIMER

BASICDEBUG

private static boolean BASICDEBUG

LOGSTATS

private static boolean LOGSTATS

startTime

private long startTime

lastLoopTime

private long lastLoopTime

numLoops

private int numLoops

boxRect

private Rectangle boxRect
Used to hold the location and size of the bouncing box (returned by getBox) to be painted.


nextPaintRect

private Rectangle nextPaintRect
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.


componentInnards

private Rectangle componentInnards
The component's painting area, not including the border.


oldComponentInnards

private Rectangle oldComponentInnards

delta

private double delta
For bouncing-box animation, the change in position per frame.


maxPosition

private int maxPosition
Constructor Detail

BasicProgressBarUI

public BasicProgressBarUI()
Method Detail

createUI

public static ComponentUI createUI(JComponent x)
Description copied from class: ComponentUI
Returns an instance of the UI delegate for the specified component. Each subclass must provide its own static createUI method that returns an instance of that UI delegate subclass. If the UI delegate subclass is stateless, it may return an instance that is shared by multiple components. If the UI delegate is stateful, then it should return a new instance per component. The default implementation of this method throws an error, as it should never be invoked.


installUI

public void installUI(JComponent c)
Description copied from class: ComponentUI
Configures the specified component appropriate for the look and feel. This method is invoked when the ComponentUI instance is being installed as the UI delegate on the specified component. This method should completely configure the component for the look and feel, including the following:
  1. Install any default property values for color, fonts, borders, icons, opacity, etc. on the component. Whenever possible, property values initialized by the client program should not be overridden.
  2. Install a LayoutManager on the component if necessary.
  3. Create/add any required sub-components to the component.
  4. Create/install event listeners on the component.
  5. Create/install a PropertyChangeListener on the component in order to detect and respond to component property changes appropriately.
  6. Install keyboard UI (mnemonics, traversal, etc.) on the component.
  7. Initialize any appropriate instance data.

Overrides:
installUI in class ComponentUI
Parameters:
c - the component where this UI delegate is being installed
See Also:
ComponentUI.uninstallUI(javax.swing.JComponent), JComponent.setUI(javax.swing.plaf.ComponentUI), JComponent.updateUI()

uninstallUI

public void uninstallUI(JComponent c)
Description copied from class: ComponentUI
Reverses configuration which was done on the specified component during installUI. This method is invoked when this UIComponent instance is being removed as the UI delegate for the specified component. This method should undo the configuration performed in installUI, being careful to leave the JComponent instance in a clean state (no extraneous listeners, look-and-feel-specific property objects, etc.). This should include the following:
  1. Remove any UI-set borders from the component.
  2. Remove any UI-set layout managers on the component.
  3. Remove any UI-added sub-components from the component.
  4. Remove any UI-added event/property listeners from the component.
  5. Remove any UI-installed keyboard UI from the component.
  6. Nullify any allocated instance data objects to allow for GC.

Overrides:
uninstallUI in class ComponentUI
Parameters:
c - the component from which this UI delegate is being removed; this argument is often ignored, but might be used if the UI object is stateless and shared by multiple components
See Also:
ComponentUI.installUI(javax.swing.JComponent), JComponent.updateUI()

installDefaults

protected void installDefaults()

uninstallDefaults

protected void uninstallDefaults()

installListeners

protected void installListeners()

startAnimationTimer

protected void startAnimationTimer()
Starts the animation thread, creating and initializing it if necessary. This method is invoked when the progress bar changes to indeterminate mode. If you implement your own animation thread, you must override this method.

Since:
1.4

stopAnimationTimer

protected void stopAnimationTimer()
Stops the animation thread. This method is invoked when the progress bar changes from indeterminate to determinate mode and when this UI is uninstalled. If you implement your own animation thread, you must override this method.

Since:
1.4

uninstallListeners

protected void uninstallListeners()
Removes all listeners installed by this object.


getPreferredInnerHorizontal

protected Dimension getPreferredInnerHorizontal()

getPreferredInnerVertical

protected Dimension getPreferredInnerVertical()

getSelectionForeground

protected Color getSelectionForeground()
The "selectionForeground" is the color of the text when it is painted over a filled area of the progress bar.


getSelectionBackground

protected Color getSelectionBackground()
The "selectionBackground" is the color of the text when it is painted over an unfilled area of the progress bar.


getCachedPercent

private int getCachedPercent()

setCachedPercent

private void setCachedPercent(int cachedPercent)

getCellLength

protected int getCellLength()
Returns the width (if HORIZONTAL) or height (if VERTICAL) of each of the indivdual cells/units to be rendered in the progress bar. However, for text rendering simplification and aesthetic considerations, this function will return 1 when the progress string is being rendered.

Returns:
the value representing the spacing between cells
See Also:
setCellLength(int), JProgressBar.isStringPainted()

setCellLength

protected void setCellLength(int cellLen)

getCellSpacing

protected int getCellSpacing()
Returns the spacing between each of the cells/units in the progress bar. However, for text rendering simplification and aesthetic considerations, this function will return 0 when the progress string is being rendered.

Returns:
the value representing the spacing between cells
See Also:
setCellSpacing(int), JProgressBar.isStringPainted()

setCellSpacing

protected void setCellSpacing(int cellSpace)

getAmountFull

protected int getAmountFull(Insets b,
                            int width,
                            int height)
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.


paint

public void paint(Graphics g,
                  JComponent c)
Delegates painting to one of two methods: paintDeterminate or paintIndeterminate.

Overrides:
paint in class ComponentUI
Parameters:
g - the Graphics context in which to paint
c - the component being painted; this argument is often ignored, but might be used if the UI object is stateless and shared by multiple components
See Also:
ComponentUI.update(java.awt.Graphics, javax.swing.JComponent)

getBox

protected Rectangle getBox(Rectangle r)
Stores the position and size of the bouncing box that would be painted for the current animation index in r and returns r. Subclasses that add to the painting performed in this class's implementation of paintIndeterminate -- 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 paintIndeterminate.

Parameters:
r - the Rectangle instance to be modified; may be null
Returns:
null if no box should be drawn; otherwise, returns the passed-in rectangle (if non-null) or a new rectangle
Since:
1.4
See Also:
setAnimationIndex(int)

updateSizes

private void updateSizes()
Updates delta, max position. Assumes componentInnards is correct (e.g. call after sizeChanged()).


getGenericBox

private Rectangle getGenericBox(Rectangle r)
Assumes that the component innards, max position, etc. are up-to-date.


getBoxLength

private int getBoxLength(int availableLength,
                         int otherDimension)
Returns the length of the "bouncing box" to be painted. This method is invoked by the default implementation of paintIndeterminate to get the width (if the progress bar is horizontal) or height (if vertical) of the box. For example:
boxRect.width = getBoxLength(componentInnards.width,
                             componentInnards.height);
 

By default, this method returns the available length divided by 6. Another possibility might be to make the bouncing box a square, which you could implement by overriding this method as follows:

protected double getBoxLength(int availableLength,
                              int otherDimension) {
    return Math.min(availableLength, otherDimension);
}
 

Parameters:
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)
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
Returns:
the size of the box dimension being determined; must be no larger than availableLength
Since:
1.4
See Also:
SwingUtilities.calculateInnerArea(javax.swing.JComponent, java.awt.Rectangle)

paintIndeterminate

protected void paintIndeterminate(Graphics g,
                                  JComponent c)
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.

Since:
1.4
See Also:
paintDeterminate(java.awt.Graphics, javax.swing.JComponent)

paintDeterminate

protected void paintDeterminate(Graphics g,
                                JComponent c)
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.

Since:
1.4
See Also:
paintIndeterminate(java.awt.Graphics, javax.swing.JComponent)

paintString

protected void paintString(Graphics g,
                           int x,
                           int y,
                           int width,
                           int height,
                           int amountFull,
                           Insets b)

paintString

private void paintString(Graphics g,
                         int x,
                         int y,
                         int width,
                         int height,
                         int fillStart,
                         int amountFull,
                         Insets b)
Paints the progress string.

Parameters:
g - Graphics used for drawing.
x - x location of bounding box
y - y location of bounding box
width - width of bounding box
height - height of bounding box
fillStart - start location, in x or y depending on orientation, of the filled portion of the progress bar.
amountFull - size of the fill region, either width or height depending upon orientation.
b - Insets of the progress bar.

getStringPlacement

protected Point getStringPlacement(Graphics g,
                                   String progressString,
                                   int x,
                                   int y,
                                   int width,
                                   int height)
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.


getPreferredSize

public Dimension getPreferredSize(JComponent c)
Description copied from class: ComponentUI
Returns the specified component's preferred size appropriate for the look and feel. If null is returned, the preferred size will be calculated by the component's layout manager instead (this is the preferred approach for any component with a specific layout manager installed). The default implementation of this method returns null.

Overrides:
getPreferredSize in class ComponentUI
Parameters:
c - the component whose preferred size is being queried; this argument is often ignored, but might be used if the UI object is stateless and shared by multiple components
See Also:
JComponent.getPreferredSize(), LayoutManager.preferredLayoutSize(java.awt.Container)

getMinimumSize

public Dimension getMinimumSize(JComponent c)
The Minimum size for this component is 10. The rationale here is that there should be at least one pixel per 10 percent.

Overrides:
getMinimumSize in class ComponentUI
Parameters:
c - the component whose minimum size is being queried; this argument is often ignored, but might be used if the UI object is stateless and shared by multiple components
Returns:
a Dimension object or null
See Also:
JComponent.getMinimumSize(), LayoutManager.minimumLayoutSize(java.awt.Container), ComponentUI.getPreferredSize(javax.swing.JComponent)

getMaximumSize

public Dimension getMaximumSize(JComponent c)
Description copied from class: ComponentUI
Returns the specified component's maximum size appropriate for the look and feel. If null is returned, the maximum size will be calculated by the component's layout manager instead (this is the preferred approach for any component with a specific layout manager installed). The default implementation of this method invokes getPreferredSize and returns that value.

Overrides:
getMaximumSize in class ComponentUI
Parameters:
c - the component whose maximum size is being queried; this argument is often ignored, but might be used if the UI object is stateless and shared by multiple components
Returns:
a Dimension object or null
See Also:
JComponent.getMaximumSize(), LayoutManager2.maximumLayoutSize(java.awt.Container)

getAnimationIndex

protected int getAnimationIndex()
Gets the index of the current animation frame.

Since:
1.4

setAnimationIndex

protected void setAnimationIndex(int newValue)
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 repaint method is invoked.

Parameters:
newValue - the new animation index; no checking is performed on its value
Since:
1.4
See Also:
incrementAnimationIndex()

sizeChanged

private boolean sizeChanged()

incrementAnimationIndex

protected void incrementAnimationIndex()
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 X milliseconds, where X is specified by the "ProgressBar.repaintInterval" UI default.

Since:
1.4
See Also:
setAnimationIndex(int)

getRepaintInterval

private int getRepaintInterval()
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.

Returns:
the repaint interval, in milliseconds

initRepaintInterval

private int initRepaintInterval()

getCycleTime

private int getCycleTime()
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.

Returns:
the cycle time, in milliseconds

initCycleTime

private int initCycleTime()

initIndeterminateDefaults

private void initIndeterminateDefaults()
Initialize cycleTime, repaintInterval, numFrames, animationIndex.


initIndeterminateValues

private void initIndeterminateValues()
Invoked by PropertyChangeHandler before startAnimationTimer(). NOTE: This might not be invoked until after the first paintIndeterminate call.


cleanUpIndeterminateValues

private void cleanUpIndeterminateValues()
Invoked by PropertyChangeHandler after stopAnimationTimer().


initAnimationIndex

private void initAnimationIndex()