1 | package net.sf.launch4j.formimpl;
|
---|
2 |
|
---|
3 | import java.awt.AWTEvent;
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.Cursor;
|
---|
6 | import java.awt.Toolkit;
|
---|
7 | import java.awt.Window;
|
---|
8 | import java.awt.event.AWTEventListener;
|
---|
9 | import java.awt.event.KeyAdapter;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.awt.event.MouseAdapter;
|
---|
12 |
|
---|
13 | import javax.swing.JComponent;
|
---|
14 | import javax.swing.SwingUtilities;
|
---|
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 implements AWTEventListener {
|
---|
23 | private final Window _window;
|
---|
24 |
|
---|
25 | public GlassPane(Window w) {
|
---|
26 | _window = w;
|
---|
27 | addMouseListener(new MouseAdapter() {});
|
---|
28 | addKeyListener(new KeyAdapter() {});
|
---|
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 event) {
|
---|
40 | Object source = event.getSource();
|
---|
41 | if (event instanceof KeyEvent
|
---|
42 | && source instanceof Component) {
|
---|
43 | /*
|
---|
44 | * If the event originated from the window w/glass pane,
|
---|
45 | * consume the event.
|
---|
46 | */
|
---|
47 | if ((SwingUtilities.windowForComponent((Component) source) == _window)) {
|
---|
48 | ((KeyEvent) 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 | }
|
---|