KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > task > TaskChain


1 /*
2  * TaskChain.java
3  *
4  * Created on 7 October 2004, 12:12
5  */

6
7 package com.thoughtriver.open.vectorvisuals.task;
8
9 import java.util.*;
10
11 /**
12  * This task can be used to create a sequence of other <CODE>AnimationTask</CODE>s
13  * that will be executed one after another. The task is considered "indefinite"
14  * in duration, but will stop itself after the last chained task is complete.
15  *
16  * @author Brandon Franklin
17  * @version $Date: 2006/11/25 09:15:42 $
18  */

19 public class TaskChain extends AnimationTask {
20
21     /** The chain of <CODE>AnimationTask</CODE>s to be executed */
22     private final List<AnimationTask> chain;
23
24     /**
25      * Creates a new instance of <CODE>TaskChain</CODE>.
26      */

27     public TaskChain() {
28         super(-1);
29         chain = Collections.synchronizedList(new LinkedList<AnimationTask>());
30     }
31
32     /**
33      * Adds the supplied task to the end of the chain of tasks to be executed in
34      * sequence.
35      *
36      * @param task the new task to add
37      */

38     public void addTask(final AnimationTask task) {
39         task.setManager(getManager());
40         chain.add(task);
41     }
42
43     /**
44      * Removes the supplied task from the chain of tasks to be executed in
45      * sequence.
46      *
47      * @param task the task to remove
48      */

49     public void removeTask(final AnimationTask task) {
50         chain.remove(task);
51     }
52
53     /**
54      * Sets the <CODE>TaskManager</CODE> that is handling the execution of
55      * this chain of tasks. This will result in <CODE>setManager</CODE> being
56      * called on each of the contained tasks, as well.
57      *
58      * @param manager the <CODE>TaskManager</CODE> that is handling the
59      * execution of this chain of tasks
60      */

61     @Override JavaDoc
62     protected void setManager(final TaskManager manager) {
63         super.setManager(manager);
64
65         for (AnimationTask task : chain) {
66             task.setManager(manager);
67         }
68     }
69
70     /**
71      * Causes the task to change its state to reflect an amount of change based
72      * on the supplied number of milliseconds having passed.
73      *
74      * @param progress the amount of progress this <CODE>AnimationTask</CODE>
75      * should be changed to reflect
76      */

77     @Override JavaDoc
78     protected void update(@SuppressWarnings JavaDoc("unused")
79     final double progress) {
80
81         synchronized (chain) {
82
83             // Signal a stop if all tasks are done
84
if (chain.isEmpty()) {
85                 stop();
86                 return;
87             }
88
89             // Execute the first task in the chain, removing it when it
90
// completes
91
AnimationTask task = chain.get(0);
92             if (!task.update()) {
93                 chain.remove(0);
94
95                 // Notify listeners of the task that's ending
96
TaskEvent event = new TaskEvent(task, ExecutionState.stopped);
97                 for (TaskListener listener : getManager().getListeners()) {
98                     listener.taskStateChanged(event);
99                 }
100             }
101
102         }
103     }
104
105 }
106
Popular Tags