From 6aee766f085ca3c6da8191b0f3d371585fbc6cdc Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Thu, 16 Jul 2015 08:26:25 +0200 Subject: [PATCH] =?utf8?q?Made=20getMenu()=20abstract=20as=20showMenu()=20?= =?utf8?q?is=20now=20in=20BaseClient.=20This=20has=20the=20benefit=20that?= =?utf8?q?=20generic=20code=20doesn't=20need=20to=20be=20repeated=20(DRY).?= =?utf8?q?=20Signed-off-by:Roland=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../application/AddressbookApplication.java | 5 +- .../addressbook/client/BaseClient.java | 42 +++++++++++- .../mxchange/addressbook/client/Client.java | 10 +-- .../client/console/ConsoleClient.java | 66 +++++++------------ 4 files changed, 73 insertions(+), 50 deletions(-) diff --git a/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java b/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java index 44fe468..00baae0 100644 --- a/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java +++ b/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java @@ -17,6 +17,7 @@ package org.mxchange.addressbook.application; import org.mxchange.addressbook.BaseFrameworkSystem; +import org.mxchange.addressbook.UnhandledUserChoiceException; import org.mxchange.addressbook.client.Client; import org.mxchange.addressbook.client.console.ConsoleClient; import org.mxchange.addressbook.manager.application.ApplicationManager; @@ -164,14 +165,14 @@ public class AddressbookApplication extends BaseFrameworkSystem implements Appli this.getClient().setCurrentMenu("main"); // --- Main loop starts here --- - while (this.getClient().isApplicationRunning()) { + while (this.getClient().isRunning()) { // The application is still active, show menu selection this.getClient().showCurrentMenu(); try { // Ask for user input and run proper method this.getClient().doUserMenuChoice(); - } catch (final Exception ex) { + } catch (final UnhandledUserChoiceException ex) { this.getLogger().catching(ex); } } diff --git a/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java b/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java index 09c7b0f..706229c 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java +++ b/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java @@ -17,13 +17,14 @@ package org.mxchange.addressbook.client; import org.mxchange.addressbook.BaseFrameworkSystem; +import org.mxchange.addressbook.menu.Menu; /** * A general client * * @author Roland Haeder */ -public class BaseClient extends BaseFrameworkSystem { +public abstract class BaseClient extends BaseFrameworkSystem { /** * Current menu choice @@ -49,12 +50,16 @@ public class BaseClient extends BaseFrameworkSystem { this.isRunning = false; } + /** + * Enables the client + */ public void enableIsRunning () { this.isRunning = true; } /** * Current menu choice + * * @return the currentMenu */ public String getCurrentMenu () { @@ -69,8 +74,41 @@ public class BaseClient extends BaseFrameworkSystem { this.currentMenu = currentMenu; } - public boolean isApplicationRunning () { + /** + * Some kind of "getter" for a Menu instance from given menu type + * + * @param menuType Menu type, e.g. "main" for main menu + * @return + */ + public abstract Menu getMenu (final String menuType); + + /** + * Determines whether the application is still active by checking some + * conditions + * + * @return Whether the application is still active + */ + public boolean isRunning () { // In console client, 0 may have been used return this.isRunning; } + + /** + * Shows given menu + * + * @param menuType Given menu to show + */ + protected void showMenu (final String menuType) { + Menu menu = this.getMenu(menuType); + + // Is the menu set? + if (!(menu instanceof Menu)) { + // Not found + // @todo Own exception? + throw new NullPointerException("Menu '" + menuType + "' not found."); + } + + // Show menu + menu.show((Client) this); + } } diff --git a/Addressbook/src/org/mxchange/addressbook/client/Client.java b/Addressbook/src/org/mxchange/addressbook/client/Client.java index 4319a59..6713139 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/Client.java +++ b/Addressbook/src/org/mxchange/addressbook/client/Client.java @@ -22,7 +22,8 @@ import org.mxchange.addressbook.contact.Contact; import org.mxchange.addressbook.menu.item.SelectableMenuItem; /** - * + * An interface for application clients + * * @author Roland Haeder */ public interface Client extends FrameworkInterface { @@ -125,15 +126,16 @@ public interface Client extends FrameworkInterface { public SelectableMenuItem getMenuItem (final char accessKey, final String text); /** - * Determines whether the application is still active by checking some + * Determines whether the client is still active by checking some * conditions * - * @return Whether the application is still active + * @return Whether the client is still active */ - public boolean isApplicationRunning (); + public boolean isRunning (); /** * Shows given menu entry in client + * * @param item Menu item to show */ public void showEntry (final SelectableMenuItem item); diff --git a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java index 841b7b6..160eab3 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java +++ b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java @@ -324,37 +324,6 @@ public class ConsoleClient extends BaseClient implements Client { } } - /** - * Fills menu map with menu entries - */ - private void fillConsoleMenuMap () { - // Initialize first (main) menu - Menu menu = new ConsoleMenu("main", this); - - // Add it - this.menus.put("main", menu); - } - - /** - * "Getter" for given menu type - * - * @param menuType Menu type instance to return - * @return Menu or null if not found - */ - private Menu getMenu (final String menuType) { - // Default is not found - Menu menu = null; - - // Check array - if (this.menus.containsKey(menuType)) { - // Found! - menu = this.menus.get(menuType); - } - - // Return it - return menu; - } - /** * Reads one character * @@ -408,21 +377,34 @@ public class ConsoleClient extends BaseClient implements Client { } /** - * Shows given menu + * Fills menu map with menu entries + */ + protected void fillConsoleMenuMap () { + // Initialize first (main) menu + Menu menu = new ConsoleMenu("main", this); + + // Add it + this.menus.put("main", menu); + } + + /** + * "Getter" for given menu type * - * @param menuType Given menu to show + * @param menuType Menu type instance to return + * @return Menu or null if not found */ - private void showMenu (final String menuType) { - Menu menu = this.getMenu(menuType); + @Override + public Menu getMenu (final String menuType) { + // Default is not found + Menu menu = null; - // Is the menu set? - if (!(menu instanceof Menu)) { - // Not found - // @todo Own exception? - throw new NullPointerException("Menu '" + menuType + "' not found."); + // Check array + if (this.menus.containsKey(menuType)) { + // Found! + menu = this.menus.get(menuType); } - // Show menu - menu.show(this); + // Return it + return menu; } } -- 2.39.5