2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook.application;
19 import java.io.IOException;
20 import java.sql.SQLException;
21 import java.text.MessageFormat;
22 import org.mxchange.addressbook.client.AddressbookClient;
23 import org.mxchange.addressbook.client.console.ConsoleClient;
24 import org.mxchange.addressbook.client.gui.SwingClient;
25 import org.mxchange.jcore.application.Application;
26 import org.mxchange.jcore.application.BaseApplication;
27 import org.mxchange.jcore.client.Client;
28 import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
29 import org.mxchange.jcore.manager.application.ApplicationManager;
30 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
33 * Address book application class. Please see ROADMAP.txt for details.
35 * @author Roland Haeder
38 public class AddressbookApplication extends BaseApplication implements Application {
43 public static final String APP_TITLE = "Adressbuch"; //NOI18N
48 public static final String APP_VERSION = "0.0"; //NOI18N
51 * Console client is enabled by default
53 private boolean consoleClient = true;
56 * GUI client is disabled by default
58 private boolean guiClient = false;
63 private LoggerBeanLocal logger;
66 * Protected constructor
68 protected AddressbookApplication () {
69 // Init properties file
70 this.initProperties();
72 // Is the bundle initialized?
73 if (!isBundledInitialized()) {
74 // Temporary initialize default bundle
75 // TODO The enum Gender uses this
81 * Bootstraps application
84 public void doBootstrap () {
85 this.getLogger().logDebug("Initializing application ..."); //NOI18N
87 // Init client variable
90 // Is console or Swing choosen?
91 if (this.isConsole()) {
93 this.getLogger().logDebug("Initializing console client ..."); //NOI18N
95 // Init console client instance
96 client = new ConsoleClient(this);
97 } else if (this.isGui()) {
99 this.getLogger().logDebug("Initializing GUI (Swing) client ..."); //NOI18N
101 // Init console instance
102 client = new SwingClient(this);
104 // Not client choosen
105 this.getLogger().logError("No client choosen. Cannot launch."); //NOI18N
109 } catch (final SQLException | IOException ex) {
111 this.abortProgramWithException(ex);
121 // Set client instance
122 this.setClient(client);
124 // The application is running at this point
125 this.getClient().enableIsRunning();
128 this.getLogger().logTrace("EXIT!"); //NOI18N
132 * Main loop of the application
135 public void doMainLoop () {
136 // Get client and cast it
137 AddressbookClient client = (AddressbookClient) this.getClient();
140 this.getLogger().logTrace("CALLED!"); //NOI18N
142 // TODO The application should be running now
143 // Output introduction
146 // Set current menu to main
147 client.setCurrentMenu("main"); //NOI18N
149 // --- Main loop starts here ---
150 while (this.getClient().isRunning()) {
151 // The application is still active, show menu selection
152 client.showCurrentMenu();
155 // Ask for user input and run proper method
156 client.doUserMenuChoice();
157 } catch (final UnhandledUserChoiceException ex) {
158 this.getLogger().logException(ex);
162 // Sleep a little to reduce system load
164 } catch (final InterruptedException ex) {
168 // --- Main loop ends here ---
171 this.getLogger().logDebug("Main loop exit - shutting down ..."); //NOI18N
175 * Shuts down the application.
178 public void doShutdown () throws SQLException, IOException {
180 this.getLogger().logTrace("CALLED!"); //NOI18N
183 this.getClient().doShutdown();
185 this.getLogger().logInfo("End of program (last line)"); //NOI18N
190 * Logs given exception
192 * @param exception Throwable
194 protected void logException (final Throwable exception) {
195 this.getLogger().logException(exception);
199 * Initializes properties
201 private void initProperties () throws IOException {
202 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
206 * Log exception and abort program.
208 * @param throwable Throwable
210 protected void abortProgramWithException (final Throwable throwable) {
212 this.logException(throwable);
219 * Enables console client by setting propper flag
221 private void enableConsoleClient () {
222 this.getLogger().logDebug("Enabling console client (may become optional client) ..."); //NOI18N
223 this.consoleClient = true;
224 this.guiClient = false;
228 * Enables GUI client by setting propper flag
230 private void enableGuiClient () {
231 this.getLogger().logDebug("Enabling GUI client (may become new default client) ..."); //NOI18N
232 this.consoleClient = false;
233 this.guiClient = true;
237 * Checks whether the client shoule be console client should be launched by
238 * checking if -console is set.
240 * @return Whether console client should be taken
242 private boolean isConsole () {
243 return this.consoleClient;
247 * Checks whether the client shoule be GUI client should be launched by
248 * checking if -gui is set.
250 * @return Whether GUI client should be taken
252 private boolean isGui () {
253 return this.guiClient;
257 * Parses all given arguments
259 * @param args Arguments from program launch
261 private void parseArguments (final String[] args) {
263 this.getLogger().logTrace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
266 this.getLogger().logDebug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
268 for (final String arg : args) {
271 case "-console": //NOI18N
272 enableConsoleClient();
275 case "-gui": //NOI18N
283 * Show introduction which depends on client
285 private void showIntro () {
287 this.getLogger().logTrace("CALLED!"); //NOI18N
289 // Let the client show it
290 this.getClient().showWelcome();
293 this.getLogger().logTrace("EXIT!"); //NOI18N
297 * Launches the application
299 * @param args Arguments handled to program
301 private void start (final String args[]) {
302 this.getLogger().logInfo("Program is started."); //NOI18N
305 // Init properties file
306 this.initProperties();
307 } catch (final IOException ex) {
308 // Something bad happened
309 this.abortProgramWithException(ex);
313 this.parseArguments(args);
315 // Launch application
316 ApplicationManager.getSingeltonManager(this).start();
318 // Good bye, but this should not be reached ...
319 this.getLogger().logWarning("Unusual exit reached."); //NOI18N
323 } catch (final SQLException | IOException ex) {
324 // Something bad happened
325 this.abortProgramWithException(ex);
330 * Main method (entry point)
332 * @param args the command line arguments
334 public static void main (String[] args) {
336 new AddressbookApplication().start(args);
340 * Getter for printable application name
342 * @return A printable name
344 public static String printableTitle () {
345 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
349 * Getter for logger instance
351 * @return Logger instance
353 protected LoggerBeanLocal getLogger () {