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;
*/
private TMenu activeMenu = null;
+ /**
+ * Active keyboard accelerators.
+ */
+ private Map<TKeypress, TMenuItem> accelerators;
+
/**
* Windows and widgets pull colors from this ColorTheme.
*/
windows = new LinkedList<TWindow>();
menus = new LinkedList<TMenu>();
subMenus = new LinkedList<TMenu>();
+ accelerators = new HashMap<TKeypress, TMenuItem>();
}
/**
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) {
return;
}
- // DEBUG
- if (event instanceof TKeypressEvent) {
- TKeypressEvent keypress = (TKeypressEvent) event;
- if (keypress.equals(kbAltX)) {
- quit = true;
- return;
- }
- }
- // DEBUG
-
// Special application-wide events -------------------------------
// Abort everything
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();
}
}
}
- */
if (event instanceof TCommandEvent) {
if (onCommand((TCommandEvent) event)) {
*/
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);
+ }
}
/**
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);
&& (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.
*
}
/**
- * 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;
}
}
/**
- * 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;
}
&& (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.
*
&& (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;
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.
*
return "blue";
case SGRYELLOW:
return "yellow";
+ default:
+ throw new IllegalArgumentException("Invalid Color value: " + value);
}
- throw new IllegalArgumentException("Invalid Color value: " + value);
}
}
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.
*
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.
*
for (TWidget widget: getChildren()) {
widget.setWidth(getWidth() - 2);
}
- getApplication().addAccelerator(menuItem, toLower(key));
+ getApplication().addAccelerator(menuItem, key.toLowerCase());
getApplication().recomputeMenuX();
activate(0);
return menuItem;