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
117 public class AddressbookApplication extends BaseFrameworkSystem implements Application {
122 public static final String APP_TITLE = "Adressbuch"; //NOI18N
125 * Application version
127 public static final String APP_VERSION = "0.0"; //NOI18N
132 private static Application selfInstance;
135 * Console client is enabled by default
137 private boolean consoleClient = true;
140 * GUI client is disabled by default
142 private boolean guiClient = false;
145 * Protected constructor
147 protected AddressbookApplication () {
153 * Getter for printable application name
155 * @return A printable name
157 public static String printableTitle () {
158 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
162 * Bootstraps application
165 public void doBootstrap () {
166 this.getLogger().debug("Initializing application ..."); //NOI18N
168 // Init client variable
169 Client client = null;
171 // Is console or Swing choosen?
172 if (this.isConsole()) {
174 this.getLogger().debug("Initializing console client ..."); //NOI18N
176 // Init console client instance
177 client = new ConsoleClient(this);
178 } else if (this.isGui()) {
180 this.getLogger().debug("Initializing GUI (Swing) client ..."); //NOI18N
182 // Init console instance
183 client = new SwingClient(this);
185 // Not client choosen
186 this.getLogger().error("No client choosen. Cannot launch."); //NOI18N
193 // Set client instance
194 this.setClient(client);
196 // The application is running at this point
197 this.getClient().enableIsRunning();
200 this.getLogger().trace("EXIT!"); //NOI18N
204 * Main loop of the application
207 public void doMainLoop () {
209 this.getLogger().trace("CALLED!"); //NOI18N
211 // @TODO The application should be running now
212 // Output introduction
215 // Set current menu to main
216 this.getClient().setCurrentMenu("main"); //NOI18N
218 // --- Main loop starts here ---
219 while (this.getClient().isRunning()) {
220 // The application is still active, show menu selection
221 this.getClient().showCurrentMenu();
224 // Ask for user input and run proper method
225 this.getClient().doUserMenuChoice();
226 } catch (final UnhandledUserChoiceException ex) {
227 this.getLogger().catching(ex);
231 // Sleep a little to reduce system load
233 } catch (final InterruptedException ex) {
237 // --- Main loop ends here ---
240 this.getLogger().debug("Main loop exit - shutting down ..."); //NOI18N
244 * Enables console client by setting propper flag
246 private void enableConsoleClient () {
247 this.getLogger().debug("Enabling console client (may become optional client) ..."); //NOI18N
248 this.consoleClient = true;
249 this.guiClient = false;
253 * Enables GUI client by setting propper flag
255 private void enableGuiClient () {
256 this.getLogger().debug("Enabling GUI client (may become new default client) ..."); //NOI18N
257 this.consoleClient = false;
258 this.guiClient = true;
262 * Checks whether the client shoule be console client should be launched by
263 * checking if -console is set.
265 * @return Whether console client should be taken
267 private boolean isConsole () {
268 return this.consoleClient;
272 * Checks whether the client shoule be GUI client should be launched by
273 * checking if -gui is set.
275 * @return Whether GUI client should be taken
277 private boolean isGui () {
278 return this.guiClient;
282 * Parses all given arguments
284 * @param args Arguments from program launch
286 private void parseArguments (final String[] args) {
288 this.getLogger().trace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
291 this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
293 for (final String arg : args) {
296 case "-console": //NOI18N
297 enableConsoleClient();
300 case "-gui": //NOI18N
308 * Show introduction which depends on client
310 private void showIntro () {
312 this.getLogger().trace("CALLED!"); //NOI18N
314 // Let the client show it
315 this.getClient().showWelcome();
318 this.getLogger().trace("EXIT!"); //NOI18N
322 * Launches the application
324 * @param args Arguments handled to program
326 private void start (final String args[]) {
327 this.getLogger().info("Program is started."); //NOI18N
330 this.parseArguments(args);
332 // Launch application
333 ApplicationManager.getManager(this).start();
335 // Good bye, but this should not be reached ...
336 this.getLogger().warn("Unusual exit reached."); //NOI18N
341 * Main method (entry point)
343 * @param args the command line arguments
345 public static void main (String[] args) {
347 new AddressbookApplication().start(args);
351 * Shuts down the application.
354 public void doShutdown () {
356 this.getLogger().trace("CALLED!"); //NOI18N
359 this.getClient().doShutdown();
361 this.getLogger().info("End of program (last line)"); //NOI18N
366 * Getter for this application
368 * @return Instance from this application
370 public static final Application getInstance () {