*
* The MIT License (MIT)
*
- * Copyright (C) 2016 Kevin Lamonte
+ * Copyright (C) 2017 Kevin Lamonte
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
*/
public class TMenuItem extends TWidget {
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Label for this menu item.
*/
*/
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.
*/
*/
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.
}
+ // ------------------------------------------------------------------------
+ // Event handlers ---------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Returns true if the mouse is currently on the menu item.
*
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.
*/
}
+ // ------------------------------------------------------------------------
+ // 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;
+ }
}
+
}