1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2005, 2010 IBM Corporation and others.
|
3
|
* All rights reserved. This program and the accompanying materials
|
4
|
* are made available under the terms of the Eclipse Public License v1.0
|
5
|
* which accompanies this distribution, and is available at
|
6
|
* http://www.eclipse.org/legal/epl-v10.html
|
7
|
*
|
8
|
* Contributors:
|
9
|
* IBM Corporation - initial API and implementation
|
10
|
*******************************************************************************/
|
11
|
|
12
|
package org.eclipse.draw2d;
|
13
|
|
14
|
import java.util.HashMap;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
17
|
|
18
|
import org.eclipse.draw2d.geometry.Rectangle;
|
19
|
|
20
|
/**
|
21
|
* Animates the layout of a figure's children. The animator will capture the
|
22
|
* effects of a layout manager, and then play back the placement of children
|
23
|
* using linear interpolation for each child's start and end locations.
|
24
|
* <P>
|
25
|
* To use an animator, hook it as a layout listener for the figure whose layout
|
26
|
* is to be animated, by calling
|
27
|
* {@link IFigure#addLayoutListener(LayoutListener)}. It is not necessary to
|
28
|
* have an animator for every figure in a composition that is undergoing
|
29
|
* animation. For example, if a figure without an animator moves during the
|
30
|
* animation, it will continue to move and layout its children normally during
|
31
|
* each step of the animation.
|
32
|
* <P>
|
33
|
* Animator must be used in conjunction with layouts. If figures are placed
|
34
|
* manually using <code>setBounds()</code>, the animator may not be able to
|
35
|
* track and playback the changes that occur.
|
36
|
*
|
37
|
* @since 3.2
|
38
|
*/
|
39
|
public class LayoutAnimator extends Animator implements LayoutListener {
|
40
|
|
41
|
static final LayoutAnimator INSTANCE = new LayoutAnimator();
|
42
|
|
43
|
/**
|
44
|
* Constructs a new Animator. The default instance ({@link #getDefault()})
|
45
|
* can be used on all figures being animated.
|
46
|
*
|
47
|
* @since 3.2
|
48
|
*/
|
49
|
protected LayoutAnimator() {
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* Returns an object encapsulating the placement of children in a container.
|
54
|
* This method is called to capture both the initial and final states.
|
55
|
*
|
56
|
* @param container
|
57
|
* the container figure
|
58
|
* @return the current state
|
59
|
* @since 3.2
|
60
|
*/
|
61
|
protected Object getCurrentState(IFigure container) {
|
62
|
Map locations = new HashMap();
|
63
|
List children = container.getChildren();
|
64
|
IFigure child;
|
65
|
for (int i = 0; i < children.size(); i++) {
|
66
|
child = (IFigure) children.get(i);
|
67
|
locations.put(child, child.getBounds().getCopy());
|
68
|
}
|
69
|
return locations;
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* Returns the default instance.
|
74
|
*
|
75
|
* @return the default instance
|
76
|
* @since 3.2
|
77
|
*/
|
78
|
public static LayoutAnimator getDefault() {
|
79
|
return INSTANCE;
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* Hooks invalidation in case animation is in progress.
|
84
|
*
|
85
|
* @see LayoutListener#invalidate(IFigure)
|
86
|
*/
|
87
|
public final void invalidate(IFigure container) {
|
88
|
if (Animation.isInitialRecording())
|
89
|
Animation.hookAnimator(container, this);
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* Hooks layout in case animation is in progress.
|
94
|
*
|
95
|
* @see org.eclipse.draw2d.LayoutListener#layout(org.eclipse.draw2d.IFigure)
|
96
|
*/
|
97
|
public final boolean layout(IFigure container) {
|
98
|
if (Animation.isAnimating())
|
99
|
return Animation.hookPlayback(container, this);
|
100
|
return false;
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Plays back the animated layout.
|
105
|
*
|
106
|
* @see Animator#playback(IFigure)
|
107
|
*/
|
108
|
protected boolean playback(IFigure container) {
|
109
|
Map initial = (Map) Animation.getInitialState(this, container);
|
110
|
Map ending = (Map) Animation.getFinalState(this, container);
|
111
|
if (initial == null)
|
112
|
return false;
|
113
|
List children = container.getChildren();
|
114
|
|
115
|
float progress = Animation.getProgress();
|
116
|
float ssergorp = 1 - progress;
|
117
|
|
118
|
Rectangle rect1, rect2;
|
119
|
|
120
|
for (int i = 0; i < children.size(); i++) {
|
121
|
IFigure child = (IFigure) children.get(i);
|
122
|
rect1 = (Rectangle) initial.get(child);
|
123
|
rect2 = (Rectangle) ending.get(child);
|
124
|
|
125
|
// TODO need to change this to hide the figure until the end.
|
126
|
if (rect1 == null)
|
127
|
continue;
|
128
|
child.setBounds(new Rectangle(Math.round(progress * rect2.x
|
129
|
+ ssergorp * rect1.x), Math.round(progress * rect2.y
|
130
|
+ ssergorp * rect1.y), Math.round(progress * rect2.width
|
131
|
+ ssergorp * rect1.width), Math.round(progress
|
132
|
* rect2.height + ssergorp * rect1.height)));
|
133
|
}
|
134
|
return true;
|
135
|
}
|
136
|
|
137
|
/**
|
138
|
* Hooks post layout in case animation is in progress.
|
139
|
*
|
140
|
* @see LayoutListener#postLayout(IFigure)
|
141
|
*/
|
142
|
public final void postLayout(IFigure container) {
|
143
|
if (Animation.isFinalRecording())
|
144
|
Animation.hookNeedsCapture(container, this);
|
145
|
}
|
146
|
|
147
|
/**
|
148
|
* This callback is unused. Reserved for possible future use.
|
149
|
*
|
150
|
* @see LayoutListener#remove(IFigure)
|
151
|
*/
|
152
|
public final void remove(IFigure child) {
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* This callback is unused. Reserved for possible future use.
|
157
|
*
|
158
|
* @see LayoutListener#setConstraint(IFigure, Object)
|
159
|
*/
|
160
|
public final void setConstraint(IFigure child, Object constraint) {
|
161
|
}
|
162
|
|
163
|
}
|