]> git.mxchange.org Git - addressbook-swing.git/blob - Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java
Better init it here
[addressbook-swing.git] / Addressbook / 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.text.MessageFormat;
20 import org.mxchange.addressbook.BaseAddressbookSystem;
21 import org.mxchange.addressbook.client.AddressbookClient;
22 import org.mxchange.addressbook.client.console.ConsoleClient;
23 import org.mxchange.addressbook.client.gui.SwingClient;
24 import org.mxchange.jcore.application.Application;
25 import org.mxchange.jcore.client.Client;
26 import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
27 import org.mxchange.jcore.manager.application.ApplicationManager;
28
29 /**
30  * ============================================
31  * AddressbookApplication management:
32  * ============================================
33  * 
34  * Inernet("public" service) and Intranet
35  * 
36  * Version 1.0+:
37  * - Single-user local application
38  * - Fields:
39  *   + Gender
40  *   + Surname
41  *   + Family name
42  *   + Company name
43  *   + Street + number
44  *   + ZIP code
45  *   + City
46  *   + Landline number
47  *   + Fax number
48  *   + Cell phone number
49  *   + Email address
50  *   + Birth day
51  *   + Comment (?)
52  * - Edit own data
53  * - Add new contact
54  * - Edit contacts
55  * - Delete contacts
56  * - Categorization of contacts
57  * 
58  * Version 1.1+:
59  * - Permanent storage in database
60  * 
61  * Version 2.0+:
62  * - Multi-user web application
63  * - Local user registration / login / resend confirmation link / password
64  *   recovery
65  * - User groups (aka. teams)
66  * - Administration area (user role)
67  *   + Create/edit/delete groups
68  *   + Edit/delete/lock/unlock user
69  *   + Assign user roles/rights
70  * - Allow other users / groups to view addressbook
71  *   + Full addressbook
72  *   + Only some categories
73  * - VCard export
74  *   + Allow users/guests (not recommended)
75  * - XML export of addressbook and compressable (ZIP)
76  * - Form to contact other user/group without need of mail program
77  *   + User can disabled this
78  * - Directory for ussers/groups (who allowed to be listed)
79  *   + Simple click to add user to own addressbook
80  *   + Search form?
81  * 
82  * Version 2.1+:
83  * - Multi-language support
84  * 
85  * Version 2.2+:("socialized")
86  * - "Social login" (OpenID consumer)
87  *   + Connect user account to social account
88  *   + Sync own data?
89  * - "Social profile"
90  *   + OpenID provider
91  *   + RSS/activity feed 
92  * 
93  * ============================================
94  * Time esitmation:
95  * ============================================
96  * 1.0 (console):
97  *   + 2 days
98  * 
99  * 1.1 (database):
100  *   + 2 day
101  *   + Initial tables: contacts, categories, contact_category
102  * 
103  * 2.0 (web):
104  *   + 3 days
105  *   + Additional tables: admins (?), admin_rights, groups,
106  *    users, user_contacts, user_user_rights, user_category_rights, 
107  * 
108  * 2.1 (language)
109  *   + 1 day
110  *   + Additional tables: languages (disable, enable language "pack" ?)
111  * 
112  * 2.2 (social):
113  *   + 3 days
114  *   + Additional tables: ???
115 *
116  * @author Roland Haeder
117  * @version 0.0
118  */
119 public class AddressbookApplication extends BaseAddressbookSystem implements Application {
120
121         /**
122          * Application title
123          */
124         public static final String APP_TITLE = "Adressbuch"; //NOI18N
125
126         /**
127          * Application version
128          */
129         public static final String APP_VERSION = "0.0"; //NOI18N
130
131         /**
132          * Self instance
133          */
134         private static Application selfInstance;
135
136         /**
137          * Console client is enabled by default
138          */
139         private boolean consoleClient = true;
140
141         /**
142          * GUI client is disabled by default
143          */
144         private boolean guiClient = false;
145
146         /**
147          * Protected constructor
148          */
149         protected AddressbookApplication () {
150                 // Set own instance
151                 selfInstance = this;
152
153                 // Always init i18n bundle in this application
154                 this.initBundle();
155         }
156
157         /**
158          * Getter for printable application name
159          *
160          * @return A printable name
161          */
162         public static String printableTitle () {
163                 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
164         }
165
166         /**
167          * Bootstraps application
168          */
169         @Override
170         public void doBootstrap () {
171                 this.getLogger().debug("Initializing application ..."); //NOI18N
172
173                 // Init client variable
174                 Client client = null;
175
176                 // Is console or Swing choosen?
177                 if (this.isConsole()) {
178                         // Debug message
179                         this.getLogger().debug("Initializing console client ..."); //NOI18N
180
181                         // Init console client instance
182                         client = new ConsoleClient(this);
183                 } else if (this.isGui()) {
184                         // Debug message
185                         this.getLogger().debug("Initializing GUI (Swing) client ..."); //NOI18N
186
187                         // Init console instance
188                         client = new SwingClient(this);
189                 } else {
190                         // Not client choosen
191                         this.getLogger().error("No client choosen. Cannot launch."); //NOI18N
192                         System.exit(1);
193                 }
194
195                 // Init client
196                 client.init();
197
198                 // Set client instance
199                 this.setClient(client);
200
201                 // The application is running at this point
202                 this.getClient().enableIsRunning();
203
204                 // Trace message
205                 this.getLogger().trace("EXIT!"); //NOI18N
206         }
207
208         /**
209          * Main loop of the application
210          */
211         @Override
212         public void doMainLoop () {
213                 // Get client and cast it
214                 AddressbookClient client = (AddressbookClient) this.getClient();
215
216                 // Debug message
217                 this.getLogger().trace("CALLED!"); //NOI18N
218
219                 // @TODO The application should be running now
220                 // Output introduction
221                 this.showIntro();
222
223                 // Set current menu to main
224                 client.setCurrentMenu("main"); //NOI18N
225
226                 // --- Main loop starts here ---
227                 while (this.getClient().isRunning()) {
228                         // The application is still active, show menu selection
229                         client.showCurrentMenu();
230
231                         try {
232                                 // Ask for user input and run proper method
233                                 client.doUserMenuChoice();
234                         } catch (final UnhandledUserChoiceException ex) {
235                                 this.getLogger().catching(ex);
236                         }
237
238                         try {
239                                 // Sleep a little to reduce system load
240                                 Thread.sleep(100);
241                         } catch (final InterruptedException ex) {
242                                 // Ignore it
243                         }
244                 }
245                 // --- Main loop ends here ---
246
247                 // Debug message
248                 this.getLogger().debug("Main loop exit - shutting down ..."); //NOI18N
249         }
250
251         /**
252          * Enables console client by setting propper flag
253          */
254         private void enableConsoleClient () {
255                 this.getLogger().debug("Enabling console client (may become optional client) ..."); //NOI18N
256                 this.consoleClient = true;
257                 this.guiClient = false;
258         }
259
260         /**
261          * Enables GUI client by setting propper flag
262          */
263         private void enableGuiClient () {
264                 this.getLogger().debug("Enabling GUI client (may become new default client) ..."); //NOI18N
265                 this.consoleClient = false;
266                 this.guiClient = true;
267         }
268
269         /**
270          * Checks whether the client shoule be console client should be launched by
271          * checking if -console is set.
272          *
273          * @return Whether console client should be taken
274          */
275         private boolean isConsole () {
276                 return this.consoleClient;
277         }
278
279         /**
280          * Checks whether the client shoule be GUI client should be launched by
281          * checking if -gui is set.
282          *
283          * @return Whether GUI client should be taken
284          */
285         private boolean isGui () {
286                 return this.guiClient;
287         }
288
289         /**
290          * Parses all given arguments
291          *
292          * @param args Arguments from program launch
293          */
294         private void parseArguments (final String[] args) {
295                 // Trace message
296                 this.getLogger().trace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
297
298                 // Debug message
299                 this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
300
301                 for (final String arg : args) {
302                         // Switch on it
303                         switch (arg) {
304                                 case "-console": //NOI18N
305                                         enableConsoleClient();
306                                         break;
307
308                                 case "-gui": //NOI18N
309                                         enableGuiClient();
310                                         break;
311                         }
312                 }
313         }
314
315         /**
316          * Show introduction which depends on client
317          */
318         private void showIntro () {
319                 // Trace message
320                 this.getLogger().trace("CALLED!"); //NOI18N
321
322                 // Let the client show it
323                 this.getClient().showWelcome();
324
325                 // Trace message
326                 this.getLogger().trace("EXIT!"); //NOI18N
327         }
328
329         /**
330          * Launches the application
331          *
332          * @param args Arguments handled to program
333          */
334         private void start (final String args[]) {
335                 this.getLogger().info("Program is started."); //NOI18N
336
337                 // Parse arguments
338                 this.parseArguments(args);
339
340                 // Launch application
341                 ApplicationManager.getManager(this).start();
342
343                 // Good bye, but this should not be reached ...
344                 this.getLogger().warn("Unusual exit reached."); //NOI18N
345                 this.doShutdown();
346         }
347
348         /**
349          * Main method (entry point)
350          *
351          * @param args the command line arguments
352          */
353         public static void main (String[] args) {
354                 // Start application
355                 new AddressbookApplication().start(args);
356         }
357
358         /**
359          * Shuts down the application.
360          */
361         @Override
362         public void doShutdown () {
363                 // Trace message
364                 this.getLogger().trace("CALLED!"); //NOI18N
365
366                 // Shutdown client
367                 this.getClient().doShutdown();
368
369                 this.getLogger().info("End of program (last line)"); //NOI18N
370                 System.exit(0);
371         }
372
373         /**
374          * Getter for this application
375          *
376          * @return Instance from this application
377          */
378         public static final Application getInstance () {
379                 // Return it
380                 return selfInstance;
381         }
382 }