]> git.mxchange.org Git - jfinancials-lib.git/blobdiff - Addressbook/src/org/mxchange/addressbook/client/BaseClient.java
Added stuff for Swing client
[jfinancials-lib.git] / Addressbook / src / org / mxchange / addressbook / client / BaseClient.java
index 09c7b0fe865c26c2dc267d49be4195902630d121..0282c91f6f65760462868118d1f7fbdf0813b638 100644 (file)
  */\r
 package org.mxchange.addressbook.client;\r
 \r
+import java.util.HashMap;\r
+import java.util.Map;\r
 import org.mxchange.addressbook.BaseFrameworkSystem;\r
+import org.mxchange.addressbook.manager.contact.ContactManager;\r
+import org.mxchange.addressbook.manager.contact.ManageableContact;\r
+import org.mxchange.addressbook.menu.Menu;\r
 \r
 /**\r
  * A general client\r
  *\r
  * @author Roland Haeder\r
  */\r
-public class BaseClient extends BaseFrameworkSystem {\r
+public abstract class BaseClient extends BaseFrameworkSystem {\r
 \r
     /**\r
      * Current menu choice\r
@@ -35,26 +40,42 @@ public class BaseClient extends BaseFrameworkSystem {
      */\r
     private boolean isRunning;\r
 \r
+    /**\r
+     * Menu system\r
+     */\r
+    private final Map<String, Menu> menus;\r
+\r
     /**\r
      * No instances can be created of this class\r
      */\r
     protected BaseClient () {\r
        super();\r
+\r
+       // Init menu map\r
+       this.menus = new HashMap<>(10);\r
     }\r
 \r
     /**\r
-     * Disables running state, so the main loop can abort.\r
+     * Shutdown method for all clients\r
      */\r
-    public void disableIsRunning () {\r
-       this.isRunning = false;\r
+    public void doShutdown () {\r
+       // Disable client\r
+       this.disableIsRunning();\r
+\r
+       // Shuts down contact manager\r
+       this.getContactManager().doShutdown();\r
     }\r
 \r
+    /**\r
+     * Enables the client\r
+     */\r
     public void enableIsRunning () {\r
        this.isRunning = true;\r
     }\r
 \r
     /**\r
      * Current menu choice\r
+     * \r
      * @return the currentMenu\r
      */\r
     public String getCurrentMenu () {\r
@@ -69,8 +90,91 @@ public class BaseClient extends BaseFrameworkSystem {
        this.currentMenu = currentMenu;\r
     }\r
 \r
-    public boolean isApplicationRunning () {\r
+    /**\r
+     * "Getter" for given menu type\r
+     *\r
+     * @param menuType Menu type instance to return\r
+     * @return Menu or null if not found\r
+     */\r
+    public Menu getMenu (final String menuType) {\r
+       // Default is not found\r
+       Menu menu = null;\r
+       \r
+       // Check array\r
+       if (this.getMenus().containsKey(menuType)) {\r
+           // Found!\r
+           menu = this.getMenus().get(menuType);\r
+       }\r
+       \r
+       // Return it\r
+       return menu;\r
+    }\r
+\r
+    /**\r
+     * Determines whether the application is still active by checking some\r
+     * conditions\r
+     * \r
+     * @return Whether the application is still active\r
+     */\r
+    public boolean isRunning () {\r
        // In console client, 0 may have been used\r
        return this.isRunning;\r
     }\r
+\r
+    /**\r
+     * Disables running state, so the main loop can abort.\r
+     */\r
+    protected void disableIsRunning () {\r
+       this.isRunning = false;\r
+    }\r
+\r
+    /**\r
+     * Fills menu map with swing menus\r
+     */\r
+    protected abstract void fillMenuMap ();\r
+\r
+    /**\r
+     * Getter for menus map\r
+     * @return Map of all menus\r
+     */\r
+    protected final Map<String, Menu> getMenus () {\r
+       return this.menus;\r
+    }\r
+\r
+    /**\r
+     * Initializes contact manager\r
+     */\r
+    protected void initContactManager () {\r
+       // Debug message\r
+       this.getLogger().debug("Initializing contact manager ...");\r
+       \r
+       // Init contact manager with console client\r
+       // @TODO Static initial amount of contacts\r
+       ManageableContact manager = new ContactManager (100, (Client) this);\r
+       \r
+       // Set it here\r
+       this.setContactManager(manager);\r
+       \r
+       // Debug message\r
+       this.getLogger().debug("Contact manager has been initialized.");\r
+    }\r
+\r
+    /**\r
+     * Shows given menu\r
+     *\r
+     * @param menuType Given menu to show\r
+     */\r
+    protected void showMenu (final String menuType) {\r
+       Menu menu = this.getMenu(menuType);\r
+       \r
+       // Is the menu set?\r
+       if (!(menu instanceof Menu)) {\r
+           // Not found\r
+           // @todo Own exception?\r
+           throw new NullPointerException("Menu '" + menuType + "' not found.");\r
+       }\r
+       \r
+       // Show menu\r
+       menu.show((Client) this);\r
+    }\r
 }\r