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.BaseAddressbookSystem;
21 import org.mxchange.addressbook.client.AddressbookClient;
22 import org.mxchange.addressbook.client.console.ConsoleClient;
23 import org.mxchange.addressbook.client.gui.SwingClient;
24 import org.mxchange.jcore.application.Application;
25 import org.mxchange.jcore.client.Client;
26 import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
27 import org.mxchange.jcore.manager.application.ApplicationManager;
30 * ============================================
31 * AddressbookApplication management:
32 * ============================================
34 * Inernet("public" service) and Intranet
37 * - Single-user local application
56 * - Categorization of contacts
59 * - Permanent storage in database
62 * - Multi-user web application
63 * - Local user registration / login / resend confirmation link / password
65 * - User groups (aka. teams)
66 * - Administration area (user role)
67 * + Create/edit/delete groups
68 * + Edit/delete/lock/unlock user
69 * + Assign user roles/rights
70 * - Allow other users / groups to view addressbook
72 * + Only some categories
74 * + Allow users/guests (not recommended)
75 * - XML export of addressbook and compressable (ZIP)
76 * - Form to contact other user/group without need of mail program
77 * + User can disabled this
78 * - Directory for ussers/groups (who allowed to be listed)
79 * + Simple click to add user to own addressbook
83 * - Multi-language support
85 * Version 2.2+:("socialized")
86 * - "Social login" (OpenID consumer)
87 * + Connect user account to social account
93 * ============================================
95 * ============================================
101 * + Initial tables: contacts, categories, contact_category
105 * + Additional tables: admins (?), admin_rights, groups,
106 * users, user_contacts, user_user_rights, user_category_rights,
110 * + Additional tables: languages (disable, enable language "pack" ?)
114 * + Additional tables: ???
116 * @author Roland Haeder
119 public class AddressbookApplication extends BaseAddressbookSystem implements Application {
124 public static final String APP_TITLE = "Adressbuch"; //NOI18N
127 * Application version
129 public static final String APP_VERSION = "0.0"; //NOI18N
134 private static Application selfInstance;
137 * Console client is enabled by default
139 private boolean consoleClient = true;
142 * GUI client is disabled by default
144 private boolean guiClient = false;
147 * Protected constructor
149 protected AddressbookApplication () {
155 * Getter for printable application name
157 * @return A printable name
159 public static String printableTitle () {
160 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
164 * Bootstraps application
167 public void doBootstrap () {
168 this.getLogger().debug("Initializing application ..."); //NOI18N
170 // Init client variable
171 Client client = null;
173 // Is console or Swing choosen?
174 if (this.isConsole()) {
176 this.getLogger().debug("Initializing console client ..."); //NOI18N
178 // Init console client instance
179 client = new ConsoleClient(this);
180 } else if (this.isGui()) {
182 this.getLogger().debug("Initializing GUI (Swing) client ..."); //NOI18N
184 // Init console instance
185 client = new SwingClient(this);
187 // Not client choosen
188 this.getLogger().error("No client choosen. Cannot launch."); //NOI18N
195 // Set client instance
196 this.setClient(client);
198 // The application is running at this point
199 this.getClient().enableIsRunning();
202 this.getLogger().trace("EXIT!"); //NOI18N
206 * Main loop of the application
209 public void doMainLoop () {
210 // Get client and cast it
211 AddressbookClient client = (AddressbookClient) this.getClient();
214 this.getLogger().trace("CALLED!"); //NOI18N
216 // @TODO The application should be running now
217 // Output introduction
220 // Set current menu to main
221 client.setCurrentMenu("main"); //NOI18N
223 // --- Main loop starts here ---
224 while (this.getClient().isRunning()) {
225 // The application is still active, show menu selection
226 client.showCurrentMenu();
229 // Ask for user input and run proper method
230 client.doUserMenuChoice();
231 } catch (final UnhandledUserChoiceException ex) {
232 this.getLogger().catching(ex);
236 // Sleep a little to reduce system load
238 } catch (final InterruptedException ex) {
242 // --- Main loop ends here ---
245 this.getLogger().debug("Main loop exit - shutting down ..."); //NOI18N
249 * Enables console client by setting propper flag
251 private void enableConsoleClient () {
252 this.getLogger().debug("Enabling console client (may become optional client) ..."); //NOI18N
253 this.consoleClient = true;
254 this.guiClient = false;
258 * Enables GUI client by setting propper flag
260 private void enableGuiClient () {
261 this.getLogger().debug("Enabling GUI client (may become new default client) ..."); //NOI18N
262 this.consoleClient = false;
263 this.guiClient = true;
267 * Checks whether the client shoule be console client should be launched by
268 * checking if -console is set.
270 * @return Whether console client should be taken
272 private boolean isConsole () {
273 return this.consoleClient;
277 * Checks whether the client shoule be GUI client should be launched by
278 * checking if -gui is set.
280 * @return Whether GUI client should be taken
282 private boolean isGui () {
283 return this.guiClient;
287 * Parses all given arguments
289 * @param args Arguments from program launch
291 private void parseArguments (final String[] args) {
293 this.getLogger().trace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
296 this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
298 for (final String arg : args) {
301 case "-console": //NOI18N
302 enableConsoleClient();
305 case "-gui": //NOI18N
313 * Show introduction which depends on client
315 private void showIntro () {
317 this.getLogger().trace("CALLED!"); //NOI18N
319 // Let the client show it
320 this.getClient().showWelcome();
323 this.getLogger().trace("EXIT!"); //NOI18N
327 * Launches the application
329 * @param args Arguments handled to program
331 private void start (final String args[]) {
332 this.getLogger().info("Program is started."); //NOI18N
335 this.parseArguments(args);
337 // Launch application
338 ApplicationManager.getManager(this).start();
340 // Good bye, but this should not be reached ...
341 this.getLogger().warn("Unusual exit reached."); //NOI18N
346 * Main method (entry point)
348 * @param args the command line arguments
350 public static void main (String[] args) {
352 new AddressbookApplication().start(args);
356 * Shuts down the application.
359 public void doShutdown () {
361 this.getLogger().trace("CALLED!"); //NOI18N
364 this.getClient().doShutdown();
366 this.getLogger().info("End of program (last line)"); //NOI18N
371 * Getter for this application
373 * @return Instance from this application
375 public static final Application getInstance () {