KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > InfoPanel


1 package net.suberic.pooka.gui;
2 import javax.swing.*;
3
4 public class InfoPanel extends JPanel {
5   JLabel currentLabel = null;
6   ConnectionMonitor monitor = null;
7   Thread JavaDoc panelTimer;
8   long messageTimeout = 30000;
9   long clearTime = -1;
10   boolean cleared= true;
11   boolean exitThread = false;
12
13   /**
14    * Creates an InfoPanel and adds a ConnectionMonitor
15    */

16   public InfoPanel() {
17     //super(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
18
super(new java.awt.BorderLayout JavaDoc());
19     currentLabel = new JLabel();
20     this.add(currentLabel, java.awt.BorderLayout.WEST);
21
22     monitor = new ConnectionMonitor();
23     monitor.monitorConnectionManager(net.suberic.pooka.Pooka.getConnectionManager());
24
25     this.add(monitor,java.awt.BorderLayout.EAST);
26
27     javax.swing.border.Border JavaDoc border = BorderFactory.createLoweredBevelBorder();
28
29     this.setBorder(border);
30     this.getInsets().top=0;
31     this.getInsets().bottom=0;
32     this.setMinimumSize(getPreferredSize());
33
34     panelTimer = new Thread JavaDoc(new Runnable JavaDoc() {
35         public void run() {
36           waitForTimeout();
37         }
38       });
39     panelTimer.start();
40   }
41
42   /**
43    * Sets the message for the InfoPanel.
44    */

45   public void setMessage(String JavaDoc newMessage) {
46     final String JavaDoc msg = newMessage;
47     Runnable JavaDoc runMe = new Runnable JavaDoc() {
48         public void run() {
49           currentLabel.setText(msg);
50           currentLabel.repaint();
51           clearTime = System.currentTimeMillis() + messageTimeout;
52           cleared = false;
53           panelTimer.interrupt();
54         }
55       };
56
57     if (SwingUtilities.isEventDispatchThread()) {
58       runMe.run();
59     } else {
60       SwingUtilities.invokeLater(runMe);
61     }
62   }
63
64   /**
65    * Clears the InfoPanel.
66    */

67   public void clear() {
68     Runnable JavaDoc runMe = new Runnable JavaDoc() {
69         public void run() {
70           currentLabel.setText("");
71           currentLabel.repaint();
72           cleared = true;
73         }
74       };
75     if (SwingUtilities.isEventDispatchThread()) {
76       runMe.run();
77     } else {
78       SwingUtilities.invokeLater(runMe);
79     }
80   }
81
82   /**
83    * Stops the panel thread.
84    */

85   public void stopThread() {
86     exitThread = true;
87     panelTimer.interrupt();
88   }
89
90   /**
91    * Waits for the timeout given and then clears the InfoPanel.
92    */

93   void waitForTimeout() {
94     while (! exitThread) {
95       long currentTime = System.currentTimeMillis();
96       long sleepTime = 30000;
97       if (currentTime >= clearTime) {
98         if (! cleared) {
99           clear();
100         }
101       } else {
102         sleepTime = clearTime - currentTime;
103       }
104
105       try {
106         Thread.currentThread().sleep(sleepTime);
107       } catch (InterruptedException JavaDoc ie) {
108
109       }
110     }
111   }
112
113 }
114
Popular Tags