X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=Addressbook%2Fsrc%2Forg%2Fmxchange%2Faddressbook%2Fapplication%2FAddressbookApplication.java;h=788c940ed3c41eb082ab699fcda3f03383381a24;hb=e48af74ac9a6c205711d04b4cf9bd9086546e8e9;hp=00baae06f58dbe960b474bee216e58eefd12e4d3;hpb=6aee766f085ca3c6da8191b0f3d371585fbc6cdc;p=addressbook-lib.git diff --git a/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java b/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java index 00baae0..788c940 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,13 +127,22 @@ 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; + + /** + * Getter for printable application name + * + * @return A printable name + */ + public static String printableTitle () { + return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); } /** @@ -140,11 +151,33 @@ 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); + } + + // Init client + client.initClient(); + + // Set client instance this.setClient(client); // The application is running at this point @@ -182,6 +215,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 +286,42 @@ 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 (); + ApplicationManager.getManager(this).start(); - this.getLogger().info("End of program (last line)"); + // Good bye, but this should not be reached ... + this.getLogger().warn("Unusual exit reached."); + this.doShutdown(); } + /** + * Main method (entry point) + * + * @param args the command line arguments + */ + public static void main (String[] args) { + // Start application + new AddressbookApplication().start(args); + } + + /** + * Shuts down the application. + */ + @Override + public void doShutdown () { + // Shutdown client + this.getClient().doShutdown(); + + this.getLogger().info("End of program (last line)"); + System.exit(0); + } }