/**
   * Updates the contact information shown, or creates a new contact if the first row is selected.
   *
   * @param evt
   */
  private void namesJComboBoxActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_namesJComboBoxActionPerformed

    this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));

    String selectedContactName = (String) namesJComboBox.getSelectedItem();

    boolean cancel = false;

    if (valuesChanged) {
      int value =
          JOptionPane.showConfirmDialog(
              this,
              "The contact has been changed. Do you want to save the changes?",
              "Contact Changed",
              JOptionPane.YES_NO_CANCEL_OPTION);

      if (value == JOptionPane.YES_OPTION) {

        String newName = contactPath + lastSelectedContact + ".con";

        try {

          FileWriter r = new FileWriter(newName);
          BufferedWriter bw = new BufferedWriter(r);

          bw.write("Name: " + lastSelectedContact + "\n");
          bw.write("E-mail: " + contactInfoJTextField.getText() + "\n");
          bw.write("Institution: " + institutionJTextArea.getText() + "\n");

          bw.close();
          r.close();

        } catch (FileNotFoundException ex) {
          JOptionPane.showMessageDialog(
              this,
              "The file " + newName + " could not be found.",
              "File Not Found",
              JOptionPane.ERROR_MESSAGE);
          Util.writeToErrorLog("Error when trying to save file: ");
          ex.printStackTrace();
        } catch (IOException ex) {
          JOptionPane.showMessageDialog(
              this,
              "An error occured when trying to save the file " + newName + ".",
              "File Error",
              JOptionPane.ERROR_MESSAGE);
          Util.writeToErrorLog("Error when trying to save file: ");
          ex.printStackTrace();
        }
      } else if (value == JOptionPane.CANCEL_OPTION) {
        cancel = true;
      }
    }

    if (!cancel) {

      lastSelectedContact = "" + namesJComboBox.getSelectedItem();

      contactInfoJTextField.setText("");
      institutionJTextArea.setText("");

      if (namesJComboBox.getSelectedIndex() == 0) {

        institutionJTextArea.setEnabled(false);
        contactInfoJTextField.setEnabled(false);
        institutionJTextArea.setEditable(false);
        contactInfoJTextField.setEditable(false);

      } else if (namesJComboBox.getSelectedIndex() == namesJComboBox.getItemCount() - 1) {

        ComboBoxInputDialog input = new ComboBoxInputDialog(this, this, true);
        input.setTitle("Create New Contact");
        input.setBorderTitle("New Contact");
        input.setVisible(true);

      } else {

        institutionJTextArea.setEnabled(true);
        contactInfoJTextField.setEnabled(true);
        institutionJTextArea.setEditable(true);
        contactInfoJTextField.setEditable(true);

        selectedContactName = contactPath + selectedContactName + ".con";

        try {
          String temp, institution = "";

          FileReader f = new FileReader(selectedContactName);
          BufferedReader b = new BufferedReader(f);

          b.readLine();
          temp = b.readLine();
          contactInfoJTextField.setText(temp.substring(temp.indexOf(": ") + 2));

          temp = b.readLine();

          while (temp != null) {
            if (temp.indexOf(": ") != -1) {
              institution += temp.substring(temp.indexOf(": ") + 2) + ", ";
            } else {
              institution += temp + ", ";
            }

            temp = b.readLine();
          }

          if (institution.endsWith(", ")) {
            institution = institution.substring(0, institution.length() - 2);
          }

          institutionJTextArea.setText(institution);

          institutionJTextArea.setEnabled(true);
          contactInfoJTextField.setEnabled(true);
          institutionJTextArea.setEditable(true);
          contactInfoJTextField.setEditable(true);

          b.close();
          f.close();

        } catch (FileNotFoundException ex) {
          JOptionPane.showMessageDialog(
              this,
              "The file " + selectedContactName + " could not be found.",
              "File Not Found",
              JOptionPane.ERROR_MESSAGE);
          Util.writeToErrorLog("Error when trying to read file: ");
          ex.printStackTrace();
        } catch (IOException ex) {
          JOptionPane.showMessageDialog(
              this,
              "An error occured when trying to read the file " + selectedContactName + ".",
              "File Error",
              JOptionPane.ERROR_MESSAGE);
          Util.writeToErrorLog("Error when trying to read file: ");
          ex.printStackTrace();
        }
      }

      valuesChanged = false;
    } else {
      namesJComboBox.setSelectedItem(lastSelectedContact);
    }

    mandatoryFieldsCheck();

    this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  } // GEN-LAST:event_namesJComboBoxActionPerformed