From: Roland Haeder Date: Tue, 28 Jul 2015 10:59:41 +0000 (+0200) Subject: Added more input fields and overwritten toString() in Gender enum X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=8b901b209e8b4d330d8bc9ae2a78c121d1110c2e;p=jaddressbook-lib.git Added more input fields and overwritten toString() in Gender enum Signed-off-by:Roland Häder --- diff --git a/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java b/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java index 9580cd13..b104090c 100644 --- a/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java +++ b/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java @@ -166,4 +166,15 @@ public class BaseFrameworkSystem implements FrameworkInterface { protected final void setTableName (final String tableName) { this.tableName = tableName; } + + /** + * Getter for human-readable string from given key + * + * @param key Key to return + * @return Human-readable message + */ + public final String getMessageStringFromKey (final String key) { + // Return message + return this.getBundle().getString(key); + } } diff --git a/Addressbook/src/org/mxchange/addressbook/FrameworkInterface.java b/Addressbook/src/org/mxchange/addressbook/FrameworkInterface.java index 7d9f6c74..fc44b3fc 100644 --- a/Addressbook/src/org/mxchange/addressbook/FrameworkInterface.java +++ b/Addressbook/src/org/mxchange/addressbook/FrameworkInterface.java @@ -47,4 +47,12 @@ public interface FrameworkInterface { * @return the application */ public Application getApplication (); + + /** + * Getter for human-readable string from given key + * + * @param key Key to return + * @return Human-readable message + */ + public String getMessageStringFromKey (final String key); } diff --git a/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java b/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java index 78efb043..7f55a382 100644 --- a/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java +++ b/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java @@ -127,6 +127,11 @@ public class AddressbookApplication extends BaseFrameworkSystem implements Appli */ public static final String APP_VERSION = "0.0"; + /** + * Self instance + */ + private static Application selfInstance; + /** * Console client is enabled by default */ @@ -137,6 +142,14 @@ public class AddressbookApplication extends BaseFrameworkSystem implements Appli */ private boolean guiClient = false; + /** + * Protected constructor + */ + protected AddressbookApplication () { + // Set own instance + selfInstance = this; + } + /** * Getter for printable application name * @@ -334,4 +347,14 @@ public class AddressbookApplication extends BaseFrameworkSystem implements Appli this.getLogger().info("End of program (last line)"); System.exit(0); } + + /** + * Getter for this application + * + * @return Instance from this application + */ + public static final Application getInstance () { + // Return it + return selfInstance; + } } diff --git a/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java b/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java index 0f8aee83..fdc59e1e 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java +++ b/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java @@ -28,7 +28,9 @@ import java.text.MessageFormat; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.DefaultComboBoxModel; +import javax.swing.InputVerifier; import javax.swing.JComboBox; +import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; @@ -274,19 +276,156 @@ public class AddressbookFrame extends BaseFrameworkSystem implements ClientFrame // Initial dimension this.addContact.setSize(500, 400); - // Only for developing: - /* DEBUG: */ this.addContact.setVisible(true); - /* * Add listener which asks for confirmation, if data has been entered * but not saved yet. The user may appriciate this ... ;-) * * @TODO Unfinished */ - + // Init 3 panels: - // 1) "Name" panel + // 1) "name" panel initNamePanel(); + + // 2) "address" panel + initAddressPanel(); + + // x)Only for developing: + /* DEBUG: */ this.addContact.setVisible(true); + } + + /** + * Initializes address panel + */ + private void initAddressPanel () { + // Panel "address" input boxes + JPanel addressPanel = new JPanel(); + addressPanel.setLayout(new BoxLayout(addressPanel, BoxLayout.Y_AXIS)); + + // Set border to titled version + addressPanel.setBorder(new TitledBorder(this.generateBorderTitle("address"))); + + // Init all elements: + // 1) Street and number together + JPanel streetNumberPanel = new JPanel(); + streetNumberPanel.setLayout(new GridLayout(1, 4)); + + // Label for street + JLabel streetLabel = new JLabel(this.getBundle().getString("AddressbookFrame.street.text")); + + // Init text field with label + JTextField street = new JTextField(20); + street.setToolTipText(this.getBundle().getString("AddressbookFrame.street.tooltipText")); + + // Add both to street panel + streetNumberPanel.add(streetLabel); + streetNumberPanel.add(street); + + // Number label + JLabel numberLabel = new JLabel(this.getBundle().getString("AddressbookFrame.number.text")); + + // And text field, but only accept numbers + JTextField number = new JTextField(4); + number.setToolTipText(this.getBundle().getString("AddressbookFrame.number.tooltipText")); + + // Add number verifier + number.setInputVerifier(new InputVerifier() { + + /** + * Method to verify that the entered data is a number. + * + * @param input Input to verify + * @return Whether the data is a number + */ + @Override + public boolean verify (final JComponent input) { + // Cast on text field + JTextField text = (JTextField) input; + + // Default is passed + boolean isValid = true; + + // Try to convert input text to a number + try { + int num = Integer.valueOf(text.getText()); + } catch (final NumberFormatException ex) { + // Didn't work + isValid = false; + } + + // Return status + return isValid; + } + }); + + // Add both to street panel + streetNumberPanel.add(numberLabel); + streetNumberPanel.add(number); + + // Add panel to address panel + addressPanel.add(streetNumberPanel); + + // 2) ZIP code and ccity name + JPanel zipCityPanel = new JPanel(); + zipCityPanel.setLayout(new GridLayout(1, 4)); + + // Label for ZIP code, again numbers only + JLabel zipLabel = new JLabel(this.getBundle().getString("AddressbookFrame.zip.text")); + + // Init text field with label + JTextField zip = new JTextField(20); + zip.setToolTipText(this.getBundle().getString("AddressbookFrame.zip.tooltipText")); + + // Add number verifier + zip.setInputVerifier(new InputVerifier() { + + /** + * Method to verify that the entered data is a number. + * + * @param input Input to verify + * @return Whether the data is a number + */ + @Override + public boolean verify (final JComponent input) { + // Cast on text field + JTextField text = (JTextField) input; + + // Default is passed + boolean isValid = true; + + // Try to convert input text to a number + try { + int num = Integer.valueOf(text.getText()); + } catch (final NumberFormatException ex) { + // Didn't work + isValid = false; + } + + // Return status + return isValid; + } + }); + + // Add both to street panel + zipCityPanel.add(zipLabel); + zipCityPanel.add(zip); + + // Label for street + JLabel cityLabel = new JLabel(this.getBundle().getString("AddressbookFrame.city.text")); + + // Init text field with label + JTextField city = new JTextField(20); + city.setToolTipText(this.getBundle().getString("AddressbookFrame.city.tooltipText")); + + // Add both to street panel + zipCityPanel.add(cityLabel); + zipCityPanel.add(city); + + // Add panel to address panel + addressPanel.add(zipCityPanel); + + // Add panel to dialog + this.addContact.add(addressPanel); } /** @@ -435,76 +574,70 @@ public class AddressbookFrame extends BaseFrameworkSystem implements ClientFrame * Initializes name panel */ private void initNamePanel () { - // 1) Panel "name" input boxes + // Panel "name" input boxes JPanel namePanel = new JPanel(); namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.Y_AXIS)); - + // Set border to titled version namePanel.setBorder(new TitledBorder(this.generateBorderTitle("name"))); - - // Get all genders - Gender[] genders = Gender.values(); - - // Init gender combo box - JComboBox gender = new JComboBox<>(new DefaultComboBoxModel<>(genders)); - + // Panel for gender JPanel gPanel = new JPanel(); gPanel.setLayout(new GridLayout(1, 2)); - // Set tooltip text - gPanel.setToolTipText(this.getBundle().getString("AddressbookFrame.gender.tooltipText")); - // Gender text field JLabel gLabel = new JLabel(this.getBundle().getString("AddressbookFrame.gender.text")); + // Get all genders + Gender[] genders = Gender.values(); + + // Init gender combo box with tool tip + JComboBox gender = new JComboBox<>(new DefaultComboBoxModel<>(genders)); + gender.setToolTipText(this.getBundle().getString("AddressbookFrame.gender.tooltipText")); + // Add both to gender panel gPanel.add(gLabel); gPanel.add(gender); - + // Add gender panel to "name" panel namePanel.add(gPanel); - + // Panel for surname JPanel sPanel = new JPanel(); sPanel.setLayout(new GridLayout(1, 2)); - - // Set too tip text - sPanel.setToolTipText(this.getBundle().getString("AddressbookFrame.surname.tooltipText")); - + // New label for surname is not needed JLabel sLabel = new JLabel(this.getBundle().getString("AddressbookFrame.surname.text")); - - // And input box + + // And input box wih tool tip JTextField surname = new JTextField(20); - + surname.setToolTipText(this.getBundle().getString("AddressbookFrame.surname.tooltipText")); + // Add both to surname panel sPanel.add(sLabel); sPanel.add(surname); - + // Add surname panel to "name" panel namePanel.add(sPanel); - + // Panel for surname JPanel fPanel = new JPanel(); fPanel.setLayout(new GridLayout(1, 2)); - - // Set too tip text - fPanel.setToolTipText(this.getBundle().getString("AddressbookFrame.familyName.tooltipText")); - + // New label for surname is not needed JLabel fLabel = new JLabel(this.getBundle().getString("AddressbookFrame.familyName.text")); - - // And input box + + // And input box wih tool tip JTextField familyName = new JTextField(20); - + familyName.setToolTipText(this.getBundle().getString("AddressbookFrame.familyName.tooltipText")); + // Add both to surname panel fPanel.add(fLabel); fPanel.add(familyName); - + // Add family namepanel to "name" panel namePanel.add(fPanel); - + // Finally add panel to dialog this.addContact.add(namePanel); } diff --git a/Addressbook/src/org/mxchange/addressbook/contact/Gender.java b/Addressbook/src/org/mxchange/addressbook/contact/Gender.java index a2740595..6bd68df3 100644 --- a/Addressbook/src/org/mxchange/addressbook/contact/Gender.java +++ b/Addressbook/src/org/mxchange/addressbook/contact/Gender.java @@ -16,6 +16,8 @@ */ package org.mxchange.addressbook.contact; +import org.mxchange.addressbook.application.AddressbookApplication; + /** * Gender enum * @author Roland Haeder @@ -104,8 +106,8 @@ public enum Gender { // Debug message //* NOISY-DEBUG: */ System.out.println("gender=" + gender); - // Is it "U"? - if (gender.getDatabaseValue().equals("U")) { + // Is it UNKNOWN? + if (gender.equals(Gender.UNKNOWN)) { // Skip this continue; } @@ -166,4 +168,15 @@ public enum Gender { protected String getMessageKey () { return this.messageKey; } + + /** + * Overwritten to return human-readable strings + * + * @return Human-readable strings + */ + @Override + public String toString () { + // Get key from bundle and return it + return AddressbookApplication.getInstance().getMessageStringFromKey(this.getMessageKey()); + } } diff --git a/Addressbook/src/org/mxchange/addressbook/localization/bundle_de_DE.properties b/Addressbook/src/org/mxchange/addressbook/localization/bundle_de_DE.properties index 9fbe609c..570c12fe 100644 --- a/Addressbook/src/org/mxchange/addressbook/localization/bundle_de_DE.properties +++ b/Addressbook/src/org/mxchange/addressbook/localization/bundle_de_DE.properties @@ -27,12 +27,21 @@ AddressbookFrame.menuItem.editOwnData.toolTipText=Erlaubt das \u00c4ndern eigene AddressbookFrame.dialog.addContact.title.text=Neue Adresse hinzuf\u00fcgen AddressbookFrame.main.title.text=Adressen auflisten AddressbookFrame.border.name.title.text=Anrede, Vorname, Nachname: +AddressbookFrame.border.address.title.text=Anschrift: AddressbookFrame.gender.text=Anrede: AddressbookFrame.gender.tooltipText=W\u00e4hlen Sie die Anrede aus. AddressbookFrame.surname.text=Vorname: AddressbookFrame.surname.tooltipText=Geben Sie den Vornamen ein. AddressbookFrame.familyName.text=Nachname: AddressbookFrame.familyName.tooltipText=Geben Sie den Nachnamen ein. +AddressbookFrame.street.text=Stra\u00dfe: +AddressbookFrame.street.tooltipText=Geben Sie die Stra\u00dfe ein. +AddressbookFrame.number.text=Hausnummer: +AddressbookFrame.number.tooltipText=Geben Sie die Hausnummer ein. +AddressbookFrame.zip.text=PLZ: +AddressbookFrame.zip.tooltipText=Geben Sie die Postleitzahl ein. +AddressbookFrame.city.text=Stadt: +AddressbookFrame.city.tooltipText=Geben Sie die Stadt ein. BaseContact.gender.unknown.text=Unbekannt BaseContact.gender.male.text=Herr BaseContact.gender.female.text=Frau diff --git a/Addressbook/src/org/mxchange/addressbook/localization/bundle_en_US.properties b/Addressbook/src/org/mxchange/addressbook/localization/bundle_en_US.properties index 1bb83cd4..7800af35 100644 --- a/Addressbook/src/org/mxchange/addressbook/localization/bundle_en_US.properties +++ b/Addressbook/src/org/mxchange/addressbook/localization/bundle_en_US.properties @@ -27,12 +27,21 @@ AddressbookFrame.menuItem.editOwnData.toolTipText=Allows the user to edit own ad AddressbookFrame.dialog.addContact.title.text=Add new address AddressbookFrame.main.title.text=List addresses AddressbookFrame.border.name.title.text=Gender, surname, family name: +AddressbookFrame.border.address.title.text=Anschrift: AddressbookFrame.gender.text=Gender: AddressbookFrame.gender.tooltipText=Choose gender. AddressbookFrame.surname.text=Surname: AddressbookFrame.surname.tooltipText=Enter surname. AddressbookFrame.familyName.text=Family name: AddressbookFrame.familyName.tooltipText=Enter family name. +AddressbookFrame.street.text=Street: +AddressbookFrame.street.tooltipText=Enter street. +AddressbookFrame.number.text=Number: +AddressbookFrame.number.tooltipText=Enter number. +AddressbookFrame.zip.text=ZIP: +AddressbookFrame.zip.tooltipText=Enter zip code. +AddressbookFrame.city.text=City: +AddressbookFrame.city.tooltipText=Enter city. BaseContact.gender.unknown.text=Unknown BaseContact.gender.male.text=Mr. BaseContact.gender.female.text=Mrs.