]> git.mxchange.org Git - addressbook-lib.git/blob - src/org/mxchange/addressbook/application/AddressbookApplication.java
Prepared for upcoming rewrite:
[addressbook-lib.git] / src / org / mxchange / addressbook / application / AddressbookApplication.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.application;
18
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;
31
32 /**
33  * Address book application class. Please see ROADMAP.txt for details.
34  * <p>
35  * @author Roland Haeder
36  * @version 0.0
37  */
38 public class AddressbookApplication extends BaseApplication implements Application {
39
40         /**
41          * Application title
42          */
43         public static final String APP_TITLE = "Adressbuch"; //NOI18N
44
45         /**
46          * Application version
47          */
48         public static final String APP_VERSION = "0.0"; //NOI18N
49
50         /**
51          * Console client is enabled by default
52          */
53         private boolean consoleClient = true;
54
55         /**
56          * GUI client is disabled by default
57          */
58         private boolean guiClient = false;
59
60         /**
61          * Logger instance
62          */
63         private LoggerBeanLocal logger;
64
65         /**
66          * Protected constructor
67          */
68         protected AddressbookApplication () {
69                 // Init properties file
70                 this.initProperties();
71
72                 // Is the bundle initialized?
73                 if (!isBundledInitialized()) {
74                         // Temporary initialize default bundle
75                         // TODO The enum Gender uses this
76                         this.initBundle();
77                 }
78         }
79
80         /**
81          * Bootstraps application
82          */
83         @Override
84         public void doBootstrap () {
85                 this.getLogger().logDebug("Initializing application ..."); //NOI18N
86
87                 // Init client variable
88                 Client client = null;
89
90                 // Is console or Swing choosen?
91                 if (this.isConsole()) {
92                         // Debug message
93                         this.getLogger().logDebug("Initializing console client ..."); //NOI18N
94
95                         // Init console client instance
96                         client = new ConsoleClient(this);
97                 } else if (this.isGui()) {
98                         // Debug message
99                         this.getLogger().logDebug("Initializing GUI (Swing) client ..."); //NOI18N
100
101                         // Init console instance
102                         client = new SwingClient(this);
103                 } else {
104                         // Not client choosen
105                         this.getLogger().logError("No client choosen. Cannot launch."); //NOI18N
106
107                         try {
108                                 this.doShutdown();
109                         } catch (final SQLException | IOException ex) {
110                                 // Abort here
111                                 this.abortProgramWithException(ex);
112                         }
113
114                         // Bye ...
115                         System.exit(1);
116                 }
117
118                 // Init client
119                 client.init();
120
121                 // Set client instance
122                 this.setClient(client);
123
124                 // The application is running at this point
125                 this.getClient().enableIsRunning();
126
127                 // Trace message
128                 this.getLogger().logTrace("EXIT!"); //NOI18N
129         }
130
131         /**
132          * Main loop of the application
133          */
134         @Override
135         public void doMainLoop () {
136                 // Get client and cast it
137                 AddressbookClient client = (AddressbookClient) this.getClient();
138
139                 // Debug message
140                 this.getLogger().logTrace("CALLED!"); //NOI18N
141
142                 // TODO The application should be running now
143                 // Output introduction
144                 this.showIntro();
145
146                 // Set current menu to main
147                 client.setCurrentMenu("main"); //NOI18N
148
149                 // --- Main loop starts here ---
150                 while (this.getClient().isRunning()) {
151                         // The application is still active, show menu selection
152                         client.showCurrentMenu();
153
154                         try {
155                                 // Ask for user input and run proper method
156                                 client.doUserMenuChoice();
157                         } catch (final UnhandledUserChoiceException ex) {
158                                 this.getLogger().logException(ex);
159                         }
160
161                         try {
162                                 // Sleep a little to reduce system load
163                                 Thread.sleep(100);
164                         } catch (final InterruptedException ex) {
165                                 // Ignore it
166                         }
167                 }
168                 // --- Main loop ends here ---
169
170                 // Debug message
171                 this.getLogger().logDebug("Main loop exit - shutting down ..."); //NOI18N
172         }
173
174         /**
175          * Shuts down the application.
176          */
177         @Override
178         public void doShutdown () throws SQLException, IOException {
179                 // Trace message
180                 this.getLogger().logTrace("CALLED!"); //NOI18N
181
182                 // Shutdown client
183                 this.getClient().doShutdown();
184
185                 this.getLogger().logInfo("End of program (last line)"); //NOI18N
186                 System.exit(0);
187         }
188
189         /**
190          * Logs given exception
191          *
192          * @param exception Throwable
193          */
194         protected void logException (final Throwable exception) {
195                 this.getLogger().logException(exception);
196         }
197
198         /**
199          * Initializes properties
200          */
201         private void initProperties () throws IOException {
202                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
203         }
204
205         /**
206          * Log exception and abort program.
207          *
208          * @param throwable Throwable
209          */
210         protected void abortProgramWithException (final Throwable throwable) {
211                 // Log exception
212                 this.logException(throwable);
213
214                 // Abort here
215                 System.exit(1);
216         }
217
218         /**
219          * Enables console client by setting propper flag
220          */
221         private void enableConsoleClient () {
222                 this.getLogger().logDebug("Enabling console client (may become optional client) ..."); //NOI18N
223                 this.consoleClient = true;
224                 this.guiClient = false;
225         }
226
227         /**
228          * Enables GUI client by setting propper flag
229          */
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;
234         }
235
236         /**
237          * Checks whether the client shoule be console client should be launched by
238          * checking if -console is set.
239          * <p>
240          * @return Whether console client should be taken
241          */
242         private boolean isConsole () {
243                 return this.consoleClient;
244         }
245
246         /**
247          * Checks whether the client shoule be GUI client should be launched by
248          * checking if -gui is set.
249          * <p>
250          * @return Whether GUI client should be taken
251          */
252         private boolean isGui () {
253                 return this.guiClient;
254         }
255
256         /**
257          * Parses all given arguments
258          * <p>
259          * @param args Arguments from program launch
260          */
261         private void parseArguments (final String[] args) {
262                 // Trace message
263                 this.getLogger().logTrace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
264
265                 // Debug message
266                 this.getLogger().logDebug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
267
268                 for (final String arg : args) {
269                         // Switch on it
270                         switch (arg) {
271                                 case "-console": //NOI18N
272                                         enableConsoleClient();
273                                         break;
274
275                                 case "-gui": //NOI18N
276                                         enableGuiClient();
277                                         break;
278                         }
279                 }
280         }
281
282         /**
283          * Show introduction which depends on client
284          */
285         private void showIntro () {
286                 // Trace message
287                 this.getLogger().logTrace("CALLED!"); //NOI18N
288
289                 // Let the client show it
290                 this.getClient().showWelcome();
291
292                 // Trace message
293                 this.getLogger().logTrace("EXIT!"); //NOI18N
294         }
295
296         /**
297          * Launches the application
298          * <p>
299          * @param args Arguments handled to program
300          */
301         private void start (final String args[]) {
302                 this.getLogger().logInfo("Program is started."); //NOI18N
303
304                 try {
305                         // Init properties file
306                         this.initProperties();
307                 } catch (final IOException ex) {
308                         // Something bad happened
309                         this.abortProgramWithException(ex);
310                 }
311
312                 // Parse arguments
313                 this.parseArguments(args);
314
315                 // Launch application
316                 ApplicationManager.getSingeltonManager(this).start();
317
318                 // Good bye, but this should not be reached ...
319                 this.getLogger().logWarning("Unusual exit reached."); //NOI18N
320
321                 try {
322                         this.doShutdown();
323                 } catch (final SQLException | IOException ex) {
324                         // Something bad happened
325                         this.abortProgramWithException(ex);
326                 }
327         }
328
329         /**
330          * Main method (entry point)
331          * <p>
332          * @param args the command line arguments
333          */
334         public static void main (String[] args) {
335                 // Start application
336                 new AddressbookApplication().start(args);
337         }
338
339         /**
340          * Getter for printable application name
341          * <p>
342          * @return A printable name
343          */
344         public static String printableTitle () {
345                 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
346         }
347
348         /**
349          * Getter for logger instance
350          *
351          * @return Logger instance
352          */
353         protected LoggerBeanLocal getLogger () {
354                 return this.logger;
355         }
356 }