|
1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.swing; |
|
27 |
|
28 import java.awt.*; |
|
29 import org.jdesktop.animation.timing.*; |
|
30 |
|
31 /** |
|
32 * The {@code ResizeAnimator} class animates the resize of a {@code Component}. |
|
33 * <p> |
|
34 * See {@link #setTargetSize} for details on how this resize is configured. |
|
35 */ |
|
36 @SuppressWarnings({"serial"}) |
|
37 public class ResizeAnimator implements TimingTarget { |
|
38 // |
|
39 // Static data |
|
40 // |
|
41 |
|
42 public static final int _SPEED = 1000; |
|
43 public static final int _MAX_DURATION = 300; |
|
44 public static final int _MIN_DURATION = 0; |
|
45 |
|
46 // |
|
47 // Instance data |
|
48 // |
|
49 |
|
50 private Animator animator; |
|
51 private Component component; |
|
52 private Dimension startSize; |
|
53 private Dimension targetSize; |
|
54 private int speed = _SPEED; |
|
55 private int minDuration = _MIN_DURATION; |
|
56 private int maxDuration = _MAX_DURATION; |
|
57 |
|
58 // |
|
59 // Constructors |
|
60 // |
|
61 |
|
62 /** |
|
63 * Constructs a {@code ResizeAnimator}. |
|
64 * |
|
65 * @param component |
|
66 * the {@code Component} to resize |
|
67 */ |
|
68 public ResizeAnimator(Component component) { |
|
69 this.component = component; |
|
70 animator = new Animator(1, this); |
|
71 } |
|
72 |
|
73 /** |
|
74 * Constructs a {@code ResizeAnimator}, immediately resizing the given |
|
75 * {@code Component}. |
|
76 * |
|
77 * @param component |
|
78 * the {@code Component} to resize |
|
79 * |
|
80 * @param size |
|
81 * the new size of the {@code Component} |
|
82 */ |
|
83 public ResizeAnimator(Component component, Dimension size) { |
|
84 this(component); |
|
85 setTargetSize(size); |
|
86 } |
|
87 |
|
88 /** |
|
89 * Constructs a {@code ResizeAnimator}. |
|
90 * |
|
91 * @param component |
|
92 * the {@code Component} to resize |
|
93 * |
|
94 * @param speed |
|
95 * the speed of the resize, in pixels per second |
|
96 */ |
|
97 public ResizeAnimator(Component component, int speed) { |
|
98 this(component); |
|
99 setSpeed(speed); |
|
100 } |
|
101 |
|
102 /** |
|
103 * Constructs a {@code ResizeAnimator}, immediately resizing the given |
|
104 * {@code Component}. |
|
105 * |
|
106 * @param component |
|
107 * the {@code Component} to resize |
|
108 * |
|
109 * @param speed |
|
110 * the speed of the resize, in pixels per second |
|
111 * |
|
112 * @param size |
|
113 * the new size of the {@code Component} |
|
114 */ |
|
115 public ResizeAnimator(Component component, int speed, Dimension size) { |
|
116 this(component, speed); |
|
117 setTargetSize(size); |
|
118 } |
|
119 |
|
120 // |
|
121 // TimingTarget methods |
|
122 // |
|
123 |
|
124 @Override |
|
125 public void begin() { |
|
126 } |
|
127 |
|
128 @Override |
|
129 public void end() { |
|
130 } |
|
131 |
|
132 @Override |
|
133 public void repeat() { |
|
134 } |
|
135 |
|
136 @Override |
|
137 public void timingEvent(float fraction) { |
|
138 int width = startSize.width + |
|
139 (int)(fraction * (targetSize.width - startSize.width)); |
|
140 |
|
141 int height = startSize.height + |
|
142 (int)(fraction * (targetSize.height - startSize.height)); |
|
143 |
|
144 component.setSize(width, height); |
|
145 } |
|
146 |
|
147 // |
|
148 // ResizeAnimator methods |
|
149 // |
|
150 |
|
151 /** |
|
152 * Gets the {@code Animator} responsible for the animation. |
|
153 */ |
|
154 public Animator getAnimator() { |
|
155 return animator; |
|
156 } |
|
157 |
|
158 /** |
|
159 * Gets the maximum time (in milliseconds) that the resize animation should |
|
160 * take. The default is 300. |
|
161 */ |
|
162 public int getMaxDuration() { |
|
163 return maxDuration; |
|
164 } |
|
165 |
|
166 /** |
|
167 * Gets the minimum time (in milliseconds) that the resize animation should |
|
168 * take. The default is 0. |
|
169 */ |
|
170 public int getMinDuration() { |
|
171 return minDuration; |
|
172 } |
|
173 |
|
174 /** |
|
175 * Gets the speed of the resize, in pixels per second. |
|
176 */ |
|
177 public int getSpeed() { |
|
178 return speed; |
|
179 } |
|
180 |
|
181 /** |
|
182 * Gets the {@code Dimension} passed to the most recent call to {@link |
|
183 * #setTargetSize}. |
|
184 */ |
|
185 public Dimension getTargetSize() { |
|
186 return targetSize; |
|
187 } |
|
188 |
|
189 /** |
|
190 * Sets the maximum time (in milliseconds) that the resize animation should |
|
191 * take. |
|
192 */ |
|
193 public void setMaxDuration(int maxDuration) { |
|
194 this.maxDuration = maxDuration; |
|
195 } |
|
196 |
|
197 /** |
|
198 * Sets the minimum time (in milliseconds) that the resize animation should |
|
199 * take. |
|
200 */ |
|
201 public void setMinDuration(int minDuration) { |
|
202 this.minDuration = minDuration; |
|
203 } |
|
204 |
|
205 /** |
|
206 * Sets the speed of the resize, in pixels per second. |
|
207 */ |
|
208 public void setSpeed(int speed) { |
|
209 this.speed = speed; |
|
210 } |
|
211 |
|
212 /** |
|
213 * Animates the resize of the {@code Component} to the given size. The |
|
214 * duration of the animation is calculated based on the size of the change |
|
215 * and the {@link #getSpeed speed} property. This value is adjusted to be |
|
216 * no less than the {@link #getMinDuration minimum duration}, and no greater |
|
217 * than the {@link #getMaxDuration maximum duration}. |
|
218 */ |
|
219 public void setTargetSize(Dimension targetSize) { |
|
220 if (animator.isRunning()) { |
|
221 animator.stop(); |
|
222 } |
|
223 |
|
224 this.targetSize = targetSize; |
|
225 Dimension currentSize = component.getSize(); |
|
226 |
|
227 if (!currentSize.equals(targetSize)) { |
|
228 int deltaWidth = Math.abs(targetSize.width - currentSize.width); |
|
229 int deltaHeight = Math.abs(targetSize.height - currentSize.height); |
|
230 int maxDelta = Math.max(deltaWidth, deltaHeight); |
|
231 |
|
232 int duration = (1000 * maxDelta) / speed; |
|
233 int maxDuration = getMaxDuration(); |
|
234 |
|
235 if (maxDuration != 0 && duration > maxDuration) { |
|
236 duration = maxDuration; |
|
237 } else |
|
238 |
|
239 if (minDuration != 0 && duration < minDuration) { |
|
240 duration = minDuration; |
|
241 } |
|
242 |
|
243 animator.setDuration(duration); |
|
244 |
|
245 startSize = currentSize; |
|
246 animator.start(); |
|
247 } |
|
248 } |
|
249 } |