]> git.mxchange.org Git - addressbook-lib.git/blob - src/org/mxchange/addressbook/application/AddressbookApplication.java
dca6fe202e2dc7e9892f2a158a474f8ef4e5e7e5
[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.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;
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          * Protected constructor
62          * <p>
63          * @throws java.io.IOException If any IO error occurs
64          */
65         protected AddressbookApplication () throws IOException {
66                 // Init properties file
67                 this.initProperties();
68
69                 // Is the bundle initialized?
70                 if (!isBundledInitialized()) {
71                         // Temporary initialize default bundle
72                         // TODO The enum Gender uses this
73                         this.initBundle();
74                 }
75         }
76
77         /**
78          * Bootstraps application
79          */
80         @Override
81         public void doBootstrap () {
82                 this.getLogger().debug("Initializing application ..."); //NOI18N
83
84                 // Init client variable
85                 Client client = null;
86
87                 // Is console or Swing choosen?
88                 if (this.isConsole()) {
89                         // Debug message
90                         this.getLogger().debug("Initializing console client ..."); //NOI18N
91
92                         // Init console client instance
93                         client = new ConsoleClient(this);
94                 } else if (this.isGui()) {
95                         // Debug message
96                         this.getLogger().debug("Initializing GUI (Swing) client ..."); //NOI18N
97
98                         // Init console instance
99                         client = new SwingClient(this);
100                 } else {
101                         // Not client choosen
102                         this.getLogger().error("No client choosen. Cannot launch."); //NOI18N
103
104                         try {
105                                 this.doShutdown();
106                         } catch (final SQLException | IOException ex) {
107                                 // Abort here
108                                 this.abortProgramWithException(ex);
109                         }
110
111                         // Bye ...
112                         System.exit(1);
113                 }
114
115                 // Init client
116                 client.init();
117
118                 // Set client instance
119                 this.setClient(client);
120
121                 // The application is running at this point
122                 this.getClient().enableIsRunning();
123
124                 // Trace message
125                 this.getLogger().trace("EXIT!"); //NOI18N
126         }
127
128         /**
129          * Main loop of the application
130          */
131         @Override
132         public void doMainLoop () {
133                 // Get client and cast it
134                 AddressbookClient client = (AddressbookClient) this.getClient();
135
136                 // Debug message
137                 this.getLogger().trace("CALLED!"); //NOI18N
138
139                 // TODO The application should be running now
140                 // Output introduction
141                 this.showIntro();
142
143                 // Set current menu to main
144                 client.setCurrentMenu("main"); //NOI18N
145
146                 // --- Main loop starts here ---
147                 while (this.getClient().isRunning()) {
148                         // The application is still active, show menu selection
149                         client.showCurrentMenu();
150
151                         try {
152                                 // Ask for user input and run proper method
153                                 client.doUserMenuChoice();
154                         } catch (final UnhandledUserChoiceException ex) {
155                                 this.getLogger().catching(ex);
156                         }
157
158                         try {
159                                 // Sleep a little to reduce system load
160                                 Thread.sleep(100);
161                         } catch (final InterruptedException ex) {
162                                 // Ignore it
163                         }
164                 }
165                 // --- Main loop ends here ---
166
167                 // Debug message
168                 this.getLogger().debug("Main loop exit - shutting down ..."); //NOI18N
169         }
170
171         /**
172          * Shuts down the application.
173          */
174         @Override
175         public void doShutdown () throws SQLException, IOException {
176                 // Trace message
177                 this.getLogger().trace("CALLED!"); //NOI18N
178
179                 // Shutdown client
180                 this.getClient().doShutdown();
181
182                 this.getLogger().info("End of program (last line)"); //NOI18N
183                 System.exit(0);
184         }
185
186         /**
187          * Enables console client by setting propper flag
188          */
189         private void enableConsoleClient () {
190                 this.getLogger().debug("Enabling console client (may become optional client) ..."); //NOI18N
191                 this.consoleClient = true;
192                 this.guiClient = false;
193         }
194
195         /**
196          * Enables GUI client by setting propper flag
197          */
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;
202         }
203
204         /**
205          * Checks whether the client shoule be console client should be launched by
206          * checking if -console is set.
207          * <p>
208          * @return Whether console client should be taken
209          */
210         private boolean isConsole () {
211                 return this.consoleClient;
212         }
213
214         /**
215          * Checks whether the client shoule be GUI client should be launched by
216          * checking if -gui is set.
217          * <p>
218          * @return Whether GUI client should be taken
219          */
220         private boolean isGui () {
221                 return this.guiClient;
222         }
223
224         /**
225          * Parses all given arguments
226          * <p>
227          * @param args Arguments from program launch
228          */
229         private void parseArguments (final String[] args) {
230                 // Trace message
231                 this.getLogger().trace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
232
233                 // Debug message
234                 this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
235
236                 for (final String arg : args) {
237                         // Switch on it
238                         switch (arg) {
239                                 case "-console": //NOI18N
240                                         enableConsoleClient();
241                                         break;
242
243                                 case "-gui": //NOI18N
244                                         enableGuiClient();
245                                         break;
246                         }
247                 }
248         }
249
250         /**
251          * Show introduction which depends on client
252          */
253         private void showIntro () {
254                 // Trace message
255                 this.getLogger().trace("CALLED!"); //NOI18N
256
257                 // Let the client show it
258                 this.getClient().showWelcome();
259
260                 // Trace message
261                 this.getLogger().trace("EXIT!"); //NOI18N
262         }
263
264         /**
265          * Launches the application
266          * <p>
267          * @param args Arguments handled to program
268          */
269         private void start (final String args[]) {
270                 this.getLogger().info("Program is started."); //NOI18N
271                 try {
272                         // Init properties file
273                         this.initProperties();
274                 } catch (final IOException ex) {
275                         // Something bad happened
276                         this.abortProgramWithException(ex);
277                 }
278
279                 // Parse arguments
280                 this.parseArguments(args);
281
282                 // Launch application
283                 ApplicationManager.getSingeltonManager(this).start();
284
285                 // Good bye, but this should not be reached ...
286                 this.getLogger().warn("Unusual exit reached."); //NOI18N
287                 try {
288                         this.doShutdown();
289                 } catch (final SQLException | IOException ex) {
290                         this.abortProgramWithException(ex);
291                 }
292         }
293
294         /**
295          * Main method (entry point)
296          * <p>
297          * @param args the command line arguments
298          */
299         public static void main (String[] args) {
300                 try {
301                         // Start application
302                         new AddressbookApplication().start(args);
303                 } catch (final IOException ex) {
304                         // Get instance
305                         BaseFrameworkSystem.getInstance().getLogger().catching(ex);
306                         System.exit(1);
307                 }
308         }
309
310         /**
311          * Getter for printable application name
312          * <p>
313          * @return A printable name
314          */
315         public static String printableTitle () {
316                 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
317         }
318 }