Skip to content

Commit

Permalink
Dialog should not close after error message is displayed
Browse files Browse the repository at this point in the history
See #6108
  • Loading branch information
stefan-kolb committed Mar 13, 2020
1 parent 4a740fd commit 1e08bb8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 70 deletions.
10 changes: 8 additions & 2 deletions src/main/java/org/jabref/gui/groups/GroupDialogView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,8 +61,8 @@ public class GroupDialogView extends BaseDialog<AbstractGroup> {

@FXML private TextField texGroupFilePath;

private final EnumMap<GroupHierarchyType,String> hierarchyText = new EnumMap<>(GroupHierarchyType.class);
private final EnumMap<GroupHierarchyType,String> hierarchyToolTip = new EnumMap<>(GroupHierarchyType.class);
private final EnumMap<GroupHierarchyType, String> hierarchyText = new EnumMap<>(GroupHierarchyType.class);
private final EnumMap<GroupHierarchyType, String> hierarchyToolTip = new EnumMap<>(GroupHierarchyType.class);

private final ControlsFxVisualizer validationVisualizer = new ControlsFxVisualizer();
private final GroupDialogViewModel viewModel;
Expand All @@ -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
Expand Down
140 changes: 72 additions & 68 deletions src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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("");
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 1e08bb8

Please # to comment.