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
75 public class AddressbookApplication extends BaseFrameworkSystem implements Application {
80 public static final String APP_TITLE = "Adressbuch"; //NOI18N
85 public static final String APP_VERSION = "0.0"; //NOI18N
90 private static Application selfInstance;
93 * Console client is enabled by default
95 private boolean consoleClient = true;
98 * GUI client is disabled by default
100 private boolean guiClient = false;
103 * Protected constructor
105 protected AddressbookApplication () {
111 * Getter for printable application name
113 * @return A printable name
115 public static String printableTitle () {
116 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
120 * Bootstraps application
123 public void doBootstrap () {
124 this.getLogger().debug("Initializing application ..."); //NOI18N
126 // Init client variable
127 Client client = null;
129 // Is console or Swing choosen?
130 if (this.isConsole()) {
132 this.getLogger().debug("Initializing console client ..."); //NOI18N
134 // Init console client instance
135 client = new ConsoleClient(this);
136 } else if (this.isGui()) {
138 this.getLogger().debug("Initializing GUI (Swing) client ..."); //NOI18N
140 // Init console instance
141 client = new SwingClient(this);
143 // Not client choosen
144 this.getLogger().error("No client choosen. Cannot launch."); //NOI18N
151 // Set client instance
152 this.setClient(client);
154 // The application is running at this point
155 this.getClient().enableIsRunning();
158 this.getLogger().trace("EXIT!"); //NOI18N
162 * Main loop of the application
165 public void doMainLoop () {
167 this.getLogger().trace("CALLED!"); //NOI18N
169 // @TODO The application should be running now
170 // Output introduction
173 // Set current menu to main
174 this.getClient().setCurrentMenu("main"); //NOI18N
176 // --- Main loop starts here ---
177 while (this.getClient().isRunning()) {
178 // The application is still active, show menu selection
179 this.getClient().showCurrentMenu();
182 // Ask for user input and run proper method
183 this.getClient().doUserMenuChoice();
184 } catch (final UnhandledUserChoiceException ex) {
185 this.getLogger().catching(ex);
189 // Sleep a little to reduce system load
191 } catch (final InterruptedException ex) {
195 // --- Main loop ends here ---
198 this.getLogger().debug("Main loop exit - shutting down ..."); //NOI18N
202 * Enables console client by setting propper flag
204 private void enableConsoleClient () {
205 this.getLogger().debug("Enabling console client (may become optional client) ..."); //NOI18N
206 this.consoleClient = true;
207 this.guiClient = false;
211 * Enables GUI client by setting propper flag
213 private void enableGuiClient () {
214 this.getLogger().debug("Enabling GUI client (may become new default client) ..."); //NOI18N
215 this.consoleClient = false;
216 this.guiClient = true;
220 * Checks whether the client shoule be console client should be launched by
221 * checking if -console is set.
223 * @return Whether console client should be taken
225 private boolean isConsole () {
226 return this.consoleClient;
230 * Checks whether the client shoule be GUI client should be launched by
231 * checking if -gui is set.
233 * @return Whether GUI client should be taken
235 private boolean isGui () {
236 return this.guiClient;
240 * Parses all given arguments
242 * @param args Arguments from program launch
244 private void parseArguments (final String[] args) {
246 this.getLogger().trace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
249 this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
251 for (final String arg : args) {
254 case "-console": //NOI18N
255 enableConsoleClient();
258 case "-gui": //NOI18N
266 * Show introduction which depends on client
268 private void showIntro () {
270 this.getLogger().trace("CALLED!"); //NOI18N
272 // Let the client show it
273 this.getClient().showWelcome();
276 this.getLogger().trace("EXIT!"); //NOI18N
280 * Launches the application
282 * @param args Arguments handled to program
284 private void start (final String args[]) {
285 this.getLogger().info("Program is started."); //NOI18N
288 this.parseArguments(args);
290 // Launch application
291 ApplicationManager.getManager(this).start();
293 // Good bye, but this should not be reached ...
294 this.getLogger().warn("Unusual exit reached."); //NOI18N
299 * Main method (entry point)
301 * @param args the command line arguments
303 public static void main (String[] args) {
305 new AddressbookApplication().start(args);
309 * Shuts down the application.
312 public void doShutdown () {
314 this.getLogger().trace("CALLED!"); //NOI18N
317 this.getClient().doShutdown();
319 this.getLogger().info("End of program (last line)"); //NOI18N
324 * Getter for this application
326 * @return Instance from this application
328 public static final Application getInstance () {