]> git.mxchange.org Git - addressbook-lib.git/blob - Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java
the contact manager instance only exists when initFrame() is called and not on object...
[addressbook-lib.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      * Getter for printable application name\r
141      * \r
142      * @return A printable name\r
143      */\r
144     public static String printableTitle () {\r
145         return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION);\r
146     }\r
147 \r
148     /**\r
149      * Bootstraps application\r
150      */\r
151     @Override\r
152     public void doBootstrap () {\r
153         this.getLogger().debug("Initializing application ...");\r
154 \r
155         // Init client variable\r
156         Client client = null;\r
157 \r
158         // Is console or Swing choosen?\r
159         if (this.isConsole()) {\r
160             // Debug message\r
161             this.getLogger().debug("Initializing console client ...");\r
162 \r
163             // Init console client instance\r
164             client = new ConsoleClient(this);\r
165         } else if (this.isGui()) {\r
166             // Debug message\r
167             this.getLogger().debug("Initializing GUI (Swing) client ...");\r
168 \r
169             // Init console instance\r
170             client = new SwingClient(this);\r
171         } else {\r
172             // Not client choosen\r
173             this.getLogger().error("No client choosen. Cannot launch.");\r
174             System.exit(1);\r
175         }\r
176 \r
177         // Init client\r
178         client.initClient();\r
179 \r
180         // Set client instance\r
181         this.setClient(client);\r
182         \r
183         // The application is running at this point\r
184         this.getClient().enableIsRunning();\r
185     }\r
186 \r
187     /**\r
188      * Main loop of the application\r
189      */\r
190     @Override\r
191     public void doMainLoop () {\r
192         // @TODO The application should be running now\r
193         \r
194         // Output introduction\r
195         this.showIntro();\r
196 \r
197         // Set current menu to main\r
198         this.getClient().setCurrentMenu("main");\r
199 \r
200         // --- Main loop starts here ---\r
201         while (this.getClient().isRunning()) {\r
202             // The application is still active, show menu selection\r
203             this.getClient().showCurrentMenu();\r
204 \r
205             try {\r
206                 // Ask for user input and run proper method\r
207                 this.getClient().doUserMenuChoice();\r
208             } catch (final UnhandledUserChoiceException ex) {\r
209                 this.getLogger().catching(ex);\r
210             }\r
211         }\r
212         // --- Main loop ends here ---\r
213 \r
214         // Debug message\r
215         this.getLogger().debug("Main loop exit - shutting down ...");\r
216     }\r
217 \r
218     /**\r
219      * Enables console client by setting propper flag\r
220      */\r
221     private void enableConsoleClient () {\r
222         this.getLogger().debug("Enabling console client (may become optional client) ...");\r
223         this.consoleClient = true;\r
224         this.guiClient = false;\r
225     }\r
226 \r
227     /**\r
228      * Enables GUI client by setting propper flag\r
229      */\r
230     private void enableGuiClient () {\r
231         this.getLogger().debug("Enabling GUI client (may become new default client) ...");\r
232         this.consoleClient = false;\r
233         this.guiClient = true;\r
234     }\r
235 \r
236     /**\r
237      * Checks whether the client shoule be console client should be launched by\r
238      * checking if -console is set.\r
239      * \r
240      * @return Whether console client should be taken\r
241      */\r
242     private boolean isConsole () {\r
243         return this.consoleClient;\r
244     }\r
245 \r
246     /**\r
247      * Checks whether the client shoule be GUI client should be launched by\r
248      * checking if -gui is set.\r
249      * \r
250      * @return Whether GUI client should be taken\r
251      */\r
252     private boolean isGui () {\r
253         return this.guiClient;\r
254     }\r
255 \r
256     /**\r
257      * Parses all given arguments\r
258      *\r
259      * @param args Arguments from program launch\r
260      */\r
261     private void parseArguments (final String[] args) {\r
262         // Debug message\r
263         this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length));\r
264         \r
265         for (final String arg : args) {\r
266             // Switch on it\r
267             switch (arg) {\r
268                 case "-console":\r
269                     enableConsoleClient();\r
270                     break;\r
271                     \r
272                 case "-gui":\r
273                     enableGuiClient();\r
274                     break;\r
275 }\r
276         }\r
277     }\r
278 \r
279     /**\r
280      * Show introduction which depends on client\r
281      */\r
282     private void showIntro () {\r
283         // Let the client show it\r
284         this.getClient().showWelcome();\r
285     }\r
286 \r
287     /**\r
288      * Launches the application\r
289      * \r
290      * @param args Arguments handled to program\r
291      */\r
292     private void start (final String args[]) {\r
293         this.getLogger().info("Program is started.");\r
294 \r
295         // Parse arguments\r
296         this.parseArguments(args);\r
297 \r
298         // Launch application\r
299         ApplicationManager.getManager(this).start();\r
300 \r
301         // Good bye, but this should not be reached ...\r
302         this.getLogger().warn("Unusual exit reached.");\r
303         this.doShutdown();\r
304     }\r
305 \r
306     /**\r
307      * Main method (entry point)\r
308      *\r
309      * @param args the command line arguments\r
310      */\r
311     public static void main (String[] args) {\r
312         // Start application\r
313         new AddressbookApplication().start(args);\r
314     }\r
315 \r
316     /**\r
317      * Shuts down the application.\r
318      */\r
319     @Override\r
320     public void doShutdown () {\r
321         // Shutdown client\r
322         this.getClient().doShutdown();\r
323 \r
324         this.getLogger().info("End of program (last line)");\r
325         System.exit(0);\r
326     }\r
327 }\r