2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.addressbook.application;
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;
28 * ============================================ AddressbookApplication
29 * management: ============================================
31 * Inernet("public" service) and Intranet
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
39 * Version 1.1+: - Permanent storage in database
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?
51 * Version 2.1+: - Multi-language support
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
57 * ============================================ Time esitmation:
58 * ============================================ 1.0 (console): + 2 days
60 * 1.1 (database): + 2 day + Initial tables: contacts, categories,
63 * 2.0 (web): + 3 days + Additional tables: admins (?), admin_rights, groups,
64 * users, user_contacts, user_user_rights, user_category_rights,
66 * 2.1 (language) + 1 day + Additional tables: languages (disable, enable
69 * 2.2 (social): + 3 days + Additional tables: ???
71 * @author Roland Haeder
74 public class AddressbookApplication extends BaseFrameworkSystem implements Application {
79 public static final String APP_TITLE = "Adressbuch"; //NOI18N
84 public static final String APP_VERSION = "0.0"; //NOI18N
89 private static Application selfInstance;
92 * Console client is enabled by default
94 private boolean consoleClient = true;
97 * GUI client is disabled by default
99 private boolean guiClient = false;
102 * Protected constructor
104 protected AddressbookApplication () {
110 * Getter for printable application name
112 * @return A printable name
114 public static String printableTitle () {
115 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
119 * Bootstraps application
122 public void doBootstrap () {
123 this.getLogger().debug("Initializing application ..."); //NOI18N
125 // Init client variable
126 Client client = null;
128 // Is console or Swing choosen?
129 if (this.isConsole()) {
131 this.getLogger().debug("Initializing console client ..."); //NOI18N
133 // Init console client instance
134 client = new ConsoleClient(this);
135 } else if (this.isGui()) {
137 this.getLogger().debug("Initializing GUI (Swing) client ..."); //NOI18N
139 // Init console instance
140 client = new SwingClient(this);
142 // Not client choosen
143 this.getLogger().error("No client choosen. Cannot launch."); //NOI18N
150 // Set client instance
151 this.setClient(client);
153 // The application is running at this point
154 this.getClient().enableIsRunning();
157 this.getLogger().trace("EXIT!"); //NOI18N
161 * Main loop of the application
164 public void doMainLoop () {
166 this.getLogger().trace("CALLED!"); //NOI18N
168 // @TODO The application should be running now
169 // Output introduction
172 // Set current menu to main
173 this.getClient().setCurrentMenu("main"); //NOI18N
175 // --- Main loop starts here ---
176 while (this.getClient().isRunning()) {
177 // The application is still active, show menu selection
178 this.getClient().showCurrentMenu();
181 // Ask for user input and run proper method
182 this.getClient().doUserMenuChoice();
183 } catch (final UnhandledUserChoiceException ex) {
184 this.getLogger().catching(ex);
188 // Sleep a little to reduce system load
190 } catch (final InterruptedException ex) {
194 // --- Main loop ends here ---
197 this.getLogger().debug("Main loop exit - shutting down ..."); //NOI18N
201 * Enables console client by setting propper flag
203 private void enableConsoleClient () {
204 this.getLogger().debug("Enabling console client (may become optional client) ..."); //NOI18N
205 this.consoleClient = true;
206 this.guiClient = false;
210 * Enables GUI client by setting propper flag
212 private void enableGuiClient () {
213 this.getLogger().debug("Enabling GUI client (may become new default client) ..."); //NOI18N
214 this.consoleClient = false;
215 this.guiClient = true;
219 * Checks whether the client shoule be console client should be launched by
220 * checking if -console is set.
222 * @return Whether console client should be taken
224 private boolean isConsole () {
225 return this.consoleClient;
229 * Checks whether the client shoule be GUI client should be launched by
230 * checking if -gui is set.
232 * @return Whether GUI client should be taken
234 private boolean isGui () {
235 return this.guiClient;
239 * Parses all given arguments
241 * @param args Arguments from program launch
243 private void parseArguments (final String[] args) {
245 this.getLogger().trace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
248 this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
250 for (final String arg : args) {
253 case "-console": //NOI18N
254 enableConsoleClient();
257 case "-gui": //NOI18N
265 * Show introduction which depends on client
267 private void showIntro () {
269 this.getLogger().trace("CALLED!"); //NOI18N
271 // Let the client show it
272 this.getClient().showWelcome();
275 this.getLogger().trace("EXIT!"); //NOI18N
279 * Launches the application
281 * @param args Arguments handled to program
283 private void start (final String args[]) {
284 this.getLogger().info("Program is started."); //NOI18N
287 this.parseArguments(args);
289 // Launch application
290 ApplicationManager.getManager(this).start();
292 // Good bye, but this should not be reached ...
293 this.getLogger().warn("Unusual exit reached."); //NOI18N
298 * Main method (entry point)
300 * @param args the command line arguments
302 public static void main (String[] args) {
304 new AddressbookApplication().start(args);
308 * Shuts down the application.
311 public void doShutdown () {
313 this.getLogger().trace("CALLED!"); //NOI18N
316 this.getClient().doShutdown();
318 this.getLogger().info("End of program (last line)"); //NOI18N
323 * Getter for this application
325 * @return Instance from this application
327 public static final Application getInstance () {