#51 working OK
authorKevin Lamonte <kevin.lamonte@gmail.com>
Thu, 22 Aug 2019 10:07:00 +0000 (05:07 -0500)
committerKevin Lamonte <kevin.lamonte@gmail.com>
Thu, 22 Aug 2019 10:07:00 +0000 (05:07 -0500)
examples/JexerTilingWindowManager2.java
src/jexer/TApplication.java
src/jexer/TTerminalWindow.java
src/jexer/tterminal/ECMA48.java

index accbf739317bf0212c1c7aa07c6fb2e934b84b11..e06119b2ba14ef5ae0b5183498b5752c84431b47 100644 (file)
@@ -1,3 +1,4 @@
+import java.util.ArrayList;
 import jexer.TAction;
 import jexer.TApplication;
 import jexer.TDesktop;
@@ -29,6 +30,10 @@ public class JexerTilingWindowManager2 extends TApplication {
      * Menu item: split the terminal horizontally.
      */
     private static final int MENU_SPLIT_HORIZONTAL = 2001;
+    /**
+     * Menu item: recreate the root terminal.
+     */
+    private static final int MENU_RESPAWN_ROOT = 2002;
 
     /**
      * Handle to the root widget.
@@ -66,6 +71,7 @@ public class JexerTilingWindowManager2 extends TApplication {
         // New commands for this example: split vertical and horizontal.
         tileMenu.addItem(MENU_SPLIT_VERTICAL, "&Vertical Split");
         tileMenu.addItem(MENU_SPLIT_HORIZONTAL, "&Horizontal Split");
+        tileMenu.addItem(MENU_RESPAWN_ROOT, "&Respawn Root Terminal");
 
         // Stock commands: a new shell with resizable window, previous, next,
         // close, and exit program.
@@ -86,64 +92,43 @@ public class JexerTilingWindowManager2 extends TApplication {
      */
     @Override
     protected boolean onMenu(TMenuEvent event) {
-        if (event.getId() == MENU_SPLIT_VERTICAL) {
+        TWidget active = getDesktop().getActiveChild();
+        TSplitPane split = null;
+
+        switch (event.getId()) {
+        case MENU_RESPAWN_ROOT:
+            assert (root == null);
+            createRootTerminal();
+            return true;
+
+        case MENU_SPLIT_VERTICAL:
             if (root == null) {
                 assert (getDesktop().getActiveChild() == null);
                 createRootTerminal();
                 return true;
             }
-            TWidget active = getDesktop().getActiveChild();
-            TSplitPane split = active.splitVertical(false,
-                new TTerminalWidget(active, active.getX(),
-                    active.getY(), active.getWidth(), active.getHeight(),
-                    new TAction() {
-                        public void DO() {
-                            if (source.getParent() instanceof TSplitPane) {
-                                ((TSplitPane) source.getParent()).removeSplit(source, true);
-                            } else if (source == root) {
-                                assert (root != null);
-                                root.remove();
-                                root = null;
-                            }
-                        }
-                    }));
+            split = active.splitVertical(false, createTerminal());
             if (active == root) {
                 root = split;
             }
-            System.err.println("\nAfter vertical split:");
-            System.err.println(getDesktop().toPrettyString());
             return true;
-        }
-        if (event.getId() == MENU_SPLIT_HORIZONTAL) {
+
+        case MENU_SPLIT_HORIZONTAL:
             if (root == null) {
                 assert (getDesktop().getActiveChild() == null);
                 createRootTerminal();
                 return true;
             }
-            TWidget active = getDesktop().getActiveChild();
-            TSplitPane split = active.splitHorizontal(false,
-                new TTerminalWidget(active, active.getX(),
-                    active.getY(), active.getWidth(), active.getHeight(),
-                    new TAction() {
-                        public void DO() {
-                            if (source.getParent() instanceof TSplitPane) {
-                                ((TSplitPane) source.getParent()).removeSplit(source, true);
-                            } else if (source == root) {
-                                assert (root != null);
-                                root.remove();
-                                root = null;
-                            }
-                        }
-                    }));
+            split = active.splitHorizontal(false, createTerminal());
             if (active == root) {
                 root = split;
             }
-            System.err.println("\nAfter horizontal split:");
-            System.err.println(getDesktop().toPrettyString());
             return true;
+
+        default:
+            return super.onMenu(event);
         }
 
-        return super.onMenu(event);
     }
 
     /**
@@ -151,15 +136,27 @@ public class JexerTilingWindowManager2 extends TApplication {
      */
     private void createRootTerminal() {
         assert (root == null);
-        root = new TTerminalWidget(getDesktop(), 0, 0,
+        disableMenuItem(MENU_RESPAWN_ROOT);
+        root = createTerminal();
+    }
+
+    /**
+     * Create a new terminal.
+     *
+     * @return the new terminal
+     */
+    private TWidget createTerminal() {
+        return new TTerminalWidget(getDesktop(), 0, 0,
             getDesktop().getWidth(), getDesktop().getHeight(),
             new TAction() {
                 public void DO() {
                     if (source.getParent() instanceof TSplitPane) {
-                        ((TSplitPane) source.getParent()).removeSplit(source, true);
-                    } else if (source == root) {
-                        assert (root != null);
-                        root.remove();
+                        ((TSplitPane) source.getParent()).removeSplit(source,
+                            true);
+                    } else {
+                        source.getApplication().enableMenuItem(
+                                MENU_RESPAWN_ROOT);
+                        source.remove();
                         root = null;
                     }
                 }
index 14ebacda7f4fa0a645c0958018d1bb8871f332e9..affc6ce4d1487118a23aa30d5673ad23c648f409 100644 (file)
@@ -869,6 +869,11 @@ public class TApplication implements Runnable {
         // resources.
         closeAllWindows();
 
+        // Close the desktop.
+        if (desktop != null) {
+            setDesktop(null);
+        }
+
         // Give the overarching application an opportunity to release
         // resources.
         onExit();
@@ -1611,6 +1616,8 @@ public class TApplication implements Runnable {
      */
     public final void setDesktop(final TDesktop desktop) {
         if (this.desktop != null) {
+            this.desktop.onPreClose();
+            this.desktop.onUnfocus();
             this.desktop.onClose();
         }
         this.desktop = desktop;
index 34e7dc411d298af3612af465b641db1e95547e83..9a24305a7b9bb252714ed1a2f043985dc4f64afd 100644 (file)
@@ -153,6 +153,9 @@ public class TTerminalWindow extends TScrollableWindow {
         super(application, i18n.getString("windowTitle"), x, y,
             80 + 2, 24 + 2, flags);
 
+        // Require at least one line for the display.
+        setMinimumWindowHeight(3);
+
         this.closeOnExit = closeOnExit;
         vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
 
@@ -202,6 +205,9 @@ public class TTerminalWindow extends TScrollableWindow {
         super(application, i18n.getString("windowTitle"), x, y,
             80 + 2, 24 + 2, flags);
 
+        // Require at least one line for the display.
+        setMinimumWindowHeight(3);
+
         this.closeOnExit = closeOnExit;
         vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
 
index 05eda72093e1d327bc47fadb3457e7484872a1f2..c277795d69450b2db44f79d77a1caa8062c11275 100644 (file)
@@ -1092,7 +1092,7 @@ public class ECMA48 implements Runnable {
      *
      * @param width the new width
      */
-    public final void setWidth(final int width) {
+    public final synchronized void setWidth(final int width) {
         this.width = width;
         rightMargin = width - 1;
         if (currentState.cursorX >= width) {
@@ -1117,7 +1117,7 @@ public class ECMA48 implements Runnable {
      *
      * @param height the new height
      */
-    public final void setHeight(final int height) {
+    public final synchronized void setHeight(final int height) {
         int delta = height - this.height;
         this.height = height;
         scrollRegionBottom += delta;