/**
  * Returns the last selected folder.
  *
  * @param lastSelectedFolder the last selected folder
  * @return the last selected folder
  */
 public String getLastSelectedFolder(LastSelectedFolder lastSelectedFolder) {
   String result = null;
   if (result == null) {
     return null;
   }
   String folder = lastSelectedFolder.getLastSelectedFolder(SequenceDbDetailsDialog.lastFolderKey);
   if (folder == null) {
     folder = lastSelectedFolder.getLastSelectedFolder();
   }
   return folder;
 }
  /**
   * Opens a file chooser where the user can select the database file.
   *
   * @param evt
   */
  private void browseDatabaseSettingsActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_browseDatabaseSettingsActionPerformed

    LastSelectedFolder lastSelectedFolder = peptideShakerGUI.getLastSelectedFolder();
    File startLocation = null;
    File utilitiesDbFolder = peptideShakerGUI.getUtilitiesUserPreferences().getDbFolder();
    if (utilitiesDbFolder != null && utilitiesDbFolder.exists()) {
      startLocation = utilitiesDbFolder;
    }
    if (startLocation == null) {
      startLocation = new File(getLastSelectedFolder(lastSelectedFolder));
    }

    UtilitiesUserPreferences utilitiesUserPreferences =
        UtilitiesUserPreferences.loadUserPreferences();
    if (utilitiesUserPreferences.getDbFolder() != null
        && utilitiesUserPreferences.getDbFolder().exists()) {
      startLocation = utilitiesUserPreferences.getDbFolder();
    }

    // First check whether a file has already been selected.
    // If so, start from that file's parent.
    if (databaseSettingsTxt.getText() != null && new File(databaseSettingsTxt.getText()).exists()) {
      File temp = new File(databaseSettingsTxt.getText());
      startLocation = temp.getParentFile();
    }

    JFileChooser fc = new JFileChooser(startLocation);
    FileFilter filter =
        new FileFilter() {
          @Override
          public boolean accept(File myFile) {
            return myFile.getName().toLowerCase().endsWith("fasta")
                || myFile.getName().toLowerCase().endsWith("fas")
                || myFile.isDirectory();
          }

          @Override
          public String getDescription() {
            return "Supported formats: FASTA (.fasta or .fas)";
          }
        };

    fc.setFileFilter(filter);
    int result = fc.showOpenDialog(this);

    if (result == JFileChooser.APPROVE_OPTION) {
      File file = fc.getSelectedFile();

      if (file.getName().contains(" ")) {
        renameFastaFileName(file);
      } else {
        databaseSettingsTxt.setText(file.getAbsolutePath());
        databaseSettingsTxt.setText(file.getAbsolutePath());
      }

      lastSelectedFolder.setLastSelectedFolder(
          SequenceDbDetailsDialog.lastFolderKey, file.getAbsolutePath());
      targetDecoySettingsButton.setEnabled(true);

      // check if the database contains decoys
      if (!file.getAbsolutePath()
          .endsWith(
              peptideShakerGUI.getUtilitiesUserPreferences().getTargetDecoyFileNameSuffix()
                  + ".fasta")) {

        int value =
            JOptionPane.showConfirmDialog(
                this,
                "The selected FASTA file does not seem to contain decoy sequences.\n"
                    + "Decoys are required by PeptideShaker. Add decoys?",
                "Add Decoy Sequences?",
                JOptionPane.YES_NO_OPTION);

        if (value == JOptionPane.NO_OPTION) {
          // do nothing
        } else if (value == JOptionPane.YES_OPTION) {
          targetDecoySettingsButtonActionPerformed(null);
        }
      }

      validateParametersInput(false);
    }
  } // GEN-LAST:event_browseDatabaseSettingsActionPerformed