]> git.mxchange.org Git - addressbook-swing.git/blob - src/org/mxchange/addressbook/application/AddressbookApplication.java
Continued:
[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          * Bootstraps application
96          */
97         @Override
98         public void doBootstrap () {
99                 this.getLogger().logDebug("Initializing application ..."); //NOI18N
100
101                 // Init client variable
102                 Client client = null;
103
104                 // Is console or Swing choosen?
105                 if (this.isConsole()) {
106                         // Debug message
107                         this.getLogger().logDebug("Initializing console client ..."); //NOI18N
108
109                         // Init console client instance
110                         client = new ConsoleClient(this);
111                 } else if (this.isGui()) {
112                         // Debug message
113                         this.getLogger().logDebug("Initializing GUI (Swing) client ..."); //NOI18N
114
115                         // Init console instance
116                         client = new SwingClient(this);
117                 } else {
118                         // Not client choosen
119                         this.getLogger().logError("No client choosen. Cannot launch."); //NOI18N
120
121                         try {
122                                 this.doShutdown();
123                         } catch (final SQLException | IOException ex) {
124                                 // Abort here
125                                 this.abortProgramWithException(ex);
126                         }
127
128                         // Bye ...
129                         System.exit(1);
130                 }
131
132                 // Init client
133                 client.init();
134
135                 // Set client instance
136                 this.setClient(client);
137
138                 // The application is running at this point
139                 this.getClient().enableIsRunning();
140
141                 // Trace message
142                 this.getLogger().logTrace("EXIT!"); //NOI18N
143         }
144
145         /**
146          * Main loop of the application
147          */
148         @Override
149         public void doMainLoop () throws MenuInitializationException {
150                 // Get client and cast it
151                 AddressbookClient client = (AddressbookClient) this.getClient();
152
153                 // Debug message
154                 this.getLogger().logTrace("CALLED!"); //NOI18N
155
156                 // TODO The application should be running now
157                 // Output introduction
158                 this.showIntro();
159
160                 // Set current menu to main
161                 client.setCurrentMenu("main"); //NOI18N
162
163                 // --- Main loop starts here ---
164                 while (this.getClient().isRunning()) {
165                         // The application is still active, show menu selection
166                         client.showCurrentMenu();
167
168                         try {
169                                 // Ask for user input and run proper method
170                                 client.doUserMenuChoice();
171                         } catch (final UnhandledUserChoiceException ex) {
172                                 // Log exception
173                                 this.getLogger().logException(ex);
174                         }
175
176                         try {
177                                 // Sleep a little to reduce system load
178                                 Thread.sleep(100);
179                         } catch (final InterruptedException ex) {
180                                 // Ignore it
181                         }
182                 }
183                 // --- Main loop ends here ---
184
185                 // Debug message
186                 this.getLogger().logDebug("Main loop exit - shutting down ..."); //NOI18N
187         }
188
189         /**
190          * Shuts down the application.
191          */
192         @Override
193         public void doShutdown () throws SQLException, IOException {
194                 // Trace message
195                 this.getLogger().logTrace("CALLED!"); //NOI18N
196
197                 // Shutdown client
198                 this.getClient().doShutdown();
199
200                 // Regular exit reached
201                 this.getLogger().logInfo("End of program (last line)"); //NOI18N
202                 System.exit(0);
203         }
204
205         /**
206          * Initializes this application
207          */
208         private void init () {
209                 // Try this
210                 try {
211                         // Init properties file
212                         this.initProperties();
213                 } catch (final IOException ex) {
214                         // Abort here
215                         this.abortProgramWithException(ex);
216                 }
217
218                 // Is the bundle initialized?
219                 if (!isBundledInitialized()) {
220                         // Temporary initialize default bundle
221                         // TODO The enum Gender uses this
222                         this.initBundle();
223                 }
224         }
225
226         /**
227          * Logs given exception
228          *
229          * @param exception Throwable
230          */
231         protected void logException (final Throwable exception) {
232                 this.getLogger().logException(exception);
233         }
234
235         /**
236          * Initializes properties
237          */
238         private void initProperties () throws IOException {
239                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
240         }
241
242         /**
243          * Log exception and abort program.
244          *
245          * @param throwable Throwable
246          */
247         protected void abortProgramWithException (final Throwable throwable) {
248                 // Log exception
249                 this.logException(throwable);
250
251                 // Abort here
252                 System.exit(1);
253         }
254
255         /**
256          * Enables console client by setting propper flag
257          */
258         private void enableConsoleClient () {
259                 this.getLogger().logDebug("Enabling console client (may become optional client) ..."); //NOI18N
260                 this.consoleClient = true;
261                 this.guiClient = false;
262         }
263
264         /**
265          * Enables GUI client by setting propper flag
266          */
267         private void enableGuiClient () {
268                 this.getLogger().logDebug("Enabling GUI client (may become new default client) ..."); //NOI18N
269                 this.consoleClient = false;
270                 this.guiClient = true;
271         }
272
273         /**
274          * Checks whether the client shoule be console client should be launched by
275          * checking if -console is set.
276          * <p>
277          * @return Whether console client should be taken
278          */
279         private boolean isConsole () {
280                 return this.consoleClient;
281         }
282
283         /**
284          * Checks whether the client shoule be GUI client should be launched by
285          * checking if -gui is set.
286          * <p>
287          * @return Whether GUI client should be taken
288          */
289         private boolean isGui () {
290                 return this.guiClient;
291         }
292
293         /**
294          * Parses all given arguments
295          * <p>
296          * @param args Arguments from program launch
297          */
298         private void parseArguments (final String[] args) {
299                 // Trace message
300                 this.getLogger().logTrace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
301
302                 // Debug message
303                 this.getLogger().logDebug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
304
305                 for (final String arg : args) {
306                         // Switch on it
307                         switch (arg) {
308                                 case "-console": //NOI18N
309                                         enableConsoleClient();
310                                         break;
311
312                                 case "-gui": //NOI18N
313                                         enableGuiClient();
314                                         break;
315                         }
316                 }
317         }
318
319         /**
320          * Show introduction which depends on client
321          */
322         private void showIntro () {
323                 // Trace message
324                 this.getLogger().logTrace("CALLED!"); //NOI18N
325
326                 // Let the client show it
327                 this.getClient().showWelcome();
328
329                 // Trace message
330                 this.getLogger().logTrace("EXIT!"); //NOI18N
331         }
332
333         /**
334          * Launches the application
335          * <p>
336          * @param args Arguments handled to program
337          */
338         private void start (final String args[]) {
339                 this.getLogger().logInfo("Program is started."); //NOI18N
340
341                 try {
342                         // Init properties file
343                         this.initProperties();
344                 } catch (final IOException ex) {
345                         // Something bad happened
346                         this.abortProgramWithException(ex);
347                 }
348
349                 // Parse arguments
350                 this.parseArguments(args);
351
352                 try {
353                         // Launch application
354                         ApplicationManager.getSingeltonManager(this).start();
355                 } catch (final MenuInitializationException ex) {
356                         // Something bad happened
357                         this.abortProgramWithException(ex);
358                 }
359
360                 // Good bye, but this should not be reached ...
361                 this.getLogger().logWarning("Unusual exit reached."); //NOI18N
362
363                 try {
364                         this.doShutdown();
365                 } catch (final SQLException | IOException ex) {
366                         // Something bad happened
367                         this.abortProgramWithException(ex);
368                 }
369         }
370
371         /**
372          * Main method (entry point)
373          * <p>
374          * @param args the command line arguments
375          */
376         public static void main (String[] args) {
377                 // Start application
378                 new AddressbookApplication().start(args);
379         }
380
381         /**
382          * Getter for printable application name
383          * <p>
384          * @return A printable name
385          */
386         public static String printableTitle () {
387                 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
388         }
389
390         /**
391          * Getter for logger instance
392          *
393          * @return Logger instance
394          */
395         protected LoggerBeanLocal getLogger () {
396                 return this.logger;
397         }
398 }