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.exceptions.UnhandledUserChoiceException;
import org.mxchange.addressbook.manager.application.ApplicationManager;
/**
* 2.2 (social):
* + 3 days
* + Additional tables: ???
- *
+*
* @author Roland Haeder
* @version 0.0
- * @since 0.0
*/
public class AddressbookApplication extends BaseFrameworkSystem implements Application {
- /**
- * Application title
- */
- public static final String APP_TITLE = "Adressbuch";
-
- /**
- * Application version
- */
- public static final String APP_VERSION = "0.0";
-
- /**
- * Console client is enabled by default
- */
- 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);
- }
-
- /**
- * Bootstraps application
- */
- @Override
- public void doBootstrap () {
- this.getLogger().debug("Initializing application ...");
-
- // 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);
+
+ /**
+ * Application title
+ */
+ public static final String APP_TITLE = "Adressbuch"; //NOI18N
+
+ /**
+ * Application version
+ */
+ public static final String APP_VERSION = "0.0"; //NOI18N
+
+ /**
+ * Self instance
+ */
+ private static Application selfInstance;
+
+ /**
+ * Console client is enabled by default
+ */
+ private boolean consoleClient = true;
+
+ /**
+ * GUI client is disabled by default
+ */
+ private boolean guiClient = false;
+
+ /**
+ * Protected constructor
+ */
+ protected AddressbookApplication () {
+ // Set own instance
+ selfInstance = this;
}
- // Init client
- client.initClient();
-
- // Set client instance
- this.setClient(client);
-
- // The application is running at this point
- this.getClient().enableIsRunning();
- }
-
- /**
- * Main loop of the application
- */
- @Override
- public void doMainLoop () {
- // Debug message
- this.getLogger().trace("CALLED!");
-
- // @TODO The application should be running now
-
- // Output introduction
- this.showIntro();
-
- // Set current menu to main
- this.getClient().setCurrentMenu("main");
-
- // --- Main loop starts here ---
- 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 UnhandledUserChoiceException ex) {
- this.getLogger().catching(ex);
- }
-
- try {
- // Sleep a little to reduce system load
- Thread.sleep(100);
- } catch (final InterruptedException ex) {
- // Ignore it
- }
+ /**
+ * Getter for printable application name
+ *
+ * @return A printable name
+ */
+ public static String printableTitle () {
+ return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
}
- // --- Main loop ends here ---
-
- // Debug message
- 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;
-}
+
+ /**
+ * Bootstraps application
+ */
+ @Override
+ public void doBootstrap () {
+ this.getLogger().debug("Initializing application ..."); //NOI18N
+
+ // Init client variable
+ Client client = null;
+
+ // Is console or Swing choosen?
+ if (this.isConsole()) {
+ // Debug message
+ this.getLogger().debug("Initializing console client ..."); //NOI18N
+
+ // Init console client instance
+ client = new ConsoleClient(this);
+ } else if (this.isGui()) {
+ // Debug message
+ this.getLogger().debug("Initializing GUI (Swing) client ..."); //NOI18N
+
+ // Init console instance
+ client = new SwingClient(this);
+ } else {
+ // Not client choosen
+ this.getLogger().error("No client choosen. Cannot launch."); //NOI18N
+ System.exit(1);
+ }
+
+ // Init client
+ client.init();
+
+ // Set client instance
+ this.setClient(client);
+
+ // The application is running at this point
+ this.getClient().enableIsRunning();
+
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
+ }
+
+ /**
+ * Main loop of the application
+ */
+ @Override
+ public void doMainLoop () {
+ // Debug message
+ this.getLogger().trace("CALLED!"); //NOI18N
+
+ // @TODO The application should be running now
+ // Output introduction
+ this.showIntro();
+
+ // Set current menu to main
+ this.getClient().setCurrentMenu("main"); //NOI18N
+
+ // --- Main loop starts here ---
+ 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 UnhandledUserChoiceException ex) {
+ this.getLogger().catching(ex);
+ }
+
+ try {
+ // Sleep a little to reduce system load
+ Thread.sleep(100);
+ } catch (final InterruptedException ex) {
+ // Ignore it
+ }
+ }
+ // --- Main loop ends here ---
+
+ // Debug message
+ this.getLogger().debug("Main loop exit - shutting down ..."); //NOI18N
+ }
+
+ /**
+ * Enables console client by setting propper flag
+ */
+ private void enableConsoleClient () {
+ this.getLogger().debug("Enabling console client (may become optional client) ..."); //NOI18N
+ 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) ..."); //NOI18N
+ 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) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
+
+ for (final String arg : args) {
+ // Switch on it
+ switch (arg) {
+ case "-console": //NOI18N
+ enableConsoleClient();
+ break;
+
+ case "-gui": //NOI18N
+ enableGuiClient();
+ break;
+ }
+ }
+ }
+
+ /**
+ * Show introduction which depends on client
+ */
+ private void showIntro () {
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+
+ // Let the client show it
+ this.getClient().showWelcome();
+
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
+ }
+
+ /**
+ * Launches the application
+ *
+ * @param args Arguments handled to program
+ */
+ private void start (final String args[]) {
+ this.getLogger().info("Program is started."); //NOI18N
+
+ // Parse arguments
+ this.parseArguments(args);
+
+ // Launch application
+ ApplicationManager.getManager(this).start();
+
+ // Good bye, but this should not be reached ...
+ this.getLogger().warn("Unusual exit reached."); //NOI18N
+ 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 () {
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+
+ // Shutdown client
+ this.getClient().doShutdown();
+
+ this.getLogger().info("End of program (last line)"); //NOI18N
+ System.exit(0);
+ }
+
+ /**
+ * Getter for this application
+ *
+ * @return Instance from this application
+ */
+ public static final Application getInstance () {
+ // Return it
+ return selfInstance;
}
- }
-
- /**
- * Show introduction which depends on client
- */
- private void showIntro () {
- // Let the client show it
- this.getClient().showWelcome();
- }
-
- /**
- * Launches the application
- *
- * @param args Arguments handled to program
- */
- private void start (final String args[]) {
- this.getLogger().info("Program is started.");
-
- // Parse arguments
- this.parseArguments(args);
-
- // Launch application
- ApplicationManager.getManager(this).start();
-
- // 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);
- }
}