1 package com
.googlecode
.lanterna
.gui2
;
3 import java
.io
.IOException
;
4 import java
.util
.Queue
;
5 import java
.util
.concurrent
.CountDownLatch
;
6 import java
.util
.concurrent
.LinkedBlockingQueue
;
9 * Created by martin on 20/06/15.
11 public abstract class AbstractTextGUIThread
implements TextGUIThread
{
13 protected final TextGUI textGUI
;
14 protected final Queue
<Runnable
> customTasks
;
15 protected ExceptionHandler exceptionHandler
;
17 public AbstractTextGUIThread(TextGUI textGUI
) {
18 this.exceptionHandler
= new ExceptionHandler() {
20 public boolean onIOException(IOException e
) {
26 public boolean onRuntimeException(RuntimeException e
) {
31 this.textGUI
= textGUI
;
32 this.customTasks
= new LinkedBlockingQueue
<Runnable
>();
36 public void invokeLater(Runnable runnable
) throws IllegalStateException
{
37 customTasks
.add(runnable
);
41 public void setExceptionHandler(ExceptionHandler exceptionHandler
) {
42 if(exceptionHandler
== null) {
43 throw new IllegalArgumentException("Cannot call setExceptionHandler(null)");
45 this.exceptionHandler
= exceptionHandler
;
49 public synchronized boolean processEventsAndUpdate() throws IOException
{
50 if(getThread() != Thread
.currentThread()) {
51 throw new IllegalStateException("Calling processEventAndUpdate outside of GUI thread");
53 textGUI
.processInput();
54 while(!customTasks
.isEmpty()) {
55 Runnable r
= customTasks
.poll();
60 if(textGUI
.isPendingUpdate()) {
61 textGUI
.updateScreen();
68 public void invokeAndWait(final Runnable runnable
) throws IllegalStateException
, InterruptedException
{
69 if(Thread
.currentThread() == getThread()) {
73 final CountDownLatch countDownLatch
= new CountDownLatch(1);
74 invokeLater(new Runnable() {
81 countDownLatch
.countDown();
85 countDownLatch
.await();