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;
// 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);
}
/**
* 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> 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> 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);
}