/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pdfdb.gui.panels;
import java.awt.*;
import java.awt.RenderingHints.Key;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import pdfdb.gui.customcomponents.IndeterminateLoadingBar;
/** Panel hosting the indeterminate loading bar.
* @author ug22cmg */
public class LoadingPanel extends JPanel implements ActionListener
{
private static final Key AA_KEY = RenderingHints.KEY_ANTIALIASING;
private static final Object AA_VALUE = RenderingHints.VALUE_ANTIALIAS_ON;
private static final int TYPE = AlphaComposite.SRC_OVER;
private float finalOpacity = 1.0F;
private float finalOpacity2 = 0.90F;
private float opacity = 0;
private int type;
private Timer t;
private IndeterminateLoadingBar bar;
/** Initializes the Loading panel */
public LoadingPanel()
{
this.t = new Timer(33, this);
this.bar = new IndeterminateLoadingBar();
super.setOpaque(false);
super.setLayout(new BorderLayout());
super.addMouseListener(new MouseManager());
super.add(this.bar, BorderLayout.CENTER);
}
/** Paints component.
* @param g The graphics object to paint with. */
@Override
protected void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
GraphicsEnvironment env = GraphicsEnvironment.
getLocalGraphicsEnvironment();
GraphicsDevice gd = env.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
BufferedImage bi = gc.createCompatibleImage(getWidth(), getHeight(), TYPE);
g2d.setRenderingHint(AA_KEY, AA_VALUE);
paintOnImage((Graphics2D) bi.getGraphics());
g2d.drawImage(bi, 0, 0, null);
super.paintComponent(g2d);
}
private void paintOnImage(Graphics2D g2d)
{
final Composite comp = AlphaComposite.getInstance(TYPE, opacity);
g2d.setRenderingHint(AA_KEY, AA_VALUE);
g2d.setComposite(comp);
g2d.setPaint(Color.BLACK);
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.dispose();
}
/** Handles the fading and showing.
* @param visible Whether the panel should fade (false) or show (true). */
@Override
public void setVisible(boolean visible)
{
type = visible ? 0 : 1;
t.start();
if (visible)
{
super.setVisible(true);
}
else
{
while (t.isRunning())
{
}
super.setVisible(false);
}
}
/** Manages the timer event. Handled by this class and not the EventController
* as this is a local event.
* @param e The action event. */
@Override
public void actionPerformed(ActionEvent e)
{
if (type == 0)
{
float o = bar.getOpacity();
bar.setOpacity((o + 0.05F > finalOpacity) ? finalOpacity : o + 0.05F);
opacity = (opacity + 0.5F > finalOpacity2) ? finalOpacity2 : opacity + 0.05F;
if (bar.getOpacity() == finalOpacity && opacity == finalOpacity2)
{
t.stop();
}
}
else
{
float o = bar.getOpacity();
bar.setOpacity((o - 0.2F < 0) ? 0 : o - 0.2F);
opacity = (opacity - 0.5F < 0) ? 0 : opacity - 0.05F;
if (bar.getOpacity() == 0 && opacity == 0)
{
t.stop();
}
}
super.repaint();
}
/** Hack to stop clicks filtering through when used as a glasspane */
private class MouseManager extends MouseAdapter
{
}
}