From bcc620dd454abad903a9c1a380743cd66e001f05 Mon Sep 17 00:00:00 2001 From: Loay Ghreeb Date: Fri, 23 Feb 2024 13:31:17 +0200 Subject: [PATCH 1/4] Added null check for selected entry type Disable the add field button if the entry type is not selected to prevent exceptions --- .../gui/preferences/customentrytypes/CustomEntryTypesTab.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/gui/preferences/customentrytypes/CustomEntryTypesTab.java b/src/main/java/org/jabref/gui/preferences/customentrytypes/CustomEntryTypesTab.java index 02317d97885..01cf9d9b883 100644 --- a/src/main/java/org/jabref/gui/preferences/customentrytypes/CustomEntryTypesTab.java +++ b/src/main/java/org/jabref/gui/preferences/customentrytypes/CustomEntryTypesTab.java @@ -87,7 +87,7 @@ public void initialize() { setupFieldsTable(); addNewEntryTypeButton.disableProperty().bind(viewModel.entryTypeValidationStatus().validProperty().not()); - addNewFieldButton.disableProperty().bind(viewModel.fieldValidationStatus().validProperty().not()); + addNewFieldButton.disableProperty().bind(viewModel.fieldValidationStatus().validProperty().not().or(viewModel.selectedEntryTypeProperty().isNull())); Platform.runLater(() -> { visualizer.initVisualization(viewModel.entryTypeValidationStatus(), addNewEntryType, true); From 77032951fd7fcf18ca5e4474c85b0c52468461b0 Mon Sep 17 00:00:00 2001 From: Loay Ghreeb Date: Fri, 23 Feb 2024 18:30:01 +0200 Subject: [PATCH 2/4] added EditableChoiceDialog --- .../java/org/jabref/gui/DialogService.java | 7 +++++++ .../org/jabref/gui/JabRefDialogService.java | 19 +++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jabref/gui/DialogService.java b/src/main/java/org/jabref/gui/DialogService.java index 8734637975c..3c3412703e4 100644 --- a/src/main/java/org/jabref/gui/DialogService.java +++ b/src/main/java/org/jabref/gui/DialogService.java @@ -14,6 +14,7 @@ import javafx.scene.control.ChoiceDialog; import javafx.scene.control.DialogPane; import javafx.scene.control.TextInputDialog; +import javafx.util.StringConverter; import org.jabref.gui.util.BaseDialog; import org.jabref.gui.util.DirectoryDialogConfiguration; @@ -42,6 +43,12 @@ default Optional showChoiceDialogAndWait(String title, String content, St return showChoiceDialogAndWait(title, content, okButtonLabel, null, choices); } + Optional showEditableChoiceDialogAndWait(String title, String content, String okButtonLabel, T defaultChoice, Collection choices, StringConverter converter); + + default Optional showEditableChoiceDialogAndWait(String title, String content, String okButtonLabel, Collection choices, StringConverter converter) { + return showEditableChoiceDialogAndWait(title, content, okButtonLabel, null, choices, converter); + } + /** * This will create and display new {@link TextInputDialog} with a text fields to enter data */ diff --git a/src/main/java/org/jabref/gui/JabRefDialogService.java b/src/main/java/org/jabref/gui/JabRefDialogService.java index 9198dfb0f30..1b9f954f845 100644 --- a/src/main/java/org/jabref/gui/JabRefDialogService.java +++ b/src/main/java/org/jabref/gui/JabRefDialogService.java @@ -23,6 +23,7 @@ import javafx.scene.control.ButtonType; import javafx.scene.control.CheckBox; import javafx.scene.control.ChoiceDialog; +import javafx.scene.control.ComboBox; import javafx.scene.control.DialogPane; import javafx.scene.control.Label; import javafx.scene.control.TextArea; @@ -35,6 +36,7 @@ import javafx.stage.Stage; import javafx.stage.Window; import javafx.util.Duration; +import javafx.util.StringConverter; import org.jabref.gui.help.ErrorConsoleAction; import org.jabref.gui.icon.IconTheme; @@ -129,8 +131,7 @@ public static String shortenDialogMessage(String dialogMessage) { return (dialogMessage.substring(0, Math.min(dialogMessage.length(), JabRefDialogService.DIALOG_SIZE_LIMIT)) + "...").trim(); } - @Override - public Optional showChoiceDialogAndWait(String title, String content, String okButtonLabel, T defaultChoice, Collection choices) { + private ChoiceDialog createChoiceDialog(String title, String content, String okButtonLabel, T defaultChoice, Collection choices) { ChoiceDialog choiceDialog = new ChoiceDialog<>(defaultChoice, choices); ((Stage) choiceDialog.getDialogPane().getScene().getWindow()).getIcons().add(IconTheme.getJabRefImage()); ButtonType okButtonType = new ButtonType(okButtonLabel, ButtonBar.ButtonData.OK_DONE); @@ -139,6 +140,20 @@ public Optional showChoiceDialogAndWait(String title, String content, Str choiceDialog.setTitle(title); choiceDialog.setContentText(content); choiceDialog.initOwner(mainWindow); + return choiceDialog; + } + + @Override + public Optional showChoiceDialogAndWait(String title, String content, String okButtonLabel, T defaultChoice, Collection choices) { + return createChoiceDialog(title, content, okButtonLabel, defaultChoice, choices).showAndWait(); + } + + @Override + public Optional showEditableChoiceDialogAndWait(String title, String content, String okButtonLabel, T defaultChoice, Collection choices, StringConverter converter) { + ChoiceDialog choiceDialog = createChoiceDialog(title, content, okButtonLabel, defaultChoice, choices); + ComboBox comboBox = (ComboBox) choiceDialog.getDialogPane().lookup(".combo-box"); + comboBox.setEditable(true); + comboBox.setConverter(converter); return choiceDialog.showAndWait(); } From 1c6ceaf195ead088c6eb002da44f6bddf33b4a10 Mon Sep 17 00:00:00 2001 From: Loay Ghreeb Date: Fri, 23 Feb 2024 18:32:44 +0200 Subject: [PATCH 3/4] Change popup to the combo box --- .../contentselectors/ContentSelectorViewModel.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jabref/gui/libraryproperties/contentselectors/ContentSelectorViewModel.java b/src/main/java/org/jabref/gui/libraryproperties/contentselectors/ContentSelectorViewModel.java index db23506e8fd..fc63f6f5514 100644 --- a/src/main/java/org/jabref/gui/libraryproperties/contentselectors/ContentSelectorViewModel.java +++ b/src/main/java/org/jabref/gui/libraryproperties/contentselectors/ContentSelectorViewModel.java @@ -23,6 +23,7 @@ import org.jabref.gui.DialogService; import org.jabref.gui.libraryproperties.PropertiesTabViewModel; +import org.jabref.gui.util.FieldsUtil; import org.jabref.logic.l10n.Localization; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.field.Field; @@ -126,8 +127,11 @@ BooleanBinding isNoKeywordSelected() { } void showInputFieldNameDialog() { - dialogService.showInputDialogAndWait(Localization.lang("Add new field name"), Localization.lang("Field name")) - .map(FieldFactory::parseField) + dialogService.showEditableChoiceDialogAndWait(Localization.lang("Add new field name"), + Localization.lang("Field name"), + Localization.lang("Add"), + FXCollections.observableArrayList(FieldFactory.getStandardFieldsWithCitationKey()), + FieldsUtil.FIELD_STRING_CONVERTER) .ifPresent(this::addFieldIfUnique); } From ce3ca53228d954f398ac13b02a0791cc25751260 Mon Sep 17 00:00:00 2001 From: Loay Ghreeb Date: Sat, 24 Feb 2024 12:54:01 +0200 Subject: [PATCH 4/4] Update unit test for ContentSelectorViewModelTest --- .../java/org/jabref/gui/JabRefDialogService.java | 1 + .../ContentSelectorViewModelTest.java | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/jabref/gui/JabRefDialogService.java b/src/main/java/org/jabref/gui/JabRefDialogService.java index 1b9f954f845..e8f49335f1e 100644 --- a/src/main/java/org/jabref/gui/JabRefDialogService.java +++ b/src/main/java/org/jabref/gui/JabRefDialogService.java @@ -154,6 +154,7 @@ public Optional showEditableChoiceDialogAndWait(String title, String cont ComboBox comboBox = (ComboBox) choiceDialog.getDialogPane().lookup(".combo-box"); comboBox.setEditable(true); comboBox.setConverter(converter); + EasyBind.subscribe(comboBox.getEditor().textProperty(), text -> comboBox.setValue(converter.fromString(text))); return choiceDialog.showAndWait(); } diff --git a/src/test/java/org/jabref/gui/libraryproperties/contentselectors/ContentSelectorViewModelTest.java b/src/test/java/org/jabref/gui/libraryproperties/contentselectors/ContentSelectorViewModelTest.java index 05dfbf152b9..309dc0a4ff8 100644 --- a/src/test/java/org/jabref/gui/libraryproperties/contentselectors/ContentSelectorViewModelTest.java +++ b/src/test/java/org/jabref/gui/libraryproperties/contentselectors/ContentSelectorViewModelTest.java @@ -9,9 +9,11 @@ import javafx.collections.FXCollections; import org.jabref.gui.DialogService; +import org.jabref.gui.util.FieldsUtil; import org.jabref.logic.l10n.Localization; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.field.Field; +import org.jabref.model.entry.field.FieldFactory; import org.jabref.model.entry.field.StandardField; import org.jabref.model.entry.field.UnknownField; @@ -146,10 +148,13 @@ private void removeKeyword(Field field, String keyword) { } private void addField(Field field) { - when(dialogService.showInputDialogAndWait( - Localization.lang("Add new field name"), Localization.lang("Field name"))) - .thenReturn(Optional.of(field.getDisplayName())); - + when(dialogService.showEditableChoiceDialogAndWait( + Localization.lang("Add new field name"), + Localization.lang("Field name"), + Localization.lang("Add"), + FXCollections.observableArrayList(FieldFactory.getStandardFieldsWithCitationKey()), + FieldsUtil.FIELD_STRING_CONVERTER)) + .thenReturn(Optional.of(field)); viewModel.showInputFieldNameDialog(); }