001    package jagafa.util.view;
002    
003    import java.awt.Color;
004    import java.awt.event.ActionEvent;
005    import java.awt.event.ActionListener;
006    
007    import javax.swing.BorderFactory;
008    import javax.swing.JComponent;
009    import javax.swing.JDialog;
010    import javax.swing.JOptionPane;
011    import javax.swing.JTextArea;
012    import javax.swing.Timer;
013    import javax.swing.border.Border;
014    
015    public class TimedDialog implements ActionListener {
016            private static int instances_ = 0;
017    
018            /**
019             * 
020             */
021            private static final long serialVersionUID = -7904960637010006214L;
022    
023            private JDialog dialog;
024    
025            private Timer closeTimer_;
026    
027            private int delay_;
028    
029            private JComponent parent_;
030    
031            public TimedDialog(String message) {
032                    super();
033                    this.parent_ = null;
034                    this.delay_ = 1900;
035                    showDialog(message, parent_);
036    
037            }
038    
039            public TimedDialog(String message, JComponent parent) {
040                    super();
041                    this.parent_ = parent;
042                    this.delay_ = 1900;
043                    showDialog(message, parent);
044    
045            }
046    
047            public TimedDialog(String message, JComponent parent, int delay) {
048                    super();
049                    this.parent_ = parent;
050    
051                    this.delay_ = delay;
052                    showDialog(message, parent);
053            }
054    
055            JTextArea label;
056    
057            private void showDialog(String message, JComponent parent) {
058                    parent.validate();
059                    //      this.setUndecorated(true);
060                    //label = new JTextArea();
061                    //label.setText(message);
062                    //this.setContentPane(parent);
063                    //this.setIconImage(JOptionPane.);
064                    //this.setAlwaysOnTop(true);
065                    //this.setResizable(false);
066    
067                    //this.setFocusable(false);
068                    //label.setEditable(false);
069                    //label.setForeground(Color.BLACK);
070                    //this.getContentPane().add(label);
071                    //this.setTitle("Hint");
072                    //JOptionPane.showInternalMessageDialog(parent_, message);
073                    //System.out.println(parent);
074                    dialog = new JOptionPane(message, 1).createDialog(parent, "HINT");
075                    dialog.setModal(false);
076                    dialog.setAlwaysOnTop(true);
077    
078                    dialog.setFocusable(true);
079                    
080                    dialog.setEnabled(true);
081                    // dialog.setUndecorated(true);
082                    //dialog.setAlwaysOnTop(true);
083    
084                    if (parent == null) {
085                            dialog.setLocation(100, 200 + instances_ * dialog.getHeight());
086                    } else {
087                            int xLocation = parent.getX() + dialog.getWidth() / 2;
088                            int yLocation = 150 + instances_ * dialog.getHeight();
089                            dialog.setLocation(xLocation, yLocation);
090                    }
091            
092                    dialog.getContentPane().setBackground(Color.RED);
093                    dialog.setVisible(true);
094                    //this.pack();
095                    /*
096                     if (parent == null) {
097                     this.setLocation(0, 400 + instances_ * this.getHeight());
098                     } else {
099                     int xLocation = parent.getX();// + this.getWidth() / 2;
100                     int yLocation = 400 + instances_ * this.getHeight();
101                     //this.setLocation(xLocation, yLocation);
102                     }*/
103                    instances_++;
104                    //      this.setVisible(true);
105                    installTimer();
106                    /*              dialog.setVisible(true);
107                     dialog.setEnabled(false);
108                     */
109                    //dialog.requestFocus();
110            }
111    
112            private void installTimer() {
113                    this.closeTimer_ = new Timer(this.delay_, this);
114                    this.closeTimer_.setRepeats(false);
115                    this.closeTimer_.start();
116            }
117    
118            public void actionPerformed(ActionEvent arg0) {
119    
120                    //this.setVisible(false);
121                    dialog.dispose();
122                    //this.dispose();
123    
124                    instances_--;
125            }
126            
127            private void addTitleBorder(String string) {
128                    Border b = BorderFactory.createLineBorder(Color.BLACK);
129    
130                    Border titleB = BorderFactory.createTitledBorder(b, string);
131                    
132    
133            }
134    
135    }