String name = null; while (true) { name = (String) JOptionPane.showInputDialog(this, "Enter Recipe Name", "Recipe Name", JOptionPane.QUESTION_MESSAGE, null, null, originalName); if (name == null) return null; if (name.equals(originalName) && allowExistingName) return name;  if (!recipeNames.contains(name)) {  Map recipeNamesHT = OrganizerHelper.getRecipeNamesFromDatabase(); Set dbRecipeNames = recipeNamesHT.keySet(); if (dbRecipeNames.contains(name)) { int answer = JOptionPane.showConfirmDialog(this, "This name is in the database; use an unique name", "Recipe Name Not Unique", JOptionPane.OK_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE); if (answer != JOptionPane.OK_OPTION) return null; } else break; } else { int answer = JOptionPane.showConfirmDialog(this, "Use an unique name", "Recipe Name Not Unique", JOptionPane.OK_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE); if (answer != JOptionPane.OK_OPTION) return null; } } return name;
return getUniqueName("Recipe", recipeNames, originalName, allowExistingName, "isRecipeNameInDatabase");
  public String getUniqueRecipeName(String originalName,                                    boolean allowExistingName) {    String name = null;    while (true) {      name = (String) JOptionPane.showInputDialog(this, "Enter Recipe Name",                                                  "Recipe Name",                                                  JOptionPane.QUESTION_MESSAGE,                                                  null, null,                                                  originalName);      if (name == null) return null;      if (name.equals(originalName) && allowExistingName)        return name;      // if name is unique in CSMART      if (!recipeNames.contains(name)) {        // ensure that name is not in the database        Map recipeNamesHT = OrganizerHelper.getRecipeNamesFromDatabase();        Set dbRecipeNames = recipeNamesHT.keySet();        if (dbRecipeNames.contains(name)) {          int answer = JOptionPane.showConfirmDialog(this,                     "This name is in the database; use an unique name",                                                 "Recipe Name Not Unique",						 JOptionPane.OK_CANCEL_OPTION,						 JOptionPane.ERROR_MESSAGE);          if (answer != JOptionPane.OK_OPTION) return null;        } else          break; // have unique name      } else {        int answer = JOptionPane.showConfirmDialog(this,						 "Use an unique name",						 "Recipe Name Not Unique",						 JOptionPane.OK_CANCEL_OPTION,						 JOptionPane.ERROR_MESSAGE);        if (answer != JOptionPane.OK_OPTION) return null;      }    }    return name;  }