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

Fixes the issue "Non valid number as font size results in an uncaught exception." #7438

Merged
merged 7 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed an issue where changing the font size makes the font size field too small. [#7085](https://github.com/JabRef/jabref/issues/7085)
- We fixed an issue with TexGroups on Linux systems, where the modification of an aux-file did not trigger an auto-update for TexGroups. Furthermore, the detection of file modifications is now more reliable. [#7412](https://github.com/JabRef/jabref/pull/7412)
- We fixed an issue where the Unicode to Latex formatter produced wrong results for characters with a codepoint higher than Character.MAX_VALUE. [#7387](https://github.com/JabRef/jabref/issues/7387)
- We fixed an issue where a non valid value as font size results in an uncaught exception. [#7415](https://github.com/JabRef/jabref/issues/7415)

### Removed

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.jabref.gui.preferences.appearance;

import java.util.regex.Pattern;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.control.CheckBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Spinner;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.util.converter.IntegerStringConverter;

import org.jabref.gui.preferences.AbstractPreferenceTabView;
import org.jabref.gui.preferences.PreferencesTab;
Expand All @@ -27,6 +31,16 @@ public class AppearanceTab extends AbstractPreferenceTabView<AppearanceTabViewMo

private final ControlsFxVisualizer validationVisualizer = new ControlsFxVisualizer();

// The fontSizeFormatter formats the input given to the fontSize spinner so that non valid values cannot be entered.
private TextFormatter<Integer> fontSizeFormatter = new TextFormatter<Integer>(new IntegerStringConverter(), 9,
Copy link
Member

Choose a reason for hiding this comment

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

As this might be handy also in other places, I would propose to extract this to a static method in the a helper class gui.util.TextFormatter

c -> {
Copy link
Member

Choose a reason for hiding this comment

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

please don't use abbreviations as variable names. Here change would be good.

if (Pattern.matches("\\d*", c.getText())) {
return c;
}
c.setText("0");
Copy link
Member

Choose a reason for hiding this comment

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

I think returning null here is slightly better, at least according to the documentation "Returning null rejects the change." https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TextFormatter.html#getFilter--

Copy link
Member

Choose a reason for hiding this comment

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

See the comments in this thread. Returning null will produce an exception in the spinner

return c;
});

public AppearanceTab() {
ViewLoader.view(this)
.root(this)
Expand All @@ -48,6 +62,7 @@ public void initialize() {
fontSize.getEditor().setAlignment(Pos.CENTER_RIGHT);
fontSize.setValueFactory(AppearanceTabViewModel.fontSizeValueFactory);
fontSize.getEditor().textProperty().bindBidirectional(viewModel.fontSizeProperty());
fontSize.getEditor().setTextFormatter(fontSizeFormatter);

themeLight.selectedProperty().bindBidirectional(viewModel.themeLightProperty());
themeDark.selectedProperty().bindBidirectional(viewModel.themeDarkProperty());
Expand Down