]> git.mxchange.org Git - addressbook-lib.git/blob - Addressbook/src/org/mxchange/addressbook/client/BaseClient.java
Global commit: Changed spaces to tabs, because all Java files have tabs for indenting
[addressbook-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          *
88          * @param currentMenu the currentMenu to set
89          */
90         public final void setCurrentMenu (final String currentMenu) {
91                 this.currentMenu = currentMenu;
92         }
93
94         /**
95          * "Getter" for given menu type
96          *
97          * @param menuType Menu type instance to return
98          * @return Menu or null if not found
99          */
100         public Menu getMenu (final String menuType) {
101                 // Default is not found
102                 Menu menu = null;
103
104                 // Check array
105                 if (this.getMenus().containsKey(menuType)) {
106                         // Found!
107                         menu = this.getMenus().get(menuType);
108                 }
109
110                 // Return it
111                 return menu;
112         }
113
114         /**
115          * Determines whether the application is still active by checking some
116          * conditions
117          *
118          * @return Whether the application is still active
119          */
120         public final boolean isRunning () {
121                 // In console client, 0 may have been used
122                 return this.isRunning;
123         }
124
125         /**
126          * Disables the client
127          */
128         protected final void disableIsRunning () {
129                 this.isRunning = false;
130         }
131
132         /**
133          * Fills menu map with swing menus
134          */
135         protected abstract void fillMenuMap ();
136
137         /**
138          * Getter for menus map
139          *
140          * @return Map of all menus
141          */
142         protected final Map<String, Menu> getMenus () {
143                 return this.menus;
144         }
145
146         /**
147          * Initializes contact manager
148          */
149         protected void initContactManager () {
150                 // Debug message
151                 this.getLogger().debug("Initializing contact manager ...");
152
153                 // Init contact manager with console client
154                 // @TODO Static initial amount of contacts
155                 ManageableContact manager = new ContactManager(100, (Client) this);
156
157                 // Set it here
158                 this.setContactManager(manager);
159
160                 // Debug message
161                 this.getLogger().debug("Contact manager has been initialized.");
162         }
163
164         /**
165          * Shows given menu
166          *
167          * @param menuType Given menu to show
168          */
169         protected void showMenu (final String menuType) {
170                 Menu menu = this.getMenu(menuType);
171
172                 // Is the menu set?
173                 if (!(menu instanceof Menu)) {
174                         // Not found
175                         // @todo Own exception?
176                         throw new NullPointerException("Menu '" + menuType + "' not found.");
177                 }
178
179                 // Show menu
180                 menu.show((Client) this);
181         }
182 }