X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=Addressbook%2Fsrc%2Forg%2Fmxchange%2Faddressbook%2Fclient%2FBaseClient.java;h=54d8bc32aad56c692a561fb867d766e523829b07;hb=ea30fd0dc674dcd10c3aead1acd47000a99d7334;hp=706229c3036de3a5c9089f01a547a4f7a2d936ef;hpb=6aee766f085ca3c6da8191b0f3d371585fbc6cdc;p=jaddressbook-share-lib.git diff --git a/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java b/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java index 706229c..54d8bc3 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java +++ b/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java @@ -1,114 +1,182 @@ -/* - * Copyright (C) 2015 Roland Haeder - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.mxchange.addressbook.client; - -import org.mxchange.addressbook.BaseFrameworkSystem; -import org.mxchange.addressbook.menu.Menu; - -/** - * A general client - * - * @author Roland Haeder - */ -public abstract class BaseClient extends BaseFrameworkSystem { - - /** - * Current menu choice - */ - private String currentMenu; - - /** - * Application is not running by default - */ - private boolean isRunning; - - /** - * No instances can be created of this class - */ - protected BaseClient () { - super(); - } - - /** - * Disables running state, so the main loop can abort. - */ - public void disableIsRunning () { - this.isRunning = false; - } - - /** - * Enables the client - */ - public void enableIsRunning () { - this.isRunning = true; - } - - /** - * Current menu choice - * - * @return the currentMenu - */ - public String getCurrentMenu () { - return this.currentMenu; - } - - /** - * Current menu choice - * @param currentMenu the currentMenu to set - */ - public void setCurrentMenu (final String currentMenu) { - this.currentMenu = currentMenu; - } - - /** - * Some kind of "getter" for a Menu instance from given menu type - * - * @param menuType Menu type, e.g. "main" for main menu - * @return - */ - public abstract Menu getMenu (final String menuType); - - /** - * Determines whether the application is still active by checking some - * conditions - * - * @return Whether the application is still active - */ - public boolean isRunning () { - // In console client, 0 may have been used - return this.isRunning; - } - - /** - * Shows given menu - * - * @param menuType Given menu to show - */ - protected void showMenu (final String menuType) { - Menu menu = this.getMenu(menuType); - - // Is the menu set? - if (!(menu instanceof Menu)) { - // Not found - // @todo Own exception? - throw new NullPointerException("Menu '" + menuType + "' not found."); - } - - // Show menu - menu.show((Client) this); - } -} +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.client; + +import java.util.HashMap; +import java.util.Map; +import org.mxchange.addressbook.BaseFrameworkSystem; +import org.mxchange.addressbook.manager.contact.ContactManager; +import org.mxchange.addressbook.manager.contact.ManageableContact; +import org.mxchange.addressbook.menu.Menu; + +/** + * A general client + * + * @author Roland Haeder + */ +public abstract class BaseClient extends BaseFrameworkSystem { + + /** + * Current menu choice + */ + private String currentMenu; + + /** + * Application is not running by default + */ + private boolean isRunning; + + /** + * Menu system + */ + private final Map menus; + + /** + * No instances can be created of this class + */ + protected BaseClient () { + super(); + + // Init menu map + this.menus = new HashMap<>(10); + } + + /** + * Shutdown method for all clients + */ + public void doShutdown () { + // Disable client + this.disableIsRunning(); + + // Shuts down contact manager + this.getContactManager().doShutdown(); + } + + /** + * Enables the client + */ + public final void enableIsRunning () { + this.isRunning = true; + } + + /** + * Current menu choice + * + * @return the currentMenu + */ + public final String getCurrentMenu () { + return this.currentMenu; + } + + /** + * Current menu choice + * + * @param currentMenu the currentMenu to set + */ + public final void setCurrentMenu (final String currentMenu) { + this.currentMenu = currentMenu; + } + + /** + * "Getter" for given menu type + * + * @param menuType Menu type instance to return + * @return Menu or null if not found + */ + public Menu getMenu (final String menuType) { + // Default is not found + Menu menu = null; + + // Check array + if (this.getMenus().containsKey(menuType)) { + // Found! + menu = this.getMenus().get(menuType); + } + + // Return it + return menu; + } + + /** + * Determines whether the application is still active by checking some + * conditions + * + * @return Whether the application is still active + */ + public final boolean isRunning () { + // In console client, 0 may have been used + return this.isRunning; + } + + /** + * Disables the client + */ + protected final void disableIsRunning () { + this.isRunning = false; + } + + /** + * Fills menu map with swing menus + */ + protected abstract void fillMenuMap (); + + /** + * Getter for menus map + * + * @return Map of all menus + */ + protected final Map getMenus () { + return this.menus; + } + + /** + * Initializes contact manager + */ + protected void initContactManager () { + // Debug message + this.getLogger().debug("Initializing contact manager ..."); + + // Init contact manager with console client + // @TODO Static initial amount of contacts + ManageableContact manager = new ContactManager(100, (Client) this); + + // Set it here + this.setContactManager(manager); + + // Debug message + this.getLogger().debug("Contact manager has been initialized."); + } + + /** + * Shows given menu + * + * @param menuType Given menu to show + */ + protected void showMenu (final String menuType) { + Menu menu = this.getMenu(menuType); + + // Is the menu set? + if (!(menu instanceof Menu)) { + // Not found + // @todo Own exception? + throw new NullPointerException("Menu '" + menuType + "' not found."); + } + + // Show menu + menu.show((Client) this); + } +}