]> git.mxchange.org Git - addressbook-swing.git/blob - Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java
Introduced a lot Swing stuff + moved some attributes
[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         // Set client instance\r
169         this.setClient(client);\r
170         \r
171         // The application is running at this point\r
172         this.getClient().enableIsRunning();\r
173     }\r
174 \r
175     /**\r
176      * Main loop of the application\r
177      */\r
178     @Override\r
179     public void doMainLoop () {\r
180         // @TODO The application should be running now\r
181         \r
182         // Output introduction\r
183         this.showIntro();\r
184 \r
185         // Set current menu to main\r
186         this.getClient().setCurrentMenu("main");\r
187 \r
188         // --- Main loop starts here ---\r
189         while (this.getClient().isRunning()) {\r
190             // The application is still active, show menu selection\r
191             this.getClient().showCurrentMenu();\r
192 \r
193             try {\r
194                 // Ask for user input and run proper method\r
195                 this.getClient().doUserMenuChoice();\r
196             } catch (final UnhandledUserChoiceException ex) {\r
197                 this.getLogger().catching(ex);\r
198             }\r
199         }\r
200         // --- Main loop ends here ---\r
201 \r
202         // Debug message\r
203         this.getLogger().debug("Main loop exit - shutting down ...");\r
204     }\r
205 \r
206     /**\r
207      * Enables console client by setting propper flag\r
208      */\r
209     private void enableConsoleClient () {\r
210         this.getLogger().debug("Enabling console client (may become optional client) ...");\r
211         this.consoleClient = true;\r
212         this.guiClient = false;\r
213     }\r
214 \r
215     /**\r
216      * Enables GUI client by setting propper flag\r
217      */\r
218     private void enableGuiClient () {\r
219         this.getLogger().debug("Enabling GUI client (may become new default client) ...");\r
220         this.consoleClient = false;\r
221         this.guiClient = true;\r
222     }\r
223 \r
224     /**\r
225      * Checks whether the client shoule be console client should be launched by\r
226      * checking if -console is set.\r
227      * \r
228      * @return Whether console client should be taken\r
229      */\r
230     private boolean isConsole () {\r
231         return this.consoleClient;\r
232     }\r
233 \r
234     /**\r
235      * Checks whether the client shoule be GUI client should be launched by\r
236      * checking if -gui is set.\r
237      * \r
238      * @return Whether GUI client should be taken\r
239      */\r
240     private boolean isGui () {\r
241         return this.guiClient;\r
242     }\r
243 \r
244     /**\r
245      * Parses all given arguments\r
246      *\r
247      * @param args Arguments from program launch\r
248      */\r
249     private void parseArguments (final String[] args) {\r
250         // Debug message\r
251         this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length));\r
252         \r
253         for (final String arg : args) {\r
254             // Switch on it\r
255             switch (arg) {\r
256                 case "-console":\r
257                     enableConsoleClient();\r
258                     break;\r
259                     \r
260                 case "-gui":\r
261                     enableGuiClient();\r
262                     break;\r
263 }\r
264         }\r
265     }\r
266 \r
267     /**\r
268      * Show introduction which depends on client\r
269      */\r
270     private void showIntro () {\r
271         // Let the client show it\r
272         this.getClient().showWelcome();\r
273     }\r
274 \r
275     /**\r
276      * Launches the application\r
277      * \r
278      * @param args Arguments handled to program\r
279      */\r
280     private void start (final String args[]) {\r
281         this.getLogger().info("Program is started.");\r
282 \r
283         // Parse arguments\r
284         this.parseArguments(args);\r
285 \r
286         // Launch application\r
287         ApplicationManager.getManager(this).start();\r
288 \r
289         this.getLogger().info("End of program (last line)");\r
290     }\r
291 \r
292     /**\r
293      * Main method (entry point)\r
294      *\r
295      * @param args the command line arguments\r
296      */\r
297     public static void main (String[] args) {\r
298         // Start application\r
299         new AddressbookApplication().start(args);\r
300     }\r
301 }\r