private void addLastEntryIfAvailable(List<ConfigurationItem> sortedConfigurationItems) {
   if (sortedConfigurationItems.isEmpty()) {
     return;
   }
   String id = sortedConfigurationItems.get(0).getId();
   if (lastEntries.containsKey(id)) {
     ConfigurationItem lastEntry = lastEntries.get(id);
     lastEntry.setId(id);
     sortedConfigurationItems.add(0, lastEntry);
   }
 }
 private ConfigurationItem prepareConfigurationItemForDialogList(
     ConfigurationItem configurationItem) {
   if (configurationItem != null && configurationItem.getValue() == null) {
     if (configurationVariablePattern != null) {
       String newConfigurationItemValue =
           replaceVariableKeysWithValues(configurationItem.getRawValue());
       configurationItem.setValue(newConfigurationItemValue);
     } else {
       configurationItem.setValue(configurationItem.getRawValue());
     }
   }
   return configurationItem;
 }
  private List<ConfigurationItem> sortConfigurationItems(
      List<ConfigurationItem> selectedConfigurationItems) {
    final List<ConfigurationItem> sortedConfigurationItems =
        new ArrayList<>(selectedConfigurationItems);

    // Sort the list by last used profile
    if (selectedConfigItem != null && selectedConfigItem.getProfile() != null) {
      // If the same field is selected again - the last selected item should go on top
      boolean removedSelectedItem = sortedConfigurationItems.remove(selectedConfigItem);

      // Last used profile group should be on top
      Collections.sort(
          sortedConfigurationItems,
          new Comparator<ConfigurationItem>() {
            @Override
            public int compare(ConfigurationItem lhs, ConfigurationItem rhs) {
              if (lhs.getProfile() != null
                  && lhs.getProfile().equalsIgnoreCase(selectedConfigItem.getProfile())
                  && rhs.getProfile() != null
                  && !rhs.getProfile().equals(selectedConfigItem.getProfile())) {
                return -1;
              } else {
                return 0;
              }
            }
          });

      // Put the last selected item on top
      if (removedSelectedItem) {
        sortedConfigurationItems.add(0, selectedConfigItem);
      }
    }
    return sortedConfigurationItems;
  }
  @VisibleForTesting
  String getSelectedConfigItemValue() {
    if (selectedConfigItem != null && selectedConfigItem.isLastEntryItem()) {
      return selectedConfigItem.getValue();
    }

    ConfigurationItem preparedConfigurationItem = prepareSelectedConfigurationItemForInput();
    if (preparedConfigurationItem != null) {
      String value = preparedConfigurationItem.getValue();
      if (preparedConfigurationItem.shouldRememberLastEntry()) {
        rememberLastEntry(preparedConfigurationItem);
      }
      return value;
    }

    return null;
  }
 private void selectItemWithNextProfile() {
   if (sortedConfigurationItems == null
       || selectedConfigItem == null
       || selectedConfigItem.getProfile() == null
       || profiles == null
       || profiles.isEmpty()) {
     return;
   }
   selectedProfileIndex = (selectedProfileIndex + 1) % profiles.size();
   String selectedProfile = profiles.get(selectedProfileIndex);
   for (int i = 0; i < sortedConfigurationItems.size(); i++) {
     ConfigurationItem item = sortedConfigurationItems.get(i);
     if (item.getProfile() != null && item.getProfile().equals(selectedProfile)) {
       setSelectedConfigItem(item);
       break;
     }
   }
 }
 private void rememberLastEntry(ConfigurationItem preparedConfigurationItem) {
   for (String id : preparedConfigurationItem.getRememberLastEntryForIds()) {
     ConfigurationItem item = new ConfigurationItem(preparedConfigurationItem);
     item.setLabel(preparedConfigurationItem.getValue());
     item.setRawValue(preparedConfigurationItem.getValue());
     item.setLastEntryItem(true);
     lastEntries.put(id, item);
   }
 }
 private ConfigurationItem prepareSelectedConfigurationItemForInput() {
   if (selectedConfigItem != null) {
     // Check if the selected config item raw value is configuration variable key
     if (helper.isConfigurationVariableKey(selectedConfigItem.getRawValue())) {
       String configurationVariableValue =
           helper.getConfigurationVariableValue(selectedConfigItem.getRawValue());
       if (configurationVariableValue != null) {
         ConfigurationItem preparedConfigurationItem = new ConfigurationItem(selectedConfigItem);
         preparedConfigurationItem.setValue(configurationVariableValue);
         return preparedConfigurationItem;
       }
     } else if (selectedConfigItem.getValue() == null) {
       ConfigurationItem preparedConfigurationItem =
           new ConfigurationItem(prepareConfigurationItemForDialogList(selectedConfigItem));
       selectedConfigItem.setValue(null);
       return preparedConfigurationItem;
     } else {
       ConfigurationItem preparedConfigurationItem = new ConfigurationItem(selectedConfigItem);
       selectedConfigItem.setValue(null);
       return preparedConfigurationItem;
     }
   }
   return null;
 }
 public void onRemoveItemButtonClicked(int position) {
   ConfigurationItem configurationItem = sortedConfigurationItems.get(position);
   lastEntries.remove(configurationItem.getId());
   sortedConfigurationItems.remove(position);
   notifyPropertyChanged(PROPERTY_DATA_SET);
 }