]> git.mxchange.org Git - jaddressbook-lib.git/blob - src/org/mxchange/addressbook/menu/MenuTools.java
Fixed call + introduced getColumnCount()
[jaddressbook-lib.git] / src / org / mxchange / addressbook / menu / MenuTools.java
1 /*\r
2  * Copyright (C) 2015 Roland Haeder\r
3  *\r
4  * This program is free software: you can redistribute it and/or modify\r
5  * it under the terms of the GNU General Public License as published by\r
6  * the Free Software Foundation, either version 3 of the License, or\r
7  * (at your option) any later version.\r
8  *\r
9  * This program is distributed in the hope that it will be useful,\r
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12  * GNU General Public License for more details.\r
13  *\r
14  * You should have received a copy of the GNU General Public License\r
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
16  */\r
17 package org.mxchange.addressbook.menu;\r
18 \r
19 import java.util.Iterator;\r
20 import java.util.Map;\r
21 import org.apache.logging.log4j.Logger;\r
22 import org.mxchange.addressbook.BaseFrameworkSystem;\r
23 import org.mxchange.addressbook.menu.item.SelectableMenuItem;\r
24 \r
25 /**\r
26  *\r
27  * @author Roland Haeder\r
28  */\r
29 public class MenuTools extends BaseFrameworkSystem {\r
30 \r
31     /**\r
32      * Gets an array with all available access keys back from given menu map.\r
33      * This can later be handle to the client's enterChar() method.\r
34      *\r
35      * @param menus A Map with all menus and their entries\r
36      * @param menuType Menu type\r
37      * @return An array with available access chars\r
38      */\r
39     public static char[] getAccessKeysFromMenuMap (final Map<String, Menu> menus, final String menuType) {\r
40         // Get logger\r
41         Logger logger = new MenuTools().getLogger();\r
42 \r
43         // First search for the proper menu class\r
44         Menu menu = menus.get(menuType);\r
45 \r
46         // Is it there?\r
47         if (!(menu instanceof Menu)) {\r
48             // Not found\r
49             // @todo Rewrite to exception\r
50             logger.error("Menu '" + menuType + "' not found.");\r
51             System.exit(1);\r
52         }\r
53 \r
54         // Get iterator\r
55         Iterator<SelectableMenuItem> iterator = menu.getMenuItemsIterator();\r
56 \r
57         // Init return array and counter 'i'\r
58         char[] accessKeys = new char[menu.getMenuItemsCount()];\r
59         int i = 0;\r
60 \r
61         // Now "walk" through all menu entries\r
62         while (iterator.hasNext()) {\r
63             // Get item\r
64             SelectableMenuItem item = iterator.next();\r
65             //* NOISY-DEBUG: */ logger.debug("item=" + item);\r
66 \r
67             // Get access key from item and add it to the array\r
68             accessKeys[i] = item.getAccessKey();\r
69             //* NOISY-DEBUG: */ logger.debug("accessKeys[" + i + "]=" + accessKeys[i]);\r
70 \r
71             // Increment counter\r
72             i++;\r
73         }\r
74 \r
75         // Return finished array\r
76         return accessKeys;\r
77     }\r
78 }\r