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.facade.contact.AddressbookContactFacade;
27 import org.mxchange.addressbook.facade.contact.ContactFacade;
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.Log;
32 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
35 * A general addressbook client
37 * @author Roland Haeder TODO: Find better name
39 public abstract class BaseAddressbookClient extends BaseClient implements AddressbookClient {
44 private String currentMenu;
50 private LoggerBeanLocal logger;
55 private final Map<String, Menu> menus;
58 * No instances can be created of this class
60 protected BaseAddressbookClient () {
62 this.menus = new HashMap<>(10);
67 Context context = new InitialContext();
70 this.logger = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
71 } catch (final NamingException ex) {
73 throw new RuntimeException(ex);
80 * @return the currentMenu
82 public final String getCurrentMenu () {
83 return this.currentMenu;
87 public final void setCurrentMenu (final String currentMenu) {
88 this.currentMenu = currentMenu;
92 * "Getter" for given menu type
94 * @param menuType Menu type instance to return
95 * @return Menu or null if not found
97 private Menu getMenu (final String menuType) {
98 // Default is not found
102 if (this.getMenus().containsKey(menuType)) {
104 menu = this.getMenus().get(menuType);
112 * Logs exception and exits program
114 * @param throwable Throwable
116 protected void abortProgramWithException (final Throwable throwable) {
118 this.logException(throwable);
125 * Fills menu map with swing menus
127 protected abstract void fillMenuMap ();
130 * Getter for logger instance
132 * @return Logger instance
134 protected LoggerBeanLocal getLogger () {
139 * Getter for menus map
141 * @return Map of all menus
143 protected final Map<String, Menu> getMenus () {
148 * Initializes contact manager
150 * @throws java.sql.SQLException If any SQL error occurs
152 protected void initContactManager () throws SQLException {
154 this.getLogger().logTrace("CALLED!"); //NOI18N
157 this.getLogger().logDebug("Initializing contact manager ..."); //NOI18N
159 // Init contact facade with console client
160 // TODO Static initial amount of contacts
161 ContactFacade facade = new AddressbookContactFacade((Client) this);
164 this.setFacade(facade);
167 this.getLogger().logDebug("Contact manager has been initialized."); //NOI18N
170 this.getLogger().logTrace("EXIT!"); //NOI18N
176 * @param throwable Throwable
178 protected void logException (final Throwable throwable) {
179 // Deligate to logger
180 this.getLogger().logException(throwable);
186 * @param menuType Given menu to show
188 protected void showMenu (final String menuType) {
190 this.getLogger().logTrace(MessageFormat.format("menuType={0} - CALLED!", menuType)); //NOI18N
192 // Get menu from type
193 Menu menu = this.getMenu(menuType);
196 if (!(menu instanceof Menu)) {
198 // TODO Own exception?
199 throw new NullPointerException(MessageFormat.format("Menu '{0}' not found.", menuType)); //NOI18N
206 this.getLogger().logTrace("EXIT!"); //NOI18N