ehsbe CommonsTM
Version 1.1.0rc3

com.ehsbe.commons.gui.util
Class StatusTask<T,V>

java.lang.Object
  extended by javax.swing.SwingWorker<T,V>
      extended by com.ehsbe.commons.gui.util.StatusTask<T,V>
Type Parameters:
T - the result type returned by this StatusTask's doInBackground and get methods
V - the type used for carrying out intermediate results by this StatusTask's publish and process methods
All Implemented Interfaces:
Runnable, Future<T>, RunnableFuture<T>

public abstract class StatusTask<T,V>
extends SwingWorker<T,V>

An message property enhanced SwingWorker, which lets one easily notify property change listeners from "message" events. Usually used to inform a statusbar about the message, which should be displayed.

Version:
$Revision: 499 $
Author:
Jens Elkner

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.SwingWorker
SwingWorker.StateValue
 
Field Summary
static String MSG_PROPNAME
          the property name to use for notifying the statusbar about changes wrt.
static String PROGRESS_PROPNAME
          the property name to use for notifying the statusbar about changes wrt.
 
Constructor Summary
StatusTask()
           
 
Method Summary
protected  T doInBackground()
          Computes a result by calling doInBackgroundSafe().
protected  T doInBackgroundSafe()
          Computes a result, or throws an exception if unable to do so.
 Throwable getException()
          Get the last throwable, which has been caught when executing doInBackgroundSafe() or getSafe() or getSafe(long, TimeUnit).
 String getMessage()
          Get the current message to be displayed in the statusbar.
 String getName()
          Get the name of this task.
 boolean getPaintProgressString()
          Check, whether the progress bar string should be painted for this task.
 String getProgressString()
          Gets the value of the progress bar string.
 T getSafe()
          Calls SwingWorker.get() but catches any throwable.
 T getSafe(long timeout, TimeUnit unit)
          Calls SwingWorker.get(long, TimeUnit) but catches any throwable.
protected  void setMessage(String message)
          Set the message, which should be displayed on the statusbar and notifies the statusbar by sending a property change "message",
 void setName(String name)
          Set the name of this task
protected  void setPaintProgressString(boolean enable)
          Set, whether the progress bar string should be painted for this task.
protected  void setProgress(int value, String sval)
          A slightly more efficient version of the call sequence setProgressString(String); SwingWorker.setProgress(int).
protected  void setProgressString(String value)
          Sets the value of the progress bar string.
 
Methods inherited from class javax.swing.SwingWorker
addPropertyChangeListener, cancel, done, execute, firePropertyChange, get, get, getProgress, getPropertyChangeSupport, getState, isCancelled, isDone, process, publish, removePropertyChangeListener, run, setProgress
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MSG_PROPNAME

public static final String MSG_PROPNAME
the property name to use for notifying the statusbar about changes wrt. the message to display.

See Also:
Constant Field Values

PROGRESS_PROPNAME

public static final String PROGRESS_PROPNAME
the property name to use for notifying the statusbar about changes wrt. the progressbar.

See Also:
Constant Field Values
Constructor Detail

StatusTask

public StatusTask()
Method Detail

setMessage

protected void setMessage(String message)
Set the message, which should be displayed on the statusbar and notifies the statusbar by sending a property change "message",

Parameters:
message - the message to display. If null it means, clear the related display.

getMessage

public String getMessage()
Get the current message to be displayed in the statusbar. Usually called, if this worker gets back to the top of the "statusbar display stack".

Returns:
the message set or null

setPaintProgressString

protected void setPaintProgressString(boolean enable)
Set, whether the progress bar string should be painted for this task. Fires a "progress" property change event with boolean values on success.

Parameters:
enable - if true paint the progress bar string, otherwise not.

getPaintProgressString

public boolean getPaintProgressString()
Check, whether the progress bar string should be painted for this task.

Returns:
true if the progress bar string should be painted.

setProgressString

protected void setProgressString(String value)
Sets the value of the progress bar string. Fires a "progress" property change event with Strings on success.

Parameters:
value - value to show or null to reset to the default.
See Also:
setPaintProgressString(boolean), JProgressBar.setString(String)

getProgressString

public String getProgressString()
Gets the value of the progress bar string.

Returns:
null if the default format is used, the string value otherwise.
See Also:
getPaintProgressString(), JProgressBar.getString()

setProgress

protected void setProgress(int value,
                           String sval)
A slightly more efficient version of the call sequence setProgressString(String); SwingWorker.setProgress(int). Fires a "progress" property change event with either String or Integers on success, whereby the later may indicate a change wrt. to the displayed String value as well.

Parameters:
value - value to show or null to reset to the default.
sval - progress bar value to set.

setName

public void setName(String name)
Set the name of this task

Parameters:
name - name to set.

getName

public String getName()
Get the name of this task.

Returns:
the name.

doInBackground

protected T doInBackground()
Computes a result by calling doInBackgroundSafe(). If a Throwable gets fired during its exection, it gets caught, its root cause logged a null returned. So this is actually a convinience method to avoid a lot of boiler plate code.

Note: this method is executed only once in a background thread.

You may overwrite it, if you wanna do your own exception handling or to postpone the throwing of the caught Throwable, until the SwingWorker.get() method or one of its derivations gets called (the default behavior of SwingWorker).

Specified by:
doInBackground in class SwingWorker<T,V>
Returns:
the computed result or null if a throwable has been caught.
See Also:
doInBackgroundSafe(), SwingWorker.doInBackground()

doInBackgroundSafe

protected T doInBackgroundSafe()
Computes a result, or throws an exception if unable to do so.

Per default it gets called by doInBackground(), only.

Overwrite this method instead of doInBackground(), if you want to get rid of the boiler plate code for Exception handling.

Returns:
null by default
See Also:
doInBackground()

getSafe

public T getSafe()
Calls SwingWorker.get() but catches any throwable. Thus it is a convinience method to avoid boiler plate code.

Returns:
null if a throwable has been caught, the result of the background comptation otherwise.
See Also:
doInBackground(), SwingWorker.get()

getSafe

public T getSafe(long timeout,
                 TimeUnit unit)
Calls SwingWorker.get(long, TimeUnit) but catches any throwable. Thus it is a convinience method to avoid boiler plate code.

Parameters:
timeout - the maximum time to wait
unit - the time unit of the timeout argument
Returns:
null if a throwable has been caught, the result of the background comptation otherwise.
See Also:
doInBackground(), SwingWorker.get()

getException

public Throwable getException()
Get the last throwable, which has been caught when executing doInBackgroundSafe() or getSafe() or getSafe(long, TimeUnit).

Returns:
null if no throwable has been caugth, the last caught throwable otherwise.

ehsbe CommonsTM
Version 1.1.0rc3

Copyright ©2005-2009 EHSBE, Walther-Rathenau-Str. 58, 39104 Magdeburg, Sachsen-Anhalt, Germany. All Rights Reserved.