1 30 31 package com.jgoodies.animation; 32 33 import java.util.Iterator ; 34 import java.util.LinkedList ; 35 import java.util.List ; 36 37 45 public abstract class AbstractAnimation implements Animation { 46 47 private final long duration; 48 private final boolean freezed; 49 private final List listenerList = new LinkedList (); 50 51 private boolean active = false; 52 53 54 56 61 protected AbstractAnimation(long duration) { 62 this(duration, false); 63 } 64 65 73 protected AbstractAnimation(long duration, boolean freezed) { 74 this.duration = duration; 75 this.freezed = freezed; 76 } 77 78 79 81 86 public final long duration() { 87 return duration; 88 } 89 90 99 public final boolean isFreezed() { 100 return freezed; 101 } 102 103 108 abstract protected void applyEffect(long time); 109 110 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 139 public final void addAnimationListener(AnimationListener listener) { 140 listenerList.add(listener); 141 } 142 143 148 public final void removeAnimationListener(AnimationListener listener) { 149 listenerList.remove(listener); 150 } 151 152 158 protected final void fireAnimationStarted(long time) { 159 AnimationEvent e = 160 new AnimationEvent(this, AnimationEvent.STARTED, time); 161 for (Iterator i = listenerList.iterator(); i.hasNext();) { 162 AnimationListener listener = (AnimationListener) i.next(); 163 listener.animationStarted(e); 164 } 165 } 166 167 173 protected final void fireAnimationStopped(long time) { 174 AnimationEvent e = 175 new AnimationEvent(this, AnimationEvent.STOPPED, time); 176 for (Iterator i = listenerList.iterator(); i.hasNext();) { 177 AnimationListener listener = (AnimationListener) i.next(); 178 listener.animationStopped(e); 179 } 180 } 181 182 187 public String toString() { 188 return "[" 189 + getClass().getName() 190 + "; duration=" 191 + duration 192 + "; active=" 193 + active+ 194 + ']'; 195 } 196 } | Popular Tags |