KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * AnimationTask.java
3  *
4  * Created on 31 May 2003, 03:05
5  */

6
7 package com.thoughtriver.open.vectorvisuals.task;
8
9 /**
10  * Implementations of <CODE>AnimationTask</CODE> are submitted to the <CODE>TaskManager</CODE>
11  * for execution. All of the tasks that are currently executing will have their
12  * <CODE>update</CODE> method called in turn using a round-robin scheme,
13  * allowing a simulated parallelism.
14  *
15  * @author Brandon Franklin
16  * @version $Date: 2006/11/25 09:15:41 $
17  */

18 public abstract class AnimationTask {
19
20     /**
21      * The number of milliseconds for which this task will persist and be
22      * performed
23      */

24     private double duration;
25
26     /** The time on the System Clock when this task was started */
27     private double startTime;
28
29     /** The time on the System Clock when this task was last updated */
30     private double updateTime;
31
32     /**
33      * The <CODE>TaskManager</CODE> that is handling the execution of this
34      * task
35      */

36     private TaskManager manager;
37
38     /**
39      * Creates a new instance of <CODE>AnimationTask</CODE> that will execute
40      * over the course of the specified number of milliseconds. If the duration
41      * is set to a value less than 0, the task will execute indefinitely.
42      *
43      * @param duration the number of milliseconds for which this task will
44      * persist and be performed, or a value less than 0 if the task is to
45      * last indefinitely
46      */

47     public AnimationTask(final int duration) {
48         this.duration = duration;
49         startTime = -1;
50         updateTime = -1;
51     }
52
53     /**
54      * Sets the <CODE>TaskManager</CODE> that is handling the execution of
55      * this task.
56      *
57      * @param manager the <CODE>TaskManager</CODE> that is handling the
58      * execution of this task
59      */

60     protected void setManager(final TaskManager manager) {
61         this.manager = manager;
62     }
63
64     /**
65      * Gets the <CODE>TaskManager</CODE> that is handling the execution of
66      * this task.
67      *
68      * @return the <CODE>TaskManager</CODE> that is handling the execution of
69      * this task
70      */

71     public TaskManager getManager() {
72         return manager;
73     }
74
75     /**
76      * Causes the task to change its state to reflect progress based on the
77      * amount of time that has passed since the task was started. The first time
78      * this method is called will be considered the starting time of the task.
79      *
80      * @return true if the task is still running, or false if it has completed
81      * and should be removed from the update queue
82      */

83     synchronized public boolean update() {
84
85         // Handle the initial case
86
if (startTime < 0) {
87             startTime = (System.nanoTime() / 1000000); // milliseconds
88

89             // Give subclasses a chance to do custom setup
90
init();
91
92             // Notify listeners
93
TaskEvent event = new TaskEvent(this, ExecutionState.running);
94             for (TaskListener listener : getManager().getListeners()) {
95                 listener.taskStateChanged(event);
96             }
97         }
98         // Handle indefinite durations
99
else if (duration < 0) {
100             startTime = updateTime;
101         }
102
103         updateTime = (System.nanoTime() / 1000000); // milliseconds
104
double diff = updateTime - startTime;
105
106         // Check for a "stop" call
107
if (duration == 0) {
108             return false;
109
110             // Handle indefinite durations
111
}
112         else if (duration < 0) {
113             update(diff);
114
115             // Handle regular durations
116
}
117         else {
118
119             // Check for expired duration
120
if (diff > duration) {
121                 update(1.0); // Ensures that the task reaches a "completed"
122
// state
123
return false;
124             }
125
126             // Call the implementation with the correct scaled progress value
127
double progress = diff / duration;
128             update(progress);
129         }
130
131         return true;
132     }
133
134     /**
135      * Immediately sets the duration of this task to 0, effectively causing it
136      * to stop running. This will work for both indefinite and normal tasks.
137      */

138     synchronized public void stop() {
139         duration = 0;
140     }
141
142     /**
143      * Called the first time the task is updated, which may be at a later time
144      * than when it was initially created. This method is therefore a good place
145      * to do things like check on the current state of the display and so forth.
146      */

147     protected void init() {
148     }
149
150     /**
151      * Causes the task to change its state to reflect the amount of progress
152      * reported. The parameter is a double with valid values between 0.0 and
153      * 1.0. To facilitate multitasking and simulated parallelism, this method
154      * should complete and return as quickly as possible to allow other tasks to
155      * be updated. Tasks that have an indefinite duration will always receive a
156      * parameter that is the number of milliseconds that have passed since the
157      * last update.
158      *
159      * @param progress the amount of progress this <CODE>AnimationTask</CODE>
160      * should be changed to reflect
161      */

162     protected abstract void update(final double progress);
163
164 }
165
Popular Tags