Merge branch 'master' of https://github.com/klamonte/jexer
authorKevin Lamonte <kevin.lamonte@gmail.com>
Sun, 29 Mar 2015 21:28:16 +0000 (17:28 -0400)
committerKevin Lamonte <kevin.lamonte@gmail.com>
Sun, 29 Mar 2015 21:28:16 +0000 (17:28 -0400)
README.md
src/jexer/TButton.java
src/jexer/TMessageBox.java
src/jexer/bits/ColorTheme.java
src/jexer/menu/TMenu.java
src/jexer/menu/TSubMenu.java

index be52f13b5ac2e3238878fe7e40c73f78bcc22951..afb1ca65d4ff7d8cacc54c9bd04dd5e0f24e46cf 100644 (file)
--- a/README.md
+++ b/README.md
@@ -185,7 +185,6 @@ Many tasks remain before calling this version 1.0:
 
 0.0.5: BUG HUNT
 
-- TSubMenu keyboard mnemonic not working
 - Swing performance.  Even with double buffering it isn't great.
 
 0.1.0: BETA RELEASE
index c05a4b1025be40ec9bf7e76c858a9c0bba404c78..fb8c08e35c4c3a48b6361036837d308c7eb3b9cc 100644 (file)
@@ -75,6 +75,16 @@ public final class TButton extends TWidget {
      */
     private TAction action;
 
+    /**
+     * Act as though the button was pressed.  This is useful for other UI
+     * elements to get the same action as if the user clicked the button.
+     */
+    public void dispatch() {
+        if (action != null) {
+            action.DO();
+        }
+    }
+
     /**
      * Private constructor.
      *
index 1f05038cf5c7f14a185ef2e42147b7ea7e971384..9de90dcdf4c2197aba6fb40a8c45e344bc4d983c 100644 (file)
@@ -33,6 +33,9 @@ package jexer;
 import java.util.ArrayList;
 import java.util.List;
 
+import jexer.event.TKeypressEvent;
+import static jexer.TKeypress.*;
+
 /**
  * TMessageBox is a system-modal dialog with buttons for OK, Cancel, Yes, or
  * No.  Call it like:
@@ -77,6 +80,16 @@ public class TMessageBox extends TWindow {
         YESNOCANCEL
     };
 
+    /**
+     * The type of this message box.
+     */
+    private Type type;
+
+    /**
+     * My buttons.
+     */
+    List<TButton> buttons;
+
     /**
      * Message boxes have these possible results.
      */
@@ -102,7 +115,6 @@ public class TMessageBox extends TWindow {
         NO
     };
 
-
     /**
      * Which button was clicked: OK, CANCEL, YES, or NO.
      */
@@ -163,6 +175,10 @@ public class TMessageBox extends TWindow {
         // Start as 50x50 at (1, 1).  These will be changed later.
         super(application, title, 1, 1, 100, 100, CENTERED | MODAL);
 
+        // Hang onto type so that we can provide more convenience in
+        // onKeypress().
+        this.type = type;
+
         // Determine width and height
         String [] lines = caption.split("\n");
         int width = title.length() + 12;
@@ -188,7 +204,7 @@ public class TMessageBox extends TWindow {
 
         // The button line
         lineI++;
-        List<TButton> buttons = new ArrayList<TButton>();
+        buttons = new ArrayList<TButton>();
 
         int buttonX = 0;
 
@@ -317,4 +333,68 @@ public class TMessageBox extends TWindow {
         }
     }
 
+    /**
+     * Handle keystrokes.
+     *
+     * @param keypress keystroke event
+     */
+    @Override
+    public void onKeypress(final TKeypressEvent keypress) {
+
+        if (this instanceof TInputBox) {
+            super.onKeypress(keypress);
+            return;
+        }
+
+        // Some convenience for message boxes: Alt won't be needed for the
+        // buttons.
+        switch (type) {
+
+        case OK:
+            if (keypress.equals(kbO)) {
+                buttons.get(0).dispatch();
+                return;
+            }
+            break;
+
+        case OKCANCEL:
+            if (keypress.equals(kbO)) {
+                buttons.get(0).dispatch();
+                return;
+            } else if (keypress.equals(kbC)) {
+                buttons.get(1).dispatch();
+                return;
+            }
+            break;
+
+        case YESNO:
+            if (keypress.equals(kbY)) {
+                buttons.get(0).dispatch();
+                return;
+            } else if (keypress.equals(kbN)) {
+                buttons.get(1).dispatch();
+                return;
+            }
+            break;
+
+        case YESNOCANCEL:
+            if (keypress.equals(kbY)) {
+                buttons.get(0).dispatch();
+                return;
+            } else if (keypress.equals(kbN)) {
+                buttons.get(1).dispatch();
+                return;
+            } else if (keypress.equals(kbC)) {
+                buttons.get(2).dispatch();
+                return;
+            }
+            break;
+
+        default:
+            throw new IllegalArgumentException("Invalid message box type: " + type);
+        }
+
+        super.onKeypress(keypress);
+    }
+
 }
index 60f2bd36c7bb0ccce1caaede6ea5e2d007ed4edb..a8c0c899ce32f27ff038f3cbabe3b3a344617c2e 100644 (file)
@@ -68,6 +68,17 @@ public final class ColorTheme {
         return attr;
     }
 
+    /**
+     * Set the color for a named theme color.
+     *
+     * @param name theme color name, e.g. "twindow.border"
+     * @param color the new color to associate with name, e.g. bold yellow on
+     * blue
+     */
+    public void setColor(final String name, final CellAttributes color) {
+        colors.put(name, color);
+    }
+
     /**
      * Save the color theme mappings to an ASCII file.
      *
index 51cc343e8606164291af84a252e561fec695625b..99add703c18fe2d9e59d241bb325b549c16638f1 100644 (file)
@@ -241,8 +241,16 @@ public final class TMenu extends TWindow {
      */
     @Override
     public void onKeypress(final TKeypressEvent keypress) {
-        if (getActiveChild() != null) {
-            if (getActiveChild() instanceof TSubMenu) {
+
+        /*
+        System.err.printf("keypress: %s active child: %s\n", keypress,
+            getActiveChild());
+         */
+
+        if (getActiveChild() != this) {
+            if ((getActiveChild() instanceof TSubMenu)
+                || (getActiveChild() instanceof TMenu)
+            ) {
                 getActiveChild().onKeypress(keypress);
                 return;
             }
@@ -261,9 +269,7 @@ public final class TMenu extends TWindow {
             return;
         }
         if (keypress.equals(kbRight)) {
-            if (!isSubMenu) {
-                getApplication().switchMenu(true);
-            }
+            getApplication().switchMenu(true);
             return;
         }
         if (keypress.equals(kbLeft)) {
index 167b6a5b11fc7d656c912b36bac62ff5d8412587..9d415d5559322d5871bc9d4e6445eb7177efd190 100644 (file)
@@ -100,6 +100,18 @@ public final class TSubMenu extends TMenuItem {
     @Override
     public void onKeypress(final TKeypressEvent keypress) {
 
+        // Open me if they hit my mnemonic.
+        if (!keypress.getKey().isFnKey()
+            && !keypress.getKey().isAlt()
+            && !keypress.getKey().isCtrl()
+            && (getMnemonic() != null)
+            && (Character.toLowerCase(getMnemonic().getShortcut())
+                == Character.toLowerCase(keypress.getKey().getChar()))
+        ) {
+            dispatch();
+            return;
+        }
+
         if (menu.isActive()) {
             menu.onKeypress(keypress);
             return;