2 * Copyright (C) 2015 Roland Haeder
\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
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
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
17 package org.mxchange.addressbook.client;
\r
19 import java.util.HashMap;
\r
20 import java.util.Map;
\r
21 import org.mxchange.addressbook.BaseFrameworkSystem;
\r
22 import org.mxchange.addressbook.manager.contact.ContactManager;
\r
23 import org.mxchange.addressbook.manager.contact.ManageableContact;
\r
24 import org.mxchange.addressbook.menu.Menu;
\r
29 * @author Roland Haeder
\r
31 public abstract class BaseClient extends BaseFrameworkSystem {
\r
34 * Current menu choice
\r
36 private String currentMenu;
\r
39 * Application is not running by default
\r
41 private boolean isRunning;
\r
46 private final Map<String, Menu> menus;
\r
49 * No instances can be created of this class
\r
51 protected BaseClient () {
\r
55 this.menus = new HashMap<>(10);
\r
59 * Disables running state, so the main loop can abort.
\r
61 public void disableIsRunning () {
\r
62 this.isRunning = false;
\r
66 * Enables the client
\r
68 public void enableIsRunning () {
\r
69 this.isRunning = true;
\r
73 * Current menu choice
\r
75 * @return the currentMenu
\r
77 public String getCurrentMenu () {
\r
78 return this.currentMenu;
\r
82 * Current menu choice
\r
83 * @param currentMenu the currentMenu to set
\r
85 public void setCurrentMenu (final String currentMenu) {
\r
86 this.currentMenu = currentMenu;
\r
90 * "Getter" for given menu type
\r
92 * @param menuType Menu type instance to return
\r
93 * @return Menu or null if not found
\r
95 public Menu getMenu (final String menuType) {
\r
96 // Default is not found
\r
100 if (this.getMenus().containsKey(menuType)) {
\r
102 menu = this.getMenus().get(menuType);
\r
110 * Determines whether the application is still active by checking some
\r
113 * @return Whether the application is still active
\r
115 public boolean isRunning () {
\r
116 // In console client, 0 may have been used
\r
117 return this.isRunning;
\r
121 * Fills menu map with swing menus
\r
123 protected abstract void fillMenuMap ();
\r
126 * Getter for menus map
\r
127 * @return Map of all menus
\r
129 protected final Map<String, Menu> getMenus () {
\r
136 * @param menuType Given menu to show
\r
138 protected void showMenu (final String menuType) {
\r
139 Menu menu = this.getMenu(menuType);
\r
141 // Is the menu set?
\r
142 if (!(menu instanceof Menu)) {
\r
144 // @todo Own exception?
\r
145 throw new NullPointerException("Menu '" + menuType + "' not found.");
\r
149 menu.show((Client) this);
\r
153 * Initializes contact manager
\r
155 protected void initContactManager () {
\r
157 this.getLogger().debug("Initializing contact manager ...");
\r
159 // Init contact manager with console client
\r
160 // @TODO Static initial amount of contacts
\r
161 ManageableContact manager = new ContactManager (100, (Client) this);
\r
164 this.setContactManager(manager);
\r
167 this.getLogger().debug("Contact manager has been initialized.");
\r