mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 12:48:00 +00:00
Move WaitCallback and WaitRunnable to forge-gui
This commit is contained in:
34
forge-gui/src/main/java/forge/util/WaitCallback.java
Normal file
34
forge-gui/src/main/java/forge/util/WaitCallback.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package forge.util;
|
||||
|
||||
import forge.FThreads;
|
||||
|
||||
public abstract class WaitCallback<T> extends Callback<T> implements Runnable {
|
||||
public class Lock {
|
||||
}
|
||||
|
||||
private final Lock lock = new Lock();
|
||||
|
||||
private T result;
|
||||
|
||||
@Override
|
||||
public final void run(T result0) {
|
||||
result = result0;
|
||||
synchronized (lock) {
|
||||
lock.notify();
|
||||
}
|
||||
}
|
||||
|
||||
public final T invokeAndWait() {
|
||||
FThreads.assertExecutedByEdt(false); //not supported if on UI thread
|
||||
FThreads.invokeInEdtLater(this);
|
||||
try {
|
||||
synchronized (lock) {
|
||||
lock.wait();
|
||||
}
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
31
forge-gui/src/main/java/forge/util/WaitRunnable.java
Normal file
31
forge-gui/src/main/java/forge/util/WaitRunnable.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package forge.util;
|
||||
|
||||
import forge.FThreads;
|
||||
|
||||
public abstract class WaitRunnable implements Runnable {
|
||||
public class Lock {
|
||||
}
|
||||
|
||||
private final Lock lock = new Lock();
|
||||
|
||||
public void invokeAndWait() {
|
||||
FThreads.assertExecutedByEdt(false); //not supported if on UI thread
|
||||
FThreads.invokeInEdtLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
WaitRunnable.this.run();
|
||||
synchronized(lock) {
|
||||
lock.notify();
|
||||
}
|
||||
}
|
||||
});
|
||||
try {
|
||||
synchronized(lock) {
|
||||
lock.wait();
|
||||
}
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user