]> git.mxchange.org Git - addressbook-swing.git/blob - src/org/mxchange/addressbook/application/AddressbookApplication.java
auto-reformatted most project files
[addressbook-swing.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 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 Haeder
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          * Console client is enabled by default
57          */
58         private boolean consoleClient = true;
59
60         /**
61          * GUI client is disabled by default
62          */
63         private boolean guiClient = false;
64
65         /**
66          * Logger instance
67          */
68         @Log
69         private LoggerBeanLocal logger;
70
71         /**
72          * Protected constructor
73          */
74         protected AddressbookApplication () {
75                 // Try this
76                 try {
77                         // Get context
78                         Context context = new InitialContext();
79
80                         // Get logger
81                         this.logger = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal");
82                 } catch (final NamingException ex) {
83                         // Output it and exit
84                         System.err.println(MessageFormat.format("Cannot initialize: {0}", ex));
85
86                         // Abort here
87                         System.exit(1);
88                 }
89
90                 // Call init method
91                 this.init();
92         }
93
94         /**
95          * Main method (entry point)
96          * <p>
97          * @param args the command line arguments
98          */
99         public static void main (String[] args) {
100                 // Start application
101                 new AddressbookApplication().start(args);
102         }
103
104         /**
105          * Getter for printable application name
106          * <p>
107          * @return A printable name
108          */
109         public static String printableTitle () {
110                 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
111         }
112
113         @Override
114         public void doBootstrap () {
115                 this.getLogger().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.getLogger().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.getLogger().logDebug("Initializing GUI (Swing) client ..."); //NOI18N
130
131                         // Init console instance
132                         client = new SwingClient(this);
133                 } else {
134                         // Not client choosen
135                         this.getLogger().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.getLogger().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.getLogger().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.getLogger().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.getLogger().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.getLogger().logTrace("CALLED!"); //NOI18N
209
210                 // Shutdown client
211                 this.getClient().doShutdown();
212
213                 // Regular exit reached
214                 this.getLogger().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.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          * 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 Gender 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.getLogger().logTrace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
292
293                 // Debug message
294                 this.getLogger().logDebug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
295
296                 for (final String arg : args) {
297                         // Switch on it
298                         switch (arg) {
299                                 case "-console": //NOI18N
300                                         enableConsoleClient();
301                                         break;
302
303                                 case "-gui": //NOI18N
304                                         enableGuiClient();
305                                         break;
306                         }
307                 }
308         }
309
310         /**
311          * Show introduction which depends on client
312          */
313         private void showIntro () {
314                 // Trace message
315                 this.getLogger().logTrace("CALLED!"); //NOI18N
316
317                 // Let the client show it
318                 this.getClient().showWelcome();
319
320                 // Trace message
321                 this.getLogger().logTrace("EXIT!"); //NOI18N
322         }
323
324         /**
325          * Launches the application
326          * <p>
327          * @param args Arguments handled to program
328          */
329         private void start (final String args[]) {
330                 this.getLogger().logInfo("Program is started."); //NOI18N
331
332                 try {
333                         // Init properties file
334                         this.initProperties();
335                 } catch (final IOException ex) {
336                         // Something bad happened
337                         this.abortProgramWithException(ex);
338                 }
339
340                 // Parse arguments
341                 this.parseArguments(args);
342
343                 try {
344                         // Launch application
345                         ApplicationManager.getSingeltonManager(this).start();
346                 } catch (final MenuInitializationException ex) {
347                         // Something bad happened
348                         this.abortProgramWithException(ex);
349                 }
350
351                 // Good bye, but this should not be reached ...
352                 this.getLogger().logWarning("Unusual exit reached."); //NOI18N
353
354                 try {
355                         this.doShutdown();
356                 } catch (final SQLException | IOException ex) {
357                         // Something bad happened
358                         this.abortProgramWithException(ex);
359                 }
360         }
361
362         /**
363          * Log exception and abort program.
364          * <p>
365          * @param throwable Throwable
366          */
367         protected void abortProgramWithException (final Throwable throwable) {
368                 // Log exception
369                 this.logException(throwable);
370                 
371                 // Abort here
372                 System.exit(1);
373         }
374
375         /**
376          * Getter for logger instance
377          * <p>
378          * @return Logger instance
379          */
380         protected LoggerBeanLocal getLogger () {
381                 return this.logger;
382         }
383
384         /**
385          * Logs given exception
386          * <p>
387          * @param exception Throwable
388          */
389         protected void logException (final Throwable exception) {
390                 this.getLogger().logException(exception);
391         }
392
393 }