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 panelTimer; 8 long messageTimeout = 30000; 9 long clearTime = -1; 10 boolean cleared= true; 11 boolean exitThread = false; 12 13 16 public InfoPanel() { 17 super(new java.awt.BorderLayout ()); 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 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 (new Runnable () { 35 public void run() { 36 waitForTimeout(); 37 } 38 }); 39 panelTimer.start(); 40 } 41 42 45 public void setMessage(String newMessage) { 46 final String msg = newMessage; 47 Runnable runMe = new Runnable () { 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 67 public void clear() { 68 Runnable runMe = new Runnable () { 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 85 public void stopThread() { 86 exitThread = true; 87 panelTimer.interrupt(); 88 } 89 90 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 ie) { 108 109 } 110 } 111 } 112 113 } 114 | Popular Tags |