refactored ttable widget to not use menu directly
[fanfix.git] / src / jexer / TApplication.java
index 41981daf3c3ca0e3943223534de125e0c943d835..658e50bbc4bf8d029c121c9c00f59bccda1564a2 100644 (file)
@@ -62,6 +62,7 @@ import jexer.backend.ECMA48Backend;
 import jexer.backend.TWindowBackend;
 import jexer.menu.TMenu;
 import jexer.menu.TMenuItem;
+import jexer.menu.TSubMenu;
 import static jexer.TCommand.*;
 import static jexer.TKeypress.*;
 
@@ -432,9 +433,9 @@ public class TApplication implements Runnable {
                         // resumes working on the primary.
                         application.secondaryEventHandler = null;
 
-                        // DO NOT UNLOCK.  Primary thread just came back from
-                        // primaryHandleEvent() and will unlock in the else
-                        // block below.  Just wake it up.
+                        // We are ready to exit, wake up the primary thread.
+                        // Remember that it is currently sleeping inside its
+                        // primaryHandleEvent().
                         synchronized (application.primaryEventHandler) {
                             application.primaryEventHandler.notify();
                         }
@@ -638,6 +639,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 +724,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 +922,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;
             }
@@ -936,6 +944,7 @@ public class TApplication implements Runnable {
                 if (desktop != null) {
                     desktop.setDimensions(0, 0, resize.getWidth(),
                         resize.getHeight() - 1);
+                    desktop.onResize(resize);
                 }
 
                 // Change menu edges if needed.
@@ -1480,7 +1489,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),
@@ -1508,6 +1517,18 @@ public class TApplication implements Runnable {
         }
     }
 
+    /**
+     * Check if application is still running.
+     *
+     * @return true if the application is running
+     */
+    public final boolean isRunning() {
+        if (quit == true) {
+            return false;
+        }
+        return true;
+    }
+
     // ------------------------------------------------------------------------
     // Screen refresh loop ----------------------------------------------------
     // ------------------------------------------------------------------------
