From: Roland Haeder Date: Wed, 22 Jul 2015 06:29:27 +0000 (+0200) Subject: the contact manager instance only exists when initFrame() is called and not on object... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e48af74ac9a6c205711d04b4cf9bd9086546e8e9;p=addressbook-swing.git the contact manager instance only exists when initFrame() is called and not on object initialization Signed-off-by:Roland Häder --- diff --git a/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java b/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java index bf3e8e9..ea890cc 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java +++ b/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java @@ -56,9 +56,6 @@ public class AddressbookFrame extends javax.swing.JFrame implements ClientFrame // Init components initComponents(); - // Has the user entered own data? - //if (client.) - // Set client here this.client = client; } @@ -175,15 +172,26 @@ public class AddressbookFrame extends javax.swing.JFrame implements ClientFrame }//GEN-LAST:event_addOwnDataActionPerformed /** - * Initializes the frame + * Setups the frame + * + * @param client Client instance */ @Override - public void initFrame (final Client client) { + public void setupFrame (final Client client) { + // Has the user entered own data? + if (!this.getClient().getContactManager().isOwnContactAdded()) { + // Debug message + this.getLogger().debug("Enabling menus: isOwnContactAdded()=false"); + + // Not entered yet, so enable menu + addOwnData.setEnabled(true); + } + /* * Set the Nimbus look and feel */ - // - /* + // + /* * If Nimbus (introduced in Java SE 6) is not available, stay with the * default look and feel. For details see * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html diff --git a/Addressbook/src/org/mxchange/addressbook/client/gui/ClientFrame.java b/Addressbook/src/org/mxchange/addressbook/client/gui/ClientFrame.java index 21a822b..02f4cad 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/gui/ClientFrame.java +++ b/Addressbook/src/org/mxchange/addressbook/client/gui/ClientFrame.java @@ -25,9 +25,9 @@ import org.mxchange.addressbook.client.Client; */ public interface ClientFrame extends FrameworkInterface { /** - * Initializes the frame + * Setups the frame * * @param client Client instance */ - public void initFrame (final Client client); + public void setupFrame (final Client client); } diff --git a/Addressbook/src/org/mxchange/addressbook/client/gui/SwingClient.java b/Addressbook/src/org/mxchange/addressbook/client/gui/SwingClient.java index 3aec6e5..4a51c94 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/gui/SwingClient.java +++ b/Addressbook/src/org/mxchange/addressbook/client/gui/SwingClient.java @@ -17,7 +17,7 @@ package org.mxchange.addressbook.client.gui; import org.mxchange.addressbook.UnhandledUserChoiceException; -import org.mxchange.addressbook.application.AddressbookApplication; +import org.mxchange.addressbook.application.Application; import org.mxchange.addressbook.client.BaseClient; import org.mxchange.addressbook.client.Client; import org.mxchange.addressbook.contact.Contact; @@ -38,7 +38,7 @@ public class SwingClient extends BaseClient implements Client { * Constructor with application instance * @param application */ - public SwingClient (final AddressbookApplication application) { + public SwingClient (final Application application) { super(); // Set application instance @@ -102,7 +102,6 @@ public class SwingClient extends BaseClient implements Client { * @param accessKey Key to access the menu * @param text Text to show to user * @return A SelectableMenuItem - * @todo Make sure the access key is unique */ @Override public SelectableMenuItem getMenuItem (final char accessKey, final String text) { @@ -119,7 +118,7 @@ public class SwingClient extends BaseClient implements Client { this.initContactManager(); // Now start the frame - this.frame.initFrame(this); + this.frame.setupFrame(this); } @Override 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 4065c25..df1854c 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java @@ -24,7 +24,7 @@ import org.mxchange.addressbook.contact.Contact; import org.mxchange.addressbook.database.backend.csv.CsvBackend; import org.mxchange.addressbook.database.frontend.BaseDatabaseFrontend; import org.mxchange.addressbook.database.storage.Storeable; -import org.mxchange.addressbook.manager.contact.ManageableContact; +import org.mxchange.addressbook.manager.contact.ContactManager; /** * Stores and retrieves Contact instances @@ -32,10 +32,22 @@ import org.mxchange.addressbook.manager.contact.ManageableContact; * @author Roland Haeder */ public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements ContactWrapper { + /** + * Constructor which accepts a contact manager + * @param manager + */ + public ContactDatabaseFrontend (final ContactManager manager) { + // Call own constructor + this(); + + // Set contact manager + this.setContactManager(manager); + } + /** * Basic constrcutor */ - public ContactDatabaseFrontend () { + protected ContactDatabaseFrontend () { super(); // Set "table" name @@ -56,12 +68,11 @@ public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements Con /** * Flushes all contact entries to database - * @param contactManager An instance of a MangeableContact class */ @Override - public void flushAllContacts (final ManageableContact contactManager) { + public void flushAllContacts () { // Get full list - List contacts = contactManager.getList(); + List contacts = this.getContactManager().getList(); // Get iterator Iterator iterator = contacts.iterator(); @@ -88,11 +99,9 @@ public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements Con /** * Reads all contacts from database backend and handles them over to the * contact manager - * - * @param contactManager Contact manager to handle loaded contacts */ @Override - public void readAllContacts (final ManageableContact contactManager) { + public void readAllContacts () { // Get iterator and case it CsvBackend backend = (CsvBackend) this.getBackend(); @@ -114,7 +123,7 @@ public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements Con Contact contact = iterator.next(); // Add contact instance to manager - contactManager.addContact(contact); + this.getContactManager().addContact(contact); } } } 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 e9f710a..c14a3c3 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactWrapper.java +++ b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactWrapper.java @@ -17,7 +17,6 @@ package org.mxchange.addressbook.database.frontend.contact; import org.mxchange.addressbook.database.frontend.DatabaseWrapper; -import org.mxchange.addressbook.manager.contact.ManageableContact; /** * @@ -32,16 +31,12 @@ public interface ContactWrapper extends DatabaseWrapper { /** * Flushes all contact entries to database - * - * @param contactManager An instance of a MangeableContact class */ - public void flushAllContacts (final ManageableContact contactManager); + public void flushAllContacts (); /** * Reads all contacts from database backend and handles them over to the * contact manager - * - * @param contactManager */ - public void readAllContacts (final ManageableContact contactManager); + public void readAllContacts (); } diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java index 1b269ec..de18989 100644 --- a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java @@ -61,10 +61,10 @@ public class ContactManager extends BaseManager implements ManageableContact { this.contacts = new ArrayList<>(maxContacts); // Init database connection - this.contactDatabase = new ContactDatabaseFrontend(); + this.contactDatabase = new ContactDatabaseFrontend(this); // Read all entries - this.contactDatabase.readAllContacts(this); + this.contactDatabase.readAllContacts(); // Debug message //* NOISY-DEBUG: */ this.getLogger().debug("client=" + client); @@ -298,6 +298,41 @@ public class ContactManager extends BaseManager implements ManageableContact { return Collections.unmodifiableList(this.contacts); } + /** + * Checks whether own contact is already added by checking all entries for + * isOwnContact flag + * + * @return Whether own contact is already added + */ + @Override + public boolean isOwnContactAdded () { + // Default is not added + boolean isAdded = false; + + // Now get it back from address book, first get an iterator + Iterator iterator = this.contacts.iterator(); + + // Check entries + while (iterator.hasNext()) { + // Get next entry + Contact contact = iterator.next(); + + // Is it valid? + if (contact instanceof Contact) { + // Get flag + isAdded = contact.isOwnContact(); + + // Is this own contact? + if (isAdded) { + // Then abort loop + break; + } + } + } + // Return result + return isAdded; + } + @Override public void listContacts () { throw new UnsupportedOperationException("Not supported yet."); @@ -463,7 +498,7 @@ public class ContactManager extends BaseManager implements ManageableContact { */ private void flush () { // Flusgh all - this.getContactDatabase().flushAllContacts(this); + this.getContactDatabase().flushAllContacts(); } /** @@ -540,38 +575,4 @@ public class ContactManager extends BaseManager implements ManageableContact { // Return result return isFound; } - - /** - * Checks whether own contact is already added by checking all entries for - * isOwnContact flag - * - * @return Whether own contact is already added - */ - private boolean isOwnContactAdded () { - // Default is not added - boolean isAdded = false; - - // Now get it back from address book, first get an iterator - Iterator iterator = this.contacts.iterator(); - - // Check entries - while (iterator.hasNext()) { - // Get next entry - Contact contact = iterator.next(); - - // Is it valid? - if (contact instanceof Contact) { - // Get flag - isAdded = contact.isOwnContact(); - - // Is this own contact? - if (isAdded) { - // Then abort loop - break; - } - } - } - // Return result - return isAdded; - } } diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java index 520f4ab..3fd7063 100644 --- a/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java @@ -114,6 +114,14 @@ public interface ManageableContact extends Manageable { */ public void searchContacts (); + /** + * Checks whether own contact is already added by checking all entries for + * isOwnContact flag + * + * @return Whether own contact is already added + */ + public boolean isOwnContactAdded (); + /** * Getter for size *