KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > launch4j > formimpl > GlassPane


1 package net.sf.launch4j.formimpl;
2
3 import java.awt.AWTEvent JavaDoc;
4 import java.awt.Component JavaDoc;
5 import java.awt.Cursor JavaDoc;
6 import java.awt.Toolkit JavaDoc;
7 import java.awt.Window JavaDoc;
8 import java.awt.event.AWTEventListener JavaDoc;
9 import java.awt.event.KeyAdapter JavaDoc;
10 import java.awt.event.KeyEvent JavaDoc;
11 import java.awt.event.MouseAdapter JavaDoc;
12
13 import javax.swing.JComponent JavaDoc;
14 import javax.swing.SwingUtilities JavaDoc;
15
16 /**
17  * This is the glass pane class that intercepts screen interactions during
18  * system busy states.
19  *
20  * Based on JavaWorld article by Yexin Chen.
21  */

22 public class GlassPane extends JComponent JavaDoc implements AWTEventListener JavaDoc {
23     private final Window JavaDoc _window;
24
25     public GlassPane(Window JavaDoc w) {
26         _window = w;
27         addMouseListener(new MouseAdapter JavaDoc() {});
28         addKeyListener(new KeyAdapter JavaDoc() {});
29         setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
30     }
31
32     /**
33      * Receives all key events in the AWT and processes the ones that originated
34      * from the current window with the glass pane.
35      *
36      * @param event
37      * the AWTEvent that was fired
38      */

39     public void eventDispatched(AWTEvent JavaDoc event) {
40         Object JavaDoc source = event.getSource();
41         if (event instanceof KeyEvent JavaDoc
42                 && source instanceof Component JavaDoc) {
43             /*
44              * If the event originated from the window w/glass pane,
45              * consume the event.
46              */

47             if ((SwingUtilities.windowForComponent((Component JavaDoc) source) == _window)) {
48                 ((KeyEvent JavaDoc) event).consume();
49             }
50         }
51     }
52
53     /**
54      * Sets the glass pane as visible or invisible. The mouse cursor will be set
55      * accordingly.
56      */

57     public void setVisible(boolean visible) {
58         if (visible) {
59             // Start receiving all events and consume them if necessary
60
Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
61         } else {
62             // Stop receiving all events
63
Toolkit.getDefaultToolkit().removeAWTEventListener(this);
64         }
65         super.setVisible(visible);
66     }
67 }
68
Popular Tags