diff --git a/src/main/java/org/jabref/gui/groups/GroupDialogView.java b/src/main/java/org/jabref/gui/groups/GroupDialogView.java index 96cf6d3b044..9d11fc25bb4 100644 --- a/src/main/java/org/jabref/gui/groups/GroupDialogView.java +++ b/src/main/java/org/jabref/gui/groups/GroupDialogView.java @@ -3,7 +3,9 @@ import java.util.EnumMap; import javafx.application.Platform; +import javafx.event.ActionEvent; import javafx.fxml.FXML; +import javafx.scene.control.Button; import javafx.scene.control.ButtonType; import javafx.scene.control.CheckBox; import javafx.scene.control.ColorPicker; @@ -59,8 +61,8 @@ public class GroupDialogView extends BaseDialog { @FXML private TextField texGroupFilePath; - private final EnumMap hierarchyText = new EnumMap<>(GroupHierarchyType.class); - private final EnumMap hierarchyToolTip = new EnumMap<>(GroupHierarchyType.class); + private final EnumMap hierarchyText = new EnumMap<>(GroupHierarchyType.class); + private final EnumMap hierarchyToolTip = new EnumMap<>(GroupHierarchyType.class); private final ControlsFxVisualizer validationVisualizer = new ControlsFxVisualizer(); private final GroupDialogViewModel viewModel; @@ -80,6 +82,10 @@ public GroupDialogView(DialogService dialogService, BibDatabaseContext currentDa setResultConverter(viewModel::resultConverter); getDialogPane().getButtonTypes().setAll(ButtonType.OK, ButtonType.CANCEL); + + final Button confirmDialogButton = (Button) getDialogPane().lookupButton(ButtonType.OK); + // handle validation before closing dialog and calling resultConverter + confirmDialogButton.addEventFilter(ActionEvent.ACTION, viewModel::validationHandler); } @FXML diff --git a/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java b/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java index 1b9304317b1..2da487145e3 100644 --- a/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java +++ b/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java @@ -18,6 +18,7 @@ import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.collections.FXCollections; +import javafx.event.Event; import javafx.scene.control.ButtonType; import javafx.scene.paint.Color; @@ -56,7 +57,6 @@ import de.saxsys.mvvmfx.utils.validation.Validator; public class GroupDialogViewModel { - // Basic Settings private final StringProperty nameProperty = new SimpleStringProperty(""); private final StringProperty descriptionProperty = new SimpleStringProperty(""); @@ -237,85 +237,89 @@ private void setupValidation() { }); } + public void validationHandler(Event event) { + ValidationStatus validationStatus = validator.getValidationStatus(); + if (validationStatus.getHighestMessage().isPresent()) { + dialogService.showErrorDialogAndWait(validationStatus.getHighestMessage().get().getMessage()); + // consume the event to prevent the dialog to close + event.consume(); + } + } + public AbstractGroup resultConverter(ButtonType button) { - if (button == ButtonType.OK) { - ValidationStatus validationStatus = validator.getValidationStatus(); - if (validationStatus.getHighestMessage().isPresent()) { - dialogService.showErrorDialogAndWait(validationStatus.getHighestMessage().get().getMessage()); - return null; - } + if (button != ButtonType.OK) { + return null; + } - AbstractGroup resultingGroup = null; - try { - String groupName = nameProperty.getValue().trim(); - if (typeExplicitProperty.getValue()) { - resultingGroup = new ExplicitGroup( + AbstractGroup resultingGroup = null; + try { + String groupName = nameProperty.getValue().trim(); + if (typeExplicitProperty.getValue()) { + resultingGroup = new ExplicitGroup( + groupName, + groupHierarchySelectedProperty.getValue(), + preferencesService.getKeywordDelimiter()); + } else if (typeKeywordsProperty.getValue()) { + if (keywordGroupRegexProperty.getValue()) { + resultingGroup = new RegexKeywordGroup( groupName, groupHierarchySelectedProperty.getValue(), - preferencesService.getKeywordDelimiter()); - } else if (typeKeywordsProperty.getValue()) { - if (keywordGroupRegexProperty.getValue()) { - resultingGroup = new RegexKeywordGroup( - groupName, - groupHierarchySelectedProperty.getValue(), - FieldFactory.parseField(keywordGroupSearchFieldProperty.getValue().trim()), - keywordGroupSearchTermProperty.getValue().trim(), - keywordGroupCaseSensitiveProperty.getValue()); - } else { - resultingGroup = new WordKeywordGroup( - groupName, - groupHierarchySelectedProperty.getValue(), - FieldFactory.parseField(keywordGroupSearchFieldProperty.getValue().trim()), - keywordGroupSearchTermProperty.getValue().trim(), - keywordGroupCaseSensitiveProperty.getValue(), - preferencesService.getKeywordDelimiter(), - false); - } - } else if (typeSearchProperty.getValue()) { - resultingGroup = new SearchGroup( + FieldFactory.parseField(keywordGroupSearchFieldProperty.getValue().trim()), + keywordGroupSearchTermProperty.getValue().trim(), + keywordGroupCaseSensitiveProperty.getValue()); + } else { + resultingGroup = new WordKeywordGroup( groupName, groupHierarchySelectedProperty.getValue(), - searchGroupSearchTermProperty.getValue().trim(), - searchGroupCaseSensitiveProperty.getValue(), - searchGroupRegexProperty.getValue()); - } else if (typeAutoProperty.getValue()) { - if (autoGroupKeywordsOptionProperty.getValue()) { - resultingGroup = new AutomaticKeywordGroup( - groupName, - groupHierarchySelectedProperty.getValue(), - FieldFactory.parseField(autoGroupKeywordsFieldProperty.getValue().trim()), - autoGroupKeywordsDelimiterProperty.getValue().charAt(0), - autoGroupKeywordsHierarchicalDelimiterProperty.getValue().charAt(0)); - } else { - resultingGroup = new AutomaticPersonsGroup( - groupName, - groupHierarchySelectedProperty.getValue(), - FieldFactory.parseField(autoGroupPersonsFieldProperty.getValue().trim())); - } - } else if (typeTexProperty.getValue()) { - resultingGroup = TexGroup.create( + FieldFactory.parseField(keywordGroupSearchFieldProperty.getValue().trim()), + keywordGroupSearchTermProperty.getValue().trim(), + keywordGroupCaseSensitiveProperty.getValue(), + preferencesService.getKeywordDelimiter(), + false); + } + } else if (typeSearchProperty.getValue()) { + resultingGroup = new SearchGroup( + groupName, + groupHierarchySelectedProperty.getValue(), + searchGroupSearchTermProperty.getValue().trim(), + searchGroupCaseSensitiveProperty.getValue(), + searchGroupRegexProperty.getValue()); + } else if (typeAutoProperty.getValue()) { + if (autoGroupKeywordsOptionProperty.getValue()) { + resultingGroup = new AutomaticKeywordGroup( groupName, groupHierarchySelectedProperty.getValue(), - Paths.get(texGroupFilePathProperty.getValue().trim()), - new DefaultAuxParser(new BibDatabase()), - Globals.getFileUpdateMonitor(), - currentDatabase.getMetaData()); - } - - if (resultingGroup != null) { - resultingGroup.setColor(colorProperty.getValue()); - resultingGroup.setDescription(descriptionProperty.getValue()); - resultingGroup.setIconName(iconProperty.getValue()); - return resultingGroup; + FieldFactory.parseField(autoGroupKeywordsFieldProperty.getValue().trim()), + autoGroupKeywordsDelimiterProperty.getValue().charAt(0), + autoGroupKeywordsHierarchicalDelimiterProperty.getValue().charAt(0)); } else { - return null; + resultingGroup = new AutomaticPersonsGroup( + groupName, + groupHierarchySelectedProperty.getValue(), + FieldFactory.parseField(autoGroupPersonsFieldProperty.getValue().trim())); } - } catch (IllegalArgumentException | IOException exception) { - dialogService.showErrorDialogAndWait(exception.getLocalizedMessage(), exception); - return null; + } else if (typeTexProperty.getValue()) { + resultingGroup = TexGroup.create( + groupName, + groupHierarchySelectedProperty.getValue(), + Paths.get(texGroupFilePathProperty.getValue().trim()), + new DefaultAuxParser(new BibDatabase()), + Globals.getFileUpdateMonitor(), + currentDatabase.getMetaData()); + } + + if (resultingGroup != null) { + resultingGroup.setColor(colorProperty.getValue()); + resultingGroup.setDescription(descriptionProperty.getValue()); + resultingGroup.setIconName(iconProperty.getValue()); + return resultingGroup; } + + return null; + } catch (IllegalArgumentException | IOException exception) { + dialogService.showErrorDialogAndWait(exception.getLocalizedMessage(), exception); + return null; } - return null; } public void setValues() {