Repository: jabref/jabref
Issue: Exception when adding field formatter in cleanup entries dialog

<!-- 
Note: Please use the GitHub issue tracker only for bug reports. 
Feature requests, questions and general feedback is now handled at http://discourse.jabref.org.
Thanks! 
--> 

JabRef version **JabRef 5.0-dev (8f463e4)** on **Windows 10 10.0 amd64, Java 1.8.0_221**

<!-- IMPORTANT NOTE ->
<!-- 
      Please always test if the bug is still reproducible in the latest development version.
      We are constantly improving JabRef and some bugs may already be fixed.
      You can download the development version at: http://builds.jabref.org/master/
      Please make a backup of your library before you try out this version.
      If you already use a development version, ensure that you use the latest one.
 -->
- [x] I have tested the latest development version from http://builds.jabref.org/master/ and the problem persists

<!-- Add a clear and concise description of what the bug is. -->

Steps to reproduce the behavior:
1. Select an entry
2. Open *Quality->Cleanup entries*
3. Try to add new field formatter by selecting field and field formatter via combo boxes

This results with uncaught exception.

I checked that the bug was introduced in 4b39b78. Before that commit the bug was not present.

<!-- If applicable, add excerpt of the bibliography file, screenshot, and excerpt of log (available in the error console) -->