@@ -1522,7 +1543,26 @@ public class TApplication implements Runnable {
         if (debugThreads) {
             System.err.printf("%d %s invertCell() %d %d\n",
                 System.currentTimeMillis(), Thread.currentThread(), x, y);
+
+            if (activeWindow != null) {
+                System.err.println("activeWindow.hasHiddenMouse() " +
+                    activeWindow.hasHiddenMouse());
+            }
+        }
+
+        // If this cell is on top of a visible window that has requested a
+        // hidden mouse, bail out.
+        if ((activeWindow != null) && (activeMenu == null)) {
+            if ((activeWindow.hasHiddenMouse() == true)
+                && (x > activeWindow.getX())
+                && (x < activeWindow.getX() + activeWindow.getWidth() - 1)
+                && (y > activeWindow.getY())
+                && (y < activeWindow.getY() + activeWindow.getHeight() - 1)
+            ) {
+                return;
+            }
         }
+
         Cell cell = getScreen().getCharXY(x, y);
         if (cell.isImage()) {
             cell.invertImage();
@@ -1758,6 +1798,10 @@ public class TApplication implements Runnable {
 
         // Flush the screen contents
         if ((images.size() > 0) || getScreen().isDirty()) {
+            if (debugThreads) {
+                System.err.printf("%d %s backend.flushScreen()\n",
+                    System.currentTimeMillis(), Thread.currentThread());
+            }
             backend.flushScreen();
         }
 
@@ -1774,6 +1818,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 -----------------------------------------------------
     // ------------------------------------------------------------------------
@@ -1889,7 +1941,8 @@ public class TApplication implements Runnable {
 
         assert (!window.isActive());
         if (activeWindow != null) {
-            assert (activeWindow.getZ() == 0);
+            // TODO: see if this assertion is really necessary.
+            // assert (activeWindow.getZ() == 0);
 
             activeWindow.setActive(false);
 
@@ -2222,6 +2275,21 @@ public class TApplication implements Runnable {
         return false;
     }
 
+    /**
+     * Check if there is a window with overridden menu flag on top.
+     *
+     * @return true if the active window is overriding the menu
+     */
+    private boolean overrideMenuWindowActive() {
+        if (activeWindow != null) {
+            if (activeWindow.hasOverriddenMenu()) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
     /**
      * Close all open windows.
      */
@@ -2552,6 +2620,7 @@ public class TApplication implements Runnable {
         if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN)
             && (mouse.isMouse1())
             && (!modalWindowActive())
+            && (!overrideMenuWindowActive())
             && (mouse.getAbsoluteY() == 0)
         ) {
 
@@ -2854,6 +2923,21 @@ public class TApplication implements Runnable {
         }
     }
 
+    /**
+     * Get the menu item associated with this ID.
+     *
+     * @param id the menu item ID
+     * @return the menu item, or null if not found
+     */
+    public final TMenuItem getMenuItem(final int id) {
+        for (TMenuItem item: menuItems) {
+            if (item.getId() == id) {
+                return item;
+            }
+        }
+        return null;
+    }
+
     /**
      * Recompute menu x positions based on their title length.
      */
@@ -2956,9 +3040,8 @@ public class TApplication implements Runnable {
      */
     public final TMenu addFileMenu() {
         TMenu fileMenu = addMenu(i18n.getString("fileMenuTitle"));
-        fileMenu.addDefaultItem(TMenu.MID_OPEN_FILE);
-        fileMenu.addSeparator();
         fileMenu.addDefaultItem(TMenu.MID_SHELL);
+        fileMenu.addSeparator();
         fileMenu.addDefaultItem(TMenu.MID_EXIT);
         TStatusBar statusBar = fileMenu.newStatusBar(i18n.
             getString("fileMenuStatus"));
@@ -3026,6 +3109,64 @@ public class TApplication implements Runnable {
         return helpMenu;
     }
 
+    /**
+     * Convenience function to add a default "Table" menu.
+     *
+     * @return the new menu
+     */
+    public final TMenu addTableMenu() {
+        TMenu tableMenu = addMenu(i18n.getString("tableMenuTitle"));
+        tableMenu.addDefaultItem(TMenu.MID_TABLE_RENAME_COLUMN, false);
+        tableMenu.addDefaultItem(TMenu.MID_TABLE_RENAME_ROW, false);
+        tableMenu.addSeparator();
+
+        TSubMenu viewMenu = tableMenu.addSubMenu(i18n.
+            getString("tableSubMenuView"));
+        viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_ROW_LABELS, false);
+        viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_COLUMN_LABELS, false);
+        viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_ROW, false);
+        viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_COLUMN, false);
+
+        TSubMenu borderMenu = tableMenu.addSubMenu(i18n.
+            getString("tableSubMenuBorders"));
+        borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_NONE, false);
+        borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_ALL, false);
+        borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_CELL_NONE, false);
+        borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_CELL_ALL, false);
+        borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_RIGHT, false);
+        borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_LEFT, false);
+        borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_TOP, false);
+        borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_BOTTOM, false);
+        borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_DOUBLE_BOTTOM, false);
+        borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_THICK_BOTTOM, false);
+        TSubMenu deleteMenu = tableMenu.addSubMenu(i18n.
+            getString("tableSubMenuDelete"));
+        deleteMenu.addDefaultItem(TMenu.MID_TABLE_DELETE_LEFT, false);
+        deleteMenu.addDefaultItem(TMenu.MID_TABLE_DELETE_UP, false);
+        deleteMenu.addDefaultItem(TMenu.MID_TABLE_DELETE_ROW, false);
+        deleteMenu.addDefaultItem(TMenu.MID_TABLE_DELETE_COLUMN, false);
+        TSubMenu insertMenu = tableMenu.addSubMenu(i18n.
+            getString("tableSubMenuInsert"));
+        insertMenu.addDefaultItem(TMenu.MID_TABLE_INSERT_LEFT, false);
+        insertMenu.addDefaultItem(TMenu.MID_TABLE_INSERT_RIGHT, false);
+        insertMenu.addDefaultItem(TMenu.MID_TABLE_INSERT_ABOVE, false);
+        insertMenu.addDefaultItem(TMenu.MID_TABLE_INSERT_BELOW, false);
+        TSubMenu columnMenu = tableMenu.addSubMenu(i18n.
+            getString("tableSubMenuColumn"));
+        columnMenu.addDefaultItem(TMenu.MID_TABLE_COLUMN_NARROW, false);
+        columnMenu.addDefaultItem(TMenu.MID_TABLE_COLUMN_WIDEN, false);
+        TSubMenu fileMenu = tableMenu.addSubMenu(i18n.
+            getString("tableSubMenuFile"));
+        fileMenu.addDefaultItem(TMenu.MID_TABLE_FILE_OPEN_CSV, false);
+        fileMenu.addDefaultItem(TMenu.MID_TABLE_FILE_SAVE_CSV, false);
+        fileMenu.addDefaultItem(TMenu.MID_TABLE_FILE_SAVE_TEXT, false);
+
+        TStatusBar statusBar = tableMenu.newStatusBar(i18n.
+            getString("tableMenuStatus"));
+        statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help"));
+        return tableMenu;
+    }
+
     // ------------------------------------------------------------------------
     // TTimer management ------------------------------------------------------
     // ------------------------------------------------------------------------
@@ -3298,7 +3439,7 @@ public class TApplication implements Runnable {
     public final TTerminalWindow openTerminal(final int x, final int y,
         final int flags, final String commandLine) {
 
-        return new TTerminalWindow(this, x, y, flags, commandLine.split("\\s"));
+        return new TTerminalWindow(this, x, y, flags, commandLine.split("\\s+"));
     }
 
     /**
@@ -3315,7 +3456,7 @@ public class TApplication implements Runnable {
     public final TTerminalWindow openTerminal(final int x, final int y,
         final int flags, final String commandLine, final boolean closeOnExit) {
 
-        return new TTerminalWindow(this, x, y, flags, commandLine.split("\\s"),
+        return new TTerminalWindow(this, x, y, flags, commandLine.split("\\s+"),
             closeOnExit);
     }