]> git.mxchange.org Git - jbonuscard-lib.git/blob - Addressbook/src/org/mxchange/addressbook/client/BaseClient.java
Introduced a lot Swing stuff + moved some attributes
[jbonuscard-lib.git] / Addressbook / src / org / mxchange / addressbook / client / BaseClient.java
1 /*\r
2  * Copyright (C) 2015 Roland Haeder\r
3  *\r
4  * This program is free software: you can redistribute it and/or modify\r
5  * it under the terms of the GNU General Public License as published by\r
6  * the Free Software Foundation, either version 3 of the License, or\r
7  * (at your option) any later version.\r
8  *\r
9  * This program is distributed in the hope that it will be useful,\r
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12  * GNU General Public License for more details.\r
13  *\r
14  * You should have received a copy of the GNU General Public License\r
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
16  */\r
17 package org.mxchange.addressbook.client;\r
18 \r
19 import java.util.HashMap;\r
20 import java.util.Map;\r
21 import org.mxchange.addressbook.BaseFrameworkSystem;\r
22 import org.mxchange.addressbook.menu.Menu;\r
23 \r
24 /**\r
25  * A general client\r
26  *\r
27  * @author Roland Haeder\r
28  */\r
29 public abstract class BaseClient extends BaseFrameworkSystem {\r
30 \r
31     /**\r
32      * Current menu choice\r
33      */\r
34     private String currentMenu;\r
35 \r
36     /**\r
37      * Application is not running by default\r
38      */\r
39     private boolean isRunning;\r
40 \r
41     /**\r
42      * Menu system\r
43      */\r
44     private final Map<String, Menu> menus;\r
45 \r
46     /**\r
47      * No instances can be created of this class\r
48      */\r
49     protected BaseClient () {\r
50         super();\r
51 \r
52         // Init menu map\r
53         this.menus = new HashMap<>(10);\r
54     }\r
55 \r
56     /**\r
57      * Disables running state, so the main loop can abort.\r
58      */\r
59     public void disableIsRunning () {\r
60         this.isRunning = false;\r
61     }\r
62 \r
63     /**\r
64      * Enables the client\r
65      */\r
66     public void enableIsRunning () {\r
67         this.isRunning = true;\r
68     }\r
69 \r
70     /**\r
71      * Current menu choice\r
72      * \r
73      * @return the currentMenu\r
74      */\r
75     public String getCurrentMenu () {\r
76         return this.currentMenu;\r
77     }\r
78 \r
79     /**\r
80      * Current menu choice\r
81      * @param currentMenu the currentMenu to set\r
82      */\r
83     public void setCurrentMenu (final String currentMenu) {\r
84         this.currentMenu = currentMenu;\r
85     }\r
86 \r
87     /**\r
88      * "Getter" for given menu type\r
89      *\r
90      * @param menuType Menu type instance to return\r
91      * @return Menu or null if not found\r
92      */\r
93     public Menu getMenu (final String menuType) {\r
94         // Default is not found\r
95         Menu menu = null;\r
96         \r
97         // Check array\r
98         if (this.getMenus().containsKey(menuType)) {\r
99             // Found!\r
100             menu = this.getMenus().get(menuType);\r
101         }\r
102         \r
103         // Return it\r
104         return menu;\r
105     }\r
106 \r
107     /**\r
108      * Determines whether the application is still active by checking some\r
109      * conditions\r
110      * \r
111      * @return Whether the application is still active\r
112      */\r
113     public boolean isRunning () {\r
114         // In console client, 0 may have been used\r
115         return this.isRunning;\r
116     }\r
117 \r
118     /**\r
119      * Fills menu map with swing menus\r
120      */\r
121     protected abstract void fillMenuMap ();\r
122 \r
123     /**\r
124      * Getter for menus map\r
125      * @return Map of all menus\r
126      */\r
127     protected final Map<String, Menu> getMenus () {\r
128         return this.menus;\r
129     }\r
130 \r
131     /**\r
132      * Shows given menu\r
133      *\r
134      * @param menuType Given menu to show\r
135      */\r
136     protected void showMenu (final String menuType) {\r
137         Menu menu = this.getMenu(menuType);\r
138         \r
139         // Is the menu set?\r
140         if (!(menu instanceof Menu)) {\r
141             // Not found\r
142             // @todo Own exception?\r
143             throw new NullPointerException("Menu '" + menuType + "' not found.");\r
144         }\r
145         \r
146         // Show menu\r
147         menu.show((Client) this);\r
148     }\r
149 }\r