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