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;
87 * @param currentMenu the currentMenu to set
89 public final void setCurrentMenu (final String currentMenu) {
90 this.currentMenu = currentMenu;
94 * "Getter" for given menu type
96 * @param menuType Menu type instance to return
97 * @return Menu or null if not found
99 public Menu getMenu (final String menuType) {
100 // Default is not found
104 if (this.getMenus().containsKey(menuType)) {
106 menu = this.getMenus().get(menuType);
114 * Determines whether the application is still active by checking some
117 * @return Whether the application is still active
119 public final boolean isRunning () {
120 // In console client, 0 may have been used
121 return this.isRunning;
125 * Disables the client
127 protected final void disableIsRunning () {
128 this.isRunning = false;
132 * Fills menu map with swing menus
134 protected abstract void fillMenuMap ();
137 * Getter for menus map
138 * @return Map of all menus
140 protected final Map<String, Menu> getMenus () {
145 * Initializes contact manager
147 protected void initContactManager () {
149 this.getLogger().debug("Initializing contact manager ...");
151 // Init contact manager with console client
152 // @TODO Static initial amount of contacts
153 ManageableContact manager = new ContactManager (100, (Client) this);
156 this.setContactManager(manager);
159 this.getLogger().debug("Contact manager has been initialized.");
165 * @param menuType Given menu to show
167 protected void showMenu (final String menuType) {
168 Menu menu = this.getMenu(menuType);
171 if (!(menu instanceof Menu)) {
173 // @todo Own exception?
174 throw new NullPointerException("Menu '" + menuType + "' not found.");
178 menu.show((Client) this);