//peer frames should be created in the same app context where the desktop is
//todo refactor this into a generic inter-appcontext invoke and wait
AppContext ac = SunToolkit.targetToAppContext(target);
if (ac != null) {
EventQueue eq = (EventQueue) ac.get(sun.awt.AppContext.EVENT_QUEUE_KEY);
if (eq != null) {
try {
Method met = EventQueue.class.getDeclaredMethod("initDispatchThread");
met.setAccessible(true);
met.invoke(eq);
} catch (Exception x) {
x.printStackTrace();
}
}
}
// invoke and wait --
EventQueue eq = getMainEventQueue();
if (eq == getSystemEventQueueImpl()) {
run.run();
synchronized (ret) {
return ret[0];
}
}
try {
Field field = EventQueue.class.getField("dispatchThread");
field.setAccessible(true);
Thread edt = (Thread) field.get(eq);
if (Thread.currentThread() == edt) {
run.run();
synchronized (ret) {
return ret[0];
}
}
} catch (Exception x) {
throw new RuntimeException(x);
}
class AWTInvocationLock {
}
Object lock = new AWTInvocationLock();
InvocationEvent event = new InvocationEvent(Toolkit.getDefaultToolkit(), run, lock, true);
try {
synchronized (lock) {
eq.postEvent(event);
lock.wait();
}
} catch (Exception x) {
throw new RuntimeException(x);
}