Skip to content

Commit

Permalink
Fix invalid regex query throws an exception (#11625)
Browse files Browse the repository at this point in the history
Label.text : A bound value cannot be set.
  • Loading branch information
LoayGhreeb authored Aug 14, 2024
1 parent 65a4fb0 commit 08111bb
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions src/main/java/org/jabref/gui/search/GlobalSearchBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.jabref.preferences.PreferencesService;
import org.jabref.preferences.SearchPreferences;

import com.tobiasdiez.easybind.EasyBind;
import impl.org.controlsfx.skin.AutoCompletePopup;
import org.controlsfx.control.textfield.AutoCompletionBinding;
import org.controlsfx.control.textfield.CustomTextField;
Expand All @@ -90,14 +91,15 @@ public class GlobalSearchBar extends HBox {
private final ToggleButton keepSearchString;
private final ToggleButton filterModeButton;
private final Tooltip searchFieldTooltip = new Tooltip();
private final Label currentResults = new Label("");
private final StateManager stateManager;
private final PreferencesService preferencesService;
private final UndoManager undoManager;
private final LibraryTabContainer tabContainer;
private final SearchPreferences searchPreferences;
private final DialogService dialogService;
private final BooleanProperty globalSearchActive = new SimpleBooleanProperty(false);
private final BooleanProperty illegalSearch = new SimpleBooleanProperty(false);
private final BooleanProperty invalidRegex = new SimpleBooleanProperty(false);
private GlobalSearchResultDialog globalSearchResultDialog;
private final SearchType searchType;

Expand All @@ -121,19 +123,29 @@ public GlobalSearchBar(LibraryTabContainer tabContainer,
searchField = SearchTextField.create(keyBindingRepository);
searchField.disableProperty().bind(needsDatabase(stateManager).not());

Label currentResults = new Label();
// fits the standard "found x entries"-message thus hinders the searchbar to jump around while searching if the tabContainer width is too small
currentResults.setPrefWidth(150);
currentResults.visibleProperty().bind(stateManager.activeSearchQuery(searchType).isPresent());
currentResults.textProperty().bind(Bindings.createStringBinding(() -> {
int matched = stateManager.searchResultSize(searchType).get();
if (matched == 0) {
searchField.pseudoClassStateChanged(CLASS_NO_RESULTS, true);
return Localization.lang("No results found.");
} else {
searchField.pseudoClassStateChanged(CLASS_RESULTS_FOUND, true);
return Localization.lang("Found %0 results.", String.valueOf(matched));
}
}, stateManager.searchResultSize(searchType)));

currentResults.textProperty().bind(EasyBind.combine(
stateManager.searchResultSize(searchType), illegalSearch, invalidRegex,
(matched, illegal, invalid) -> {
if (illegal) {
searchField.pseudoClassStateChanged(CLASS_NO_RESULTS, true);
return Localization.lang("Search failed: illegal search expression");
} else if (invalid) {
searchField.pseudoClassStateChanged(CLASS_NO_RESULTS, true);
return Localization.lang("Invalid regular expression");
} else if (matched.intValue() == 0) {
searchField.pseudoClassStateChanged(CLASS_NO_RESULTS, true);
return Localization.lang("No results found.");
} else {
searchField.pseudoClassStateChanged(CLASS_RESULTS_FOUND, true);
return Localization.lang("Found %0 results.", String.valueOf(matched));
}
}
));

searchField.setTooltip(searchFieldTooltip);
searchFieldTooltip.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
Expand Down Expand Up @@ -331,14 +343,18 @@ public void updateSearchQuery() {

// Invalid regular expression
if (regularExpressionButton.isSelected() && !validRegex()) {
currentResults.setText(Localization.lang("Invalid regular expression"));
invalidRegex.setValue(true);
return;
} else {
invalidRegex.setValue(false);
}

SearchQuery searchQuery = new SearchQuery(this.searchField.getText(), searchPreferences.getSearchFlags());
if (!searchQuery.isValid()) {
informUserAboutInvalidSearchQuery();
illegalSearch.set(true);
return;
} else {
illegalSearch.set(false);
}
stateManager.activeSearchQuery(searchType).set(Optional.of(searchQuery));
}
Expand All @@ -353,15 +369,6 @@ private boolean validRegex() {
return true;
}

private void informUserAboutInvalidSearchQuery() {
searchField.pseudoClassStateChanged(CLASS_NO_RESULTS, true);

stateManager.activeSearchQuery(searchType).set(Optional.empty());

String illegalSearch = Localization.lang("Search failed: illegal search expression");
currentResults.setText(illegalSearch);
}

public void setAutoCompleter(SuggestionProvider<Author> searchCompleter) {
if (preferencesService.getAutoCompletePreferences().shouldAutoComplete()) {
AutoCompletionTextInputBinding<Author> autoComplete = AutoCompletionTextInputBinding.autoComplete(searchField,
Expand Down

0 comments on commit 08111bb

Please sign in to comment.