<details>
  <summary>Log File</summary>
  
  ```
22:41:59.563 [JavaFX Application Thread] ERROR org.jabref.FallbackExceptionHandler - Uncaught exception occurred in Thread[JavaFX Application Thread,5,main]
java.lang.ClassCastException: java.lang.String cannot be cast to org.jabref.model.entry.field.Field
	at org.jabref.gui.cleanup.FieldFormatterCleanupsPanel.getFieldFormatterCleanup(FieldFormatterCleanupsPanel.java:220) ~[main/:?]
	at org.jabref.gui.cleanup.FieldFormatterCleanupsPanel.updateDescription(FieldFormatterCleanupsPanel.java:147) ~[main/:?]
	at org.jabref.gui.cleanup.FieldFormatterCleanupsPanel.lambda$getSelectorPanel$276(FieldFormatterCleanupsPanel.java:176) ~[main/:?]
	at org.fxmisc.easybind.EasyBind.lambda$subscribe$12(EasyBind.java:263) ~[easybind-1.0.3.jar:?]
	at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361) ~[jfxrt.jar:?]
	at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81) ~[jfxrt.jar:?]
	at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:105) ~[jfxrt.jar:?]
	at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112) ~[jfxrt.jar:?]
	at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146) ~[jfxrt.jar:?]
	at javafx.scene.control.ComboBoxBase.setValue(ComboBoxBase.java:150) ~[jfxrt.jar:?]
	at javafx.scene.control.ComboBox.updateValue(ComboBox.java:463) ~[jfxrt.jar:?]
	at javafx.scene.control.ComboBox.access$200(ComboBox.java:192) ~[jfxrt.jar:?]
	at javafx.scene.control.ComboBox$3.changed(ComboBox.java:446) ~[jfxrt.jar:?]
	at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361) ~[jfxrt.jar:?]
	at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81) ~[jfxrt.jar:?]
	at javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(ReadOnlyObjectPropertyBase.java:74) ~[jfxrt.jar:?]
	at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:102) ~[jfxrt.jar:?]
	at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112) ~[jfxrt.jar:?]
	at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146) ~[jfxrt.jar:?]
	at javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:102) ~[jfxrt.jar:?]
	at javafx.scene.control.ComboBox$ComboBoxSelectionModel.lambda$new$0(ComboBox.java:494) ~[jfxrt.jar:?]
	at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:137) ~[jfxrt.jar:?]
	at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81) ~[jfxrt.jar:?]
	at javafx.beans.property.ReadOnlyIntegerPropertyBase.fireValueChangedEvent(ReadOnlyIntegerPropertyBase.java:72) ~[jfxrt.jar:?]
	at javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:102) ~[jfxrt.jar:?]
	at javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:113) ~[jfxrt.jar:?]
	at javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:147) ~[jfxrt.jar:?]
	at javafx.scene.control.SelectionModel.setSelectedIndex(SelectionModel.java:68) ~[jfxrt.jar:?]
	at javafx.scene.control.SingleSelectionModel.updateSelectedIndex(SingleSelectionModel.java:215) ~[jfxrt.jar:?]
	at javafx.scene.control.SingleSelectionModel.select(SingleSelectionModel.java:149) ~[jfxrt.jar:?]
	at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.lambda$createListView$1(ComboBoxListViewSkin.java:484) ~[jfxrt.jar:?]
	at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:349) ~[jfxrt.jar:?]
	at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81) ~[jfxrt.jar:?]
	at javafx.beans.property.ReadOnlyIntegerPropertyBase.fireValueChangedEvent(ReadOnlyIntegerPropertyBase.java:72) ~[jfxrt.jar:?]
	at javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:102) ~[jfxrt.jar:?]
	at javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:113) ~[jfxrt.jar:?]
	at javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:147) ~[jfxrt.jar:?]
	at javafx.scene.control.SelectionModel.setSelectedIndex(SelectionModel.java:68) ~[jfxrt.jar:?]
	at javafx.scene.control.MultipleSelectionModelBase.select(MultipleSelectionModelBase.java:404) ~[jfxrt.jar:?]
	at javafx.scene.control.MultipleSelectionModelBase.clearAndSelect(MultipleSelectionModelBase.java:356) ~[jfxrt.jar:?]
	at javafx.scene.control.ListView$ListViewBitSetSelectionModel.clearAndSelect(ListView.java:1403) ~[jfxrt.jar:?]
	at com.sun.javafx.scene.control.behavior.CellBehaviorBase.simpleSelect(CellBehaviorBase.java:256) ~[jfxrt.jar:?]
	at com.sun.javafx.scene.control.behavior.CellBehaviorBase.doSelect(CellBehaviorBase.java:220) ~[jfxrt.jar:?]
	at com.sun.javafx.scene.control.behavior.CellBehaviorBase.mousePressed(CellBehaviorBase.java:150) ~[jfxrt.jar:?]
	at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:95) ~[jfxrt.jar:?]
	at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89) ~[jfxrt.jar:?]
	at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) ~[jfxrt.jar:?]
	at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) ~[jfxrt.jar:?]
	at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) ~[jfxrt.jar:?]
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) ~[jfxrt.jar:?]
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) ~[jfxrt.jar:?]
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) ~[jfxrt.jar:?]
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) ~[jfxrt.jar:?]
	at javafx.event.Event.fireEvent(Event.java:198) ~[jfxrt.jar:?]
	at javafx.scene.Scene$MouseHandler.process(Scene.java:3757) ~[jfxrt.jar:?]
	at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485) ~[jfxrt.jar:?]
	at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) ~[jfxrt.jar:?]
	at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) ~[jfxrt.jar:?]
	at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394) ~[jfxrt.jar:?]
	at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295) ~[jfxrt.jar:?]
	at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_221]
	at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432) ~[jfxrt.jar:?]
	at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410) ~[jfxrt.jar:?]
	at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431) ~[jfxrt.jar:?]
	at com.sun.glass.ui.View.handleMouseEvent(View.java:555) ~[jfxrt.jar:?]
	at com.sun.glass.ui.View.notifyMouse(View.java:937) ~[jfxrt.jar:?]
	at com.sun.glass.ui.win.WinApplication._enterNestedEventLoopImpl(Native Method) ~[jfxrt.jar:?]
	at com.sun.glass.ui.win.WinApplication._enterNestedEventLoop(WinApplication.java:204) ~[jfxrt.jar:?]
	at com.sun.glass.ui.Application.enterNestedEventLoop(Application.java:511) ~[jfxrt.jar:?]
	at com.sun.glass.ui.EventLoop.enter(EventLoop.java:107) ~[jfxrt.jar:?]
	at com.sun.javafx.tk.quantum.QuantumToolkit.enterNestedEventLoop(QuantumToolkit.java:633) ~[jfxrt.jar:?]
	at javafx.stage.Stage.showAndWait(Stage.java:474) ~[jfxrt.jar:?]
	at javafx.scene.control.HeavyweightDialog.showAndWait(HeavyweightDialog.java:162) ~[jfxrt.jar:?]
	at javafx.scene.control.Dialog.showAndWait(Dialog.java:341) ~[jfxrt.jar:?]
	at org.jabref.gui.cleanup.CleanupAction.action(CleanupAction.java:46) ~[main/:?]
	at org.jabref.gui.BasePanel.runCommand(BasePanel.java:609) ~[main/:?]
	at org.jabref.gui.actions.OldDatabaseCommandWrapper.execute(OldDatabaseCommandWrapper.java:40) ~[main/:?]
	at org.jabref.gui.actions.JabRefAction.lambda$new$110(JabRefAction.java:29) ~[main/:?]
	at org.controlsfx.control.action.Action.handle(Action.java:419) ~[controlsfx-8.40.16-SNAPSHOT.jar:8.40.16.SNAPSHOT]
	at org.controlsfx.control.action.Action.handle(Action.java:64) ~[controlsfx-8.40.16-SNAPSHOT.jar:8.40.16.SNAPSHOT]
	at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) ~[jfxrt.jar:?]
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) ~[jfxrt.jar:?]
	at javafx.event.Event.fireEvent(Event.java:198) ~[jfxrt.jar:?]
	at javafx.scene.control.MenuItem.fire(MenuItem.java:462) ~[jfxrt.jar:?]
	at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405) ~[jfxrt.jar:?]
	at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$6(ContextMenuContent.java:1358) ~[jfxrt.jar:?]
	at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) ~[jfxrt.jar:?]
	at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) ~[jfxrt.jar:?]
	at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) ~[jfxrt.jar:?]
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) ~[jfxrt.jar:?]
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) ~[jfxrt.jar:?]
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) ~[jfxrt.jar:?]
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) ~[jfxrt.jar:?]
	at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) ~[jfxrt.jar:?]
	at javafx.event.Event.fireEvent(Event.java:198) ~[jfxrt.jar:?]
	at javafx.scene.Scene$MouseHandler.process(Scene.java:3757) ~[jfxrt.jar:?]
	at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485) ~[jfxrt.jar:?]
	at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) ~[jfxrt.jar:?]
	at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) ~[jfxrt.jar:?]
	at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394) ~[jfxrt.jar:?]
	at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295) ~[jfxrt.jar:?]
	at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_221]
	at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432) ~[jfxrt.jar:?]
	at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410) ~[jfxrt.jar:?]
	at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431) ~[jfxrt.jar:?]
	at com.sun.glass.ui.View.handleMouseEvent(View.java:555) ~[jfxrt.jar:?]
	at com.sun.glass.ui.View.notifyMouse(View.java:937) ~[jfxrt.jar:?]
	at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) ~[jfxrt.jar:?]
	at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177) ~[jfxrt.jar:?]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_221]
  ```
