Debian Codegraph @

src-jdk8/com/jidesoft/plaf/windows/AnimationController.java

raw
1 /*
2 * @(#)AnimationController.java 4/11/2017
3 *
4 * Copyright 2002 - 2017 JIDE Software Inc. All rights reserved.
5 */
6
7 package com.jidesoft.plaf.windows;
8
9 import com.jidesoft.plaf.windows.TMSchema.Part;
10 import com.jidesoft.plaf.windows.TMSchema.Prop;
11 import com.jidesoft.plaf.windows.TMSchema.State;
12 import com.jidesoft.plaf.windows.XPStyle.Skin;
13 import sun.awt.AppContext;
14 import sun.security.action.GetBooleanAction;
15 import sun.swing.UIClientPropertyKey;
16
17 import javax.swing.*;
18 import java.awt.*;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.ActionListener;
21 import java.beans.PropertyChangeEvent;
22 import java.beans.PropertyChangeListener;
23 import java.security.AccessController;
24 import java.util.ArrayList;
25 import java.util.EnumMap;
26 import java.util.Map;
27 import java.util.WeakHashMap;
28
29 import static com.jidesoft.plaf.windows.TMSchema.State.*;
30
31 /**
32 * Copied from JDK source code and modified to provide additional integration between JIDE components and native windows
33 * L&F.
34 */
35 class AnimationController implements ActionListener, PropertyChangeListener {
36
37 private final static boolean VISTA_ANIMATION_DISABLED =
38 AccessController.doPrivileged(new GetBooleanAction("swing.disablevistaanimation"));
39
40
41 private final static Object ANIMATION_CONTROLLER_KEY =
42 new StringBuilder("ANIMATION_CONTROLLER_KEY");
43
44 private final Map<JComponent, Map<Part, AnimationState>> animationStateMap =
45 new WeakHashMap<JComponent, Map<Part, AnimationState>>();
46
47 //this timer is used to cause repaint on animated components
48 //30 repaints per second should give smooth animation affect
49 private final javax.swing.Timer timer =
50 new javax.swing.Timer(1000 / 30, this);
51
52 private static synchronized AnimationController getAnimationController() {
53 AppContext appContext = AppContext.getAppContext();
54 Object obj = appContext.get(ANIMATION_CONTROLLER_KEY);
55 if (obj == null) {
56 obj = new AnimationController();
57 appContext.put(ANIMATION_CONTROLLER_KEY, obj);
58 }
59 return (AnimationController) obj;
60 }
61
62 private AnimationController() {
63 timer.setRepeats(true);
64 timer.setCoalesce(true);
65 //we need to dispose the controller on l&f change
66 UIManager.addPropertyChangeListener(this);
67 }
68
69 private static void triggerAnimation(JComponent c,
70 Part part, State newState) {
71 if (c instanceof JTabbedPane
72 || part == Part.TP_BUTTON) {
73 //idk: we can not handle tabs animation because
74 //the same (component,part) is used to handle all the tabs
75 //and we can not track the states
76 //Vista theme might have transition duration for toolbar buttons
77 //but native application does not seem to animate them
78 return;
79 }
80 AnimationController controller =
81 AnimationController.getAnimationController();
82 State oldState = controller.getState(c, part);
83 if (oldState != newState) {
84 controller.putState(c, part, newState);
85 if (newState == State.DEFAULTED) {
86 // it seems for DEFAULTED button state Vista does animation from
87 // HOT
88 oldState = State.HOT;
89 }
90 if (oldState != null) {
91 long duration;
92 if (newState == State.DEFAULTED) {
93 //Only button might have DEFAULTED state
94 //idk: do not know how to get the value from Vista
95 //one second seems plausible value
96 duration = 1000;
97 }
98 else {
99 duration = XPStyle.getXP().getThemeTransitionDuration(
100 c, part,
101 normalizeState(oldState),
102 normalizeState(newState),
103 Prop.TRANSITIONDURATIONS);
104 }
105 controller.startAnimation(c, part, oldState, newState, duration);
106 }
107 }
108 }
109
110 // for scrollbar up, down, left and right button pictures are
111 // defined by states. It seems that theme has duration defined
112 // only for up button states thus we doing this translation here.
113 private static State normalizeState(State state) {
114 State rv;
115 switch (state) {
116 case DOWNPRESSED:
117 /* falls through */
118 case LEFTPRESSED:
119 /* falls through */
120 case RIGHTPRESSED:
121 rv = UPPRESSED;
122 break;
123
124 case DOWNDISABLED:
125 /* falls through */
126 case LEFTDISABLED:
127 /* falls through */
128 case RIGHTDISABLED:
129 rv = UPDISABLED;
130 break;
131
132 case DOWNHOT:
133 /* falls through */
134 case LEFTHOT:
135 /* falls through */
136 case RIGHTHOT:
137 rv = UPHOT;
138 break;
139
140 case DOWNNORMAL:
141 /* falls through */
142 case LEFTNORMAL:
143 /* falls through */
144 case RIGHTNORMAL:
145 rv = UPNORMAL;
146 break;
147
148 default:
149 rv = state;
150 break;
151 }
152 return rv;
153 }
154
155 private synchronized State getState(JComponent component, Part part) {
156 State rv = null;
157 Object tmpObject =
158 component.getClientProperty(PartUIClientPropertyKey.getKey(part));
159 if (tmpObject instanceof State) {
160 rv = (State) tmpObject;
161 }
162 return rv;
163 }
164
165 private synchronized void putState(JComponent component, Part part,
166 State state) {
167 component.putClientProperty(PartUIClientPropertyKey.getKey(part),
168 state);
169 }
170
171 private synchronized void startAnimation(JComponent component,
172 Part part,
173 State startState,
174 State endState,
175 long millis) {
176 boolean isForwardAndReverse = false;
177 if (endState == State.DEFAULTED) {
178 isForwardAndReverse = true;
179 }
180 Map<Part, AnimationState> map = animationStateMap.get(component);
181 if (millis <= 0) {
182 if (map != null) {
183 map.remove(part);
184 if (map.size() == 0) {
185 animationStateMap.remove(component);
186 }
187 }
188 return;
189 }
190 if (map == null) {
191 map = new EnumMap<Part, AnimationState>(Part.class);
192 animationStateMap.put(component, map);
193 }
194 map.put(part,
195 new AnimationState(startState, millis, isForwardAndReverse));
196 if (!timer.isRunning()) {
197 timer.start();
198 }
199 }
200
201 static void paintSkin(JComponent component, Skin skin,
202 Graphics g, int dx, int dy, int dw, int dh, State state) {
203 if (VISTA_ANIMATION_DISABLED) {
204 skin.paintSkinRaw(g, dx, dy, dw, dh, state);
205 return;
206 }
207 triggerAnimation(component, skin.part, state);
208 AnimationController controller = getAnimationController();
209 synchronized (controller) {
210 AnimationState animationState = null;
211 Map<Part, AnimationState> map =
212 controller.animationStateMap.get(component);
213 if (map != null) {
214 animationState = map.get(skin.part);
215 }
216 if (animationState != null) {
217 animationState.paintSkin(skin, g, dx, dy, dw, dh, state);
218 }
219 else {
220 skin.paintSkinRaw(g, dx, dy, dw, dh, state);
221 }
222 }
223 }
224
225 /**
226 * As of Java 10, com.sun.java.swing.plaf.windows.WindowsLookAndFeel is no longer available on macOS thus
227 * "instanceof WindowsLookAndFeel" directives will result in a NoClassDefFoundError during runtime. This method
228 * was introduced to avoid this exception.
229 *
230 * @param lnf
231 * @return true if it is a WindowsLookAndFeel.
232 */
233 public static boolean isWindowsLookAndFeel(LookAndFeel lnf) {
234 if (lnf == null) {
235 return false;
236 }
237 else {
238 try {
239 Class c = Class.forName("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
240 return c.isInstance(lnf);
241 }
242 catch (ClassNotFoundException cnfe) {
243 // if it is not possible to load the Windows LnF class, the
244 // given lnf instance cannot be an instance of the Windows
245 // LnF class
246 return false;
247 }
248 }
249 }
250
251 public synchronized void propertyChange(PropertyChangeEvent e) {
252 if ("lookAndFeel" == e.getPropertyName()
253 && !isWindowsLookAndFeel((LookAndFeel) e.getNewValue())) {
254 dispose();
255 }
256 }
257
258 public synchronized void actionPerformed(ActionEvent e) {
259 java.util.List<JComponent> componentsToRemove = null;
260 java.util.List<Part> partsToRemove = null;
261 for (JComponent component : animationStateMap.keySet()) {
262 component.repaint();
263 if (partsToRemove != null) {
264 partsToRemove.clear();
265 }
266 Map<Part, AnimationState> map = animationStateMap.get(component);
267 if (!component.isShowing()
268 || map == null
269 || map.size() == 0) {
270 if (componentsToRemove == null) {
271 componentsToRemove = new ArrayList<JComponent>();
272 }
273 componentsToRemove.add(component);
274 continue;
275 }
276 for (Part part : map.keySet()) {
277 if (map.get(part).isDone()) {
278 if (partsToRemove == null) {
279 partsToRemove = new ArrayList<Part>();
280 }
281 partsToRemove.add(part);
282 }
283 }
284 if (partsToRemove != null) {
285 if (partsToRemove.size() == map.size()) {
286 //animation is done for the component
287 if (componentsToRemove == null) {
288 componentsToRemove = new ArrayList<JComponent>();
289 }
290 componentsToRemove.add(component);
291 }
292 else {
293 for (Part part : partsToRemove) {
294 map.remove(part);
295 }
296 }
297 }
298 }
299 if (componentsToRemove != null) {
300 for (JComponent component : componentsToRemove) {
301 animationStateMap.remove(component);
302 }
303 }
304 if (animationStateMap.size() == 0) {
305 timer.stop();
306 }
307 }
308
309 private synchronized void dispose() {
310 timer.stop();
311 UIManager.removePropertyChangeListener(this);
312 synchronized (AnimationController.class) {
313 AppContext.getAppContext()
314 .put(ANIMATION_CONTROLLER_KEY, null);
315 }
316 }
317
318 private static class AnimationState {
319 private final State startState;
320
321 //animation duration in nanoseconds
322 private final long duration;
323
324 //animatin start time in nanoseconds
325 private long startTime;
326
327 //direction the alpha value is changing
328 //forward - from 0 to 1
329 //!forward - from 1 to 0
330 private boolean isForward = true;
331
332 //if isForwardAndReverse the animation continually goes
333 //forward and reverse. alpha value is changing from 0 to 1 then
334 //from 1 to 0 and so forth
335 private boolean isForwardAndReverse;
336
337 private float progress;
338
339 AnimationState(final State startState,
340 final long milliseconds,
341 boolean isForwardAndReverse) {
342 assert startState != null && milliseconds > 0;
343 assert SwingUtilities.isEventDispatchThread();
344
345 this.startState = startState;
346 this.duration = milliseconds * 1000000;
347 this.startTime = System.nanoTime();
348 this.isForwardAndReverse = isForwardAndReverse;
349 progress = 0f;
350 }
351
352 private void updateProgress() {
353 assert SwingUtilities.isEventDispatchThread();
354
355 if (isDone()) {
356 return;
357 }
358 long currentTime = System.nanoTime();
359
360 progress = ((float) (currentTime - startTime))
361 / duration;
362 progress = Math.max(progress, 0); //in case time was reset
363 if (progress >= 1) {
364 progress = 1;
365 if (isForwardAndReverse) {
366 startTime = currentTime;
367 progress = 0;
368 isForward = !isForward;
369 }
370 }
371 }
372
373 void paintSkin(Skin skin, Graphics _g,
374 int dx, int dy, int dw, int dh, State state) {
375 assert SwingUtilities.isEventDispatchThread();
376
377 updateProgress();
378 if (!isDone()) {
379 Graphics2D g = (Graphics2D) _g.create();
380 skin.paintSkinRaw(g, dx, dy, dw, dh, startState);
381 float alpha;
382 if (isForward) {
383 alpha = progress;
384 }
385 else {
386 alpha = 1 - progress;
387 }
388 g.setComposite(AlphaComposite.SrcOver.derive(alpha));
389 skin.paintSkinRaw(g, dx, dy, dw, dh, state);
390 g.dispose();
391 }
392 else {
393 skin.paintSkinRaw(_g, dx, dy, dw, dh, state);
394 }
395 }
396
397 boolean isDone() {
398 assert SwingUtilities.isEventDispatchThread();
399
400 return progress >= 1;
401 }
402 }
403
404 private static class PartUIClientPropertyKey
405 implements UIClientPropertyKey {
406
407 private static final Map<Part, PartUIClientPropertyKey> map =
408 new EnumMap<Part, PartUIClientPropertyKey>(Part.class);
409
410 static synchronized PartUIClientPropertyKey getKey(Part part) {
411 PartUIClientPropertyKey rv = map.get(part);
412 if (rv == null) {
413 rv = new PartUIClientPropertyKey(part);
414 map.put(part, rv);
415 }
416 return rv;
417 }
418
419 private final Part part;
420
421 private PartUIClientPropertyKey(Part part) {
422 this.part = part;
423 }
424
425 public String toString() {
426 return part.toString();
427 }
428 }
429 }