2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2016 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import jexer
.TKeypress
;
33 import jexer
.bits
.CellAttributes
;
34 import jexer
.bits
.GraphicsChars
;
35 import jexer
.event
.TKeypressEvent
;
36 import static jexer
.TKeypress
.*;
39 * TSubMenu is a special case menu item that wraps another TMenu.
41 public final class TSubMenu
extends TMenuItem
{
44 * The menu window. Note package private access.
49 * Package private constructor.
51 * @param parent parent widget
52 * @param title menu title. Title must contain a keyboard shortcut,
53 * denoted by prefixing a letter with "&", e.g. "&File"
54 * @param x column relative to parent
55 * @param y row relative to parent
57 TSubMenu(final TMenu parent
, final String title
, final int x
, final int y
) {
58 super(parent
, TMenu
.MID_UNUSED
, x
, y
, title
);
63 this.menu
= new TMenu(parent
.getApplication(), x
, getAbsoluteY() - 1,
65 setWidth(menu
.getWidth() + 2);
67 this.menu
.isSubMenu
= true;
71 * Draw the menu title.
77 CellAttributes menuColor
;
78 if (isAbsoluteActive()) {
79 menuColor
= getTheme().getColor("tmenu.highlighted");
82 menuColor
= getTheme().getColor("tmenu");
84 menuColor
= getTheme().getColor("tmenu.disabled");
89 getScreen().putCharXY(getWidth() - 2, 0, GraphicsChars
.CP437
[0x10],
96 * @param keypress keystroke event
99 public void onKeypress(final TKeypressEvent keypress
) {
101 // Open me if they hit my mnemonic.
102 if (!keypress
.getKey().isFnKey()
103 && !keypress
.getKey().isAlt()
104 && !keypress
.getKey().isCtrl()
105 && (getMnemonic() != null)
106 && (Character
.toLowerCase(getMnemonic().getShortcut())
107 == Character
.toLowerCase(keypress
.getKey().getChar()))
113 if (menu
.isActive()) {
114 menu
.onKeypress(keypress
);
118 if (keypress
.equals(kbEnter
)) {
123 if (keypress
.equals(kbRight
)) {
128 if (keypress
.equals(kbDown
)) {
129 getParent().switchWidget(true);
133 if (keypress
.equals(kbUp
)) {
134 getParent().switchWidget(false);
138 if (keypress
.equals(kbLeft
)) {
139 TMenu parentMenu
= (TMenu
) getParent();
140 if (parentMenu
.isSubMenu
) {
141 getApplication().closeSubMenu();
143 getApplication().switchMenu(false);
148 if (keypress
.equals(kbEsc
)) {
149 getApplication().closeMenu();
155 * Override dispatch() to do nothing.
158 public void dispatch() {
159 assert (isEnabled());
160 if (isAbsoluteActive()) {
161 if (!menu
.isActive()) {
162 getApplication().addSubMenu(menu
);
163 menu
.setActive(true);
169 * Returns my active widget.
171 * @return widget that is active, or this if no children
174 public TWidget
getActiveChild() {
175 if (menu
.isActive()) {
178 // Menu not active, return me
183 * Convenience function to add a custom menu item.
185 * @param id menu item ID. Must be greater than 1024.
186 * @param label menu item label
187 * @param key global keyboard accelerator
188 * @return the new menu item
190 public TMenuItem
addItem(final int id
, final String label
,
191 final TKeypress key
) {
193 return menu
.addItem(id
, label
, key
);
197 * Convenience function to add a menu item.
199 * @param id menu item ID. Must be greater than 1024.
200 * @param label menu item label
201 * @return the new menu item
203 public TMenuItem
addItem(final int id
, final String label
) {
204 return menu
.addItem(id
, label
);
208 * Convenience function to add one of the default menu items.
210 * @param id menu item ID. Must be between 0 (inclusive) and 1023
212 * @return the new menu item
214 public TMenuItem
addDefaultItem(final int id
) {
215 return menu
.addDefaultItem(id
);
219 * Convenience function to add a menu separator.
221 public void addSeparator() {
226 * Convenience function to add a sub-menu.
228 * @param title menu title. Title must contain a keyboard shortcut,
229 * denoted by prefixing a letter with "&", e.g. "&File"
230 * @return the new sub-menu
232 public TSubMenu
addSubMenu(final String title
) {
233 return menu
.addSubMenu(title
);