X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fmenu%2FTMenuItem.java;h=63a5355e348f0e553215a67c88d45c9979a856d5;hb=d36057dfab8def933a64be042b039d76708ac5ba;hp=9ffc1ea075a6448c561e21ef5008f87961f7f4ff;hpb=eb29bbb5ec70c43895dd0f053630c7e3cd402cba;p=fanfix.git diff --git a/src/jexer/menu/TMenuItem.java b/src/jexer/menu/TMenuItem.java index 9ffc1ea..63a5355 100644 --- a/src/jexer/menu/TMenuItem.java +++ b/src/jexer/menu/TMenuItem.java @@ -43,6 +43,10 @@ import static jexer.TKeypress.*; */ public class TMenuItem extends TWidget { + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Label for this menu item. */ @@ -54,29 +58,11 @@ public class TMenuItem extends TWidget { */ private int id = TMenu.MID_UNUSED; - /** - * Get the menu item ID. - * - * @return the id - */ - public final int getId() { - return id; - } - /** * When true, this item can be checked or unchecked. */ private boolean checkable = false; - /** - * Set checkable flag. - * - * @param checkable if true, this menu item can be checked/unchecked - */ - public final void setCheckable(final boolean checkable) { - this.checkable = checkable; - } - /** * When true, this item is checked. */ @@ -93,40 +79,9 @@ public class TMenuItem extends TWidget { */ private MnemonicString mnemonic; - /** - * Get the mnemonic string for this menu item. - * - * @return mnemonic string - */ - public final MnemonicString getMnemonic() { - return mnemonic; - } - - /** - * Get a global accelerator key for this menu item. - * - * @return global keyboard accelerator, or null if no key is associated - * with this item - */ - public final TKeypress getKey() { - return key; - } - - /** - * Set a global accelerator key for this menu item. - * - * @param key global keyboard accelerator - */ - public final void setKey(final TKeypress key) { - this.key = key; - - if (key != null) { - int newWidth = (label.length() + 4 + key.toString().length() + 2); - if (newWidth > getWidth()) { - setWidth(newWidth); - } - } - } + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ /** * Package private constructor. @@ -190,6 +145,10 @@ public class TMenuItem extends TWidget { } + // ------------------------------------------------------------------------ + // Event handlers --------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Returns true if the mouse is currently on the menu item. * @@ -206,6 +165,39 @@ public class TMenuItem extends TWidget { return false; } + /** + * Handle mouse button releases. + * + * @param mouse mouse button release event + */ + @Override + public void onMouseUp(final TMouseEvent mouse) { + if ((mouseOnMenuItem(mouse)) && (mouse.isMouse1())) { + dispatch(); + return; + } + } + + /** + * Handle keystrokes. + * + * @param keypress keystroke event + */ + @Override + public void onKeypress(final TKeypressEvent keypress) { + if (keypress.equals(kbEnter)) { + dispatch(); + return; + } + + // Pass to parent for the things we don't care about. + super.onKeypress(keypress); + } + + // ------------------------------------------------------------------------ + // TWidget ---------------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Draw a menu item with label. */ @@ -249,44 +241,73 @@ public class TMenuItem extends TWidget { } + // ------------------------------------------------------------------------ + // TMenuItem -------------------------------------------------------------- + // ------------------------------------------------------------------------ + /** - * Dispatch event(s) due to selection or click. + * Get the menu item ID. + * + * @return the id */ - public void dispatch() { - assert (isEnabled()); + public final int getId() { + return id; + } - getApplication().postMenuEvent(new TMenuEvent(id)); - if (checkable) { - checked = !checked; - } + /** + * Set checkable flag. + * + * @param checkable if true, this menu item can be checked/unchecked + */ + public final void setCheckable(final boolean checkable) { + this.checkable = checkable; } /** - * Handle mouse button releases. + * Get the mnemonic string for this menu item. * - * @param mouse mouse button release event + * @return mnemonic string */ - @Override - public void onMouseUp(final TMouseEvent mouse) { - if ((mouseOnMenuItem(mouse)) && (mouse.isMouse1())) { - dispatch(); - return; - } + public final MnemonicString getMnemonic() { + return mnemonic; } /** - * Handle keystrokes. + * Get a global accelerator key for this menu item. * - * @param keypress keystroke event + * @return global keyboard accelerator, or null if no key is associated + * with this item */ - @Override - public void onKeypress(final TKeypressEvent keypress) { - if (keypress.equals(kbEnter)) { - dispatch(); - return; + public final TKeypress getKey() { + return key; + } + + /** + * Set a global accelerator key for this menu item. + * + * @param key global keyboard accelerator + */ + public final void setKey(final TKeypress key) { + this.key = key; + + if (key != null) { + int newWidth = (label.length() + 4 + key.toString().length() + 2); + if (newWidth > getWidth()) { + setWidth(newWidth); + } } + } - // Pass to parent for the things we don't care about. - super.onKeypress(keypress); + /** + * Dispatch event(s) due to selection or click. + */ + public void dispatch() { + assert (isEnabled()); + + getApplication().postMenuEvent(new TMenuEvent(id)); + if (checkable) { + checked = !checked; + } } + }