2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
33 import jexer
.TKeypress
;
35 import jexer
.bits
.CellAttributes
;
36 import jexer
.bits
.GraphicsChars
;
37 import jexer
.event
.TKeypressEvent
;
38 import static jexer
.TKeypress
.*;
41 * TSubMenu is a special case menu item that wraps another TMenu.
43 public final class TSubMenu
extends TMenuItem
{
46 * The menu window. Note package private access.
51 * Package private constructor.
53 * @param parent parent widget
54 * @param title menu title. Title must contain a keyboard shortcut,
55 * denoted by prefixing a letter with "&", e.g. "&File"
56 * @param x column relative to parent
57 * @param y row relative to parent
59 TSubMenu(final TMenu parent
, final String title
, final int x
, final int y
) {
60 super(parent
, TMenu
.MID_UNUSED
, x
, y
, title
);
65 this.menu
= new TMenu(parent
.getApplication(), x
, getAbsoluteY() - 1,
67 setWidth(menu
.getWidth() + 2);
69 this.menu
.isSubMenu
= true;
73 * Draw the menu title.
79 CellAttributes background
= getTheme().getColor("tmenu");
80 CellAttributes menuColor
;
81 CellAttributes menuMnemonicColor
;
82 if (getAbsoluteActive()) {
83 menuColor
= getTheme().getColor("tmenu.highlighted");
84 menuMnemonicColor
= getTheme().getColor("tmenu.mnemonic.highlighted");
87 menuColor
= getTheme().getColor("tmenu");
88 menuMnemonicColor
= getTheme().getColor("tmenu.mnemonic");
90 menuColor
= getTheme().getColor("tmenu.disabled");
91 menuMnemonicColor
= getTheme().getColor("tmenu.disabled");
96 getScreen().putCharXY(getWidth() - 2, 0, GraphicsChars
.CP437
[0x10],
103 * @param keypress keystroke event
106 public void onKeypress(final TKeypressEvent keypress
) {
108 if (menu
.getActive()) {
109 menu
.onKeypress(keypress
);
113 if (keypress
.equals(kbEnter
)) {
118 if (keypress
.equals(kbRight
)) {
123 if (keypress
.equals(kbDown
)) {
124 getParent().switchWidget(true);
128 if (keypress
.equals(kbUp
)) {
129 getParent().switchWidget(false);
133 if (keypress
.equals(kbLeft
)) {
134 TMenu parentMenu
= (TMenu
) getParent();
135 if (parentMenu
.isSubMenu
) {
136 getApplication().closeSubMenu();
138 getApplication().switchMenu(false);
143 if (keypress
.equals(kbEsc
)) {
144 getApplication().closeMenu();
150 * Override dispatch() to do nothing.
153 public void dispatch() {
154 assert (getEnabled());
155 if (getAbsoluteActive()) {
156 if (!menu
.getActive()) {
157 getApplication().addSubMenu(menu
);
158 menu
.setActive(true);
164 * Returns my active widget.
166 * @return widget that is active, or this if no children
169 public TWidget
getActiveChild() {
170 if (menu
.getActive()) {
173 // Menu not active, return me
178 * Convenience function to add a custom menu item.
180 * @param id menu item ID. Must be greater than 1024.
181 * @param label menu item label
182 * @param key global keyboard accelerator
183 * @return the new menu item
185 public TMenuItem
addItem(final int id
, final String label
,
186 final TKeypress key
) {
188 return menu
.addItem(id
, label
, key
);
192 * Convenience function to add a menu item.
194 * @param id menu item ID. Must be greater than 1024.
195 * @param label menu item label
196 * @return the new menu item
198 public TMenuItem
addItem(final int id
, final String label
) {
199 return menu
.addItem(id
, label
);
203 * Convenience function to add one of the default menu items.
205 * @param id menu item ID. Must be between 0 (inclusive) and 1023
207 * @return the new menu item
209 public TMenuItem
addDefaultItem(final int id
) {
210 return menu
.addDefaultItem(id
);
214 * Convenience function to add a menu separator.
216 public void addSeparator() {
221 * Convenience function to add a sub-menu.
223 * @param title menu title. Title must contain a keyboard shortcut,
224 * denoted by prefixing a letter with "&", e.g. "&File"
225 * @return the new sub-menu
227 public TSubMenu
addSubMenu(final String title
) {
228 return menu
.addSubMenu(title
);