From: Roland Haeder Date: Tue, 21 Jul 2015 13:10:28 +0000 (+0200) Subject: Added a lot stuff: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e06df9f02fa3fc03c21d5b2c3e2c8cb476be4ea3;p=addressbook-swing.git Added a lot stuff: - Introduced doShutdown() - Added status bar to Swing frame - Added "Exit program" menu item Signed-off-by:Roland Häder --- diff --git a/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java b/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java index 0b6e918..788c940 100644 --- a/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java +++ b/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java @@ -136,6 +136,15 @@ public class AddressbookApplication extends BaseFrameworkSystem implements Appli */ private boolean guiClient = false; + /** + * Getter for printable application name + * + * @return A printable name + */ + public static String printableTitle () { + return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); + } + /** * Bootstraps application */ @@ -289,7 +298,9 @@ public class AddressbookApplication extends BaseFrameworkSystem implements Appli // Launch application ApplicationManager.getManager(this).start(); - this.getLogger().info("End of program (last line)"); + // Good bye, but this should not be reached ... + this.getLogger().warn("Unusual exit reached."); + this.doShutdown(); } /** @@ -301,4 +312,16 @@ public class AddressbookApplication extends BaseFrameworkSystem implements Appli // Start application new AddressbookApplication().start(args); } + + /** + * Shuts down the application. + */ + @Override + public void doShutdown () { + // Shutdown client + this.getClient().doShutdown(); + + this.getLogger().info("End of program (last line)"); + System.exit(0); + } } diff --git a/Addressbook/src/org/mxchange/addressbook/application/Application.java b/Addressbook/src/org/mxchange/addressbook/application/Application.java index 503cfe5..aa773e5 100644 --- a/Addressbook/src/org/mxchange/addressbook/application/Application.java +++ b/Addressbook/src/org/mxchange/addressbook/application/Application.java @@ -32,4 +32,9 @@ public interface Application extends FrameworkInterface { * Run the main loop */ public void doMainLoop(); + + /** + * Shutdown the application + */ + public void doShutdown (); } diff --git a/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java b/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java index 04b1ad5..0282c91 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java +++ b/Addressbook/src/org/mxchange/addressbook/client/BaseClient.java @@ -56,10 +56,14 @@ public abstract class BaseClient extends BaseFrameworkSystem { } /** - * Disables running state, so the main loop can abort. + * Shutdown method for all clients */ - public void disableIsRunning () { - this.isRunning = false; + public void doShutdown () { + // Disable client + this.disableIsRunning(); + + // Shuts down contact manager + this.getContactManager().doShutdown(); } /** @@ -117,6 +121,13 @@ public abstract class BaseClient extends BaseFrameworkSystem { return this.isRunning; } + /** + * Disables running state, so the main loop can abort. + */ + protected void disableIsRunning () { + this.isRunning = false; + } + /** * Fills menu map with swing menus */ @@ -130,6 +141,24 @@ public abstract class BaseClient extends BaseFrameworkSystem { return this.menus; } + /** + * Initializes contact manager + */ + protected void initContactManager () { + // Debug message + this.getLogger().debug("Initializing contact manager ..."); + + // Init contact manager with console client + // @TODO Static initial amount of contacts + ManageableContact manager = new ContactManager (100, (Client) this); + + // Set it here + this.setContactManager(manager); + + // Debug message + this.getLogger().debug("Contact manager has been initialized."); + } + /** * Shows given menu * @@ -148,22 +177,4 @@ public abstract class BaseClient extends BaseFrameworkSystem { // Show menu menu.show((Client) this); } - - /** - * Initializes contact manager - */ - protected void initContactManager () { - // Debug message - this.getLogger().debug("Initializing contact manager ..."); - - // Init contact manager with console client - // @TODO Static initial amount of contacts - ManageableContact manager = new ContactManager (100, (Client) this); - - // Set it here - this.setContactManager(manager); - - // Debug message - this.getLogger().debug("Contact manager has been initialized."); - } } diff --git a/Addressbook/src/org/mxchange/addressbook/client/Client.java b/Addressbook/src/org/mxchange/addressbook/client/Client.java index a776c88..0d5e154 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/Client.java +++ b/Addressbook/src/org/mxchange/addressbook/client/Client.java @@ -35,6 +35,11 @@ public interface Client extends FrameworkInterface { */ public void displayAddressBox (final Contact contact); + /** + * Shuts down the client and therefore whole application + */ + public void doShutdown (); + /** * Displays a message to the user * diff --git a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java index 82d80d8..4803fee 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java +++ b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java @@ -101,7 +101,18 @@ public class ConsoleClient extends BaseClient implements Client { @Override public void displayOtherDataBox (final Contact contact) { // Cellphone and such ... - this.outputMessage("Telefonnumer: " + contact.getPhoneNumber() + "\nFaxnummer: " + contact.getFaxNumber() + "\nHandy: " + contact.getCellphoneNumber() + "\nKommentar:\n" + contact.getComment()); + this.outputMessage(MessageFormat.format("Telefonnumer: {0}\nFaxnummer: {1}\nHandy: {2}\nKommentar:\n{3}", contact.getPhoneNumber(), contact.getFaxNumber(), contact.getCellphoneNumber(), contact.getComment())); + } + + /** + * Shutdown this client + */ + @Override + public void doShutdown () { + // Parent call + super.doShutdown(); + + // @TODO Add other shutdown stuff } @Override @@ -143,12 +154,12 @@ public class ConsoleClient extends BaseClient implements Client { break; case '0': // Program exit - this.disableIsRunning(); + this.getApplication().doShutdown(); break; default: // @TODO throw own exception - throw new UnhandledUserChoiceException("Choice '" + choice + "' not handled yet."); + throw new UnhandledUserChoiceException(MessageFormat.format("Choice '{0}' not handled yet.", choice)); } } @@ -296,7 +307,7 @@ public class ConsoleClient extends BaseClient implements Client { */ @Override public void showWelcome () { - this.outputMessage("Welcome to " + AddressbookApplication.APP_TITLE + " v" + AddressbookApplication.APP_VERSION); + this.outputMessage("Welcome to " + AddressbookApplication.printableTitle()); this.outputMessage(""); this.outputMessage("Copyright(c) 2015 by Roland Haeder, this is free software"); diff --git a/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.form b/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.form index 112b81f..7c1b688 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.form +++ b/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.form @@ -1,10 +1,58 @@
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -23,13 +71,43 @@ - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java b/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java index 1a629c0..6b0e835 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java +++ b/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java @@ -18,6 +18,7 @@ package org.mxchange.addressbook.client.gui; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.mxchange.addressbook.application.AddressbookApplication; import org.mxchange.addressbook.application.Application; import org.mxchange.addressbook.client.Client; import org.mxchange.addressbook.manager.contact.ManageableContact; @@ -83,22 +84,67 @@ public class AddressbookFrame extends javax.swing.JFrame implements ClientFrame // //GEN-BEGIN:initComponents private void initComponents() { + statusPane = new javax.swing.JScrollPane(); + statusText = new javax.swing.JTextPane(); + mainMenu = new javax.swing.JMenuBar(); + jMenu1 = new javax.swing.JMenu(); + jMenuItem1 = new javax.swing.JMenuItem(); + jMenu2 = new javax.swing.JMenu(); + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + setTitle(AddressbookApplication.printableTitle()); + setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); + setName("main"); // NOI18N + + statusText.setBackground(new java.awt.Color(240, 240, 240)); + java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/mxchange/addressbook/client/gui/Bundle"); // NOI18N + statusText.setText(bundle.getString("AddressbookFrame.status.text")); // NOI18N + statusText.setName("status"); // NOI18N + statusPane.setViewportView(statusText); + statusText.getAccessibleContext().setAccessibleName(bundle.getString("AddressbookFrame.status.AccessibleContext.accessibleName")); // NOI18N + + jMenu1.setText(bundle.getString("AddressbookFrame.text")); // NOI18N + jMenu1.setFocusable(false); + jMenu1.setName(""); // NOI18N + + jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.ALT_MASK)); + jMenuItem1.setText(bundle.getString("AddressbookFrame.jMenuItem1.text")); // NOI18N + jMenuItem1.setToolTipText(bundle.getString("AddressbookFrame.jMenuItem1.toolTipText")); // NOI18N + jMenuItem1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jMenuItem1ActionPerformed(evt); + } + }); + jMenu1.add(jMenuItem1); + + mainMenu.add(jMenu1); + + jMenu2.setText(bundle.getString("AddressbookFrame.jMenu2.text")); // NOI18N + mainMenu.add(jMenu2); + + setJMenuBar(mainMenu); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGap(0, 400, Short.MAX_VALUE) + .addComponent(statusPane, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGap(0, 300, Short.MAX_VALUE) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addGap(0, 257, Short.MAX_VALUE) + .addComponent(statusPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) ); pack(); }// //GEN-END:initComponents + private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed + // Close application instance + this.getClient().getApplication().doShutdown(); + }//GEN-LAST:event_jMenuItem1ActionPerformed + /** * Initializes the frame */ @@ -155,5 +201,11 @@ public class AddressbookFrame extends javax.swing.JFrame implements ClientFrame } // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JMenu jMenu1; + private javax.swing.JMenu jMenu2; + private javax.swing.JMenuItem jMenuItem1; + private javax.swing.JMenuBar mainMenu; + private javax.swing.JScrollPane statusPane; + private javax.swing.JTextPane statusText; // End of variables declaration//GEN-END:variables } diff --git a/Addressbook/src/org/mxchange/addressbook/client/gui/Bundle.properties b/Addressbook/src/org/mxchange/addressbook/client/gui/Bundle.properties new file mode 100644 index 0000000..f499144 --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/client/gui/Bundle.properties @@ -0,0 +1,6 @@ +AddressbookFrame.jMenu2.text=Edit +AddressbookFrame.text=File +AddressbookFrame.jMenuItem1.text=Exit program +AddressbookFrame.jMenuItem1.toolTipText=Exits the program cleanly. +AddressbookFrame.status.AccessibleContext.accessibleName= +AddressbookFrame.status.text=Welcome ... diff --git a/Addressbook/src/org/mxchange/addressbook/client/gui/SwingClient.java b/Addressbook/src/org/mxchange/addressbook/client/gui/SwingClient.java index 88dfb96..3aec6e5 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/gui/SwingClient.java +++ b/Addressbook/src/org/mxchange/addressbook/client/gui/SwingClient.java @@ -63,9 +63,17 @@ public class SwingClient extends BaseClient implements Client { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } + @Override + public void doShutdown () { + // Parent call + super.doShutdown(); + + // @TODO Add other shutdown stuff + } + @Override public void doUserMenuChoice () throws UnhandledUserChoiceException { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + // Not implemented here } @Override @@ -121,7 +129,7 @@ public class SwingClient extends BaseClient implements Client { @Override public void showCurrentMenu () { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + // Not implemented here } @Override @@ -131,7 +139,7 @@ public class SwingClient extends BaseClient implements Client { @Override public void showWelcome () { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + // Not implemented here } @Override diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java index fad842e..5b2d42d 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java @@ -27,6 +27,11 @@ import org.mxchange.addressbook.database.storage.Storeable; */ public interface DatabaseBackend extends FrameworkInterface { + /** + * Shuts down this backend + */ + public void doShutdown (); + /** * Rewinds backend */ diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java index 750d60e..0bc97f3 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java @@ -93,6 +93,20 @@ public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBacken return list.iterator(); } + /** + * Shuts down this backend + */ + @Override + public void doShutdown () { + try { + // Close file + this.storageFile.close(); + } catch (final IOException ex) { + this.getLogger().catching(ex); + System.exit(1); + } + } + /** * Get length of underlaying file * diff --git a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java index 87126f0..4065c25 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java @@ -45,6 +45,15 @@ public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements Con this.initBackend(); } + /** + * Shuts down the database layer + */ + @Override + public void doShutdown () { + // Shutdown backend + this.getBackend().doShutdown(); + } + /** * Flushes all contact entries to database * @param contactManager An instance of a MangeableContact class diff --git a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactWrapper.java b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactWrapper.java index ce574ab..e9f710a 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactWrapper.java +++ b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactWrapper.java @@ -25,6 +25,11 @@ import org.mxchange.addressbook.manager.contact.ManageableContact; */ public interface ContactWrapper extends DatabaseWrapper { + /** + * Shuts down the database layer + */ + public void doShutdown (); + /** * Flushes all contact entries to database * diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java index 0499011..1b269ec 100644 --- a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java @@ -279,6 +279,15 @@ public class ContactManager extends BaseManager implements ManageableContact { this.registerContact(contact); } + /** + * Shuts down this contact manager + */ + @Override + public void doShutdown () { + // Shut down the database layer + this.contactDatabase.doShutdown(); + } + /** * Getter for whole contact list * diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java index a928505..520f4ab 100644 --- a/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java @@ -27,6 +27,11 @@ import org.mxchange.addressbook.manager.Manageable; */ public interface ManageableContact extends Manageable { + /** + * Shuts down this contact manager + */ + public void doShutdown (); + /** * List all contacts */