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.io.IOException;
20 import java.text.MessageFormat;
21 import org.mxchange.addressbook.BaseAddressbookSystem;
22 import org.mxchange.addressbook.client.AddressbookClient;
23 import org.mxchange.addressbook.client.console.ConsoleClient;
24 import org.mxchange.addressbook.client.gui.SwingClient;
25 import org.mxchange.jcore.BaseFrameworkSystem;
26 import org.mxchange.jcore.application.Application;
27 import org.mxchange.jcore.client.Client;
28 import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
29 import org.mxchange.jcore.manager.application.ApplicationManager;
32 * ============================================
33 * AddressbookApplication management:
34 * ============================================
36 * Inernet("public" service) and Intranet
39 * - Single-user local application
58 * - Categorization of contacts
61 * - Permanent storage in database
64 * - Multi-user web application
65 * - Local user registration / login / resend confirmation link / password
67 * - User groups (aka. teams)
68 * - Administration area (user role)
69 * + Create/edit/delete groups
70 * + Edit/delete/lock/unlock user
71 * + Assign user roles/rights
72 * - Allow other users / groups to view addressbook
74 * + Only some categories
76 * + Allow users/guests (not recommended)
77 * - XML export of addressbook and compressable (ZIP)
78 * - Form to contact other user/group without need of mail program
79 * + User can disabled this
80 * - Directory for ussers/groups (who allowed to be listed)
81 * + Simple click to add user to own addressbook
85 * - Multi-language support
87 * Version 2.2+:("socialized")
88 * - "Social login" (OpenID consumer)
89 * + Connect user account to social account
95 * ============================================
97 * ============================================
103 * + Initial tables: contacts, categories, contact_category
107 * + Additional tables: admins (?), admin_rights, groups,
108 * users, user_contacts, user_user_rights, user_category_rights,
112 * + Additional tables: languages (disable, enable language "pack" ?)
116 * + Additional tables: ???
118 * @author Roland Haeder
121 public class AddressbookApplication extends BaseAddressbookSystem implements Application {
126 public static final String APP_TITLE = "Adressbuch"; //NOI18N
129 * Application version
131 public static final String APP_VERSION = "0.0"; //NOI18N
134 * Console client is enabled by default
136 private boolean consoleClient = true;
139 * GUI client is disabled by default
141 private boolean guiClient = false;
144 * Protected constructor
145 * @throws java.io.IOException If any IO error occurs
147 protected AddressbookApplication () throws IOException {
148 // Init properties file
149 this.initProperties();
156 * Bootstraps application
159 public void doBootstrap () {
160 this.getLogger().debug("Initializing application ..."); //NOI18N
162 // Init client variable
163 Client client = null;
165 // Is console or Swing choosen?
166 if (this.isConsole()) {
168 this.getLogger().debug("Initializing console client ..."); //NOI18N
170 // Init console client instance
171 client = new ConsoleClient(this);
172 } else if (this.isGui()) {
174 this.getLogger().debug("Initializing GUI (Swing) client ..."); //NOI18N
176 // Init console instance
177 client = new SwingClient(this);
179 // Not client choosen
180 this.getLogger().error("No client choosen. Cannot launch."); //NOI18N
187 // Set client instance
188 this.setClient(client);
190 // The application is running at this point
191 this.getClient().enableIsRunning();
194 this.getLogger().trace("EXIT!"); //NOI18N
198 * Main loop of the application
201 public void doMainLoop () {
202 // Get client and cast it
203 AddressbookClient client = (AddressbookClient) this.getClient();
206 this.getLogger().trace("CALLED!"); //NOI18N
208 // @TODO The application should be running now
209 // Output introduction
212 // Set current menu to main
213 client.setCurrentMenu("main"); //NOI18N
215 // --- Main loop starts here ---
216 while (this.getClient().isRunning()) {
217 // The application is still active, show menu selection
218 client.showCurrentMenu();
221 // Ask for user input and run proper method
222 client.doUserMenuChoice();
223 } catch (final UnhandledUserChoiceException ex) {
224 this.getLogger().catching(ex);
228 // Sleep a little to reduce system load
230 } catch (final InterruptedException ex) {
234 // --- Main loop ends here ---
237 this.getLogger().debug("Main loop exit - shutting down ..."); //NOI18N
241 * Shuts down the application.
244 public void doShutdown () {
246 this.getLogger().trace("CALLED!"); //NOI18N
249 this.getClient().doShutdown();
251 this.getLogger().info("End of program (last line)"); //NOI18N
256 * Enables console client by setting propper flag
258 private void enableConsoleClient () {
259 this.getLogger().debug("Enabling console client (may become optional client) ..."); //NOI18N
260 this.consoleClient = true;
261 this.guiClient = false;
265 * Enables GUI client by setting propper flag
267 private void enableGuiClient () {
268 this.getLogger().debug("Enabling GUI client (may become new default client) ..."); //NOI18N
269 this.consoleClient = false;
270 this.guiClient = true;
274 * Checks whether the client shoule be console client should be launched by
275 * checking if -console is set.
277 * @return Whether console client should be taken
279 private boolean isConsole () {
280 return this.consoleClient;
284 * Checks whether the client shoule be GUI client should be launched by
285 * checking if -gui is set.
287 * @return Whether GUI client should be taken
289 private boolean isGui () {
290 return this.guiClient;
294 * Parses all given arguments
296 * @param args Arguments from program launch
298 private void parseArguments (final String[] args) {
300 this.getLogger().trace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
303 this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
305 for (final String arg : args) {
308 case "-console": //NOI18N
309 enableConsoleClient();
312 case "-gui": //NOI18N
320 * Show introduction which depends on client
322 private void showIntro () {
324 this.getLogger().trace("CALLED!"); //NOI18N
326 // Let the client show it
327 this.getClient().showWelcome();
330 this.getLogger().trace("EXIT!"); //NOI18N
334 * Launches the application
336 * @param args Arguments handled to program
338 private void start (final String args[]) {
339 this.getLogger().info("Program is started."); //NOI18N
342 this.parseArguments(args);
344 // Launch application
345 ApplicationManager.getManager(this).start();
347 // Good bye, but this should not be reached ...
348 this.getLogger().warn("Unusual exit reached."); //NOI18N
353 * Main method (entry point)
355 * @param args the command line arguments
357 public static void main (String[] args) {
360 new AddressbookApplication().start(args);
361 } catch (final IOException ex) {
363 BaseFrameworkSystem.getInstance().getLogger().catching(ex);
369 * Getter for printable application name
371 * @return A printable name
373 public static String printableTitle () {
374 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N