2
* Copyright (c) 2005-2010 Laf-Widget Kirill Grouchnikov. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
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.
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.
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.
30
package org.pushingpixels.lafwidget.animation;
32
import java.awt.Component;
35
import org.pushingpixels.trident.Timeline;
36
import org.pushingpixels.trident.ease.Spline;
37
import org.pushingpixels.trident.ease.TimelineEase;
40
* Animation configuration manager.
42
* @author Kirill Grouchnikov
45
public class AnimationConfigurationManager {
46
private static final Spline DEFAULT_EASE = new Spline(0.5f);
51
private static AnimationConfigurationManager instance;
53
private long timelineDuration;
56
* Contains {@link AnimationFacet} instances.
58
private Set<AnimationFacet> globalAllowed;
61
* Key - {@link AnimationFacet}, value - set of {@link Class} instances.
63
private Map<AnimationFacet, Set<Class<?>>> classAllowed;
66
* Key - {@link AnimationFacet}, value - set of {@link Class} instances.
68
private Map<AnimationFacet, Set<Class<?>>> classDisallowed;
71
* Key - {@link AnimationFacet}, value - set of {@link Component} instances.
73
private Map<AnimationFacet, Set<Component>> instanceAllowed;
76
* Key - {@link AnimationFacet}, value - set of {@link Component} instances.
78
private Map<AnimationFacet, Set<Component>> instanceDisallowed;
81
* Returns the configuration manager instance.
83
* @return Configuration manager instance.
85
public static synchronized AnimationConfigurationManager getInstance() {
86
if (instance == null) {
87
instance = new AnimationConfigurationManager();
93
* Creates a new instance.
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;
105
* Allows animations of the specified facet on all controls.
107
* @param animationFacet
108
* Animation facet to allow.
110
public synchronized void allowAnimations(AnimationFacet animationFacet) {
111
this.globalAllowed.add(animationFacet);
115
* Allows animations of the specified facet on all controls of specified
118
* @param animationFacet
119
* Animation facet to allow.
121
* Control class for allowing the animation facet.
123
public synchronized void allowAnimations(AnimationFacet animationFacet,
125
Set<Class<?>> existingAllowed = this.classAllowed.get(animationFacet);
126
if (existingAllowed == null) {
127
existingAllowed = new HashSet<Class<?>>();
128
this.classAllowed.put(animationFacet, existingAllowed);
130
existingAllowed.add(clazz);
132
Set<Class<?>> existingDisallowed = this.classDisallowed
133
.get(animationFacet);
134
if (existingDisallowed != null) {
135
existingDisallowed.remove(clazz);
140
* Allows animations of the specified facet on all controls of specified
143
* @param animationFacet
144
* Animation facet to allow.
146
* Control classes for allowing the animation facet.
148
public synchronized void allowAnimations(AnimationFacet animationFacet,
150
for (int i = 0; i < clazz.length; i++) {
151
allowAnimations(animationFacet, clazz[i]);
156
* Allows animations of the specified facet on the specified control.
158
* @param animationFacet
159
* Animation facet to allow.
161
* Control for allowing the animation facet.
163
public synchronized void allowAnimations(AnimationFacet animationFacet,
165
Set<Component> existingAllowed = this.instanceAllowed
166
.get(animationFacet);
167
if (existingAllowed == null) {
168
existingAllowed = new HashSet<Component>();
169
this.instanceAllowed.put(animationFacet, existingAllowed);
171
existingAllowed.add(comp);
173
Set<Component> existingDisallowed = this.instanceDisallowed
174
.get(animationFacet);
175
if (existingDisallowed != null) {
176
existingDisallowed.remove(comp);
181
* Disallows animations of the specified facet on all controls.
183
* @param animationFacet
184
* Animation facet to disallow.
186
public synchronized void disallowAnimations(AnimationFacet animationFacet) {
187
this.globalAllowed.remove(animationFacet);
191
* Disallows animations of the specified facet on all controls of specified
194
* @param animationFacet
195
* Animation facet to disallow.
197
* Control class for disallowing the animation facet.
199
public synchronized void disallowAnimations(AnimationFacet animationFacet,
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);
208
Set<Class<?>> existingDisallowed = this.classDisallowed
209
.get(animationFacet);
210
if (existingDisallowed == null) {
211
existingDisallowed = new HashSet<Class<?>>();
212
this.classDisallowed.put(animationFacet, existingDisallowed);
214
existingDisallowed.add(clazz);
218
* Disallows animations of the specified facet on all controls of specified
221
* @param animationFacet
222
* Animation facet to disallow.
224
* Control classes for disallowing the animation facet.
226
public synchronized void disallowAnimations(AnimationFacet animationFacet,
228
for (int i = 0; i < clazz.length; i++) {
229
disallowAnimations(animationFacet, clazz[i]);
234
* Disallows animations of the specified facet on the specified control.
236
* @param animationFacet
237
* Animation facet to disallow.
239
* Control for disallowing the animation facet.
241
public synchronized void disallowAnimations(AnimationFacet animationFacet,
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);
251
Set<Component> existingDisallowed = this.instanceDisallowed
252
.get(animationFacet);
253
if (existingDisallowed == null) {
254
existingDisallowed = new HashSet<Component>();
255
this.instanceDisallowed.put(animationFacet, existingDisallowed);
257
existingDisallowed.add(comp);
261
* Checks whether the specified animation facet is allowed on the specified
264
* @param animationFacet
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.
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))
279
Set<Component> instanceAllowed = this.instanceAllowed
280
.get(animationFacet);
281
if (instanceAllowed != null) {
282
if (instanceAllowed.contains(comp))
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))
297
if (classAllowed != null) {
298
for (Class<?> allowed : classAllowed) {
299
if (allowed.isAssignableFrom(clazz))
304
if (this.globalAllowed.contains(animationFacet))
309
public void setTimelineDuration(long timelineDuration) {
310
this.timelineDuration = timelineDuration;
313
public long getTimelineDuration() {
314
return timelineDuration;
317
public void configureTimeline(Timeline timeline) {
318
timeline.setDuration(this.timelineDuration);
319
timeline.setEase(DEFAULT_EASE);
322
public void configureModifiedTimeline(Timeline timeline) {
323
timeline.setDuration(5 * this.timelineDuration);
324
timeline.setEase(new TimelineEase() {
326
public float map(float durationFraction) {
327
if (durationFraction < 0.8f) {
330
return 5.0f * (durationFraction - 0.8f);