01: package abbot.editor.widgets;
02:
03: import java.awt.Color;
04: import java.awt.Cursor;
05: import java.awt.Graphics;
06: import java.awt.KeyEventDispatcher;
07: import java.awt.KeyboardFocusManager;
08: import java.awt.Rectangle;
09: import java.awt.event.KeyEvent;
10: import java.awt.event.MouseAdapter;
11: import java.awt.event.MouseMotionAdapter;
12: import javax.swing.JComponent;
13: import javax.swing.JFrame;
14: import javax.swing.SwingUtilities;
15: import abbot.util.AWT;
16:
17: /** Prevents mouse and key input to a {@link JComponent} or {@link JFrame},
18: * while dimming the component and displaying a wait cursor.
19: */
20: public class WaitIndicator extends AbstractComponentDecorator implements
21: KeyEventDispatcher {
22:
23: /** Place the wait indicator over the entire frame. */
24: public WaitIndicator(JFrame frame) {
25: this (frame.getLayeredPane());
26: }
27:
28: /** Place the wait indicator over the given component. */
29: public WaitIndicator(JComponent target) {
30: super (target);
31: KeyboardFocusManager.getCurrentKeyboardFocusManager()
32: .addKeyEventDispatcher(this );
33: getPainter().addMouseListener(new MouseAdapter() {
34: });
35: getPainter().addMouseMotionListener(new MouseMotionAdapter() {
36: });
37: getPainter().setCursor(
38: Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
39: }
40:
41: /** Remove the wait indicator. */
42: public void dispose() {
43: super .dispose();
44: KeyboardFocusManager.getCurrentKeyboardFocusManager()
45: .removeKeyEventDispatcher(this );
46: }
47:
48: /** Consume events targeted at our target component. Return true to
49: * consume the event.
50: */
51: public boolean dispatchKeyEvent(KeyEvent e) {
52: return SwingUtilities.isDescendingFrom(e.getComponent(),
53: getComponent());
54: }
55:
56: /** The default dims the blocked component. */
57: public void paint(Graphics g) {
58: Color c = AWT.alpha(getComponent().getBackground(), 128);
59: Rectangle r = getDecorationBounds();
60: g = g.create();
61: g.setColor(c);
62: g.fillRect(r.x, r.y, r.width, r.height);
63: g.dispose();
64: }
65: }
|