2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.addressbook.client;
19 import java.sql.SQLException;
20 import java.text.MessageFormat;
21 import java.util.HashMap;
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;
34 * A general addressbook client
36 * @author Roland Haeder TODO: Find better name
38 public abstract class BaseAddressbookClient extends BaseClient implements AddressbookClient {
43 private String currentMenu;
48 private final Map<String, Menu> menus;
53 private LoggerBeanLocal logger;
56 * No instances can be created of this class
58 protected BaseAddressbookClient () {
60 this.menus = new HashMap<>(10);
65 Context context = new InitialContext();
68 this.logger = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal");
69 } catch (final NamingException ex) {
71 throw new RuntimeException(ex);
78 * @return the currentMenu
80 public final String getCurrentMenu () {
81 return this.currentMenu;
85 public final void setCurrentMenu (final String currentMenu) {
86 this.currentMenu = currentMenu;
90 * "Getter" for given menu type
92 * @param menuType Menu type instance to return
93 * @return Menu or null if not found
95 private Menu getMenu (final String menuType) {
96 // Default is not found
100 if (this.getMenus().containsKey(menuType)) {
102 menu = this.getMenus().get(menuType);
110 * Fills menu map with swing menus
112 protected abstract void fillMenuMap ();
115 * Getter for menus map
117 * @return Map of all menus
119 protected final Map<String, Menu> getMenus () {
124 * Initializes contact manager
126 * @throws java.sql.SQLException If any SQL error occurs
128 protected void initContactManager () throws SQLException {
130 this.getLogger().logTrace("CALLED!"); //NOI18N
133 this.getLogger().logDebug("Initializing contact manager ..."); //NOI18N
135 // Init contact manager with console client
136 // TODO Static initial amount of contacts
137 ManageableContactAddressbook manager = new AddressbookContactManager((Client) this);
140 this.setManager(manager);
143 this.getLogger().logDebug("Contact manager has been initialized."); //NOI18N
146 this.getLogger().logTrace("EXIT!"); //NOI18N
152 * @param menuType Given menu to show
154 protected void showMenu (final String menuType) {
156 this.getLogger().logTrace(MessageFormat.format("menuType={0} - CALLED!", menuType)); //NOI18N
158 // Get menu from type
159 Menu menu = this.getMenu(menuType);
162 if (!(menu instanceof Menu)) {
164 // TODO Own exception?
165 throw new NullPointerException(MessageFormat.format("Menu '{0}' not found.", menuType)); //NOI18N
172 this.getLogger().logTrace("EXIT!"); //NOI18N
176 * Getter for logger instance
178 * @return Logger instance
180 protected LoggerBeanLocal getLogger () {
187 * @param throwable Throwable
189 protected void logException (final Throwable throwable) {
190 // Deligate to logger
191 this.getLogger().logException(throwable);
195 * Logs exception and exits program
197 * @param throwable Throwable
199 protected void abortProgramWithException (final Throwable throwable) {
201 this.logException(throwable);