| 1 |
/* |
| 2 |
* Copyright (c) 2005-2010 Laf-Widget Kirill Grouchnikov. 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 Laf-Widget Kirill Grouchnikov 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 |
package org.pushingpixels.lafwidget.animation; |
| 31 |
|
| 32 |
import java.awt.Component; |
| 33 |
import java.util.*; |
| 34 |
|
| 35 |
import org.pushingpixels.trident.Timeline; |
| 36 |
import org.pushingpixels.trident.ease.Spline; |
| 37 |
import org.pushingpixels.trident.ease.TimelineEase; |
| 38 |
|
| 39 |
/** |
| 40 |
* Animation configuration manager. |
| 41 |
* |
| 42 |
* @author Kirill Grouchnikov |
| 43 |
* @since 2.1 |
| 44 |
*/ |
| 45 |
public class AnimationConfigurationManager { |
| 46 |
private static final Spline DEFAULT_EASE = new Spline(0.5f); |
| 47 |
|
| 48 |
/** |
| 49 |
* Singleton instance. |
| 50 |
*/ |
| 51 |
private static AnimationConfigurationManager instance; |
| 52 |
|
| 53 |
private long timelineDuration; |
| 54 |
|
| 55 |
/** |
| 56 |
* Contains {@link AnimationFacet} instances. |
| 57 |
*/ |
| 58 |
private Set<AnimationFacet> globalAllowed; |
| 59 |
|
| 60 |
/** |
| 61 |
* Key - {@link AnimationFacet}, value - set of {@link Class} instances. |
| 62 |
*/ |
| 63 |
private Map<AnimationFacet, Set<Class<?>>> classAllowed; |
| 64 |
|
| 65 |
/** |
| 66 |
* Key - {@link AnimationFacet}, value - set of {@link Class} instances. |
| 67 |
*/ |
| 68 |
private Map<AnimationFacet, Set<Class<?>>> classDisallowed; |
| 69 |
|
| 70 |
/** |
| 71 |
* Key - {@link AnimationFacet}, value - set of {@link Component} instances. |
| 72 |
*/ |
| 73 |
private Map<AnimationFacet, Set<Component>> instanceAllowed; |
| 74 |
|
| 75 |
/** |
| 76 |
* Key - {@link AnimationFacet}, value - set of {@link Component} instances. |
| 77 |
*/ |
| 78 |
private Map<AnimationFacet, Set<Component>> instanceDisallowed; |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns the configuration manager instance. |
| 82 |
* |
| 83 |
* @return Configuration manager instance. |
| 84 |
*/ |
| 85 |
public static synchronized AnimationConfigurationManager getInstance() { |
| 86 |
if (instance == null) { |
| 87 |
instance = new AnimationConfigurationManager(); |
| 88 |
} |
| 89 |
return instance; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Creates a new instance. |
| 94 |
*/ |
| 95 |
private AnimationConfigurationManager() { |
| 96 |
this.globalAllowed = new HashSet<AnimationFacet>(); |
| 97 |
this.classAllowed = new HashMap<AnimationFacet, Set<Class<?>>>(); |
| 98 |
this.classDisallowed = new HashMap<AnimationFacet, Set<Class<?>>>(); |
| 99 |
this.instanceAllowed = new HashMap<AnimationFacet, Set<Component>>(); |
| 100 |
this.instanceDisallowed = new HashMap<AnimationFacet, Set<Component>>(); |
| 101 |
this.timelineDuration = 200; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Allows animations of the specified facet on all controls. |
| 106 |
* |
| 107 |
* @param animationFacet |
| 108 |
* Animation facet to allow. |
| 109 |
*/ |
| 110 |
public synchronized void allowAnimations(AnimationFacet animationFacet) { |
| 111 |
this.globalAllowed.add(animationFacet); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Allows animations of the specified facet on all controls of specified |
| 116 |
* class. |
| 117 |
* |
| 118 |
* @param animationFacet |
| 119 |
* Animation facet to allow. |
| 120 |
* @param clazz |
| 121 |
* Control class for allowing the animation facet. |
| 122 |
*/ |
| 123 |
public synchronized void allowAnimations(AnimationFacet animationFacet, |
| 124 |
Class<?> clazz) { |
| 125 |
Set<Class<?>> existingAllowed = this.classAllowed.get(animationFacet); |
| 126 |
if (existingAllowed == null) { |
| 127 |
existingAllowed = new HashSet<Class<?>>(); |
| 128 |
this.classAllowed.put(animationFacet, existingAllowed); |
| 129 |
} |
| 130 |
existingAllowed.add(clazz); |
| 131 |
|
| 132 |
Set<Class<?>> existingDisallowed = this.classDisallowed |
| 133 |
.get(animationFacet); |
| 134 |
if (existingDisallowed != null) { |
| 135 |
existingDisallowed.remove(clazz); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Allows animations of the specified facet on all controls of specified |
| 141 |
* classes. |
| 142 |
* |
| 143 |
* @param animationFacet |
| 144 |
* Animation facet to allow. |
| 145 |
* @param clazz |
| 146 |
* Control classes for allowing the animation facet. |
| 147 |
*/ |
| 148 |
public synchronized void allowAnimations(AnimationFacet animationFacet, |
| 149 |
Class<?>[] clazz) { |
| 150 |
for (int i = 0; i < clazz.length; i++) { |
| 151 |
allowAnimations(animationFacet, clazz[i]); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Allows animations of the specified facet on the specified control. |
| 157 |
* |
| 158 |
* @param animationFacet |
| 159 |
* Animation facet to allow. |
| 160 |
* @param comp |
| 161 |
* Control for allowing the animation facet. |
| 162 |
*/ |
| 163 |
public synchronized void allowAnimations(AnimationFacet animationFacet, |
| 164 |
Component comp) { |
| 165 |
Set<Component> existingAllowed = this.instanceAllowed |
| 166 |
.get(animationFacet); |
| 167 |
if (existingAllowed == null) { |
| 168 |
existingAllowed = new HashSet<Component>(); |
| 169 |
this.instanceAllowed.put(animationFacet, existingAllowed); |
| 170 |
} |
| 171 |
existingAllowed.add(comp); |
| 172 |
|
| 173 |
Set<Component> existingDisallowed = this.instanceDisallowed |
| 174 |
.get(animationFacet); |
| 175 |
if (existingDisallowed != null) { |
| 176 |
existingDisallowed.remove(comp); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Disallows animations of the specified facet on all controls. |
| 182 |
* |
| 183 |
* @param animationFacet |
| 184 |
* Animation facet to disallow. |
| 185 |
*/ |
| 186 |
public synchronized void disallowAnimations(AnimationFacet animationFacet) { |
| 187 |
this.globalAllowed.remove(animationFacet); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Disallows animations of the specified facet on all controls of specified |
| 192 |
* class. |
| 193 |
* |
| 194 |
* @param animationFacet |
| 195 |
* Animation facet to disallow. |
| 196 |
* @param clazz |
| 197 |
* Control class for disallowing the animation facet. |
| 198 |
*/ |
| 199 |
public synchronized void disallowAnimations(AnimationFacet animationFacet, |
| 200 |
Class<?> clazz) { |
| 201 |
Set<Class<?>> existingAllowed = this.classAllowed.get(animationFacet); |
| 202 |
if (existingAllowed != null) { |
| 203 |
existingAllowed.remove(clazz); |
| 204 |
if (existingAllowed.size() == 0) |
| 205 |
this.classAllowed.remove(animationFacet); |
| 206 |
} |
| 207 |
|
| 208 |
Set<Class<?>> existingDisallowed = this.classDisallowed |
| 209 |
.get(animationFacet); |
| 210 |
if (existingDisallowed == null) { |
| 211 |
existingDisallowed = new HashSet<Class<?>>(); |
| 212 |
this.classDisallowed.put(animationFacet, existingDisallowed); |
| 213 |
} |
| 214 |
existingDisallowed.add(clazz); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Disallows animations of the specified facet on all controls of specified |
| 219 |
* classes. |
| 220 |
* |
| 221 |
* @param animationFacet |
| 222 |
* Animation facet to disallow. |
| 223 |
* @param clazz |
| 224 |
* Control classes for disallowing the animation facet. |
| 225 |
*/ |
| 226 |
public synchronized void disallowAnimations(AnimationFacet animationFacet, |
| 227 |
Class<?>[] clazz) { |
| 228 |
for (int i = 0; i < clazz.length; i++) { |
| 229 |
disallowAnimations(animationFacet, clazz[i]); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Disallows animations of the specified facet on the specified control. |
| 235 |
* |
| 236 |
* @param animationFacet |
| 237 |
* Animation facet to disallow. |
| 238 |
* @param comp |
| 239 |
* Control for disallowing the animation facet. |
| 240 |
*/ |
| 241 |
public synchronized void disallowAnimations(AnimationFacet animationFacet, |
| 242 |
Component comp) { |
| 243 |
Set<Component> existingAllowed = this.instanceAllowed |
| 244 |
.get(animationFacet); |
| 245 |
if (existingAllowed != null) { |
| 246 |
existingAllowed.remove(comp); |
| 247 |
if (existingAllowed.size() == 0) |
| 248 |
this.instanceAllowed.remove(animationFacet); |
| 249 |
} |
| 250 |
|
| 251 |
Set<Component> existingDisallowed = this.instanceDisallowed |
| 252 |
.get(animationFacet); |
| 253 |
if (existingDisallowed == null) { |
| 254 |
existingDisallowed = new HashSet<Component>(); |
| 255 |
this.instanceDisallowed.put(animationFacet, existingDisallowed); |
| 256 |
} |
| 257 |
existingDisallowed.add(comp); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Checks whether the specified animation facet is allowed on the specified |
| 262 |
* component. |
| 263 |
* |
| 264 |
* @param animationFacet |
| 265 |
* Animation facet. |
| 266 |
* @param comp |
| 267 |
* Component. Can be <code>null</code>. |
| 268 |
* @return <code>true</code> if the specified animation facet is allowed on |
| 269 |
* the specified component, <code>false</code> otherwise. |
| 270 |
*/ |
| 271 |
public synchronized boolean isAnimationAllowed( |
| 272 |
AnimationFacet animationFacet, Component comp) { |
| 273 |
Set<Component> instanceDisallowed = this.instanceDisallowed |
| 274 |
.get(animationFacet); |
| 275 |
if (instanceDisallowed != null) { |
| 276 |
if (instanceDisallowed.contains(comp)) |
| 277 |
return false; |
| 278 |
} |
| 279 |
Set<Component> instanceAllowed = this.instanceAllowed |
| 280 |
.get(animationFacet); |
| 281 |
if (instanceAllowed != null) { |
| 282 |
if (instanceAllowed.contains(comp)) |
| 283 |
return true; |
| 284 |
} |
| 285 |
|
| 286 |
if (comp != null) { |
| 287 |
Class<?> clazz = comp.getClass(); |
| 288 |
Set<Class<?>> classAllowed = this.classAllowed.get(animationFacet); |
| 289 |
Set<Class<?>> classDisallowed = this.classDisallowed |
| 290 |
.get(animationFacet); |
| 291 |
if (classDisallowed != null) { |
| 292 |
for (Class<?> disallowed : classDisallowed) { |
| 293 |
if (disallowed.isAssignableFrom(clazz)) |
| 294 |
return false; |
| 295 |
} |
| 296 |
} |
| 297 |
if (classAllowed != null) { |
| 298 |
for (Class<?> allowed : classAllowed) { |
| 299 |
if (allowed.isAssignableFrom(clazz)) |
| 300 |
return true; |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
if (this.globalAllowed.contains(animationFacet)) |
| 305 |
return true; |
| 306 |
return false; |
| 307 |
} |
| 308 |
|
| 309 |
public void setTimelineDuration(long timelineDuration) { |
| 310 |
this.timelineDuration = timelineDuration; |
| 311 |
} |
| 312 |
|
| 313 |
public long getTimelineDuration() { |
| 314 |
return timelineDuration; |
| 315 |
} |
| 316 |
|
| 317 |
public void configureTimeline(Timeline timeline) { |
| 318 |
timeline.setDuration(this.timelineDuration); |
| 319 |
timeline.setEase(DEFAULT_EASE); |
| 320 |
} |
| 321 |
|
| 322 |
public void configureModifiedTimeline(Timeline timeline) { |
| 323 |
timeline.setDuration(5 * this.timelineDuration); |
| 324 |
timeline.setEase(new TimelineEase() { |
| 325 |
@Override |
| 326 |
public float map(float durationFraction) { |
| 327 |
if (durationFraction < 0.8f) { |
| 328 |
return 0.0f; |
| 329 |
} |
| 330 |
return 5.0f * (durationFraction - 0.8f); |
| 331 |
} |
| 332 |
}); |
| 333 |
} |
| 334 |
} |