/** Creates the view's listeners */
  public void createListeners() {

    // If the table selection changes, set the currentInsuranceCompany accordingly (Listens to
    // keyboard and mouse)
    insuranceCompaniesTable
        .getSelectionModel()
        .addListSelectionListener(
            new ListSelectionListener() {
              public void valueChanged(ListSelectionEvent e) {
                if ((e.getValueIsAdjusting() == false)
                    && (insuranceCompaniesTable.getSelectedRow() != -1)) {
                  int selectedCompanyId =
                      Integer.valueOf(
                          (String)
                              insuranceCompaniesTable.getValueAt(
                                  insuranceCompaniesTable.getSelectedRow(), 0));
                  controller.selectInsuranceCompany(selectedCompanyId);
                }
              }
            });

    // Add a new InsuranceCompany
    addInsuranceCompanyButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            controller.addInsuranceCompany();
          }
        });

    // Save an InsuranceCompany
    saveInsuranceCompanyButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            // Check if a row is selected
            if (insuranceCompaniesTable.getSelectedRow() != -1) {
              int selectedCompanyId =
                  Integer.parseInt(
                      (String)
                          insuranceCompaniesTable.getValueAt(
                              insuranceCompaniesTable.getSelectedRow(), 0));
              String[] newData = {
                companyNameTextField.getText(),
                telephoneTextField.getText(),
                urlTextField.getText(),
                insuranceTypesTextField.getText(),
                percentageTextField.getText(),
                generalDescriptionTextField.getText()
              };

              // Input validation
              boolean checkInput = true;
              for (String input : newData) {
                if ((input.equals("")) || (input.equals(":"))) {
                  checkInput = false;
                }
              }
              // Use regex pattern to check double values
              if (!percentageTextField.getText().matches("(-|\\+)?[0-9]+(\\.[0-9]+)?")) {
                checkInput = false;
              }

              if (checkInput) {
                recordEdited = insuranceCompaniesTable.getSelectedRow();
                controller.updateInsuranceCompany(selectedCompanyId, newData);
              } else {
                showErrorMsg(
                    "Error Saving Insurance Company",
                    "Please enter proper values:\n(Fields cannot be blank or contain \":\")");
              }

            } else {
              showErrorMsg("Error Saving Insurance Company", "Please select a valid table row.");
            }
          }
        });

    // Delete an InsuranceCompany
    deleteInsuranceCompanyButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            // Check if a row is selected
            if (insuranceCompaniesTable.getSelectedRow() != -1) {
              int i =
                  JOptionPane.showConfirmDialog(
                      null,
                      "<html>Are you sure you want to delete following Insurance Company:<br><br><b>ID: "
                          + (String)
                              insuranceCompaniesTable.getValueAt(
                                  insuranceCompaniesTable.getSelectedRow(), 0)
                          + " - "
                          + (String)
                              insuranceCompaniesTable.getValueAt(
                                  insuranceCompaniesTable.getSelectedRow(), 1)
                          + "</b></html>",
                      "Delete Insurance Company",
                      JOptionPane.YES_NO_OPTION);

              if (i == JOptionPane.YES_OPTION) {
                int selectedCompanyId =
                    Integer.parseInt(
                        (String)
                            insuranceCompaniesTable.getValueAt(
                                insuranceCompaniesTable.getSelectedRow(), 0));
                controller.deleteInsuranceCompany(selectedCompanyId);
                searchTextField.setText("");
              }
            } else {
              showErrorMsg("Error Deleting Insurance Company", "Please select a valid table row.");
            }
          }
        });

    // Enable dynamic searching, by just typing into the searchTextField
    searchTextField
        .getDocument()
        .addDocumentListener(
            new DocumentListener() {
              public void changedUpdate(DocumentEvent e) {}

              public void removeUpdate(DocumentEvent e) {
                searchInsuranceCompany();
              }

              public void insertUpdate(DocumentEvent e) {
                searchInsuranceCompany();
              }
            });

    // Clear the searchTextField
    clearSearchButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            searchTextField.setText("");
          }
        });

    // Enable sorting
    sortComboBox.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            String sortStrategy = (String) sortComboBox.getSelectedItem();

            if (sortStrategy.equals("ID")) {
              model.setSortingStrategy(null);
            } else if (sortStrategy.equals("Company Name")) {
              model.setSortingStrategy(new InsuranceCompanyNameComparator());
            } else {
              model.setSortingStrategy(new InsuranceCompanyPercentageComparator());
            }

            updateTable();
          }
        });
  }