Skip to content

Commit e1a9244

Browse files
authored
Change recursion into two calls (#14184)
1 parent 15e856a commit e1a9244

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

jabgui/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -494,29 +494,33 @@ private void fetchAndMerge(EntryBasedFetcher fetcher) {
494494

495495
public void setFocusToField(Field field) {
496496
UiTaskExecutor.runInJavaFXThread(() -> {
497-
Field actualField = field;
498-
boolean fieldFound = false;
499-
for (Tab tab : tabbed.getTabs()) {
500-
tabbed.getSelectionModel().select(tab);
501-
if ((tab instanceof FieldsEditorTab fieldsEditorTab)
502-
&& fieldsEditorTab.getShownFields().contains(actualField)) {
503-
tabbed.getSelectionModel().select(tab);
504-
Platform.runLater(() -> fieldsEditorTab.requestFocus(actualField));
505-
// This line explicitly brings focus back to the main window containing the Entry Editor.
506-
getScene().getWindow().requestFocus();
507-
fieldFound = true;
508-
break;
509-
}
510-
}
511-
if (!fieldFound) {
512-
Field aliasField = EntryConverter.FIELD_ALIASES.get(field);
513-
if (aliasField != null) {
514-
setFocusToField(aliasField);
515-
}
516-
}
497+
getTabContainingField(field).ifPresentOrElse(
498+
tab -> selectTabAndField(tab, field),
499+
() -> {
500+
Field aliasField = EntryConverter.FIELD_ALIASES.get(field);
501+
getTabContainingField(aliasField).ifPresent(tab -> selectTabAndField(tab, aliasField));
502+
}
503+
);
517504
});
518505
}
519506

507+
private void selectTabAndField(FieldsEditorTab tab, Field field) {
508+
Platform.runLater(() -> {
509+
tabbed.getSelectionModel().select(tab);
510+
tab.requestFocus(field);
511+
});
512+
// This line explicitly brings focus back to the main window containing the Entry Editor.
513+
getScene().getWindow().requestFocus();
514+
}
515+
516+
private Optional<FieldsEditorTab> getTabContainingField(Field field) {
517+
return tabbed.getTabs().stream()
518+
.filter(FieldsEditorTab.class::isInstance)
519+
.map(FieldsEditorTab.class::cast)
520+
.filter(tab -> tab.getShownFields().contains(field))
521+
.findFirst();
522+
}
523+
520524
@Override
521525
public void nextPreviewStyle() {
522526
this.previewPanel.nextPreviewStyle();

0 commit comments

Comments
 (0)