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