#38 notify backend of lost connection, version bump to 0.3.1
authorKevin Lamonte <kevin.lamonte@gmail.com>
Sat, 2 Mar 2019 01:43:09 +0000 (19:43 -0600)
committerKevin Lamonte <kevin.lamonte@gmail.com>
Sat, 2 Mar 2019 01:43:09 +0000 (19:43 -0600)
build.xml
src/jexer/TApplication.java
src/jexer/TCommand.java
src/jexer/backend/ECMA48Terminal.java
src/jexer/backend/GenericBackend.java

index b4669e36fcc37bdefc1c9d26f7d0d3fcd4f7ac0d..c212a757d345c2351ca795e7635e23fabcf657bf 100644 (file)
--- a/build.xml
+++ b/build.xml
@@ -30,7 +30,7 @@
 
 <project name="jexer" basedir="." default="jar">
 
-  <property name="version"       value="0.3.0"/>
+  <property name="version"       value="0.3.1"/>
   <property name="src.dir"       value="src"/>
   <property name="resources.dir" value="resources"/>
   <property name="build.dir"     value="build"/>
@@ -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"
+        >
       <fileset dir="${src.dir}" defaultexcludes="yes">
         <include name="jexer/**/*.java"/>
       </fileset>
index 1b396a73eca33bd80f6cbfbc4765113e4783d7b3..264b8c8220e3fddb3b062e612a97ed74761f22c2 100644 (file)
@@ -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 -----------------------------------------------------
     // ------------------------------------------------------------------------
index 49b7e67122c738424254f894037639bc1392b646..874a29dda7fcf2ceae1d92c622d034fb31fb78e9 100644 (file)
@@ -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 --------------------------------------------------------------
index afd153579e961c9637412a32faf73792394ac85f..d10437c1f49dd377bce18ac786b9ecfae3555357 100644 (file)
@@ -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();
     }
index fa72956c8e27c6c247005816ff8f3489b505d070..8d8fb3752243161eed12545ce3ebba9c5cfd4ba2 100644 (file)
@@ -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<TInputEvent> 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));
+                    }
+                }
+            }
         }
     }