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