]> git.mxchange.org Git - addressbook-swing.git/blob - Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java
A lot more improvements:
[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"; //NOI18N
81
82         /**
83          * Application version
84          */
85         public static final String APP_VERSION = "0.0"; //NOI18N
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); //NOI18N
117         }
118
119         /**
120          * Bootstraps application
121          */
122         @Override
123         public void doBootstrap () {
124                 this.getLogger().debug("Initializing application ..."); //NOI18N
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 ..."); //NOI18N
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 ..."); //NOI18N
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."); //NOI18N
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().trace("EXIT!"); //NOI18N
159         }
160
161         /**
162          * Main loop of the application
163          */
164         @Override
165         public void doMainLoop () {
166                 // Debug message
167                 this.getLogger().trace("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                 this.getClient().setCurrentMenu("main"); //NOI18N
175
176                 // --- Main loop starts here ---
177                 while (this.getClient().isRunning()) {
178                         // The application is still active, show menu selection
179                         this.getClient().showCurrentMenu();
180
181                         try {
182                                 // Ask for user input and run proper method
183                                 this.getClient().doUserMenuChoice();
184                         } catch (final UnhandledUserChoiceException ex) {
185                                 this.getLogger().catching(ex);
186                         }
187
188                         try {
189                                 // Sleep a little to reduce system load
190                                 Thread.sleep(100);
191                         } catch (final InterruptedException ex) {
192                                 // Ignore it
193                         }
194                 }
195                 // --- Main loop ends here ---
196
197                 // Debug message
198                 this.getLogger().debug("Main loop exit - shutting down ..."); //NOI18N
199         }
200
201         /**
202          * Enables console client by setting propper flag
203          */
204         private void enableConsoleClient () {
205                 this.getLogger().debug("Enabling console client (may become optional client) ..."); //NOI18N
206                 this.consoleClient = true;
207                 this.guiClient = false;
208         }
209
210         /**
211          * Enables GUI client by setting propper flag
212          */
213         private void enableGuiClient () {
214                 this.getLogger().debug("Enabling GUI client (may become new default client) ..."); //NOI18N
215                 this.consoleClient = false;
216                 this.guiClient = true;
217         }
218
219         /**
220          * Checks whether the client shoule be console client should be launched by
221          * checking if -console is set.
222          *
223          * @return Whether console client should be taken
224          */
225         private boolean isConsole () {
226                 return this.consoleClient;
227         }
228
229         /**
230          * Checks whether the client shoule be GUI client should be launched by
231          * checking if -gui is set.
232          *
233          * @return Whether GUI client should be taken
234          */
235         private boolean isGui () {
236                 return this.guiClient;
237         }
238
239         /**
240          * Parses all given arguments
241          *
242          * @param args Arguments from program launch
243          */
244         private void parseArguments (final String[] args) {
245                 // Trace message
246                 this.getLogger().trace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
247
248                 // Debug message
249                 this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
250
251                 for (final String arg : args) {
252                         // Switch on it
253                         switch (arg) {
254                                 case "-console": //NOI18N
255                                         enableConsoleClient();
256                                         break;
257
258                                 case "-gui": //NOI18N
259                                         enableGuiClient();
260                                         break;
261                         }
262                 }
263         }
264
265         /**
266          * Show introduction which depends on client
267          */
268         private void showIntro () {
269                 // Trace message
270                 this.getLogger().trace("CALLED!"); //NOI18N
271
272                 // Let the client show it
273                 this.getClient().showWelcome();
274
275                 // Trace message
276                 this.getLogger().trace("EXIT!"); //NOI18N
277         }
278
279         /**
280          * Launches the application
281          *
282          * @param args Arguments handled to program
283          */
284         private void start (final String args[]) {
285                 this.getLogger().info("Program is started."); //NOI18N
286
287                 // Parse arguments
288                 this.parseArguments(args);
289
290                 // Launch application
291                 ApplicationManager.getManager(this).start();
292
293                 // Good bye, but this should not be reached ...
294                 this.getLogger().warn("Unusual exit reached."); //NOI18N
295                 this.doShutdown();
296         }
297
298         /**
299          * Main method (entry point)
300          *
301          * @param args the command line arguments
302          */
303         public static void main (String[] args) {
304                 // Start application
305                 new AddressbookApplication().start(args);
306         }
307
308         /**
309          * Shuts down the application.
310          */
311         @Override
312         public void doShutdown () {
313                 // Trace message
314                 this.getLogger().trace("CALLED!"); //NOI18N
315
316                 // Shutdown client
317                 this.getClient().doShutdown();
318
319                 this.getLogger().info("End of program (last line)"); //NOI18N
320                 System.exit(0);
321         }
322
323         /**
324          * Getter for this application
325          *
326          * @return Instance from this application
327          */
328         public static final Application getInstance () {
329                 // Return it
330                 return selfInstance;
331         }
332 }