2 * Copyright (C) 2016 Roland Häder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook.menu;
19 import java.util.Iterator;
21 import org.mxchange.addressbook.BaseAddressbookSystem;
22 import org.mxchange.addressbook.menu.item.SelectableMenuItem;
23 import org.mxchange.jcore.exceptions.MenuInitializationException;
28 * @author Roland Häder<roland@mxchange.org>
30 public class MenuTools extends BaseAddressbookSystem {
33 * Gets an array with all available access keys back from given menu map.
34 * This can later be handle to the client's enterChar() method.
36 * @param menus A Map with all menus and their entries
37 * @param menuType Menu type
39 * @return An array with available access chars
41 * @throws org.mxchange.jcore.exceptions.MenuInitializationException If the
42 * menu cannot be initialized
44 public static char[] getAccessKeysFromMenuMap (final Map<String, Menu> menus, final String menuType) throws MenuInitializationException {
45 // First search for the proper menu class
46 Menu menu = menus.get(menuType);
49 if (!(menu instanceof Menu)) {
51 throw new MenuInitializationException(menu, menuType);
55 Iterator<SelectableMenuItem> iterator = menu.getMenuItemsIterator();
57 // Init return array and counter 'i'
58 char[] accessKeys = new char[menu.getMenuItemsCount()];
61 // Now "walk" through all menu entries
62 while (iterator.hasNext()) {
64 SelectableMenuItem item = iterator.next();
65 //* NOISY-DEBUG: */ logger.logDebug("item=" + item);
67 // Get access key from item and add it to the array
68 accessKeys[i] = item.getAccessKey();
69 //* NOISY-DEBUG: */ logger.logDebug("accessKeys[" + i + "]=" + accessKeys[i]);
75 // Return finished array