]> git.mxchange.org Git - addressbook-swing.git/blob - Addressbook/src/org/mxchange/addressbook/client/BaseClient.java
04b1ad5ce34a484a1b7538aaf413f798e7ce098e
[addressbook-swing.git] / Addressbook / 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      * Disables running state, so the main loop can abort.\r
60      */\r
61     public void disableIsRunning () {\r
62         this.isRunning = false;\r
63     }\r
64 \r
65     /**\r
66      * Enables the client\r
67      */\r
68     public void enableIsRunning () {\r
69         this.isRunning = true;\r
70     }\r
71 \r
72     /**\r
73      * Current menu choice\r
74      * \r
75      * @return the currentMenu\r
76      */\r
77     public String getCurrentMenu () {\r
78         return this.currentMenu;\r
79     }\r
80 \r
81     /**\r
82      * Current menu choice\r
83      * @param currentMenu the currentMenu to set\r
84      */\r
85     public void setCurrentMenu (final String currentMenu) {\r
86         this.currentMenu = currentMenu;\r
87     }\r
88 \r
89     /**\r
90      * "Getter" for given menu type\r
91      *\r
92      * @param menuType Menu type instance to return\r
93      * @return Menu or null if not found\r
94      */\r
95     public Menu getMenu (final String menuType) {\r
96         // Default is not found\r
97         Menu menu = null;\r
98         \r
99         // Check array\r
100         if (this.getMenus().containsKey(menuType)) {\r
101             // Found!\r
102             menu = this.getMenus().get(menuType);\r
103         }\r
104         \r
105         // Return it\r
106         return menu;\r
107     }\r
108 \r
109     /**\r
110      * Determines whether the application is still active by checking some\r
111      * conditions\r
112      * \r
113      * @return Whether the application is still active\r
114      */\r
115     public boolean isRunning () {\r
116         // In console client, 0 may have been used\r
117         return this.isRunning;\r
118     }\r
119 \r
120     /**\r
121      * Fills menu map with swing menus\r
122      */\r
123     protected abstract void fillMenuMap ();\r
124 \r
125     /**\r
126      * Getter for menus map\r
127      * @return Map of all menus\r
128      */\r
129     protected final Map<String, Menu> getMenus () {\r
130         return this.menus;\r
131     }\r
132 \r
133     /**\r
134      * Shows given menu\r
135      *\r
136      * @param menuType Given menu to show\r
137      */\r
138     protected void showMenu (final String menuType) {\r
139         Menu menu = this.getMenu(menuType);\r
140         \r
141         // Is the menu set?\r
142         if (!(menu instanceof Menu)) {\r
143             // Not found\r
144             // @todo Own exception?\r
145             throw new NullPointerException("Menu '" + menuType + "' not found.");\r
146         }\r
147         \r
148         // Show menu\r
149         menu.show((Client) this);\r
150     }\r
151 \r
152     /**\r
153      * Initializes contact manager\r
154      */\r
155     protected void initContactManager () {\r
156         // Debug message\r
157         this.getLogger().debug("Initializing contact manager ...");\r
158         \r
159         // Init contact manager with console client\r
160         // @TODO Static initial amount of contacts\r
161         ManageableContact manager = new ContactManager (100, (Client) this);\r
162         \r
163         // Set it here\r
164         this.setContactManager(manager);\r
165         \r
166         // Debug message\r
167         this.getLogger().debug("Contact manager has been initialized.");\r
168     }\r
169 }\r