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