1 /* 2 * aTunes 3 * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors 4 * 5 * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors 6 * 7 * http://www.atunes.org 8 * http://sourceforge.net/projects/atunes 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * as published by the Free Software Foundation; either version 2 13 * of the License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 */ 20 21 package net.sourceforge.atunes.gui; 22 23 import java.awt.Window; 24 import java.awt.event.ActionEvent; 25 import java.awt.event.ActionListener; 26 import java.lang.ref.WeakReference; 27 28 import javax.swing.Timer; 29 30 /** 31 * Helper class for window fading 32 */ 33 public class WindowFader { 34 35 private final WeakReference<Window> window; 36 private Timer fadeInTimer; 37 private Timer fadeOutTimer; 38 private int currentOpacity; 39 private final int duration; 40 41 /** 42 * @param window 43 * @param duration 44 */ WindowFader(final Window window, final int duration)45 public WindowFader(final Window window, final int duration) { 46 this.window = new WeakReference<Window>(window); 47 this.duration = duration; 48 } 49 50 /** 51 * Starts the fade-in of the window 52 */ fadeIn()53 public void fadeIn() { 54 stop(); 55 Window w = this.window.get(); 56 if (w != null) { 57 GuiUtils.setWindowOpacity(w, this.currentOpacity / 100.0f); 58 w.setVisible(true); 59 } 60 this.fadeInTimer = new Timer(this.duration, new ActionListener() { 61 62 @Override 63 public void actionPerformed(final ActionEvent e) { 64 WindowFader.this.currentOpacity += 20; 65 if (WindowFader.this.currentOpacity <= 100) { 66 Window w = WindowFader.this.window.get(); 67 if (w != null) { 68 GuiUtils.setWindowOpacity(w, 69 WindowFader.this.currentOpacity / 100.0f); 70 w.repaint(); 71 } 72 } else { 73 fadeInFinished(); 74 WindowFader.this.currentOpacity = 100; 75 WindowFader.this.fadeInTimer.stop(); 76 } 77 } 78 79 }); 80 this.fadeInTimer.setRepeats(true); 81 this.fadeInTimer.start(); 82 } 83 84 /** 85 * Starts the fade-out of the window 86 */ fadeOut()87 public void fadeOut() { 88 stop(); 89 this.fadeOutTimer = new Timer(this.duration, new ActionListener() { 90 91 @Override 92 public void actionPerformed(final ActionEvent e) { 93 WindowFader.this.currentOpacity -= 10; 94 if (WindowFader.this.currentOpacity >= 0) { 95 Window w = WindowFader.this.window.get(); 96 if (w != null) { 97 GuiUtils.setWindowOpacity(w, 98 WindowFader.this.currentOpacity / 100.0f); 99 w.repaint(); 100 } 101 } else { 102 fadeOutFinished(); 103 WindowFader.this.fadeOutTimer.stop(); 104 WindowFader.this.currentOpacity = 0; 105 } 106 } 107 108 }); 109 this.fadeOutTimer.setRepeats(true); 110 this.fadeOutTimer.start(); 111 } 112 fadeOutFinished()113 protected void fadeOutFinished() { 114 Window w = this.window.get(); 115 if (w != null) { 116 w.dispose(); 117 } 118 } 119 fadeInFinished()120 protected void fadeInFinished() { 121 } 122 123 /** 124 * Stops all fading effects 125 */ stop()126 private void stop() { 127 if (this.fadeInTimer != null) { 128 this.fadeInTimer.stop(); 129 this.fadeInTimer = null; 130 } 131 if (this.fadeOutTimer != null) { 132 this.fadeOutTimer.stop(); 133 this.fadeOutTimer = null; 134 } 135 } 136 137 /** 138 * Stops all fading effects and disposes window 139 */ clear()140 public void clear() { 141 stop(); 142 this.currentOpacity = 0; 143 Window w = this.window.get(); 144 if (w != null) { 145 GuiUtils.setWindowOpacity(w, 1); 146 w.dispose(); 147 } 148 149 } 150 151 } 152