]> git.mxchange.org Git - jfinancials-lib.git/blob - src/org/mxchange/addressbook/client/BaseAddressbookClient.java
f14cd81cac9566c3cc7671198e4c13c2b39ffdba
[jfinancials-lib.git] / src / org / mxchange / addressbook / client / BaseAddressbookClient.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.sql.SQLException;
20 import java.text.MessageFormat;
21 import java.util.HashMap;
22 import java.util.Map;
23 import org.mxchange.addressbook.manager.contact.AddressbookContactManager;
24 import org.mxchange.addressbook.menu.Menu;
25 import org.mxchange.jcore.client.BaseClient;
26 import org.mxchange.jcore.client.Client;
27 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
28 import org.mxchange.jcore.manager.database.ManageableDatabase;
29
30 /**
31  * A general addressbook client
32  *
33  * @author Roland Haeder
34  */
35 public abstract class BaseAddressbookClient extends BaseClient {
36
37         /**
38          * Current menu choice
39          */
40         private String currentMenu;
41
42         /**
43          * Menu system
44          */
45         private final Map<String, Menu> menus;
46
47         /**
48          * No instances can be created of this class
49          */
50         protected BaseAddressbookClient () {
51                 // Init menu map
52                 this.menus = new HashMap<>(10);
53         }
54
55         /**
56          * Current menu choice
57          *
58          * @return the currentMenu
59          */
60         public final String getCurrentMenu () {
61                 return this.currentMenu;
62         }
63
64         /**
65          * Current menu choice
66          *
67          * @param currentMenu the currentMenu to set
68          */
69         public final void setCurrentMenu (final String currentMenu) {
70                 this.currentMenu = currentMenu;
71         }
72
73         /**
74          * "Getter" for given menu type
75          *
76          * @param menuType Menu type instance to return
77          * @return Menu or null if not found
78          */
79         public Menu getMenu (final String menuType) {
80                 // Default is not found
81                 Menu menu = null;
82
83                 // Check array
84                 if (this.getMenus().containsKey(menuType)) {
85                         // Found!
86                         menu = this.getMenus().get(menuType);
87                 }
88
89                 // Return it
90                 return menu;
91         }
92
93         /**
94          * Fills menu map with swing menus
95          */
96         protected abstract void fillMenuMap ();
97
98         /**
99          * Getter for menus map
100          *
101          * @return Map of all menus
102          */
103         protected final Map<String, Menu> getMenus () {
104                 return this.menus;
105         }
106
107         /**
108          * Initializes contact manager
109          * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the database backend is not supported
110          * @throws java.sql.SQLException If any SQL error occurs
111          */
112         protected void initContactManager () throws UnsupportedDatabaseBackendException, SQLException {
113                 // Trace message
114                 this.getLogger().trace("CALLED!"); //NOI18N
115                 
116                 // Debug message
117                 this.getLogger().debug("Initializing contact manager ..."); //NOI18N
118                 
119                 // Init contact manager with console client
120                 // TODO Static initial amount of contacts
121                 ManageableDatabase manager = new AddressbookContactManager((Client) this);
122                 
123                 // Set it here
124                 this.setContactManager(manager);
125                 
126                 // Debug message
127                 this.getLogger().debug("Contact manager has been initialized."); //NOI18N
128                 
129                 // Trace message
130                 this.getLogger().trace("EXIT!"); //NOI18N
131         }
132
133         /**
134          * Shows given menu
135          *
136          * @param menuType Given menu to show
137          */
138         protected void showMenu (final String menuType) {
139                 // Trace message
140                 this.getLogger().trace(MessageFormat.format("menuType={0} - CALLED!", menuType)); //NOI18N
141
142                 Menu menu = this.getMenu(menuType);
143
144                 // Is the menu set?
145                 if (!(menu instanceof Menu)) {
146                         // Not found
147                         // TODO Own exception?
148                         throw new NullPointerException(MessageFormat.format("Menu '{0}' not found.", menuType)); //NOI18N
149                 }
150
151                 // Show menu
152                 menu.show(this);
153
154                 // Trace message
155                 this.getLogger().trace("EXIT!"); //NOI18N
156         }
157 }