*/\r
package org.mxchange.addressbook.application;\r
\r
+import java.text.MessageFormat;\r
import org.mxchange.addressbook.BaseFrameworkSystem;\r
+import org.mxchange.addressbook.UnhandledUserChoiceException;\r
import org.mxchange.addressbook.client.Client;\r
import org.mxchange.addressbook.client.console.ConsoleClient;\r
+import org.mxchange.addressbook.client.gui.SwingClient;\r
import org.mxchange.addressbook.manager.application.ApplicationManager;\r
\r
/**\r
/**\r
* Application title\r
*/\r
- public static final String APP_TITLE = "Addressbuch";\r
+ public static final String APP_TITLE = "Adressbuch";\r
\r
/**\r
* Application version\r
public static final String APP_VERSION = "0.0";\r
\r
/**\r
- * Main method (entry point)\r
- *\r
- * @param args the command line arguments\r
+ * Console client is enabled by default\r
*/\r
- public static void main (String[] args) {\r
- // Start application\r
- new AddressbookApplication().start ();\r
- }\r
+ private boolean consoleClient = true;\r
+\r
+ /**\r
+ * GUI client is disabled by default\r
+ */\r
+ private boolean guiClient = false;\r
\r
/**\r
* Bootstraps application\r
@Override\r
public void doBootstrap () {\r
this.getLogger().debug("Initializing application ...");\r
- \r
- // Init client instance\r
- Client client = new ConsoleClient(this);\r
\r
- // Init client instance\r
+ // Init client variable\r
+ Client client = null;\r
+\r
+ // Is console or Swing choosen?\r
+ if (this.isConsole()) {\r
+ // Debug message\r
+ this.getLogger().debug("Initializing console client ...");\r
+\r
+ // Init console client instance\r
+ client = new ConsoleClient(this);\r
+ } else if (this.isGui()) {\r
+ // Debug message\r
+ this.getLogger().debug("Initializing GUI (Swing) client ...");\r
+\r
+ // Init console instance\r
+ client = new SwingClient(this);\r
+ } else {\r
+ // Not client choosen\r
+ this.getLogger().error("No client choosen. Cannot launch.");\r
+ System.exit(1);\r
+ }\r
+\r
+ // Init client\r
+ client.initClient();\r
+\r
+ // Set client instance\r
this.setClient(client);\r
\r
// The application is running at this point\r
this.getClient().setCurrentMenu("main");\r
\r
// --- Main loop starts here ---\r
- while (this.getClient().isApplicationRunning()) {\r
+ while (this.getClient().isRunning()) {\r
// The application is still active, show menu selection\r
this.getClient().showCurrentMenu();\r
\r
try {\r
// Ask for user input and run proper method\r
- this.getClient().doUserChoice();\r
- } catch (final Exception ex) {\r
+ this.getClient().doUserMenuChoice();\r
+ } catch (final UnhandledUserChoiceException ex) {\r
this.getLogger().catching(ex);\r
}\r
}\r
this.getLogger().debug("Main loop exit - shutting down ...");\r
}\r
\r
+ /**\r
+ * Enables console client by setting propper flag\r
+ */\r
+ private void enableConsoleClient () {\r
+ this.getLogger().debug("Enabling console client (may become optional client) ...");\r
+ this.consoleClient = true;\r
+ this.guiClient = false;\r
+ }\r
+\r
+ /**\r
+ * Enables GUI client by setting propper flag\r
+ */\r
+ private void enableGuiClient () {\r
+ this.getLogger().debug("Enabling GUI client (may become new default client) ...");\r
+ this.consoleClient = false;\r
+ this.guiClient = true;\r
+ }\r
+\r
+ /**\r
+ * Checks whether the client shoule be console client should be launched by\r
+ * checking if -console is set.\r
+ * \r
+ * @return Whether console client should be taken\r
+ */\r
+ private boolean isConsole () {\r
+ return this.consoleClient;\r
+ }\r
+\r
+ /**\r
+ * Checks whether the client shoule be GUI client should be launched by\r
+ * checking if -gui is set.\r
+ * \r
+ * @return Whether GUI client should be taken\r
+ */\r
+ private boolean isGui () {\r
+ return this.guiClient;\r
+ }\r
+\r
+ /**\r
+ * Parses all given arguments\r
+ *\r
+ * @param args Arguments from program launch\r
+ */\r
+ private void parseArguments (final String[] args) {\r
+ // Debug message\r
+ this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length));\r
+ \r
+ for (final String arg : args) {\r
+ // Switch on it\r
+ switch (arg) {\r
+ case "-console":\r
+ enableConsoleClient();\r
+ break;\r
+ \r
+ case "-gui":\r
+ enableGuiClient();\r
+ break;\r
+}\r
+ }\r
+ }\r
+\r
/**\r
* Show introduction which depends on client\r
*/\r
\r
/**\r
* Launches the application\r
+ * \r
+ * @param args Arguments handled to program\r
*/\r
- private void start () {\r
+ private void start (final String args[]) {\r
this.getLogger().info("Program is started.");\r
\r
+ // Parse arguments\r
+ this.parseArguments(args);\r
+\r
// Launch application\r
- ApplicationManager.getManager(this).start ();\r
+ ApplicationManager.getManager(this).start();\r
\r
this.getLogger().info("End of program (last line)");\r
}\r
\r
+ /**\r
+ * Main method (entry point)\r
+ *\r
+ * @param args the command line arguments\r
+ */\r
+ public static void main (String[] args) {\r
+ // Start application\r
+ new AddressbookApplication().start(args);\r
+ }\r
}\r