From: Kevin Lamonte Date: Sat, 14 Mar 2015 21:14:12 +0000 (-0400) Subject: keyboard accelerators X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=e826b451baf0d1e66d09ce03a6fefee2eb8386f5;p=nikiroo-utils.git keyboard accelerators --- diff --git a/src/jexer/TApplication.java b/src/jexer/TApplication.java index 35aa32d..d2b8f44 100644 --- a/src/jexer/TApplication.java +++ b/src/jexer/TApplication.java @@ -34,8 +34,10 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.Collections; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import jexer.bits.CellAttributes; import jexer.bits.ColorTheme; @@ -109,6 +111,11 @@ public class TApplication { */ private TMenu activeMenu = null; + /** + * Active keyboard accelerators. + */ + private Map accelerators; + /** * Windows and widgets pull colors from this ColorTheme. */ @@ -204,6 +211,7 @@ public class TApplication { windows = new LinkedList(); menus = new LinkedList(); subMenus = new LinkedList(); + accelerators = new HashMap(); } /** @@ -394,8 +402,8 @@ public class TApplication { private void metaHandleEvent(final TInputEvent event) { /* - System.err.printf(String.format("metaHandleEvents event: %s\n", - event)); System.err.flush(); + System.err.printf(String.format("metaHandleEvents event: %s\n", + event)); System.err.flush(); */ if (quit) { @@ -404,16 +412,6 @@ public class TApplication { return; } - // DEBUG - if (event instanceof TKeypressEvent) { - TKeypressEvent keypress = (TKeypressEvent) event; - if (keypress.equals(kbAltX)) { - quit = true; - return; - } - } - // DEBUG - // Special application-wide events ------------------------------- // Abort everything @@ -530,15 +528,16 @@ public class TApplication { return; } - /* - TODO - if (event instanceof TKeypressEvent) { TKeypressEvent keypress = (TKeypressEvent) event; + // See if this key matches an accelerator, and if so dispatch the // menu event. TKeypress keypressLowercase = keypress.getKey().toLowerCase(); - TMenuItem item = accelerators.get(keypressLowercase); + TMenuItem item = null; + synchronized (accelerators) { + item = accelerators.get(keypressLowercase); + } if (item != null) { // Let the menu item dispatch item.dispatch(); @@ -550,7 +549,6 @@ public class TApplication { } } } - */ if (event instanceof TCommandEvent) { if (onCommand((TCommandEvent) event)) { @@ -1126,11 +1124,13 @@ public class TApplication { */ public final void addAccelerator(final TMenuItem item, final TKeypress keypress) { - /* - TODO - assert((keypress in accelerators) is null); - accelerators[keypress] = item; - */ + + // System.err.printf("addAccelerator: key %s item %s\n", keypress, item); + + synchronized (accelerators) { + assert (accelerators.get(keypress) == null); + accelerators.put(keypress, item); + } } /** diff --git a/src/jexer/TCommand.java b/src/jexer/TCommand.java index 62ff2be..bacc6f1 100644 --- a/src/jexer/TCommand.java +++ b/src/jexer/TCommand.java @@ -158,6 +158,16 @@ public class TCommand { return (type == that.type); } + /** + * Hashcode uses all fields in equals(). + * + * @return the hash + */ + @Override + public int hashCode() { + return type; + } + public static final TCommand cmAbort = new TCommand(ABORT); public static final TCommand cmExit = new TCommand(EXIT); public static final TCommand cmQuit = new TCommand(EXIT); diff --git a/src/jexer/TKeypress.java b/src/jexer/TKeypress.java index 54be5d3..8143f15 100644 --- a/src/jexer/TKeypress.java +++ b/src/jexer/TKeypress.java @@ -298,6 +298,25 @@ public final class TKeypress { && (shift == that.shift)); } + /** + * Hashcode uses all fields in equals(). + * + * @return the hash + */ + @Override + public int hashCode() { + int A = 13; + int B = 23; + int hash = A; + hash = (B * hash) + (isKey ? 1 : 0); + hash = (B * hash) + fnKey; + hash = (B * hash) + (int)ch; + hash = (B * hash) + (alt ? 1 : 0); + hash = (B * hash) + (ctrl ? 1 : 0); + hash = (B * hash) + (shift ? 1 : 0); + return hash; + } + /** * Make human-readable description of this TKeypress. * @@ -471,16 +490,14 @@ public final class TKeypress { } /** - * Convert a keypress to lowercase. Function keys and ctrl keys are not - * converted. + * Convert a keypress to lowercase. Function keys and alt/ctrl keys are + * not converted. * - * @param key keypress to convert * @return a new instance with the key converted */ - public static TKeypress toLower(final TKeypress key) { - TKeypress newKey = new TKeypress(key.isKey, key.fnKey, key.ch, - key.alt, key.ctrl, key.shift); - if (!(key.isKey) && (key.ch >= 'A') && (key.ch <= 'Z') && (!key.ctrl)) { + public TKeypress toLowerCase() { + TKeypress newKey = new TKeypress(isKey, fnKey, ch, alt, ctrl, shift); + if (!isKey && (ch >= 'A') && (ch <= 'Z') && !ctrl && !alt) { newKey.shift = false; newKey.ch += 32; } @@ -488,16 +505,14 @@ public final class TKeypress { } /** - * Convert a keypress to uppercase. Function keys and ctrl keys are not - * converted. + * Convert a keypress to uppercase. Function keys and alt/ctrl keys are + * not converted. * - * @param key keypress to convert * @return a new instance with the key converted */ - public static TKeypress toUpper(final TKeypress key) { - TKeypress newKey = new TKeypress(key.isKey, key.fnKey, key.ch, - key.alt, key.ctrl, key.shift); - if (!(key.isKey) && (key.ch >= 'a') && (key.ch <= 'z') && (!key.ctrl)) { + public TKeypress toUpperCase() { + TKeypress newKey = new TKeypress(isKey, fnKey, ch, alt, ctrl, shift); + if (!isKey && (ch >= 'a') && (ch <= 'z') && !ctrl && !alt) { newKey.shift = true; newKey.ch -= 32; } diff --git a/src/jexer/bits/Cell.java b/src/jexer/bits/Cell.java index 3c0eb14..511963c 100644 --- a/src/jexer/bits/Cell.java +++ b/src/jexer/bits/Cell.java @@ -107,6 +107,21 @@ public final class Cell extends CellAttributes { && (ch == that.ch)); } + /** + * Hashcode uses all fields in equals(). + * + * @return the hash + */ + @Override + public int hashCode() { + int A = 13; + int B = 23; + int hash = A; + hash = (B * hash) + super.hashCode(); + hash = (B * hash) + (int)ch; + return hash; + } + /** * Set my field values to that's field. * diff --git a/src/jexer/bits/CellAttributes.java b/src/jexer/bits/CellAttributes.java index a3d912c..d1aea2c 100644 --- a/src/jexer/bits/CellAttributes.java +++ b/src/jexer/bits/CellAttributes.java @@ -242,12 +242,32 @@ public class CellAttributes { && (backColor == that.backColor)); } + /** + * Hashcode uses all fields in equals(). + * + * @return the hash + */ + @Override + public int hashCode() { + int A = 13; + int B = 23; + int hash = A; + hash = (B * hash) + (bold ? 1 : 0); + hash = (B * hash) + (blink ? 1 : 0); + hash = (B * hash) + (underline ? 1 : 0); + hash = (B * hash) + (reverse ? 1 : 0); + hash = (B * hash) + (protect ? 1 : 0); + hash = (B * hash) + foreColor.hashCode(); + hash = (B * hash) + backColor.hashCode(); + return hash; + } + /** * Set my field values to that's field. * * @param rhs another CellAttributes instance */ - public void setTo(Object rhs) { + public void setTo(final Object rhs) { CellAttributes that = (CellAttributes) rhs; this.bold = that.bold; diff --git a/src/jexer/bits/Color.java b/src/jexer/bits/Color.java index 707b244..54ae66a 100644 --- a/src/jexer/bits/Color.java +++ b/src/jexer/bits/Color.java @@ -215,6 +215,16 @@ public final class Color { return (value == that.value); } + /** + * Hashcode uses all fields in equals(). + * + * @return the hash + */ + @Override + public int hashCode() { + return value; + } + /** * Make human-readable description of this Color. * @@ -239,8 +249,9 @@ public final class Color { return "blue"; case SGRYELLOW: return "yellow"; + default: + throw new IllegalArgumentException("Invalid Color value: " + value); } - throw new IllegalArgumentException("Invalid Color value: " + value); } } diff --git a/src/jexer/event/TCommandEvent.java b/src/jexer/event/TCommandEvent.java index fb3050b..e430daf 100644 --- a/src/jexer/event/TCommandEvent.java +++ b/src/jexer/event/TCommandEvent.java @@ -86,6 +86,21 @@ public final class TCommandEvent extends TInputEvent { return (cmd.equals(that)); } + /** + * Hashcode uses all fields in equals(). + * + * @return the hash + */ + @Override + public int hashCode() { + int A = 13; + int B = 23; + int hash = A; + hash = (B * hash) + getTime().hashCode(); + hash = (B * hash) + cmd.hashCode(); + return hash; + } + /** * Make human-readable description of this TCommandEvent. * diff --git a/src/jexer/event/TKeypressEvent.java b/src/jexer/event/TKeypressEvent.java index 03c4f71..bf8a51a 100644 --- a/src/jexer/event/TKeypressEvent.java +++ b/src/jexer/event/TKeypressEvent.java @@ -115,6 +115,21 @@ public final class TKeypressEvent extends TInputEvent { return (key.equals(that)); } + /** + * Hashcode uses all fields in equals(). + * + * @return the hash + */ + @Override + public int hashCode() { + int A = 13; + int B = 23; + int hash = A; + hash = (B * hash) + getTime().hashCode(); + hash = (B * hash) + key.hashCode(); + return hash; + } + /** * Make human-readable description of this TKeypressEvent. * diff --git a/src/jexer/menu/TMenu.java b/src/jexer/menu/TMenu.java index 75871c0..c993808 100644 --- a/src/jexer/menu/TMenu.java +++ b/src/jexer/menu/TMenu.java @@ -351,7 +351,7 @@ public final class TMenu extends TWindow { for (TWidget widget: getChildren()) { widget.setWidth(getWidth() - 2); } - getApplication().addAccelerator(menuItem, toLower(key)); + getApplication().addAccelerator(menuItem, key.toLowerCase()); getApplication().recomputeMenuX(); activate(0); return menuItem;