001: package org.gui4j.core.swing;
002:
003: import java.awt.Component;
004: import java.awt.Cursor;
005: import java.awt.KeyEventDispatcher;
006: import java.awt.Toolkit;
007: import java.awt.event.KeyEvent;
008: import java.awt.event.MouseAdapter;
009: import java.awt.event.MouseEvent;
010:
011: import javax.swing.FocusManager;
012: import javax.swing.JPanel;
013: import javax.swing.SwingUtilities;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017:
018: /**
019: * KKB, 8.4.03: This code was originally copied from a Swing Forum entry on
020: * Sun's website: http://forum.java.sun.com/thread.jsp?forum=57&thread=294121
021: *
022: * A Panel that can be blocked.<br>
023: * Just set an instance of this class as the glassPane
024: * of your JFrame and call <code>block()</code> as needed.
025: */
026: public class BlockingGlassPane extends JPanel {
027: private static final Log log = LogFactory
028: .getLog(BlockingGlassPane.class);
029:
030: private int blockCount = 0;
031: private BlockMouse blockMouse = new BlockMouse();
032: private BlockKeys blockKeys = new BlockKeys();
033: protected boolean beep;
034:
035: /**
036: * Constructor
037: * @param beep
038: */
039: public BlockingGlassPane(boolean beep) {
040: this .beep = beep;
041: setVisible(false);
042: setOpaque(false);
043: addMouseListener(blockMouse);
044: }
045:
046: /**
047: * Start or end blocking.
048: * @param block should blocking be started or ended
049: */
050: public void block(boolean block) {
051: if (block) {
052: if (blockCount == 0) {
053: setVisible(true);
054: setCursor(Cursor
055: .getPredefinedCursor(Cursor.WAIT_CURSOR));
056: FocusManager.getCurrentManager().addKeyEventDispatcher(
057: blockKeys);
058: }
059: blockCount++;
060: } else {
061: if (blockCount == 0) {
062: log.warn("block(false) called with blockCount == 0");
063: }
064: if (blockCount > 0) {
065: blockCount--;
066: }
067: if (blockCount == 0) {
068: FocusManager.getCurrentManager()
069: .removeKeyEventDispatcher(blockKeys);
070: setCursor(Cursor.getDefaultCursor());
071: setVisible(false);
072: }
073: }
074: }
075:
076: /**
077: * Test if this glasspane is blocked.
078: * @return <code>true</code> if currently blocked
079: */
080: public boolean isBlocked() {
081: return blockCount > 0;
082: }
083:
084: /**
085: * The key dispatcher to block the keys.
086: */
087: private class BlockKeys implements KeyEventDispatcher {
088: public boolean dispatchKeyEvent(KeyEvent ev) {
089: Component source = ev.getComponent();
090: if (source != null
091: && SwingUtilities.isDescendingFrom(source,
092: getParent())) {
093: if (beep) {
094: Toolkit.getDefaultToolkit().beep();
095: }
096: ev.consume();
097: return true;
098: }
099: return false;
100: }
101: }
102:
103: /** The mouse listener used to block the mouse.
104: */
105: private class BlockMouse extends MouseAdapter {
106: public void mouseClicked(MouseEvent ev) {
107: if (beep) {
108: Toolkit.getDefaultToolkit().beep();
109: }
110: ev.consume();
111: }
112: }
113: }
|