001: package org.uispec4j.interception.handlers;
002:
003: import org.uispec4j.Window;
004: import org.uispec4j.utils.ComponentUtils;
005:
006: public class ClosedInterceptionDetectionHandler extends
007: AbstractInterceptionHandlerDecorator {
008: public static final String MODAL_DIALOG_NOT_CLOSED_ERROR_MESSAGE = "Modal window was not closed - make sure that setVisible(false) gets called "
009: + "by the production code";
010:
011: private long timeout;
012: private Object lock = new Object();
013: private boolean exceptionThrown = false;
014: private String windowTitle;
015: private Window window;
016: private boolean windowClosed = false;
017: private Thread thread;
018:
019: public ClosedInterceptionDetectionHandler(
020: InterceptionHandler innerHandler, long timeout) {
021: super (innerHandler);
022: this .timeout = timeout;
023: }
024:
025: public void process(final Window window) {
026: if (!window.isModal().isTrue()) {
027: super .process(window);
028: return;
029: }
030: this .window = window;
031: windowTitle = window.getTitle();
032: thread = new Thread() {
033: public void run() {
034: try {
035: long delay = timeout / 100;
036: for (int i = 0; i < 100; i++) {
037: try {
038: sleep(delay);
039: } catch (InterruptedException e) {
040: return;
041: }
042: if (!window.getAwtComponent().isVisible()) {
043: notifyWindowClosed();
044: return;
045: }
046: }
047: } finally {
048: ComponentUtils.close(window);
049: }
050: }
051: };
052: thread.setName(thread.getName()
053: + " [WindowInterceptor.CloseHandler]");
054: thread.start();
055: try {
056: super .process(window);
057: } catch (RuntimeException e) {
058: notifyExceptionThrown();
059: throw e;
060: } catch (Error e) {
061: notifyExceptionThrown();
062: throw e;
063: } catch (Throwable e) {
064: throw new Error(e);
065: }
066: }
067:
068: private void notifyExceptionThrown() {
069: synchronized (lock) {
070: exceptionThrown = true;
071: lock.notify();
072: }
073: }
074:
075: private void notifyWindowClosed() {
076: synchronized (lock) {
077: windowClosed = true;
078: lock.notify();
079: }
080: }
081:
082: public void checkWindowWasClosed() {
083: if (window == null) {
084: return;
085: }
086: synchronized (lock) {
087: if (!windowClosed && !exceptionThrown) {
088: try {
089: lock.wait(timeout);
090: } catch (InterruptedException e) {
091: }
092: if (!windowClosed) {
093: throw new WindowNotClosedError(
094: "Modal window '"
095: + windowTitle
096: + "' was not closed - make sure that setVisible(false) gets called "
097: + "by the production code");
098: }
099: }
100: }
101: }
102:
103: public void stop() {
104: if (thread != null) {
105: thread.interrupt();
106: }
107: }
108: }
|