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

Handle regular expressions in global search #5381

Merged
merged 4 commits into from
Oct 27, 2019
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
4 changes: 4 additions & 0 deletions src/main/java/org/jabref/gui/Base.css
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,10 @@
-fx-text-fill: -jr-search-text;
}

.mainToolbar .search-field .label {
-fx-padding: 0em 1.8em 0em 0em;
}

/* The little arrow that shows up when not all tool-bar icons fit into the tool-bar.
We want to have a look that matches our icons in the tool-bar */
.mainToolbar .tool-bar-overflow-button > .arrow {
Expand Down
35 changes: 34 additions & 1 deletion src/main/java/org/jabref/gui/search/GlobalSearchBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.css.PseudoClass;
import javafx.event.Event;
Expand Down Expand Up @@ -48,13 +51,18 @@
import org.jabref.gui.search.rules.describer.SearchDescribers;
import org.jabref.gui.util.BindingsHelper;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.gui.util.IconValidationDecorator;
import org.jabref.gui.util.TooltipTextUtil;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.search.SearchQuery;
import org.jabref.model.entry.Author;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.SearchPreferences;

import de.saxsys.mvvmfx.utils.validation.FunctionBasedValidator;
import de.saxsys.mvvmfx.utils.validation.ValidationMessage;
import de.saxsys.mvvmfx.utils.validation.Validator;
import de.saxsys.mvvmfx.utils.validation.visualization.ControlsFxVisualizer;
import impl.org.controlsfx.skin.AutoCompletePopup;
import org.controlsfx.control.textfield.AutoCompletionBinding;
import org.fxmisc.easybind.EasyBind;
Expand All @@ -81,6 +89,7 @@ public class GlobalSearchBar extends HBox {
private final Tooltip tooltip = new Tooltip();
private final StateManager stateManager;
private SearchDisplayMode searchDisplayMode;
private Validator regexValidator;

public GlobalSearchBar(JabRefFrame frame, StateManager stateManager) {
super();
Expand Down Expand Up @@ -137,6 +146,15 @@ public GlobalSearchBar(JabRefFrame frame, StateManager stateManager) {
searchField.setMaxWidth(initialSize);
HBox.setHgrow(searchField, Priority.ALWAYS);

regexValidator = new FunctionBasedValidator<>(
searchField.textProperty(),
query -> !(regularExp.isSelected() && !validRegex()),
ValidationMessage.error(Localization.lang("Invalid regular expression"))
);
ControlsFxVisualizer visualizer = new ControlsFxVisualizer();
visualizer.setDecoration(new IconValidationDecorator(Pos.CENTER_LEFT));
Platform.runLater(() -> { visualizer.initVisualization(regexValidator.getValidationStatus(), searchField); });

Timer searchTask = FxTimer.create(java.time.Duration.ofMillis(SEARCH_DELAY), this::performSearch);
searchField.textProperty().addListener((observable, oldValue, newValue) -> searchTask.restart());

Expand Down Expand Up @@ -219,15 +237,30 @@ public void performSearch() {
return;
}

// Invalid regular expression
if (!regexValidator.getValidationStatus().isValid()) {
currentResults.setText(Localization.lang("Invalid regular expression"));
return;
}

SearchQuery searchQuery = new SearchQuery(this.searchField.getText(), this.caseSensitive.isSelected(), this.regularExp.isSelected());
if (!searchQuery.isValid()) {
informUserAboutInvalidSearchQuery();
return;
}

stateManager.setSearchQuery(searchQuery);
}

private boolean validRegex() {
try {
Pattern.compile(searchField.getText());
} catch (PatternSyntaxException e) {
LOGGER.debug(e.getMessage());
return false;
}
return true;
}

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

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,7 @@ should\ contain\ a\ four\ digit\ number=should contain a four digit number
should\ contain\ a\ valid\ page\ number\ range=should contain a valid page number range
No\ results\ found.=No results found.
Found\ %0\ results.=Found %0 results.
Invalid\ regular\ expression=Invalid regular expression
plain\ text=plain text
This\ search\ contains\ entries\ in\ which\ any\ field\ contains\ the\ regular\ expression\ <b>%0</b>=This search contains entries in which any field contains the regular expression <b>%0</b>
This\ search\ contains\ entries\ in\ which\ any\ field\ contains\ the\ term\ <b>%0</b>=This search contains entries in which any field contains the term <b>%0</b>
Expand Down