2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.addressbook.client;
19 import java.util.HashMap;
21 import org.mxchange.addressbook.BaseFrameworkSystem;
22 import org.mxchange.addressbook.manager.contact.ContactManager;
23 import org.mxchange.addressbook.manager.contact.ManageableContact;
24 import org.mxchange.addressbook.menu.Menu;
29 * @author Roland Haeder
31 public abstract class BaseClient extends BaseFrameworkSystem {
36 private String currentMenu;
39 * Application is not running by default
41 private boolean isRunning;
46 private final Map<String, Menu> menus;
49 * No instances can be created of this class
51 protected BaseClient () {
55 this.menus = new HashMap<>(10);
59 * Shutdown method for all clients
61 public void doShutdown () {
63 this.disableIsRunning();
65 // Shuts down contact manager
66 this.getContactManager().doShutdown();
72 public final void enableIsRunning () {
73 this.isRunning = true;
79 * @return the currentMenu
81 public final String getCurrentMenu () {
82 return this.currentMenu;
88 * @param currentMenu the currentMenu to set
90 public final void setCurrentMenu (final String currentMenu) {
91 this.currentMenu = currentMenu;
95 * "Getter" for given menu type
97 * @param menuType Menu type instance to return
98 * @return Menu or null if not found
100 public Menu getMenu (final String menuType) {
101 // Default is not found
105 if (this.getMenus().containsKey(menuType)) {
107 menu = this.getMenus().get(menuType);
115 * Determines whether the application is still active by checking some
118 * @return Whether the application is still active
120 public final boolean isRunning () {
121 // In console client, 0 may have been used
122 return this.isRunning;
126 * Disables the client
128 protected final void disableIsRunning () {
129 this.isRunning = false;
133 * Fills menu map with swing menus
135 protected abstract void fillMenuMap ();
138 * Getter for menus map
140 * @return Map of all menus
142 protected final Map<String, Menu> getMenus () {
147 * Initializes contact manager
149 protected void initContactManager () {
151 this.getLogger().debug("Initializing contact manager ...");
153 // Init contact manager with console client
154 // @TODO Static initial amount of contacts
155 ManageableContact manager = new ContactManager(100, (Client) this);
158 this.setContactManager(manager);
161 this.getLogger().debug("Contact manager has been initialized.");
167 * @param menuType Given menu to show
169 protected void showMenu (final String menuType) {
170 Menu menu = this.getMenu(menuType);
173 if (!(menu instanceof Menu)) {
175 // @todo Own exception?
176 throw new NullPointerException("Menu '" + menuType + "' not found.");
180 menu.show((Client) this);