</details>


Changed files:
src/main/java/org/jabref/gui/cleanup/FieldFormatterCleanupsPanel.java

diff --git a/src/main/java/org/jabref/gui/cleanup/FieldFormatterCleanupsPanel.java b/src/main/java/org/jabref/gui/cleanup/FieldFormatterCleanupsPanel.java
index 12cd47ccf9..23917990eb 100644
--- a/src/main/java/org/jabref/gui/cleanup/FieldFormatterCleanupsPanel.java
+++ b/src/main/java/org/jabref/gui/cleanup/FieldFormatterCleanupsPanel.java
@@ -4,6 +4,8 @@ import java.util.List;
 import java.util.Objects;
 import java.util.Optional;
 import java.util.Set;
+import java.util.TreeSet;
+import java.util.stream.Collectors;
 
 import javafx.beans.value.ChangeListener;
 import javafx.beans.value.ObservableValue;
@@ -40,18 +42,17 @@ public class FieldFormatterCleanupsPanel extends GridPane {
 
     private static final String DESCRIPTION = Localization.lang("Description") + ": ";
     private final CheckBox cleanupEnabled;
+    private final FieldFormatterCleanups defaultFormatters;
+    private final List<Formatter> availableFormatters;
     private FieldFormatterCleanups fieldFormatterCleanups;
     private ListView<FieldFormatterCleanup> actionsList;
     private ComboBox<Formatter> formattersCombobox;
-    private ComboBox<Field> selectFieldCombobox;
+    private ComboBox<String> selectFieldCombobox;
     private Button addButton;
     private Label descriptionAreaText;
     private Button removeButton;
     private Button resetButton;
     private Button recommendButton;
-
-    private final FieldFormatterCleanups defaultFormatters;
-    private final List<Formatter> availableFormatters;
     private ObservableList<FieldFormatterCleanup> actions;
 
     public FieldFormatterCleanupsPanel(String description, FieldFormatterCleanups defaultFormatters) {
@@ -104,7 +105,7 @@ public class FieldFormatterCleanupsPanel extends GridPane {
         actionsList = new ListView<>(actions);
         actionsList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
         new ViewModelListCellFactory<FieldFormatterCleanup>()
-                .withText(action -> action.getField() + ": " + action.getFormatter().getName())
+                .withText(action -> action.getField().getDisplayName() + ": " + action.getFormatter().getName())
                 .withStringTooltip(action -> action.getFormatter().getDescription())
                 .install(actionsList);
         add(actionsList, 1, 1, 3, 1);
@@ -164,7 +165,8 @@ public class FieldFormatterCleanupsPanel extends GridPane {
         GridPane builder = new GridPane();
         Set<Field> fields = FieldFactory.getCommonFields();
         fields.add(InternalField.KEY_FIELD);
-        selectFieldCombobox = new ComboBox<>(FXCollections.observableArrayList(fields));
+        Set<String> fieldsString = fields.stream().map(Field::getDisplayName).sorted().collect(Collectors.toCollection(TreeSet::new));
+        selectFieldCombobox = new ComboBox<>(FXCollections.observableArrayList(fieldsString));
         selectFieldCombobox.setEditable(true);
         builder.add(selectFieldCombobox, 1, 1);
 
@@ -217,7 +219,7 @@ public class FieldFormatterCleanupsPanel extends GridPane {
 
     private FieldFormatterCleanup getFieldFormatterCleanup() {
         Formatter selectedFormatter = formattersCombobox.getValue();
-        Field field = selectFieldCombobox.getValue();
+        Field field = FieldFactory.parseField(selectFieldCombobox.getSelectionModel().getSelectedItem());
         return new FieldFormatterCleanup(field, selectedFormatter);
     }
 
@@ -242,5 +244,4 @@ public class FieldFormatterCleanupsPanel extends GridPane {
             setStatus(cleanupEnabled.isSelected());
         }
     }
-
 }