BlockingGlassPane.java
001 /*
002  *  Copyright 1994-2009 Sun Microsystems, Inc. All Rights Reserved.
003  *  Under BSD licence, LGPL compatible.
004  *
005  *  This file is part of GATE (see http://gate.ac.uk/), and is free
006  *  software, licenced under the GNU Library General Public License,
007  *  Version 2, June 1991 (in the distribution as file licence.html,
008  *  and also available at http://gate.ac.uk/gate/licence.html).
009  *
010  *  From Sun forums:
011  *  <a href="http://forums.sun.com/thread.jspa?threadID=294121&forumID=57">
012  *  Swing [Archive] - Block JFrame and JDialog with JGlassPane</a>
013  *
014  *  Retrieved by Thomas Heitz - 03 july 2009.
015  *
016  *  $Id$
017  */
018 
019 package gate.swing;
020 
021 import java.awt.*;
022 import java.awt.event.*;
023 import javax.swing.*;
024 
025 /**
026  * A Panel that can be blocked.
027  <br>
028  * Just set an instance of this class as the glassPane
029  * of your JFrame and call <code>block()</code> as needed.
030  */
031 public class BlockingGlassPane extends JPanel {
032 
033   private int        blockCount = 0;
034   private BlockMouse blockMouse = new BlockMouse();
035   private BlockKeys  blockKeys  = new BlockKeys();
036 
037   /**
038    * Constructor.
039    */
040   public BlockingGlassPane() {
041     setVisible(false);
042     setOpaque (false);
043 
044     addMouseListener(blockMouse);
045   }
046 
047   /**
048    * Start or end blocking.
049    *
050    @param block   should blocking be started or ended
051    */
052   public void block(boolean block) {
053     if (block) {
054       if (blockCount == 0) {
055         setVisible(true);
056 
057         setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
058 
059         FocusManager.getCurrentManager().addKeyEventDispatcher(blockKeys);
060       }
061       blockCount++;
062     else {
063       blockCount--;
064       if (blockCount == 0) {
065         FocusManager.getCurrentManager().removeKeyEventDispatcher(blockKeys);
066 
067         setCursor(Cursor.getDefaultCursor());
068 
069         setVisible(false);
070       }
071     }
072   }
073 
074   /**
075    * Test if this glasspane is blocked.
076    *
077    @return    <code>true</code> if currently blocked
078    */
079   public boolean isBlocked() {
080     return blockCount > 0;
081   }
082 
083   /**
084    * The key dispatcher to block the keys.
085    */
086   private class BlockKeys implements KeyEventDispatcher {
087     public boolean dispatchKeyEvent(KeyEvent ev) {
088       Component source = ev.getComponent();
089       if (source != null &&
090           SwingUtilities.isDescendingFrom(source, getParent())) {
091         Toolkit.getDefaultToolkit().beep();
092         ev.consume();
093         return true;
094       }
095       return false;
096     }
097   }
098 
099   /**
100    * The mouse listener used to block the mouse.
101    */
102   private class BlockMouse extends MouseAdapter {
103     public void mouseClicked(MouseEvent ev) {
104       Toolkit.getDefaultToolkit().beep();
105     }
106   }
107 }