From abb84744979f74e96ec604357895fc3130aec913 Mon Sep 17 00:00:00 2001 From: Kevin Lamonte Date: Fri, 1 Mar 2019 19:43:09 -0600 Subject: [PATCH] #38 notify backend of lost connection, version bump to 0.3.1 --- build.xml | 7 ++++--- src/jexer/TApplication.java | 19 +++++++++++++++++-- src/jexer/TCommand.java | 6 ++++++ src/jexer/backend/ECMA48Terminal.java | 13 +++++++++++-- src/jexer/backend/GenericBackend.java | 14 ++++++++++++++ 5 files changed, 52 insertions(+), 7 deletions(-) diff --git a/build.xml b/build.xml index b4669e36..c212a757 100644 --- a/build.xml +++ b/build.xml @@ -30,7 +30,7 @@ - + @@ -92,8 +92,9 @@ version="true" use="true" access="protected" - windowtitle="Jexer - Java Text User Interface - API docs"> - + windowtitle="Jexer - Java Text User Interface - API docs" + additionalparam="--frames" + > diff --git a/src/jexer/TApplication.java b/src/jexer/TApplication.java index 1b396a73..264b8c82 100644 --- a/src/jexer/TApplication.java +++ b/src/jexer/TApplication.java @@ -638,6 +638,8 @@ public class TApplication implements Runnable { * Run this application until it exits. */ public void run() { + // System.err.println("*** TApplication.run() begins ***"); + // Start the main consumer thread primaryEventHandler = new WidgetEventHandler(this, true); (new Thread(primaryEventHandler)).start(); @@ -721,6 +723,11 @@ public class TApplication implements Runnable { // resources. closeAllWindows(); + // Give the overarching application an opportunity to release + // resources. + onExit(); + + // System.err.println("*** TApplication.run() exits ***"); } // ------------------------------------------------------------------------ @@ -914,7 +921,7 @@ public class TApplication implements Runnable { // Abort everything if (event instanceof TCommandEvent) { TCommandEvent command = (TCommandEvent) event; - if (command.getCmd().equals(cmAbort)) { + if (command.equals(cmAbort)) { exit(); return; } @@ -1481,7 +1488,7 @@ public class TApplication implements Runnable { String version = getClass().getPackage().getImplementationVersion(); if (version == null) { // This is Java 9+, use a hardcoded string here. - version = "0.3.0"; + version = "0.3.1"; } messageBox(i18n.getString("aboutDialogTitle"), MessageFormat.format(i18n.getString("aboutDialogText"), version), @@ -1810,6 +1817,14 @@ public class TApplication implements Runnable { } } + /** + * Subclasses can use this hook to cleanup resources. Called as the last + * step of TApplication.run(). + */ + public void onExit() { + // Default does nothing. + } + // ------------------------------------------------------------------------ // TWindow management ----------------------------------------------------- // ------------------------------------------------------------------------ diff --git a/src/jexer/TCommand.java b/src/jexer/TCommand.java index 49b7e671..874a29dd 100644 --- a/src/jexer/TCommand.java +++ b/src/jexer/TCommand.java @@ -135,6 +135,11 @@ public class TCommand { */ public static final int SAVE = 30; + /** + * Backend disconnected. + */ + public static final int BACKEND_DISCONNECT = 100; + public static final TCommand cmAbort = new TCommand(ABORT); public static final TCommand cmExit = new TCommand(EXIT); public static final TCommand cmQuit = new TCommand(EXIT); @@ -155,6 +160,7 @@ public class TCommand { public static final TCommand cmHelp = new TCommand(HELP); public static final TCommand cmSave = new TCommand(SAVE); public static final TCommand cmMenu = new TCommand(MENU); + public static final TCommand cmBackendDisconnect = new TCommand(BACKEND_DISCONNECT); // ------------------------------------------------------------------------ // Variables -------------------------------------------------------------- diff --git a/src/jexer/backend/ECMA48Terminal.java b/src/jexer/backend/ECMA48Terminal.java index afd15357..d10437c1 100644 --- a/src/jexer/backend/ECMA48Terminal.java +++ b/src/jexer/backend/ECMA48Terminal.java @@ -50,10 +50,12 @@ import jexer.TImage; import jexer.bits.Cell; import jexer.bits.CellAttributes; import jexer.bits.Color; +import jexer.event.TCommandEvent; import jexer.event.TInputEvent; import jexer.event.TKeypressEvent; import jexer.event.TMouseEvent; import jexer.event.TResizeEvent; +import static jexer.TCommand.*; import static jexer.TKeypress.*; /** @@ -1472,8 +1474,15 @@ public class ECMA48Terminal extends LogicalScreen } } // while ((done == false) && (stopReaderThread == false)) - // TODO: pass an event up to TApplication to tell it this Backend is - // done. + // Pass an event up to TApplication to tell it this Backend is done. + synchronized (eventQueue) { + eventQueue.add(new TCommandEvent(cmBackendDisconnect)); + } + if (listener != null) { + synchronized (listener) { + listener.notifyAll(); + } + } // System.err.println("*** run() exiting..."); System.err.flush(); } diff --git a/src/jexer/backend/GenericBackend.java b/src/jexer/backend/GenericBackend.java index fa72956c..8d8fb375 100644 --- a/src/jexer/backend/GenericBackend.java +++ b/src/jexer/backend/GenericBackend.java @@ -31,6 +31,8 @@ package jexer.backend; import java.util.List; import jexer.event.TInputEvent; +import jexer.event.TCommandEvent; +import static jexer.TCommand.*; /** * This abstract class provides a screen, keyboard, and mouse to @@ -108,6 +110,18 @@ public abstract class GenericBackend implements Backend { public void getEvents(final List queue) { if (terminal.hasEvents()) { terminal.getEvents(queue); + + // This default backend assumes a single user, and if that user + // becomes disconnected we should terminate the application. + if (queue.size() > 0) { + TInputEvent event = queue.get(queue.size() - 1); + if (event instanceof TCommandEvent) { + TCommandEvent command = (TCommandEvent) event; + if (command.equals(cmBackendDisconnect)) { + queue.add(new TCommandEvent(cmAbort)); + } + } + } } } -- 2.27.0