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