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 if(Thread
.currentThread() == getThread()) {
41 customTasks
.add(runnable
);
46 public void setExceptionHandler(ExceptionHandler exceptionHandler
) {
47 if(exceptionHandler
== null) {
48 throw new IllegalArgumentException("Cannot call setExceptionHandler(null)");
50 this.exceptionHandler
= exceptionHandler
;
54 public synchronized boolean processEventsAndUpdate() throws IOException
{
55 if(getThread() != Thread
.currentThread()) {
56 throw new IllegalStateException("Calling processEventAndUpdate outside of GUI thread");
58 textGUI
.processInput();
59 while(!customTasks
.isEmpty()) {
60 Runnable r
= customTasks
.poll();
65 if(textGUI
.isPendingUpdate()) {
66 textGUI
.updateScreen();
73 public void invokeAndWait(final Runnable runnable
) throws IllegalStateException
, InterruptedException
{
74 final CountDownLatch countDownLatch
= new CountDownLatch(1);
75 invokeLater(new Runnable() {
79 countDownLatch
.countDown();
82 countDownLatch
.await();