/**
* Copyright (c) 2009 Juwi MacMillan Group GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.juwimm.cms.gui;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.Timer;
public class FadingPanel extends JComponent implements ActionListener {
private Timer ticker = null;
private int alpha = 0;
private int step;
private FadeListener fadeListener;
public FadingPanel(FadeListener fadeListener) {
this.fadeListener = fadeListener;
}
public void setVisible(boolean visible) {
super.setVisible(visible);
if (visible) {
if (ticker != null) {
ticker.stop();
}
alpha = 0;
step = 25;
ticker = new Timer(50, this);
ticker.start();
} else {
if (ticker != null) {
ticker.stop();
ticker = null;
}
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(new Color(255, 255, 255, alpha));
Rectangle clip = g.getClipBounds();
g.fillRect(clip.x, clip.y, clip.width, clip.height);
}
public void switchDirection() {
step = -step;
ticker.start();
}
public void actionPerformed(ActionEvent e) {
alpha += step;
if (alpha >= 255) {
alpha = 255;
ticker.stop();
fadeListener.fadeOutFinished();
} else if (alpha < 0) {
alpha = 0;
ticker.stop();
fadeListener.fadeInFinished();
}
repaint();
}
}