]> git.mxchange.org Git - addressbook-lib.git/blob - src/org/mxchange/addressbook/menu/MenuTools.java
Prepared for upcoming rewrite:
[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.text.MessageFormat;
20 import java.util.Iterator;
21 import java.util.Map;
22 import org.mxchange.addressbook.BaseAddressbookSystem;
23 import org.mxchange.addressbook.menu.item.SelectableMenuItem;
24
25 /**
26  * Menu utilities
27  *
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          */
40         public static char[] getAccessKeysFromMenuMap (final Map<String, Menu> menus, final String menuType) {
41                 // First search for the proper menu class
42                 Menu menu = menus.get(menuType);
43
44                 // Is it there?
45                 if (!(menu instanceof Menu)) {
46                         // Not found
47                         // TODO Rewrite to exception
48                         System.err.println(MessageFormat.format("menu is not implementing Menu: {0}", menu)); //NOI18N
49                         System.exit(1);
50                 }
51
52                 // Get iterator
53                 Iterator<SelectableMenuItem> iterator = menu.getMenuItemsIterator();
54
55                 // Init return array and counter 'i'
56                 char[] accessKeys = new char[menu.getMenuItemsCount()];
57                 int i = 0;
58
59                 // Now "walk" through all menu entries
60                 while (iterator.hasNext()) {
61                         // Get item
62                         SelectableMenuItem item = iterator.next();
63                         //* NOISY-DEBUG: */ logger.logDebug("item=" + item);
64
65                         // Get access key from item and add it to the array
66                         accessKeys[i] = item.getAccessKey();
67                         //* NOISY-DEBUG: */ logger.logDebug("accessKeys[" + i + "]=" + accessKeys[i]);
68
69                         // Increment counter
70                         i++;
71                 }
72
73                 // Return finished array
74                 return accessKeys;
75         }
76 }