KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > animation > AbstractAnimation


1 /*
2  * Copyright (c) 2001-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.animation;
32
33 import java.util.Iterator JavaDoc;
34 import java.util.LinkedList JavaDoc;
35 import java.util.List JavaDoc;
36
37 /**
38  * An abstract class that minimizes the effort required to implement
39  * the {@link Animation} interface. Defines the duration and freezed state
40  * and provides a listener list.
41  *
42  * @author Karsten Lentzsch
43  * @version $Revision: 1.3 $
44  */

45 public abstract class AbstractAnimation implements Animation {
46
47     private final long duration;
48     private final boolean freezed;
49     private final List JavaDoc listenerList = new LinkedList JavaDoc();
50
51     private boolean active = false;
52
53
54     // Instance Creation *****************************************************
55

56     /**
57      * Constructs an <code>Animation</code> with the specified duration.
58      *
59      * @param duration the animation's duration
60      */

61     protected AbstractAnimation(long duration) {
62         this(duration, false);
63     }
64
65     /**
66      * Constructs an <code>Animation</code> with the specified duration
67      * and freezed mode.
68      *
69      * @param duration the animation's duration
70      * @param freezed true indicates that the effect will be retained after
71      * the animation is finished, false resets the effect to the time 0
72      */

73     protected AbstractAnimation(long duration, boolean freezed) {
74         this.duration = duration;
75         this.freezed = freezed;
76     }
77
78     
79     // ***********************************************************************
80

81     /**
82      * Returns this animation's duration.
83      *
84      * @return this animation's duration
85      */

86     public final long duration() {
87         return duration;
88     }
89
90     /**
91      * Answers whether the animation effect should be freezed after
92      * we exceeded the animation's duration. If this is not the case,
93      * the animation effect of time 0 will be set.
94      *
95      * @return true indicates that the effect will be retained if the
96      * animation duration exceeded; false indicates that the effect
97      * will be reset
98      */

99     public final boolean isFreezed() {
100         return freezed;
101     }
102
103     /**
104      * Applies the animation effect for the given time to the animation target.
105      *
106      * @param time the time used to determine the animation effect
107      */

108     abstract protected void applyEffect(long time);
109
110     /**
111      * Performs the animation at the given time: applies the animation
112      * effect to the animation target, fires appropriate events,
113      * and resets the effect if we exceeded the animations duration.
114      *
115      * @param time the time used to determine the animation effect
116      */

117     public void animate(long time) {
118         if (time >= duration) {
119             if (active) {
120                 applyEffect(isFreezed() ? duration - 1 : 0);
121                 fireAnimationStopped(time);
122                 active = false;
123             }
124             return;
125         }
126
127         if (!active) {
128             active = true;
129             fireAnimationStarted(time);
130         }
131         applyEffect(time);
132     }
133
134     /**
135      * Adds an <code>AnimationListener</code> to this animation.
136      *
137      * @param listener the <code>AnimationListener</code> to add
138      */

139     public final void addAnimationListener(AnimationListener listener) {
140         listenerList.add(listener);
141     }
142
143     /**
144      * Removes an <code>AnimationListener</code> to this animation.
145      *
146      * @param listener the <code>AnimationListener</code> to remove
147      */

148     public final void removeAnimationListener(AnimationListener listener) {
149         listenerList.remove(listener);
150     }
151
152     /**
153      * Fires an event that indicates that the animation has been started
154      * at the specified time.
155      *
156      * @param time the time that will be reported in the event
157      */

158     protected final void fireAnimationStarted(long time) {
159         AnimationEvent e =
160             new AnimationEvent(this, AnimationEvent.STARTED, time);
161         for (Iterator JavaDoc i = listenerList.iterator(); i.hasNext();) {
162             AnimationListener listener = (AnimationListener) i.next();
163             listener.animationStarted(e);
164         }
165     }
166
167     /**
168      * Fires an event that indicates that the animation has been stopped
169      * at the specified time.
170      *
171      * @param time the time that will be reported in the event
172      */

173     protected final void fireAnimationStopped(long time) {
174         AnimationEvent e =
175             new AnimationEvent(this, AnimationEvent.STOPPED, time);
176         for (Iterator JavaDoc i = listenerList.iterator(); i.hasNext();) {
177             AnimationListener listener = (AnimationListener) i.next();
178             listener.animationStopped(e);
179         }
180     }
181
182     /**
183      * Returns a string representation for this animation.
184      *
185      * @return a string representation for this animation
186      */

187     public String JavaDoc toString() {
188         return "["
189             + getClass().getName()
190             + "; duration="
191             + duration
192             + "; active="
193             + active+
194             + ']';
195     }
196 }
Popular Tags