From: Kevin Lamonte Date: Sun, 29 Mar 2015 17:57:56 +0000 (-0400) Subject: bugfixes X-Git-Tag: fanfix-swing-0.0.1~12^2~13^2~298 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=91c9a8376f979d2b3df43f14519a30a183d332c5;p=fanfix-swing.git bugfixes --- diff --git a/README.md b/README.md index be52f13b..afb1ca65 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 0fc36a2d..30e94edc 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 44ab5e3b..8f55ce8c 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 60f706e3..3c518cee 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 165d951b..ecdb7f7c 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 1f1b1197..a109710b 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;