]> git.mxchange.org Git - addressbook-lib.git/blob - src/org/mxchange/addressbook/menu/MenuTools.java
87f8c114733b6ef686d8f7a6d35638c0fc5d092e
[addressbook-lib.git] / src / org / mxchange / addressbook / menu / MenuTools.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.menu;
18
19 import java.util.Iterator;
20 import java.util.Map;
21 import org.mxchange.addressbook.BaseAddressbookSystem;
22 import org.mxchange.addressbook.menu.item.SelectableMenuItem;
23 import org.mxchange.jcore.exceptions.MenuInitializationException;
24
25 /**
26  * Menu utilities
27  * <p>
28  * @author Roland Haeder
29  */
30 public class MenuTools extends BaseAddressbookSystem {
31
32         /**
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.
35          * <p>
36          * @param menus A Map with all menus and their entries
37          * @param menuType Menu type
38          * @return An array with available access chars
39          * @throws org.mxchange.jcore.exceptions.MenuInitializationException If the menu cannot be initialized
40          */
41         public static char[] getAccessKeysFromMenuMap (final Map<String, Menu> menus, final String menuType) throws MenuInitializationException {
42                 // First search for the proper menu class
43                 Menu menu = menus.get(menuType);
44
45                 // Is it there?
46                 if (!(menu instanceof Menu)) {
47                         // Not found
48                         throw new MenuInitializationException(menu, menuType);
49                 }
50
51                 // Get iterator
52                 Iterator<SelectableMenuItem> iterator = menu.getMenuItemsIterator();
53
54                 // Init return array and counter 'i'
55                 char[] accessKeys = new char[menu.getMenuItemsCount()];
56                 int i = 0;
57
58                 // Now "walk" through all menu entries
59                 while (iterator.hasNext()) {
60                         // Get item
61                         SelectableMenuItem item = iterator.next();
62                         //* NOISY-DEBUG: */ logger.logDebug("item=" + item);
63
64                         // Get access key from item and add it to the array
65                         accessKeys[i] = item.getAccessKey();
66                         //* NOISY-DEBUG: */ logger.logDebug("accessKeys[" + i + "]=" + accessKeys[i]);
67
68                         // Increment counter
69                         i++;
70                 }
71
72                 // Return finished array
73                 return accessKeys;
74         }
75 }