From 91c9a8376f979d2b3df43f14519a30a183d332c5 Mon Sep 17 00:00:00 2001 From: Kevin Lamonte Date: Sun, 29 Mar 2015 13:57:56 -0400 Subject: [PATCH] bugfixes --- README.md | 1 - src/jexer/TButton.java | 10 ++++ src/jexer/TMessageBox.java | 84 +++++++++++++++++++++++++++++++++- src/jexer/bits/ColorTheme.java | 11 +++++ src/jexer/menu/TMenu.java | 16 +++++-- src/jexer/menu/TSubMenu.java | 12 +++++ 6 files changed, 126 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index be52f13..afb1ca6 100644 --- 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 diff --git a/src/jexer/TButton.java b/src/jexer/TButton.java index 0fc36a2..30e94ed 100644 --- a/src/jexer/TButton.java +++ b/src/jexer/TButton.java @@ -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. * diff --git a/src/jexer/TMessageBox.java b/src/jexer/TMessageBox.java index 44ab5e3..8f55ce8 100644 --- a/src/jexer/TMessageBox.java +++ b/src/jexer/TMessageBox.java @@ -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 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 buttons = new ArrayList(); + buttons = new ArrayList(); 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); + } + } diff --git a/src/jexer/bits/ColorTheme.java b/src/jexer/bits/ColorTheme.java index 60f706e..3c518ce 100644 --- a/src/jexer/bits/ColorTheme.java +++ b/src/jexer/bits/ColorTheme.java @@ -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. * diff --git a/src/jexer/menu/TMenu.java b/src/jexer/menu/TMenu.java index 165d951..ecdb7f7 100644 --- a/src/jexer/menu/TMenu.java +++ b/src/jexer/menu/TMenu.java @@ -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)) { diff --git a/src/jexer/menu/TSubMenu.java b/src/jexer/menu/TSubMenu.java index 1f1b119..a109710 100644 --- a/src/jexer/menu/TSubMenu.java +++ b/src/jexer/menu/TSubMenu.java @@ -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; -- 2.27.0