KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > jfccontrols > SwingBlockUtil


1 /**
2  * com.mckoi.jfccontrols.SwingBlockUtil 23 Aug 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.jfccontrols;
26
27 import java.awt.*;
28 import javax.swing.SwingUtilities JavaDoc;
29
30 /**
31  * Helper class for providing blocking behaviour on the AWT/Swing event
32  * dispatcher thread without freezing up the user interface. While the call
33  * to 'block' will block with respect to the callee, events will still be
34  * serviced from within the 'block' method.
35  * <p>
36  * I consider this a mild hack. This class may be incompatible with future
37  * versions of Java if the AWT event mechanism is altered. It may also not
38  * work happily with non-Sun based implementations of Java.
39  *
40  * @author Tobias Downer
41  */

42
43 public class SwingBlockUtil {
44
45   /**
46    * The state we are currently in.
47    */

48   private int block_state = 0;
49
50
51   /**
52    * Utility that blocks the Swing EventDispatchThread, and then emulates the
53    * inner loop of the dispatcher thread itself. This allows for repaint and
54    * button events to be processed. When the block has finished, this method
55    * will return and return control to the originating event dispatcher.
56    */

57   public void block() {
58
59     synchronized (this) {
60       if (!SwingUtilities.isEventDispatchThread()) {
61         throw new Error JavaDoc("Not on the event dispatcher.");
62       }
63       if (block_state != 0) {
64         // This situation would occur when a component generates an event (such
65
// as the user pressing a button) that performs a query. Therefore
66
// there are multi-levels of queries being executed.
67
throw new Error JavaDoc("Can't nest queries.");
68       }
69
70       block_state = 1;
71     }
72
73     EventQueue theQueue = eventQueue();
74     while (isBlocked()) {
75       try {
76         // This is essentially the body of EventDispatchThread
77
AWTEvent event = theQueue.getNextEvent();
78         Object JavaDoc src = event.getSource();
79         // can't call theQueue.dispatchEvent, so I pasted its body here
80
if (event instanceof ActiveEvent) {
81           ((ActiveEvent) event).dispatch();
82         } else if (src instanceof Component) {
83           ((Component) src).dispatchEvent(event);
84         } else if (src instanceof MenuComponent) {
85           ((MenuComponent) src).dispatchEvent(event);
86         } else {
87           System.err.println("unable to dispatch event: " + event);
88         }
89       }
90       catch (Throwable JavaDoc e) {
91         // Any exceptions thrown here are logged, but we don't break the loop.
92
System.err.println("Exception thrown during block util dispatching:");
93         e.printStackTrace(System.err);
94       }
95     }
96
97     block_state = 0;
98
99   }
100
101   /**
102    * Unblocks any call to the 'block' method. This method can safely be
103    * executed from any thread (even the Swing dispatcher thread).
104    */

105   public void unblock() {
106     // Execute the runnable on the event dispatcher,
107
SwingUtilities.invokeLater(new Runnable JavaDoc() {
108       public void run() {
109         if (block_state == 1) {
110           block_state = 2;
111         }
112       }
113     });
114   }
115
116   /**
117    * Returns true if the event dispatcher is blocked.
118    */

119   private boolean isBlocked() {
120     return block_state <= 1;
121   }
122
123   /**
124    * Returns the current system EventQueue.
125    */

126   private static EventQueue eventQueue() {
127     return Toolkit.getDefaultToolkit().getSystemEventQueue();
128   }
129
130 }
131
Popular Tags