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.text.MessageFormat;
20 import java.util.HashMap;
22 import org.mxchange.addressbook.BaseFrameworkSystem;
23 import org.mxchange.addressbook.manager.contact.ContactManager;
24 import org.mxchange.addressbook.manager.contact.ManageableContact;
25 import org.mxchange.addressbook.menu.Menu;
30 * @author Roland Haeder
32 public abstract class BaseClient extends BaseFrameworkSystem {
37 private String currentMenu;
40 * Application is not running by default
42 private boolean isRunning;
47 private final Map<String, Menu> menus;
50 * No instances can be created of this class
52 protected BaseClient () {
56 this.menus = new HashMap<>(10);
60 * Shutdown method for all clients
62 public void doShutdown () {
64 this.getLogger().trace("CALLED!"); //NOI18N
67 this.disableIsRunning();
69 // Shuts down contact manager
70 this.getContactManager().doShutdown();
73 this.getLogger().trace("EXIT!"); //NOI18N
79 public final void enableIsRunning () {
80 this.isRunning = true;
86 * @return the currentMenu
88 public final String getCurrentMenu () {
89 return this.currentMenu;
95 * @param currentMenu the currentMenu to set
97 public final void setCurrentMenu (final String currentMenu) {
98 this.currentMenu = currentMenu;
102 * "Getter" for given menu type
104 * @param menuType Menu type instance to return
105 * @return Menu or null if not found
107 public Menu getMenu (final String menuType) {
108 // Default is not found
112 if (this.getMenus().containsKey(menuType)) {
114 menu = this.getMenus().get(menuType);
122 * Determines whether the application is still active by checking some
125 * @return Whether the application is still active
127 public final boolean isRunning () {
128 // In console client, 0 may have been used
129 return this.isRunning;
133 * Disables the client
135 protected final void disableIsRunning () {
136 this.isRunning = false;
140 * Fills menu map with swing menus
142 protected abstract void fillMenuMap ();
145 * Getter for menus map
147 * @return Map of all menus
149 protected final Map<String, Menu> getMenus () {
154 * Initializes contact manager
156 protected void initContactManager () {
158 this.getLogger().trace("CALLED!"); //NOI18N
161 this.getLogger().debug("Initializing contact manager ...");
163 // Init contact manager with console client
164 // @TODO Static initial amount of contacts
165 ManageableContact manager = new ContactManager(100, (Client) this);
168 this.setContactManager(manager);
171 this.getLogger().debug("Contact manager has been initialized.");
174 this.getLogger().trace("EXIT!"); //NOI18N
180 * @param menuType Given menu to show
182 protected void showMenu (final String menuType) {
184 this.getLogger().trace(MessageFormat.format("menuType={0} - CALLED!", menuType)); //NOI18N
186 Menu menu = this.getMenu(menuType);
189 if (!(menu instanceof Menu)) {
191 // @todo Own exception?
192 throw new NullPointerException("Menu '" + menuType + "' not found.");
196 menu.show((Client) this);
199 this.getLogger().trace("EXIT!"); //NOI18N