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