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.Collections;
22 import java.util.HashMap;
24 import javax.naming.Context;
25 import javax.naming.InitialContext;
26 import javax.naming.NamingException;
27 import org.mxchange.addressbook.facade.contact.AddressbookContactFacade;
28 import org.mxchange.addressbook.facade.contact.ContactFacade;
29 import org.mxchange.addressbook.menu.Menu;
30 import org.mxchange.jcore.client.BaseClient;
31 import org.mxchange.jcore.client.Client;
32 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
33 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
36 * A general addressbook client
38 * @author Roland Haeder TODO: Find better name
40 public abstract class BaseAddressbookClient extends BaseClient implements AddressbookClient {
45 private String currentMenu;
51 private LoggerBeanLocal logger;
56 private final Map<String, Menu> menus;
59 * No instances can be created of this class
61 protected BaseAddressbookClient () {
63 this.menus = new HashMap<>(10);
68 Context context = new InitialContext();
71 this.logger = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
72 } catch (final NamingException ex) {
74 throw new RuntimeException(ex);
81 * @return the currentMenu
83 public String getCurrentMenu () {
84 return this.currentMenu;
88 public void setCurrentMenu (final String currentMenu) {
89 this.currentMenu = currentMenu;
93 * "Getter" for given menu type
95 * @param menuType Menu type instance to return
97 * @return Menu or null if not found
99 private Menu getMenu (final String menuType) {
100 // Default is not found
104 if (this.getMenus().containsKey(menuType)) {
106 menu = this.getMenus().get(menuType);
114 * Logs exception and exits program
116 * @param throwable Throwable
118 protected void abortProgramWithException (final Throwable throwable) {
120 this.logException(throwable);
127 * Fills menu map with swing menus
129 protected abstract void fillMenuMap ();
132 * Getter for logger instance
134 * @return Logger instance
136 protected LoggerBeanLocal getLogger () {
141 * Getter for menus map
143 * @return Map of all menus
145 protected Map<String, Menu> getMenus () {
146 return Collections.unmodifiableMap(this.menus);
150 * Initializes contact manager
152 * @throws java.sql.SQLException If any SQL error occurs
154 protected void initContactManager () throws SQLException {
156 this.getLogger().logTrace("CALLED!"); //NOI18N
159 this.getLogger().logDebug("Initializing contact manager ..."); //NOI18N
161 // Init contact facade with console client
162 // TODO Static initial amount of contacts
163 ContactFacade facade = new AddressbookContactFacade((Client) this);
166 this.setFacade(facade);
169 this.getLogger().logDebug("Contact manager has been initialized."); //NOI18N
172 this.getLogger().logTrace("EXIT!"); //NOI18N
178 * @param throwable Throwable
180 protected void logException (final Throwable throwable) {
181 // Deligate to logger
182 this.getLogger().logException(throwable);
188 * @param menuType Given menu to show
190 protected void showMenu (final String menuType) {
192 this.getLogger().logTrace(MessageFormat.format("menuType={0} - CALLED!", menuType)); //NOI18N
194 // Get menu from type
195 Menu menu = this.getMenu(menuType);
198 if (!(menu instanceof Menu)) {
200 // TODO Own exception?
201 throw new NullPointerException(MessageFormat.format("Menu '{0}' not found.", menuType)); //NOI18N
208 this.getLogger().logTrace("EXIT!"); //NOI18N