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