*/\r
package org.mxchange.addressbook.client;\r
\r
+import java.util.HashMap;\r
+import java.util.Map;\r
import org.mxchange.addressbook.BaseFrameworkSystem;\r
+import org.mxchange.addressbook.menu.Menu;\r
\r
/**\r
* A general client\r
*\r
* @author Roland Haeder\r
*/\r
-public class BaseClient extends BaseFrameworkSystem {\r
+public abstract class BaseClient extends BaseFrameworkSystem {\r
\r
/**\r
* Current menu choice\r
*/\r
private boolean isRunning;\r
\r
+ /**\r
+ * Menu system\r
+ */\r
+ private final Map<String, Menu> menus;\r
+\r
/**\r
* No instances can be created of this class\r
*/\r
protected BaseClient () {\r
super();\r
+\r
+ // Init menu map\r
+ this.menus = new HashMap<>(10);\r
}\r
\r
/**\r
this.isRunning = false;\r
}\r
\r
+ /**\r
+ * Enables the client\r
+ */\r
public void enableIsRunning () {\r
this.isRunning = true;\r
}\r
\r
/**\r
* Current menu choice\r
+ * \r
* @return the currentMenu\r
*/\r
public String getCurrentMenu () {\r
this.currentMenu = currentMenu;\r
}\r
\r
- public boolean isApplicationRunning () {\r
+ /**\r
+ * "Getter" for given menu type\r
+ *\r
+ * @param menuType Menu type instance to return\r
+ * @return Menu or null if not found\r
+ */\r
+ public Menu getMenu (final String menuType) {\r
+ // Default is not found\r
+ Menu menu = null;\r
+ \r
+ // Check array\r
+ if (this.getMenus().containsKey(menuType)) {\r
+ // Found!\r
+ menu = this.getMenus().get(menuType);\r
+ }\r
+ \r
+ // Return it\r
+ return menu;\r
+ }\r
+\r
+ /**\r
+ * Determines whether the application is still active by checking some\r
+ * conditions\r
+ * \r
+ * @return Whether the application is still active\r
+ */\r
+ public boolean isRunning () {\r
// In console client, 0 may have been used\r
return this.isRunning;\r
}\r
+\r
+ /**\r
+ * Fills menu map with swing menus\r
+ */\r
+ protected abstract void fillMenuMap ();\r
+\r
+ /**\r
+ * Getter for menus map\r
+ * @return Map of all menus\r
+ */\r
+ protected final Map<String, Menu> getMenus () {\r
+ return this.menus;\r
+ }\r
+\r
+ /**\r
+ * Shows given menu\r
+ *\r
+ * @param menuType Given menu to show\r
+ */\r
+ protected void showMenu (final String menuType) {\r
+ Menu menu = this.getMenu(menuType);\r
+ \r
+ // Is the menu set?\r
+ if (!(menu instanceof Menu)) {\r
+ // Not found\r
+ // @todo Own exception?\r
+ throw new NullPointerException("Menu '" + menuType + "' not found.");\r
+ }\r
+ \r
+ // Show menu\r
+ menu.show((Client) this);\r
+ }\r
}\r