01: package org.uispec4j.utils;
02:
03: import org.uispec4j.Trigger;
04: import org.uispec4j.interception.toolkit.UISpecDisplay;
05:
06: import javax.swing.*;
07: import java.lang.reflect.InvocationTargetException;
08:
09: public class TriggerRunner {
10: public static void runInCurrentThread(Trigger trigger) {
11: try {
12: trigger.run();
13: } catch (Exception e) {
14: throw new RuntimeException(e);
15: }
16: }
17:
18: public static void runInSwingThread(final Trigger trigger) {
19: if (SwingUtilities.isEventDispatchThread()) {
20: runInCurrentThread(trigger);
21: } else {
22: final ExceptionContainer container = new ExceptionContainer();
23: try {
24: SwingUtilities.invokeAndWait(new Runnable() {
25: public void run() {
26: try {
27: runInCurrentThread(trigger);
28: } catch (Throwable e) {
29: container.set(e);
30: }
31: }
32: });
33: } catch (InterruptedException e) {
34: throw new RuntimeException(e.getCause());
35: } catch (InvocationTargetException e) {
36: throw new RuntimeException(e.getCause());
37: }
38: container.rethrowIfNeeded();
39: }
40: }
41:
42: public static void runInUISpecThread(final Trigger trigger) {
43: UISpecDisplay.instance().runInNewThread(new Runnable() {
44: public void run() {
45: try {
46: trigger.run();
47: } catch (Throwable e) {
48: UISpecDisplay.instance().store(e);
49: }
50: }
51: });
52: }
53: }
|