]> git.mxchange.org Git - addressbook-lib.git/blob - src/org/mxchange/addressbook/client/BaseClient.java
Project relocated (a bit better now?) + continued with Swing client
[addressbook-lib.git] / src / org / mxchange / addressbook / client / BaseClient.java
1 /*\r
2  * Copyright (C) 2015 Roland Haeder\r
3  *\r
4  * This program is free software: you can redistribute it and/or modify\r
5  * it under the terms of the GNU General Public License as published by\r
6  * the Free Software Foundation, either version 3 of the License, or\r
7  * (at your option) any later version.\r
8  *\r
9  * This program is distributed in the hope that it will be useful,\r
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12  * GNU General Public License for more details.\r
13  *\r
14  * You should have received a copy of the GNU General Public License\r
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
16  */\r
17 package org.mxchange.addressbook.client;\r
18 \r
19 import java.util.HashMap;\r
20 import java.util.Map;\r
21 import org.mxchange.addressbook.BaseFrameworkSystem;\r
22 import org.mxchange.addressbook.manager.contact.ContactManager;\r
23 import org.mxchange.addressbook.manager.contact.ManageableContact;\r
24 import org.mxchange.addressbook.menu.Menu;\r
25 \r
26 /**\r
27  * A general client\r
28  *\r
29  * @author Roland Haeder\r
30  */\r
31 public abstract class BaseClient extends BaseFrameworkSystem {\r
32 \r
33     /**\r
34      * Current menu choice\r
35      */\r
36     private String currentMenu;\r
37 \r
38     /**\r
39      * Application is not running by default\r
40      */\r
41     private boolean isRunning;\r
42 \r
43     /**\r
44      * Menu system\r
45      */\r
46     private final Map<String, Menu> menus;\r
47 \r
48     /**\r
49      * No instances can be created of this class\r
50      */\r
51     protected BaseClient () {\r
52         super();\r
53 \r
54         // Init menu map\r
55         this.menus = new HashMap<>(10);\r
56     }\r
57 \r
58     /**\r
59      * Shutdown method for all clients\r
60      */\r
61     public void doShutdown () {\r
62         // Disable client\r
63         this.disableIsRunning();\r
64 \r
65         // Shuts down contact manager\r
66         this.getContactManager().doShutdown();\r
67     }\r
68 \r
69     /**\r
70      * Enables the client\r
71      */\r
72     public void enableIsRunning () {\r
73         this.isRunning = true;\r
74     }\r
75 \r
76     /**\r
77      * Current menu choice\r
78      * \r
79      * @return the currentMenu\r
80      */\r
81     public String getCurrentMenu () {\r
82         return this.currentMenu;\r
83     }\r
84 \r
85     /**\r
86      * Current menu choice\r
87      * @param currentMenu the currentMenu to set\r
88      */\r
89     public void setCurrentMenu (final String currentMenu) {\r
90         this.currentMenu = currentMenu;\r
91     }\r
92 \r
93     /**\r
94      * "Getter" for given menu type\r
95      *\r
96      * @param menuType Menu type instance to return\r
97      * @return Menu or null if not found\r
98      */\r
99     public Menu getMenu (final String menuType) {\r
100         // Default is not found\r
101         Menu menu = null;\r
102         \r
103         // Check array\r
104         if (this.getMenus().containsKey(menuType)) {\r
105             // Found!\r
106             menu = this.getMenus().get(menuType);\r
107         }\r
108         \r
109         // Return it\r
110         return menu;\r
111     }\r
112 \r
113     /**\r
114      * Determines whether the application is still active by checking some\r
115      * conditions\r
116      * \r
117      * @return Whether the application is still active\r
118      */\r
119     public boolean isRunning () {\r
120         // In console client, 0 may have been used\r
121         return this.isRunning;\r
122     }\r
123 \r
124     /**\r
125      * Disables running state, so the main loop can abort.\r
126      */\r
127     protected void disableIsRunning () {\r
128         this.isRunning = false;\r
129     }\r
130 \r
131     /**\r
132      * Fills menu map with swing menus\r
133      */\r
134     protected abstract void fillMenuMap ();\r
135 \r
136     /**\r
137      * Getter for menus map\r
138      * @return Map of all menus\r
139      */\r
140     protected final Map<String, Menu> getMenus () {\r
141         return this.menus;\r
142     }\r
143 \r
144     /**\r
145      * Initializes contact manager\r
146      */\r
147     protected void initContactManager () {\r
148         // Debug message\r
149         this.getLogger().debug("Initializing contact manager ...");\r
150         \r
151         // Init contact manager with console client\r
152         // @TODO Static initial amount of contacts\r
153         ManageableContact manager = new ContactManager (100, (Client) this);\r
154         \r
155         // Set it here\r
156         this.setContactManager(manager);\r
157         \r
158         // Debug message\r
159         this.getLogger().debug("Contact manager has been initialized.");\r
160     }\r
161 \r
162     /**\r
163      * Shows given menu\r
164      *\r
165      * @param menuType Given menu to show\r
166      */\r
167     protected void showMenu (final String menuType) {\r
168         Menu menu = this.getMenu(menuType);\r
169         \r
170         // Is the menu set?\r
171         if (!(menu instanceof Menu)) {\r
172             // Not found\r
173             // @todo Own exception?\r
174             throw new NullPointerException("Menu '" + menuType + "' not found.");\r
175         }\r
176         \r
177         // Show menu\r
178         menu.show((Client) this);\r
179     }\r
180 }\r