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.BaseFrameworkSystem;
26 import org.mxchange.jcore.application.Application;
27 import org.mxchange.jcore.application.BaseApplication;
28 import org.mxchange.jcore.client.Client;
29 import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
30 import org.mxchange.jcore.manager.application.ApplicationManager;
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;
61 * Protected constructor
63 * @throws java.io.IOException If any IO error occurs
65 protected AddressbookApplication () throws IOException {
66 // Init properties file
67 this.initProperties();
69 // Is the bundle initialized?
70 if (!isBundledInitialized()) {
71 // Temporary initialize default bundle
72 // TODO The enum Gender uses this
78 * Bootstraps application
81 public void doBootstrap () {
82 this.getLogger().debug("Initializing application ..."); //NOI18N
84 // Init client variable
87 // Is console or Swing choosen?
88 if (this.isConsole()) {
90 this.getLogger().debug("Initializing console client ..."); //NOI18N
92 // Init console client instance
93 client = new ConsoleClient(this);
94 } else if (this.isGui()) {
96 this.getLogger().debug("Initializing GUI (Swing) client ..."); //NOI18N
98 // Init console instance
99 client = new SwingClient(this);
101 // Not client choosen
102 this.getLogger().error("No client choosen. Cannot launch."); //NOI18N
106 } catch (final SQLException | IOException ex) {
108 this.abortProgramWithException(ex);
118 // Set client instance
119 this.setClient(client);
121 // The application is running at this point
122 this.getClient().enableIsRunning();
125 this.getLogger().trace("EXIT!"); //NOI18N
129 * Main loop of the application
132 public void doMainLoop () {
133 // Get client and cast it
134 AddressbookClient client = (AddressbookClient) this.getClient();
137 this.getLogger().trace("CALLED!"); //NOI18N
139 // TODO The application should be running now
140 // Output introduction
143 // Set current menu to main
144 client.setCurrentMenu("main"); //NOI18N
146 // --- Main loop starts here ---
147 while (this.getClient().isRunning()) {
148 // The application is still active, show menu selection
149 client.showCurrentMenu();
152 // Ask for user input and run proper method
153 client.doUserMenuChoice();
154 } catch (final UnhandledUserChoiceException ex) {
155 this.getLogger().catching(ex);
159 // Sleep a little to reduce system load
161 } catch (final InterruptedException ex) {
165 // --- Main loop ends here ---
168 this.getLogger().debug("Main loop exit - shutting down ..."); //NOI18N
172 * Shuts down the application.
175 public void doShutdown () throws SQLException, IOException {
177 this.getLogger().trace("CALLED!"); //NOI18N
180 this.getClient().doShutdown();
182 this.getLogger().info("End of program (last line)"); //NOI18N
187 * Enables console client by setting propper flag
189 private void enableConsoleClient () {
190 this.getLogger().debug("Enabling console client (may become optional client) ..."); //NOI18N
191 this.consoleClient = true;
192 this.guiClient = false;
196 * Enables GUI client by setting propper flag
198 private void enableGuiClient () {
199 this.getLogger().debug("Enabling GUI client (may become new default client) ..."); //NOI18N
200 this.consoleClient = false;
201 this.guiClient = true;
205 * Checks whether the client shoule be console client should be launched by
206 * checking if -console is set.
208 * @return Whether console client should be taken
210 private boolean isConsole () {
211 return this.consoleClient;
215 * Checks whether the client shoule be GUI client should be launched by
216 * checking if -gui is set.
218 * @return Whether GUI client should be taken
220 private boolean isGui () {
221 return this.guiClient;
225 * Parses all given arguments
227 * @param args Arguments from program launch
229 private void parseArguments (final String[] args) {
231 this.getLogger().trace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
234 this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
236 for (final String arg : args) {
239 case "-console": //NOI18N
240 enableConsoleClient();
243 case "-gui": //NOI18N
251 * Show introduction which depends on client
253 private void showIntro () {
255 this.getLogger().trace("CALLED!"); //NOI18N
257 // Let the client show it
258 this.getClient().showWelcome();
261 this.getLogger().trace("EXIT!"); //NOI18N
265 * Launches the application
267 * @param args Arguments handled to program
269 private void start (final String args[]) {
270 this.getLogger().info("Program is started."); //NOI18N
272 // Init properties file
273 this.initProperties();
274 } catch (final IOException ex) {
275 // Something bad happened
276 this.abortProgramWithException(ex);
280 this.parseArguments(args);
282 // Launch application
283 ApplicationManager.getSingeltonManager(this).start();
285 // Good bye, but this should not be reached ...
286 this.getLogger().warn("Unusual exit reached."); //NOI18N
289 } catch (final SQLException | IOException ex) {
290 this.abortProgramWithException(ex);
295 * Main method (entry point)
297 * @param args the command line arguments
299 public static void main (String[] args) {
302 new AddressbookApplication().start(args);
303 } catch (final IOException ex) {
305 BaseFrameworkSystem.getInstance().getLogger().catching(ex);
311 * Getter for printable application name
313 * @return A printable name
315 public static String printableTitle () {
316 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N