From d16c6288f14c09b779d9d8f395529d411cdd156e Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Tue, 21 Jul 2015 13:22:44 +0200 Subject: [PATCH] =?utf8?q?Introduced=20a=20lot=20Swing=20stuff=20+=20moved?= =?utf8?q?=20some=20attributes=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 | 122 ++++++++++++++-- .../addressbook/client/BaseClient.java | 43 +++++- .../client/console/ConsoleClient.java | 59 +++----- .../addressbook/client/gui/SwingClient.java | 136 ++++++++++++++++++ .../manager/contact/ContactManager.java | 10 ++ .../manager/contact/ManageableContact.java | 11 ++ .../menu/{console => }/BaseMenu.java | 15 +- .../addressbook/menu/console/ConsoleMenu.java | 5 + 8 files changed, 341 insertions(+), 60 deletions(-) create mode 100644 Addressbook/src/org/mxchange/addressbook/client/gui/SwingClient.java rename Addressbook/src/org/mxchange/addressbook/menu/{console => }/BaseMenu.java (87%) diff --git a/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java b/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java index 2d3237a..fcda1bc 100644 --- a/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java +++ b/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java @@ -16,10 +16,12 @@ */ package org.mxchange.addressbook.application; +import java.text.MessageFormat; 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.client.gui.SwingClient; import org.mxchange.addressbook.manager.application.ApplicationManager; /** @@ -117,7 +119,7 @@ public class AddressbookApplication extends BaseFrameworkSystem implements Appli /** * Application title */ - public static final String APP_TITLE = "Addressbuch"; + public static final String APP_TITLE = "Adressbuch"; /** * Application version @@ -125,14 +127,14 @@ public class AddressbookApplication extends BaseFrameworkSystem implements Appli public static final String APP_VERSION = "0.0"; /** - * Main method (entry point) - * - * @param args the command line arguments + * Console client is enabled by default */ - public static void main (String[] args) { - // Start application - new AddressbookApplication().start(); - } + private boolean consoleClient = true; + + /** + * GUI client is disabled by default + */ + private boolean guiClient = false; /** * Bootstraps application @@ -140,11 +142,30 @@ public class AddressbookApplication extends BaseFrameworkSystem implements Appli @Override public void doBootstrap () { this.getLogger().debug("Initializing application ..."); - - // Init client instance - Client client = new ConsoleClient(this); - // Init client instance + // Init client variable + Client client = null; + + // Is console or Swing choosen? + if (this.isConsole()) { + // Debug message + this.getLogger().debug("Initializing console client ..."); + + // Init console client instance + client = new ConsoleClient(this); + } else if (this.isGui()) { + // Debug message + this.getLogger().debug("Initializing GUI (Swing) client ..."); + + // Init console instance + client = new SwingClient(this); + } else { + // Not client choosen + this.getLogger().error("No client choosen. Cannot launch."); + System.exit(1); + } + + // Set client instance this.setClient(client); // The application is running at this point @@ -182,6 +203,67 @@ public class AddressbookApplication extends BaseFrameworkSystem implements Appli this.getLogger().debug("Main loop exit - shutting down ..."); } + /** + * Enables console client by setting propper flag + */ + private void enableConsoleClient () { + this.getLogger().debug("Enabling console client (may become optional client) ..."); + this.consoleClient = true; + this.guiClient = false; + } + + /** + * Enables GUI client by setting propper flag + */ + private void enableGuiClient () { + this.getLogger().debug("Enabling GUI client (may become new default client) ..."); + this.consoleClient = false; + this.guiClient = true; + } + + /** + * Checks whether the client shoule be console client should be launched by + * checking if -console is set. + * + * @return Whether console client should be taken + */ + private boolean isConsole () { + return this.consoleClient; + } + + /** + * Checks whether the client shoule be GUI client should be launched by + * checking if -gui is set. + * + * @return Whether GUI client should be taken + */ + private boolean isGui () { + return this.guiClient; + } + + /** + * Parses all given arguments + * + * @param args Arguments from program launch + */ + private void parseArguments (final String[] args) { + // Debug message + this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length)); + + for (final String arg : args) { + // Switch on it + switch (arg) { + case "-console": + enableConsoleClient(); + break; + + case "-gui": + enableGuiClient(); + break; +} + } + } + /** * Show introduction which depends on client */ @@ -192,14 +274,28 @@ public class AddressbookApplication extends BaseFrameworkSystem implements Appli /** * Launches the application + * + * @param args Arguments handled to program */ - private void start () { + private void start (final String args[]) { this.getLogger().info("Program is started."); + // Parse arguments + this.parseArguments(args); + // Launch application ApplicationManager.getManager(this).start(); this.getLogger().info("End of program (last line)"); } + /** + * Main method (entry point) + * + * @param args the command line arguments + */ + public static void main (String[] args) { + // Start application + new AddressbookApplication().start(args); + } } diff --git a/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java b/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java index 706229c..399ddb4 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java +++ b/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java @@ -16,6 +16,8 @@ */ package org.mxchange.addressbook.client; +import java.util.HashMap; +import java.util.Map; import org.mxchange.addressbook.BaseFrameworkSystem; import org.mxchange.addressbook.menu.Menu; @@ -36,11 +38,19 @@ public abstract class BaseClient extends BaseFrameworkSystem { */ private boolean isRunning; + /** + * Menu system + */ + private final Map menus; + /** * No instances can be created of this class */ protected BaseClient () { super(); + + // Init menu map + this.menus = new HashMap<>(10); } /** @@ -75,12 +85,24 @@ public abstract class BaseClient extends BaseFrameworkSystem { } /** - * Some kind of "getter" for a Menu instance from given menu type + * "Getter" for given menu type * - * @param menuType Menu type, e.g. "main" for main menu - * @return + * @param menuType Menu type instance to return + * @return Menu or null if not found */ - public abstract Menu getMenu (final String menuType); + public Menu getMenu (final String menuType) { + // Default is not found + Menu menu = null; + + // Check array + if (this.getMenus().containsKey(menuType)) { + // Found! + menu = this.getMenus().get(menuType); + } + + // Return it + return menu; + } /** * Determines whether the application is still active by checking some @@ -93,6 +115,19 @@ public abstract class BaseClient extends BaseFrameworkSystem { return this.isRunning; } + /** + * Fills menu map with swing menus + */ + protected abstract void fillMenuMap (); + + /** + * Getter for menus map + * @return Map of all menus + */ + protected final Map getMenus () { + return this.menus; + } + /** * Shows given menu * diff --git a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java index 160eab3..5249c5f 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java +++ b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java @@ -18,8 +18,6 @@ package org.mxchange.addressbook.client.console; import java.text.MessageFormat; import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; import java.util.Scanner; import org.mxchange.addressbook.UnhandledUserChoiceException; import org.mxchange.addressbook.application.AddressbookApplication; @@ -39,11 +37,6 @@ import org.mxchange.addressbook.menu.item.console.ConsoleMenuItem; * @author Roland Haeder */ public class ConsoleClient extends BaseClient implements Client { - /** - * Menu system - */ - private final Map menus; - /** * Scanner instance for reading data from console input */ @@ -65,11 +58,8 @@ public class ConsoleClient extends BaseClient implements Client { // Init scanner instance this.scanner = new Scanner(System.in); - // Init menu map - this.menus = new HashMap<>(10); - // Fill menu map - this.fillConsoleMenuMap(); + this.fillMenuMap(); } /** @@ -123,7 +113,7 @@ public class ConsoleClient extends BaseClient implements Client { @Override public void doUserMenuChoice () throws UnhandledUserChoiceException { // Get all access keys from menu - char[] accessKeys = MenuTools.getAccessKeysFromMenuMap(this.menus, this.getCurrentMenu()); + char[] accessKeys = MenuTools.getAccessKeysFromMenuMap(this.getMenus(), this.getCurrentMenu()); // Output textural message and ask for a char as input char choice = this.enterChar(accessKeys, "Bitte Auswahl eingeben (0=Programm beenden): "); @@ -142,11 +132,19 @@ public class ConsoleClient extends BaseClient implements Client { this.getContactManager().addOtherAddress(); break; - case '4': // Change other addess + case '4': // List contacts + this.getContactManager().listContacts(); + break; + + case '5': // Search addresses + this.getContactManager().searchContacts(); + break; + + case '6': // Change other addess this.getContactManager().changeOtherAddress(); break; - case '5': // Delete other address + case '7': // Delete other address this.getContactManager().deleteOtherAddress(); break; @@ -276,6 +274,11 @@ public class ConsoleClient extends BaseClient implements Client { this.showMenu(this.getCurrentMenu()); } + /** + * Shows given menu entry to user + * + * @param item Menu entry + */ @Override public void showEntry (final SelectableMenuItem item) { // Access key then text @@ -293,7 +296,7 @@ public class ConsoleClient extends BaseClient implements Client { // Debug message this.getLogger().debug("Intro shown to user"); - } + } @Override public void userChooseChangeContactData (final Contact contact) throws UnhandledUserChoiceException { @@ -379,32 +382,12 @@ public class ConsoleClient extends BaseClient implements Client { /** * Fills menu map with menu entries */ - protected void fillConsoleMenuMap () { + @Override + protected final void fillMenuMap () { // 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 - */ - @Override - public 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; + this.getMenus().put("main", menu); } } diff --git a/Addressbook/src/org/mxchange/addressbook/client/gui/SwingClient.java b/Addressbook/src/org/mxchange/addressbook/client/gui/SwingClient.java new file mode 100644 index 0000000..83417e1 --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/client/gui/SwingClient.java @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.client.gui; + +import org.mxchange.addressbook.UnhandledUserChoiceException; +import org.mxchange.addressbook.application.AddressbookApplication; +import org.mxchange.addressbook.client.BaseClient; +import org.mxchange.addressbook.client.Client; +import org.mxchange.addressbook.contact.Contact; +import org.mxchange.addressbook.menu.Menu; +import org.mxchange.addressbook.menu.item.SelectableMenuItem; + +/** + * + * @author Roland Haeder + */ +public class SwingClient extends BaseClient implements Client { + /** + * Constructor with application instance + * @param application + */ + public SwingClient (final AddressbookApplication application) { + super(); + + // Set application instance + this.setApplication(application); + + // Init contact manager here + this.initContactManager(this); + + // Fill menu map + this.fillMenuMap(); + } + + @Override + public void displayAddressBox (final Contact contact) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void displayNameBox (final Contact contact) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void displayOtherDataBox (final Contact contact) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void doUserMenuChoice () throws UnhandledUserChoiceException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public char enterChar (final char[] validChars, String message) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public int enterInt (final int minimum, final int maximum, final String message) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String enterString (final int minLength, final int maxLength, final String message, final boolean allowEmpty) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Menu getMenu (final String menuType) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + /** + * Returns a Swing menu item + * + * @param accessKey Key to access the menu + * @param text Text to show to user + * @return A SelectableMenuItem + * @todo Make sure the access key is unique + */ + @Override + public SelectableMenuItem getMenuItem (final char accessKey, final String text) { + // Returns null as the menu is now no longer controlled here. + return null; + } + + @Override + public void outputMessage (final String message) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void showCurrentMenu () { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void showEntry (final SelectableMenuItem item) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void showWelcome () { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void userChooseChangeContactData (final Contact contact) throws UnhandledUserChoiceException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + /** + * Fills menu map with swing menus + */ + @Override + protected final void fillMenuMap () { + // Nothing to fill here as the Swing frame is handling this all + } + +} diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java index 419436d..0499011 100644 --- a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java @@ -289,6 +289,11 @@ public class ContactManager extends BaseManager implements ManageableContact { return Collections.unmodifiableList(this.contacts); } + @Override + public void listContacts () { + throw new UnsupportedOperationException("Not supported yet."); + } + /** * Adds given contact to address book and flushes all entries to database * @@ -316,6 +321,11 @@ public class ContactManager extends BaseManager implements ManageableContact { this.flush(); } + @Override + public void searchContacts () { + throw new UnsupportedOperationException("Not supported yet."); + } + /** * Getter for size * diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java index 94d4d55..a928505 100644 --- a/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java @@ -26,6 +26,12 @@ import org.mxchange.addressbook.manager.Manageable; * @author Roland Haeder */ public interface ManageableContact extends Manageable { + + /** + * List all contacts + */ + public void listContacts (); + /** * Adds given contact to address book * @@ -98,6 +104,11 @@ public interface ManageableContact extends Manageable { */ public List getList (); + /** + * Searches address book for a contact + */ + public void searchContacts (); + /** * Getter for size * diff --git a/Addressbook/src/org/mxchange/addressbook/menu/console/BaseMenu.java b/Addressbook/src/org/mxchange/addressbook/menu/BaseMenu.java similarity index 87% rename from Addressbook/src/org/mxchange/addressbook/menu/console/BaseMenu.java rename to Addressbook/src/org/mxchange/addressbook/menu/BaseMenu.java index afa83c3..d81c071 100644 --- a/Addressbook/src/org/mxchange/addressbook/menu/console/BaseMenu.java +++ b/Addressbook/src/org/mxchange/addressbook/menu/BaseMenu.java @@ -14,14 +14,13 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package org.mxchange.addressbook.menu.console; +package org.mxchange.addressbook.menu; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.mxchange.addressbook.BaseFrameworkSystem; import org.mxchange.addressbook.client.Client; -import org.mxchange.addressbook.menu.AddressbookMenu; import org.mxchange.addressbook.menu.item.SelectableMenuItem; /** @@ -80,6 +79,15 @@ public class BaseMenu extends BaseFrameworkSystem { } } + /** + * Getter for menu list + * + * @return menuList List of menu entries + */ + protected final List getMenuList () { + return this.menuList; + } + /** * Initializes menu * @param menuType Menu type to initialize @@ -88,8 +96,5 @@ public class BaseMenu extends BaseFrameworkSystem { protected void initMenu (final String menuType, final Client client) { // Init menu list this.menuList = new ArrayList<>(5); - - // Add all items - AddressbookMenu.addItemsToList(this.menuList, menuType, client); } } diff --git a/Addressbook/src/org/mxchange/addressbook/menu/console/ConsoleMenu.java b/Addressbook/src/org/mxchange/addressbook/menu/console/ConsoleMenu.java index 82d9f6b..cefa2c5 100644 --- a/Addressbook/src/org/mxchange/addressbook/menu/console/ConsoleMenu.java +++ b/Addressbook/src/org/mxchange/addressbook/menu/console/ConsoleMenu.java @@ -17,6 +17,8 @@ package org.mxchange.addressbook.menu.console; import org.mxchange.addressbook.client.Client; +import org.mxchange.addressbook.menu.AddressbookMenu; +import org.mxchange.addressbook.menu.BaseMenu; import org.mxchange.addressbook.menu.Menu; /** @@ -31,5 +33,8 @@ public class ConsoleMenu extends BaseMenu implements Menu { */ public ConsoleMenu (final String menuType, final Client client) { this.initMenu(menuType, client); + + // Add all items + AddressbookMenu.addItemsToList(this.getMenuList(), menuType, client); } } -- 2.39.5