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 () {
54 this.menus = new HashMap<>(10);
58 * Shutdown method for all clients
60 public void doShutdown () {
62 this.getLogger().trace("CALLED!"); //NOI18N
65 this.disableIsRunning();
67 // Shuts down contact manager
68 this.getContactManager().doShutdown();
71 this.getLogger().trace("EXIT!"); //NOI18N
77 public final void enableIsRunning () {
78 this.isRunning = true;
84 * @return the currentMenu
86 public final String getCurrentMenu () {
87 return this.currentMenu;
93 * @param currentMenu the currentMenu to set
95 public final void setCurrentMenu (final String currentMenu) {
96 this.currentMenu = currentMenu;
100 * "Getter" for given menu type
102 * @param menuType Menu type instance to return
103 * @return Menu or null if not found
105 public Menu getMenu (final String menuType) {
106 // Default is not found
110 if (this.getMenus().containsKey(menuType)) {
112 menu = this.getMenus().get(menuType);
120 * Determines whether the application is still active by checking some
123 * @return Whether the application is still active
125 public final boolean isRunning () {
126 // In console client, 0 may have been used
127 return this.isRunning;
131 * Disables the client
133 protected final void disableIsRunning () {
134 this.isRunning = false;
138 * Fills menu map with swing menus
140 protected abstract void fillMenuMap ();
143 * Getter for menus map
145 * @return Map of all menus
147 protected final Map<String, Menu> getMenus () {
152 * Initializes contact manager
154 protected void initContactManager () {
156 this.getLogger().trace("CALLED!"); //NOI18N
159 this.getLogger().debug("Initializing contact manager ..."); //NOI18N
161 // Init contact manager with console client
162 // @TODO Static initial amount of contacts
163 ManageableContact manager = new ContactManager((Client) this);
166 this.setContactManager(manager);
169 this.getLogger().debug("Contact manager has been initialized."); //NOI18N
172 this.getLogger().trace("EXIT!"); //NOI18N
178 * @param menuType Given menu to show
180 protected void showMenu (final String menuType) {
182 this.getLogger().trace(MessageFormat.format("menuType={0} - CALLED!", menuType)); //NOI18N
184 Menu menu = this.getMenu(menuType);
187 if (!(menu instanceof Menu)) {
189 // @todo Own exception?
190 throw new NullPointerException(MessageFormat.format("Menu '{0}' not found.", menuType)); //NOI18N
194 menu.show((Client) this);
197 this.getLogger().trace("EXIT!"); //NOI18N