1 24 25 package com.mckoi.jfccontrols; 26 27 import java.awt.*; 28 import javax.swing.SwingUtilities ; 29 30 42 43 public class SwingBlockUtil { 44 45 48 private int block_state = 0; 49 50 51 57 public void block() { 58 59 synchronized (this) { 60 if (!SwingUtilities.isEventDispatchThread()) { 61 throw new Error ("Not on the event dispatcher."); 62 } 63 if (block_state != 0) { 64 throw new Error ("Can't nest queries."); 68 } 69 70 block_state = 1; 71 } 72 73 EventQueue theQueue = eventQueue(); 74 while (isBlocked()) { 75 try { 76 AWTEvent event = theQueue.getNextEvent(); 78 Object src = event.getSource(); 79 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 e) { 91 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 105 public void unblock() { 106 SwingUtilities.invokeLater(new Runnable () { 108 public void run() { 109 if (block_state == 1) { 110 block_state = 2; 111 } 112 } 113 }); 114 } 115 116 119 private boolean isBlocked() { 120 return block_state <= 1; 121 } 122 123 126 private static EventQueue eventQueue() { 127 return Toolkit.getDefaultToolkit().getSystemEventQueue(); 128 } 129 130 } 131 | Popular Tags |