View Javadoc

1   package gov.nasa.pds.ltdt.gui.util;
2   
3   import java.awt.Color;
4   import java.awt.Cursor;
5   import java.awt.Graphics;
6   import java.awt.GridBagConstraints;
7   import java.awt.GridBagLayout;
8   import java.awt.KeyboardFocusManager;
9   import java.awt.event.KeyEvent;
10  import java.awt.event.KeyListener;
11  import java.awt.event.MouseAdapter;
12  import java.awt.event.MouseMotionAdapter;
13  import java.util.Collections;
14  
15  import javax.swing.JComponent;
16  import javax.swing.JLabel;
17  import javax.swing.SwingUtilities;
18  import javax.swing.border.Border;
19  import javax.swing.border.EmptyBorder;
20  
21  
22  public class ProcessWaitGlassPane extends JComponent
23  implements KeyListener
24  {
25  private final static Color DEFAULT_BACKGROUND = new Color(128, 128, 128, 128);
26  private final static Border MESSAGE_BORDER = new EmptyBorder(10, 10, 10, 10);
27  
28  private JLabel message = new JLabel();
29  
30  public ProcessWaitGlassPane()
31  {
32  	setOpaque( false );
33  	setBackground( DEFAULT_BACKGROUND );
34  	setLayout( new GridBagLayout() );
35  	add(message, new GridBagConstraints());
36  
37  	message.setOpaque(true);
38  	message.setBorder(MESSAGE_BORDER);
39  
40  	//  Disable Mouse, Key and Focus events for the glass pane
41  
42  	addMouseListener( new MouseAdapter() {} );
43  	addMouseMotionListener( new MouseMotionAdapter() {} );
44  
45  	addKeyListener( this );
46  
47      setFocusTraversalKeys(
48          KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET );
49      setFocusTraversalKeys(
50          KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET );
51  }
52  
53  protected void paintComponent(Graphics g)
54  {
55  	g.setColor( getBackground() );
56  	g.fillRect(0, 0, getSize().width, getSize().height);
57  }
58  
59  public void setBackground(Color background)
60  {
61  	super.setBackground( background );
62  
63  	Color messageBackground =
64  		new Color(background.getRed(), background.getGreen(), background.getBlue());
65  	message.setBackground( messageBackground );
66  }
67  
68  public void keyPressed(KeyEvent e)
69  {
70  	e.consume();
71  }
72  
73  public void keyTyped(KeyEvent e) {}
74  
75  public void keyReleased(KeyEvent e)
76  {
77  	e.consume();
78  }
79  
80  public void activate(String text)
81  {
82  	if  (text != null && text.length() > 0)
83  	{
84  		message.setVisible( true );
85  		message.setText( text );
86  		message.setForeground( getForeground() );
87  	}
88  	else
89  		message.setVisible( false );
90  
91  	setVisible( true );
92  	setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
93  	requestFocusInWindow();
94  }
95  
96  public void deactivate()
97  {
98  	SwingUtilities.invokeLater(new Runnable()
99  	{
100 		public void run()
101 		{
102 			setCursor(null);
103 			setVisible( false );
104 		}
105 	});
106 }
107 
108 
109 }