package GUI;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.JWindow;
import Communication.Status;
import GUI.Interfaces.Background;
import GUI.Interfaces.NotificationListener;
/**
* Displays a forwarded text, and if not <code>null</code> for {@link Status} is forwarded, also a
* {@link Status} displayed as an icon.
*
* <p>A {@link NotificationWindow} can't be moved or resized by the user. It's size is defined
* by the value passed by at the constructor.</p>
*
* <p>By standard it is fully translucent. Calling startDisplaying() it's opaque gets increased
* over time until it is fully opaque. Then return to the translucent state over time again until
* it gets disposed at the end. Notifies all {@link NotificationListener} on that event.</p>
*
* @author Sebastian
*
*/
public class NotificationWindow extends JWindow{
/**
* The message to be displayed
*/
private String message;
private float opacity = 0.0f;
private boolean starting = true;
private int fullDisplayCount = 0;
private NotificationListener notificationListener;
private BufferedImage[] images = new BufferedImage[4];
private Status status;
public NotificationWindow(Rectangle rectangle, String text, Status status, NotificationListener listener) {
message = text;
setBounds(rectangle);
Background background = new StandardBackground(false, rectangle, -1);
add((Component) background);
background.setBounds(0, 0, rectangle.width, rectangle.height);
this.status = status;
com.sun.awt.AWTUtilities.setWindowOpacity(this, 0.0f);
notificationListener = listener;
try {
BufferedImage[] tempImages = {
ImageIO.read(new File("Ressources/online.png")),
ImageIO.read(new File("Ressources/away.png")),
ImageIO.read(new File("Ressources/invisible.png")),
ImageIO.read(new File("Ressources/offline.png")),
};
images = tempImages;
} catch (IOException e) {
e.printStackTrace();
}
setVisible(true);
setAlwaysOnTop(true);
}
public void startDisplaying(){
Timer timer = new Timer();
final NotificationWindow thisWindow = this;
timer.schedule(new TimerTask(){
@Override
public void run() {
if(starting){
if(opacity <= 0.95){
opacity += 0.05;
}else{
starting = false;
}
}else{
fullDisplayCount++;
if(fullDisplayCount > 150){
if(opacity > 0.1){
opacity -= 0.07;
}else{
notificationListener.notificationDisposed(thisWindow);
dispose();
}
}
}
com.sun.awt.AWTUtilities.setWindowOpacity(thisWindow, opacity);
}}, 25, 25);
}
public void paint(Graphics g){
super.paint(g);
if(status != null){
BufferedImage image = null;
switch(status){
case Online : image = images[0]; break;
case NotAvailable : image = images[1]; break;
case Invisible : image = images[2]; break;
case Offline : image = images[3]; break;
}
g.drawString(message, 70, 50);
g.drawImage(image, 20, 28, null);
}else{
g.drawString(message, 15, 50);
}
}
public void componentHidden(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
public void componentMoved(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
public void componentShown(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
}