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 * ============================================
29 * AddressbookApplication management:
30 * ============================================
32 * Inernet("public" service) and Intranet
35 * - Single-user local application
54 * - Categorization of contacts
57 * - Permanent storage in database
60 * - Multi-user web application
61 * - Local user registration / login / resend confirmation link / password
63 * - User groups (aka. teams)
64 * - Administration area (user role)
65 * + Create/edit/delete groups
66 * + Edit/delete/lock/unlock user
67 * + Assign user roles/rights
68 * - Allow other users / groups to view addressbook
70 * + Only some categories
72 * + Allow users/guests (not recommended)
73 * - XML export of addressbook and compressable (ZIP)
74 * - Form to contact other user/group without need of mail program
75 * + User can disabled this
76 * - Directory for ussers/groups (who allowed to be listed)
77 * + Simple click to add user to own addressbook
81 * - Multi-language support
83 * Version 2.2+:("socialized")
84 * - "Social login" (OpenID consumer)
85 * + Connect user account to social account
91 * ============================================
93 * ============================================
99 * + Initial tables: contacts, categories, contact_category
103 * + Additional tables: admins (?), admin_rights, groups,
104 * users, user_contacts, user_user_rights, user_category_rights,
108 * + Additional tables: languages (disable, enable language "pack" ?)
112 * + Additional tables: ???
114 * @author Roland Haeder
118 public class AddressbookApplication extends BaseFrameworkSystem implements Application {
123 public static final String APP_TITLE = "Adressbuch";
126 * Application version
128 public static final String APP_VERSION = "0.0";
133 private static Application selfInstance;
136 * Console client is enabled by default
138 private boolean consoleClient = true;
141 * GUI client is disabled by default
143 private boolean guiClient = false;
146 * Protected constructor
148 protected AddressbookApplication () {
154 * Getter for printable application name
156 * @return A printable name
158 public static String printableTitle () {
159 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION);
163 * Bootstraps application
166 public void doBootstrap () {
167 this.getLogger().debug("Initializing application ...");
169 // Init client variable
170 Client client = null;
172 // Is console or Swing choosen?
173 if (this.isConsole()) {
175 this.getLogger().debug("Initializing console client ...");
177 // Init console client instance
178 client = new ConsoleClient(this);
179 } else if (this.isGui()) {
181 this.getLogger().debug("Initializing GUI (Swing) client ...");
183 // Init console instance
184 client = new SwingClient(this);
186 // Not client choosen
187 this.getLogger().error("No client choosen. Cannot launch.");
194 // Set client instance
195 this.setClient(client);
197 // The application is running at this point
198 this.getClient().enableIsRunning();
202 * Main loop of the application
205 public void doMainLoop () {
207 this.getLogger().trace("CALLED!");
209 // @TODO The application should be running now
210 // Output introduction
213 // Set current menu to main
214 this.getClient().setCurrentMenu("main");
216 // --- Main loop starts here ---
217 while (this.getClient().isRunning()) {
218 // The application is still active, show menu selection
219 this.getClient().showCurrentMenu();
222 // Ask for user input and run proper method
223 this.getClient().doUserMenuChoice();
224 } catch (final UnhandledUserChoiceException ex) {
225 this.getLogger().catching(ex);
229 // Sleep a little to reduce system load
231 } catch (final InterruptedException ex) {
235 // --- Main loop ends here ---
238 this.getLogger().debug("Main loop exit - shutting down ...");
242 * Enables console client by setting propper flag
244 private void enableConsoleClient () {
245 this.getLogger().debug("Enabling console client (may become optional client) ...");
246 this.consoleClient = true;
247 this.guiClient = false;
251 * Enables GUI client by setting propper flag
253 private void enableGuiClient () {
254 this.getLogger().debug("Enabling GUI client (may become new default client) ...");
255 this.consoleClient = false;
256 this.guiClient = true;
260 * Checks whether the client shoule be console client should be launched by
261 * checking if -console is set.
263 * @return Whether console client should be taken
265 private boolean isConsole () {
266 return this.consoleClient;
270 * Checks whether the client shoule be GUI client should be launched by
271 * checking if -gui is set.
273 * @return Whether GUI client should be taken
275 private boolean isGui () {
276 return this.guiClient;
280 * Parses all given arguments
282 * @param args Arguments from program launch
284 private void parseArguments (final String[] args) {
286 this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length));
288 for (final String arg : args) {
292 enableConsoleClient();
303 * Show introduction which depends on client
305 private void showIntro () {
306 // Let the client show it
307 this.getClient().showWelcome();
311 * Launches the application
313 * @param args Arguments handled to program
315 private void start (final String args[]) {
316 this.getLogger().info("Program is started.");
319 this.parseArguments(args);
321 // Launch application
322 ApplicationManager.getManager(this).start();
324 // Good bye, but this should not be reached ...
325 this.getLogger().warn("Unusual exit reached.");
330 * Main method (entry point)
332 * @param args the command line arguments
334 public static void main (String[] args) {
336 new AddressbookApplication().start(args);
340 * Shuts down the application.
343 public void doShutdown () {
345 this.getClient().doShutdown();
347 this.getLogger().info("End of program (last line)");
352 * Getter for this application
354 * @return Instance from this application
356 public static final Application getInstance () {