#51 working OK
[fanfix.git] / src / jexer / TTerminalWidget.java
index db405cfd72e0b13640f1b9bcded25ae03adf1c5d..39d761c64a1b2ae06fe2df6a5cbd1a143a8b4b5e 100644 (file)
@@ -229,9 +229,6 @@ public class TTerminalWidget extends TScrollableWidget
         super(parent, x, y, width, height);
 
         this.closeAction = closeAction;
-        if (closeAction != null) {
-            this.closeAction.data = this;
-        }
 
         String [] fullCommand;
 
@@ -310,9 +307,6 @@ public class TTerminalWidget extends TScrollableWidget
         super(parent, x, y, width, height);
 
         this.closeAction = closeAction;
-        if (closeAction != null) {
-            this.closeAction.data = this;
-        }
 
         if (System.getProperty("jexer.TTerminal.shell") != null) {
             String shell = System.getProperty("jexer.TTerminal.shell");
@@ -390,6 +384,14 @@ public class TTerminalWidget extends TScrollableWidget
                 // Update the scroll bars
                 reflowData();
 
+                if (!isDrawable()) {
+                    // We lost the connection, onShellExit() called an action
+                    // that ultimately removed this widget from the UI
+                    // hierarchy, so no one cares if we update the display.
+                    // Bail out.
+                    return;
+                }
+
                 if ((scrollback == null) || emulator.isReading()) {
                     scrollback = copyBuffer(emulator.getScrollbackBuffer());
                     display = copyBuffer(emulator.getDisplayBuffer());
@@ -826,17 +828,19 @@ public class TTerminalWidget extends TScrollableWidget
     public void onShellExit() {
         TApplication app = getApplication();
         if (app != null) {
-            app.invokeLater(new Runnable() {
-                public void run() {
-                    if (closeAction != null) {
-                        closeAction.DO();
+            if (closeAction != null) {
+                // We have to put this action inside invokeLater() because it
+                // could be executed during draw() when syncing with ECMA48.
+                app.invokeLater(new Runnable() {
+                    public void run() {
+                        closeAction.DO(TTerminalWidget.this);
                     }
-                    if (getApplication() != null) {
-                        getApplication().postEvent(new TMenuEvent(
-                            TMenu.MID_REPAINT));
-                    }
-                }
-            });
+                });
+            }
+            if (getApplication() != null) {
+                getApplication().postEvent(new TMenuEvent(
+                    TMenu.MID_REPAINT));
+            }
         }
     }