import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class NotificationIO
{
private int howFar2Move;
public NotificationIO(ArrayList<JPanel> panelsForNotifs)
{
howFar2Move = panelsForNotifs.size();
for(int i = 0;i<panelsForNotifs.size();i++){
JPanel infoPanel = panelsForNotifs.get(i);
JDialog dialog = new JDialog(new JFrame(),"New Post Alert");
infoPanel.setMaximumSize(new Dimension(200,100));
dialog.setSize(200,100);
dialog.getContentPane().add(infoPanel);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension d = toolkit.getScreenSize();
int x = d.width - dialog.getWidth();
int y = d.height - getScreenBottomInset(toolkit);
dialog.setLocation(x,y);
dialog.setAlwaysOnTop(true);
dialog.setFocusableWindowState(false);
dialog.addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent we) {
we.getWindow().setFocusableWindowState(true);
we.getWindow().removeWindowListener(this);
}
});
dialog.setVisible(true);
start(dialog,howFar2Move);
closeFrameTimer(dialog);
howFar2Move--;
}
}
private void start(JDialog dialog,int far)
{
final int howFar = far;
final JDialog currentDialog = dialog;
Thread thread = new Thread(new Runnable(){
public void run(){
boolean exit = false;
while(!exit){
Toolkit toolkit = Toolkit.getDefaultToolkit();
int bottomInset = getScreenBottomInset(toolkit);
int speed = 4;
boolean done = false;
while(!done){
try{
Thread.sleep(10);
}catch(InterruptedException e){
System.out.println("interrupted");
done = true;
}
Dimension d = toolkit.getScreenSize();
Rectangle r = currentDialog.getBounds();
currentDialog.setLocation(r.x, r.y-speed);
if((d.height - r.y) >= ((r.height*howFar) + bottomInset)){
toolkit.beep();
done = true;
}
}
exit = true;
}
}
});
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
private void closeFrameTimer(JDialog pop)
{
final JDialog popup = pop;
final java.util.Timer timer = new java.util.Timer();
int delay = 20000 - (1000 * howFar2Move * 2);
timer.schedule(new TimerTask(){
public void run(){
popup.dispose();
timer.cancel();
}
},delay);
}
private int getScreenBottomInset(Toolkit toolkit)
{
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
Insets insets = toolkit.getScreenInsets(gc);
return insets.bottom;
}
public static void main(String[] args)
{
final ArrayList<JPanel> panels = new ArrayList<JPanel>();
JPanel testPanel = new JPanel();
JLabel testLabel = new JLabel("BLAHBLAH -- 1");
testPanel.add(testLabel);
panels.add(testPanel);
JPanel testPanel2 = new JPanel();
JLabel testLabel2 = new JLabel("BLAH -- 2");
testPanel2.add(testLabel2);
panels.add(testPanel2);
JPanel testPanel3 = new JPanel();
JLabel testLabel3 = new JLabel("BLAH -- 3");
testPanel3.add(testLabel3);
panels.add(testPanel3);
Thread notificationThread = new Thread(){
public void run(){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new NotificationIO(panels);
}
});
}
};
notificationThread.start();
}
}