]> git.mxchange.org Git - jfinancials-lib.git/blob - Addressbook/src/org/mxchange/addressbook/client/BaseClient.java
Continued with re-implementation of Swing frame
[jfinancials-lib.git] / Addressbook / src / org / mxchange / addressbook / client / BaseClient.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.client;
18
19 import java.util.HashMap;
20 import java.util.Map;
21 import org.mxchange.addressbook.BaseFrameworkSystem;
22 import org.mxchange.addressbook.manager.contact.ContactManager;
23 import org.mxchange.addressbook.manager.contact.ManageableContact;
24 import org.mxchange.addressbook.menu.Menu;
25
26 /**
27  * A general client
28  *
29  * @author Roland Haeder
30  */
31 public abstract class BaseClient extends BaseFrameworkSystem {
32
33     /**
34      * Current menu choice
35      */
36     private String currentMenu;
37
38     /**
39      * Application is not running by default
40      */
41     private boolean isRunning;
42
43     /**
44      * Menu system
45      */
46     private final Map<String, Menu> menus;
47
48     /**
49      * No instances can be created of this class
50      */
51     protected BaseClient () {
52         super();
53
54         // Init menu map
55         this.menus = new HashMap<>(10);
56     }
57
58     /**
59      * Shutdown method for all clients
60      */
61     public void doShutdown () {
62         // Disable client
63         this.disableIsRunning();
64
65         // Shuts down contact manager
66         this.getContactManager().doShutdown();
67     }
68
69     /**
70      * Enables the client
71      */
72     public final void enableIsRunning () {
73         this.isRunning = true;
74     }
75
76     /**
77      * Current menu choice
78      * 
79      * @return the currentMenu
80      */
81     public final String getCurrentMenu () {
82         return this.currentMenu;
83     }
84
85     /**
86      * Current menu choice
87      * @param currentMenu the currentMenu to set
88      */
89     public final void setCurrentMenu (final String currentMenu) {
90         this.currentMenu = currentMenu;
91     }
92
93     /**
94      * "Getter" for given menu type
95      *
96      * @param menuType Menu type instance to return
97      * @return Menu or null if not found
98      */
99     public Menu getMenu (final String menuType) {
100         // Default is not found
101         Menu menu = null;
102         
103         // Check array
104         if (this.getMenus().containsKey(menuType)) {
105             // Found!
106             menu = this.getMenus().get(menuType);
107         }
108         
109         // Return it
110         return menu;
111     }
112
113     /**
114      * Determines whether the application is still active by checking some
115      * conditions
116      * 
117      * @return Whether the application is still active
118      */
119     public final boolean isRunning () {
120         // In console client, 0 may have been used
121         return this.isRunning;
122     }
123
124     /**
125      * Disables the client
126      */
127     protected final void disableIsRunning () {
128         this.isRunning = false;
129     }
130
131     /**
132      * Fills menu map with swing menus
133      */
134     protected abstract void fillMenuMap ();
135
136     /**
137      * Getter for menus map
138      * @return Map of all menus
139      */
140     protected final Map<String, Menu> getMenus () {
141         return this.menus;
142     }
143
144     /**
145      * Initializes contact manager
146      */
147     protected void initContactManager () {
148         // Debug message
149         this.getLogger().debug("Initializing contact manager ...");
150         
151         // Init contact manager with console client
152         // @TODO Static initial amount of contacts
153         ManageableContact manager = new ContactManager (100, (Client) this);
154         
155         // Set it here
156         this.setContactManager(manager);
157         
158         // Debug message
159         this.getLogger().debug("Contact manager has been initialized.");
160     }
161
162     /**
163      * Shows given menu
164      *
165      * @param menuType Given menu to show
166      */
167     protected void showMenu (final String menuType) {
168         Menu menu = this.getMenu(menuType);
169         
170         // Is the menu set?
171         if (!(menu instanceof Menu)) {
172             // Not found
173             // @todo Own exception?
174             throw new NullPointerException("Menu '" + menuType + "' not found.");
175         }
176         
177         // Show menu
178         menu.show((Client) this);
179     }
180 }