nl.coderight.jazz
Class Task<V>

java.lang.Object
  extended by nl.coderight.jazz.Task<V>

public abstract class Task<V>
extends Object

Use Task for (long running) background processes.

 Example:
 
 public class LoadContactsTask extends Task>{

        public List process() throws Exception {
                return DatabaseService.getInstance().getContacts();
        }

        protected void onStart() {
                ProgressEvent progressEvent = new ProgressEvent(ProgressEventType.START);
                progressEvent.setMessage("Loading contacts...");
                progressEvent.setIndeterminate(true);
                postEvent(progressEvent);
        }
        
        protected void onFinish() {
                postEvent(new ProgressEvent(ProgressEventType.STOP));
                
                ContactEvent contactEvent = new ContactEvent(ContactEventType.LOAD_SUCCESS);
                contactEvent.setContacts(getResult());
                postEvent(contactEvent);
        }
        
        protected void onError() {
                MessageEvent error = new MessageEvent(MessageEventType.ERROR);
                error.setMessage(getError().getMessage());
                postEvent(error);                       
        }
 }
 

See Also:
Controller.executeTask(Task), Controller.scheduleTask(Task, nl.coderight.jazz.Task.Schedule)

Nested Class Summary
static class Task.FixedDelaySchedule
           
static class Task.FixedRateSchedule
           
static class Task.TaskState
          Enumeration of the possible Task states.
 
Constructor Summary
Task()
           
 
Method Summary
 void cancel()
          Cancels running task.
 ProgressMonitor createProgressMonitor(String message, InputStream inputStream)
          Creates a ProgressMonitor.
 ProgressMonitor createProgressMonitor(String message, String note, int min, int max)
          Creates a standard ProgressMonitor.
 Exception getError()
          Returns the error (if set).
 V getResult()
          Returns the result from process.
 Task.TaskState getState()
          Returns current state.
 boolean hasError()
          Returns true if an error is set.
 boolean isCanceled()
          Returns true if task was canceled.
protected  void onCanceled()
          Invoked when task is canceled.
protected  void onError()
          Invoked when an error occurred.
protected  void onFinish()
          Invoked when task is finished.
protected  void onStart()
          Invoked when task starts running.
 void postEvent(Event event)
          Use to post events like ProgressEvents and message events.
abstract  V process()
          Performs a (long running) operation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Task

public Task()
Method Detail

getState

public final Task.TaskState getState()
Returns current state.

See Also:
Task.TaskState

getError

public final Exception getError()
Returns the error (if set).

Returns:
Exception

hasError

public final boolean hasError()
Returns true if an error is set.


getResult

public final V getResult()
Returns the result from process.


cancel

public final void cancel()
Cancels running task.


isCanceled

public final boolean isCanceled()
Returns true if task was canceled.


postEvent

public final void postEvent(Event event)
Use to post events like ProgressEvents and message events.


createProgressMonitor

public final ProgressMonitor createProgressMonitor(String message,
                                                   String note,
                                                   int min,
                                                   int max)
Creates a standard ProgressMonitor.

See Also:
ProgressMonitor

createProgressMonitor

public final ProgressMonitor createProgressMonitor(String message,
                                                   InputStream inputStream)
Creates a ProgressMonitor.

See Also:
ProgressMonitor

process

public abstract V process()
                   throws Exception
Performs a (long running) operation.

Returns:
result or null if none
Throws:
Exception

onStart

protected void onStart()
Invoked when task starts running.


onFinish

protected void onFinish()
Invoked when task is finished.


onError

protected void onError()
Invoked when an error occurred.


onCanceled

protected void onCanceled()
Invoked when task is canceled.



Copyright 2010 CodeRight, All rights reserved.