Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select newly added jstyle in table to prevent exception #5556

Merged
merged 1 commit into from
Nov 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,7 @@ public StyleSelectDialogViewModel(DialogService dialogService, StyleLoader loade
styles.addAll(loadStyles());

String currentStyle = preferences.getCurrentStyle();
Optional<StyleSelectItemViewModel> lastUsedStyle = styles.stream().filter(style -> style.getStylePath().equals(currentStyle)).findFirst();

if (lastUsedStyle.isPresent()) {
selectedItem.setValue(lastUsedStyle.get());
} else {
selectedItem.setValue(styles.get(0));
}
selectedItem.setValue(getStyleOrDefault(currentStyle));
}

public StyleSelectItemViewModel fromOOBibStyle(OOBibStyle style) {
Expand All @@ -75,6 +69,7 @@ public void addStyleFile() {
if (loader.addStyleIfValid(stylePath)) {
preferences.setCurrentStyle(stylePath);
styles.setAll(loadStyles());
selectedItem.setValue(getStyleOrDefault(stylePath));
} else {
dialogService.showErrorDialogAndWait(Localization.lang("Invalid style selected"), Localization.lang("You must select a valid style file."));
}
Expand Down Expand Up @@ -129,4 +124,8 @@ public void storePrefs() {
preferences.setCurrentStyle(selectedItem.getValue().getStylePath());
preferencesService.setOpenOfficePreferences(preferences);
}

private StyleSelectItemViewModel getStyleOrDefault(String stylePath) {
return styles.stream().filter(style -> style.getStylePath().equals(stylePath)).findFirst().orElse(styles.get(0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if styles is empty?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Styles can never be empty because we have two internal default styles that are always available.

}
}