]> git.mxchange.org Git - jaddressbook-share-lib.git/blob - Addressbook/src/org/mxchange/addressbook/client/BaseAddressbookClient.java
Renamed some classes/interfaces to make it clear they belong only to this project...
[jaddressbook-share-lib.git] / Addressbook / 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.text.MessageFormat;
20 import java.util.HashMap;
21 import java.util.Map;
22 import org.mxchange.addressbook.manager.contact.AddressbookContactManager;
23 import org.mxchange.addressbook.menu.Menu;
24 import org.mxchange.jcore.client.BaseClient;
25 import org.mxchange.jcore.client.Client;
26 import org.mxchange.jcore.manager.database.ManageableDatabase;
27
28 /**
29  * A general addressbook client
30  *
31  * @author Roland Haeder
32  */
33 public abstract class BaseAddressbookClient extends BaseClient {
34
35         /**
36          * Current menu choice
37          */
38         private String currentMenu;
39
40         /**
41          * Menu system
42          */
43         private final Map<String, Menu> menus;
44
45         /**
46          * No instances can be created of this class
47          */
48         protected BaseAddressbookClient () {
49                 // Init menu map
50                 this.menus = new HashMap<>(10);
51         }
52
53         /**
54          * Current menu choice
55          *
56          * @return the currentMenu
57          */
58         public final String getCurrentMenu () {
59                 return this.currentMenu;
60         }
61
62         /**
63          * Current menu choice
64          *
65          * @param currentMenu the currentMenu to set
66          */
67         public final void setCurrentMenu (final String currentMenu) {
68                 this.currentMenu = currentMenu;
69         }
70
71         /**
72          * "Getter" for given menu type
73          *
74          * @param menuType Menu type instance to return
75          * @return Menu or null if not found
76          */
77         public Menu getMenu (final String menuType) {
78                 // Default is not found
79                 Menu menu = null;
80
81                 // Check array
82                 if (this.getMenus().containsKey(menuType)) {
83                         // Found!
84                         menu = this.getMenus().get(menuType);
85                 }
86
87                 // Return it
88                 return menu;
89         }
90
91         /**
92          * Fills menu map with swing menus
93          */
94         protected abstract void fillMenuMap ();
95
96         /**
97          * Getter for menus map
98          *
99          * @return Map of all menus
100          */
101         protected final Map<String, Menu> getMenus () {
102                 return this.menus;
103         }
104
105         /**
106          * Initializes contact manager
107          */
108         protected void initContactManager () {
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                 ManageableDatabase manager = new AddressbookContactManager((Client) this);
118                 
119                 // Set it here
120                 this.setContactManager(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 }