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