]> git.mxchange.org Git - jfinancials-swing.git/blob - src/org/mxchange/addressbook/client/BaseAddressbookClient.java
Prepared for upcoming rewrite:
[jfinancials-swing.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 javax.naming.Context;
24 import javax.naming.InitialContext;
25 import javax.naming.NamingException;
26 import org.mxchange.addressbook.manager.contact.AddressbookContactManager;
27 import org.mxchange.addressbook.manager.contact.ManageableContactAddressbook;
28 import org.mxchange.addressbook.menu.Menu;
29 import org.mxchange.jcore.client.BaseClient;
30 import org.mxchange.jcore.client.Client;
31 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
32
33 /**
34  * A general addressbook client
35  * <p>
36  * @author Roland Haeder TODO: Find better name
37  */
38 public abstract class BaseAddressbookClient extends BaseClient implements AddressbookClient {
39
40         /**
41          * Current menu choice
42          */
43         private String currentMenu;
44
45         /**
46          * Menu system
47          */
48         private final Map<String, Menu> menus;
49
50         /**
51          * Logger instance
52          */
53         private LoggerBeanLocal logger;
54
55         /**
56          * No instances can be created of this class
57          */
58         protected BaseAddressbookClient () {
59                 // Init menu map
60                 this.menus = new HashMap<>(10);
61
62                 // Try it
63                 try {
64                         // Get context
65                         Context context = new InitialContext();
66
67                         // Lookup logger
68                         this.logger = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal");
69                 } catch (final NamingException ex) {
70                         // Continue to throw
71                         throw new RuntimeException(ex);
72                 }
73         }
74
75         /**
76          * Current menu choice
77          * <p>
78          * @return the currentMenu
79          */
80         public final String getCurrentMenu () {
81                 return this.currentMenu;
82         }
83
84         @Override
85         public final void setCurrentMenu (final String currentMenu) {
86                 this.currentMenu = currentMenu;
87         }
88
89         /**
90          * "Getter" for given menu type
91          * <p>
92          * @param menuType Menu type instance to return
93          * @return Menu or null if not found
94          */
95         private Menu getMenu (final String menuType) {
96                 // Default is not found
97                 Menu menu = null;
98
99                 // Check array
100                 if (this.getMenus().containsKey(menuType)) {
101                         // Found!
102                         menu = this.getMenus().get(menuType);
103                 }
104
105                 // Return it
106                 return menu;
107         }
108
109         /**
110          * Fills menu map with swing menus
111          */
112         protected abstract void fillMenuMap ();
113
114         /**
115          * Getter for menus map
116          * <p>
117          * @return Map of all menus
118          */
119         protected final Map<String, Menu> getMenus () {
120                 return this.menus;
121         }
122
123         /**
124          * Initializes contact manager
125          * <p>
126          * @throws java.sql.SQLException If any SQL error occurs
127          */
128         protected void initContactManager () throws SQLException {
129                 // Trace message
130                 this.getLogger().logTrace("CALLED!"); //NOI18N
131
132                 // Debug message
133                 this.getLogger().logDebug("Initializing contact manager ..."); //NOI18N
134
135                 // Init contact manager with console client
136                 // TODO Static initial amount of contacts
137                 ManageableContactAddressbook manager = new AddressbookContactManager((Client) this);
138
139                 // Set it here
140                 this.setManager(manager);
141
142                 // Debug message
143                 this.getLogger().logDebug("Contact manager has been initialized."); //NOI18N
144
145                 // Trace message
146                 this.getLogger().logTrace("EXIT!"); //NOI18N
147         }
148
149         /**
150          * Shows given menu
151          * <p>
152          * @param menuType Given menu to show
153          */
154         protected void showMenu (final String menuType) {
155                 // Trace message
156                 this.getLogger().logTrace(MessageFormat.format("menuType={0} - CALLED!", menuType)); //NOI18N
157
158                 // Get menu from type
159                 Menu menu = this.getMenu(menuType);
160
161                 // Is the menu set?
162                 if (!(menu instanceof Menu)) {
163                         // Not found
164                         // TODO Own exception?
165                         throw new NullPointerException(MessageFormat.format("Menu '{0}' not found.", menuType)); //NOI18N
166                 }
167
168                 // Show menu
169                 menu.show(this);
170
171                 // Trace message
172                 this.getLogger().logTrace("EXIT!"); //NOI18N
173         }
174
175         /**
176          * Getter for logger instance
177          *
178          * @return Logger instance
179          */
180         protected LoggerBeanLocal getLogger () {
181                 return this.logger;
182         }
183
184         /**
185          * Logs an exception
186          *
187          * @param throwable Throwable
188          */
189         protected void logException (final Throwable throwable) {
190                 // Deligate to logger
191                 this.getLogger().logException(throwable);
192         }
193
194         /**
195          * Logs exception and exits program
196          *
197          * @param throwable Throwable
198          */
199         protected void abortProgramWithException (final Throwable throwable) {
200                 // Log exception
201                 this.logException(throwable);
202
203                 // Abort here
204                 System.exit(1);
205         }
206 }