package bluej.compiler;
import bluej.utility.javafx.FXPlatformRunnable;
import javafx.application.Platform;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
| This class adapts CompileObserver messages to run on the JavaFX GUI thread.
|
| @author Davin McCall
|
final public class EventqueueCompileObserverAdapter
implements CompileObserver{
private FXCompileObserver link;
| Constructor for EventqueueCompileObserver. The link parameter is a compiler
| observer; all messages will be passed on to it, but on the GUI thread.
|
public EventqueueCompileObserverAdapter(FXCompileObserver link)
{
this.link = link;
}
| This method switches execution to the GUI thread.
|
private void runOnEventQueue(FXPlatformRunnable action)
{
CompletableFuture<Optional<Throwable>> f = new CompletableFuture<>();
Platform.runLater(() -> {
try
{
action.run();
}
catch (Throwable t)
{
f.complete(Optional.of(t));
}
finally
{
if (!f.isDone())
f.complete(Optional.empty());
}
});
try
{
Optional<Throwable> optThrow = f.get();
if (optThrow.isPresent())
throw new RuntimeException(optThrow.get());
}
catch (InterruptedException | ExecutionException e)
{
throw new RuntimeException(e);
}
}
@Override
public synchronized void compilerMessage(Diagnostic diagnostic, CompileType type)
{
runOnEventQueue(() -> link.compilerMessage(diagnostic, type));
}
@Override
public synchronized void startCompile(CompileInputFile[] csources, CompileReason reason, CompileType type, int compilationSequence)
{
runOnEventQueue(() -> link.startCompile(csources, reason, type, compilationSequence));
}
@Override
public synchronized void endCompile(CompileInputFile[] sources, boolean successful, CompileType type, int compilationSequence)
{
runOnEventQueue(() -> link.endCompile(sources, successful, type, compilationSequence));
}
}
top,
use,
map,
class EventqueueCompileObserverAdapter
. EventqueueCompileObserverAdapter
. runOnEventQueue
. compilerMessage
. startCompile
. endCompile
90 neLoCode
+ 5 LoComm