edu.cs.ai.kreator.control
Class KreatorWorker<T>

java.lang.Object
  extended by javax.swing.SwingWorker<T,KreatorWorker.Status>
      extended by edu.cs.ai.kreator.control.KreatorWorker<T>
Type Parameters:
T - The return value of myDoInBackground()
All Implemented Interfaces:
java.lang.Runnable, java.util.concurrent.Future<T>, java.util.concurrent.RunnableFuture<T>
Direct Known Subclasses:
EventKreatorWorker, KreatorScriptEngine.ScriptWorker

public abstract class KreatorWorker<T>
extends javax.swing.SwingWorker<T,KreatorWorker.Status>

A KReatorWorker represents a thread, which can be executed by our WorkerController. Information about its status and progress are then displayed in the GUI. Every worker should implement this class and be passed to WorkerController for execution.

Author:
Niklas Wulf

Nested Class Summary
 class KreatorWorker.Status
           
 
Nested classes/interfaces inherited from class javax.swing.SwingWorker
javax.swing.SwingWorker.StateValue
 
Field Summary
static org.apache.log4j.Logger LOG
           
 
Constructor Summary
KreatorWorker(KreatorConsole console)
          Creates a new worker, which should be executed by WorkerController.
 
Method Summary
protected  T doInBackground()
          This is called upon execution of this worker.
protected  void done()
          This is called when execution finishes.
 void forceKill()
          Aborts the thread immediately.
 void forceResume()
          Resumes the thread immediately.
 void forceSuspend()
          Suspends the thread immediately.
 KreatorConsole getConsole()
          Returns the workers connected KreatorConsole or null if the worker is not connected to a console.
protected  KreatorWorker.Status getStatus()
          Returns the current progress and status message.
protected abstract  T myDoInBackground()
          This is called upon execution of this worker.
protected  void myDone()
          This is called when execution finishes.
protected  void pleaseAbort()
          Tells the thread of this worker to abort.
protected  void pleaseWait()
          Tells the thread of this worker to wait until #resume() is called.
protected  void process(java.util.List<KreatorWorker.Status> progress)
          Displays the information in the main user interface.
 void publish(java.lang.String msg, int progress)
          May be called by every thread, to publish some progress and status information.
protected  void resetShouldAbort()
          After having told the worker to abort, one may use this function to revoke this plead.
protected  void resume()
          Tells the thread to resume.
 boolean shouldAbort()
          Returns true if the thread of this worker should stop it's execution.
 boolean shouldWait()
          Returns true if the thread of this worker should pause.
 
Methods inherited from class javax.swing.SwingWorker
addPropertyChangeListener, cancel, execute, firePropertyChange, get, get, getProgress, getPropertyChangeSupport, getState, isCancelled, isDone, publish, removePropertyChangeListener, run, setProgress
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG

public static final org.apache.log4j.Logger LOG
Constructor Detail

KreatorWorker

public KreatorWorker(KreatorConsole console)
Creates a new worker, which should be executed by WorkerController. Connects this worker's output with the given ViewStatus. Every worker should extend

Parameters:
output -
Method Detail

doInBackground

protected T doInBackground()
                    throws AbortedExecutionException
This is called upon execution of this worker.
First registers this worker with the worker controller.
We do this, so that the controller knows, which thread belongs to which worker.

Then starts myDoInBackground(), which has to be implemented by every KReatorWorker.

Specified by:
doInBackground in class javax.swing.SwingWorker<T,KreatorWorker.Status>
Throws:
AbortedExecutionException
See Also:
WorkerController.registerWorker(KreatorWorker), WorkerController.getMyWorker()

myDoInBackground

protected abstract T myDoInBackground()
                               throws AbortedExecutionException
This is called upon execution of this worker.
Has to be implemented by every worker.

Returns:
Throws:
java.lang.Exception
AbortedExecutionException

done

protected void done()
This is called when execution finishes. Successful or not.
First unregisters this worker with the worker controller. Then calls myDone(), which may be implemented by any KReatorWorker.

Overrides:
done in class javax.swing.SwingWorker<T,KreatorWorker.Status>

myDone

protected void myDone()
This is called when execution finishes. Successful or not.
May be implemented by any KReatorWorker.


publish

public void publish(java.lang.String msg,
                    int progress)
May be called by every thread, to publish some progress and status information. This information is then displayed in the main user interface.

Parameters:
msg - a status message
progress - the threads progress.
  • -1 for "Ready"
  • 0 for indetermined
  • 1-100 for actual progress.

process

protected void process(java.util.List<KreatorWorker.Status> progress)
Displays the information in the main user interface.

Overrides:
process in class javax.swing.SwingWorker<T,KreatorWorker.Status>

getConsole

public KreatorConsole getConsole()
Returns the workers connected KreatorConsole or null if the worker is not connected to a console.

Returns:
the workers connected KreatorConsole or null if the worker is not connected to a console

getStatus

protected KreatorWorker.Status getStatus()
Returns the current progress and status message.

Returns:
the current progress and status message

shouldWait

public boolean shouldWait()
Returns true if the thread of this worker should pause.

Returns:
true if the thread of this worker should pause.

pleaseWait

protected void pleaseWait()
Tells the thread of this worker to wait until #resume() is called.


resume

protected void resume()
Tells the thread to resume.


shouldAbort

public boolean shouldAbort()
Returns true if the thread of this worker should stop it's execution.

Returns:
true if the thread of this worker should stop it's execution.

pleaseAbort

protected void pleaseAbort()
Tells the thread of this worker to abort.


resetShouldAbort

protected void resetShouldAbort()
After having told the worker to abort, one may use this function to revoke this plead. This is useful if you have reacted to the AbortedExecutionException from inside the working method and want to continue working.

See Also:
WorkerController#cancleWorkerAbortion(KreatorWorker)

forceKill

public void forceKill()
Aborts the thread immediately.


forceSuspend

public void forceSuspend()
Suspends the thread immediately.


forceResume

public void forceResume()
Resumes the thread immediately.