#51 working OK
[fanfix.git] / examples / JexerTilingWindowManager2.java
index cc23aa76f0840309361e879fa40d7916eecb5f69..e06119b2ba14ef5ae0b5183498b5752c84431b47 100644 (file)
@@ -1,12 +1,13 @@
+import java.util.ArrayList;
 import jexer.TAction;
 import jexer.TApplication;
 import jexer.TDesktop;
+import jexer.TPanel;
 import jexer.TTerminalWidget;
+import jexer.TSplitPane;
 import jexer.TWidget;
 import jexer.event.TKeypressEvent;
 import jexer.event.TMenuEvent;
-import jexer.event.TMouseEvent;
-import jexer.event.TResizeEvent;
 import jexer.menu.TMenu;
 
 /**
@@ -29,12 +30,16 @@ 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.
      */
     private TWidget root = null;
-    
+
     /**
      * Main entry point.
      */
@@ -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.
@@ -78,17 +84,7 @@ public class JexerTilingWindowManager2 extends TApplication {
         tileMenu.addDefaultItem(TMenu.MID_EXIT);
 
         // Spin up the root terminal
-        root = new TTerminalWidget(getDesktop(), 0, 0,
-            getDesktop().getWidth(), getDesktop().getHeight(),
-            new TAction() {
-                public void DO() {
-                    // TODO: if root's parent is TSplitPane, call
-                    // TSplitPane.removeSplit(TWidget).
-                    if (root != null) {
-                        root.remove();
-                    }
-                }
-            });
+        createRootTerminal();
     }
 
     /**
@@ -96,30 +92,75 @@ public class JexerTilingWindowManager2 extends TApplication {
      */
     @Override
     protected boolean onMenu(TMenuEvent event) {
-        if (event.getId() == MENU_SPLIT_VERTICAL) {
-            splitVertical();
+        TWidget active = getDesktop().getActiveChild();
+        TSplitPane split = null;
+
+        switch (event.getId()) {
+        case MENU_RESPAWN_ROOT:
+            assert (root == null);
+            createRootTerminal();
             return true;
-        }
-        if (event.getId() == MENU_SPLIT_HORIZONTAL) {
-            splitHorizontal();
+
+        case MENU_SPLIT_VERTICAL:
+            if (root == null) {
+                assert (getDesktop().getActiveChild() == null);
+                createRootTerminal();
+                return true;
+            }
+            split = active.splitVertical(false, createTerminal());
+            if (active == root) {
+                root = split;
+            }
+            return true;
+
+        case MENU_SPLIT_HORIZONTAL:
+            if (root == null) {
+                assert (getDesktop().getActiveChild() == null);
+                createRootTerminal();
+                return true;
+            }
+            split = active.splitHorizontal(false, createTerminal());
+            if (active == root) {
+                root = split;
+            }
             return true;
+
+        default:
+            return super.onMenu(event);
         }
 
-        return super.onMenu(event);
     }
 
     /**
-     * Perform the vertical split.
+     * Create the root terminal.
      */
-    private void splitVertical() {
-        // TODO
+    private void createRootTerminal() {
+        assert (root == null);
+        disableMenuItem(MENU_RESPAWN_ROOT);
+        root = createTerminal();
     }
 
     /**
-     * Perform the horizontal split.
+     * Create a new terminal.
+     *
+     * @return the new terminal
      */
-    private void splitHorizontal() {
-        // TODO
+    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 {
+                        source.getApplication().enableMenuItem(
+                                MENU_RESPAWN_ROOT);
+                        source.remove();
+                        root = null;
+                    }
+                }
+            });
     }
 
 }