]> git.mxchange.org Git - addressbook-swing.git/blob - src/org/mxchange/addressbook/menu/MenuTools.java
14ab00ee9f852c83b4c8b5985dc406d6c54bcb3e
[addressbook-swing.git] / src / org / mxchange / addressbook / menu / MenuTools.java
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
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 Häder<roland@mxchange.org>
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          * <p>
39          * @return An array with available access chars
40          * <p>
41          * @throws org.mxchange.jcore.exceptions.MenuInitializationException If the
42          * menu cannot be initialized
43          */
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);
47
48                 // Is it there?
49                 if (!(menu instanceof Menu)) {
50                         // Not found
51                         throw new MenuInitializationException(menu, menuType);
52                 }
53
54                 // Get iterator
55                 Iterator<SelectableMenuItem> iterator = menu.getMenuItemsIterator();
56
57                 // Init return array and counter 'i'
58                 char[] accessKeys = new char[menu.getMenuItemsCount()];
59                 int i = 0;
60
61                 // Now "walk" through all menu entries
62                 while (iterator.hasNext()) {
63                         // Get item
64                         SelectableMenuItem item = iterator.next();
65                         //* NOISY-DEBUG: */ logger.logDebug("item=" + item);
66
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]);
70
71                         // Increment counter
72                         i++;
73                 }
74
75                 // Return finished array
76                 return accessKeys;
77         }
78 }