Skip to content

Commit

Permalink
Get rid of SearchQueryHighlightObservable
Browse files Browse the repository at this point in the history
Use SearchQuery directly

TODO: fix updating of search results
  • Loading branch information
Siedlerchr committed May 19, 2019
1 parent 1210a2a commit d0c5f2e
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 228 deletions.
23 changes: 0 additions & 23 deletions src/main/java/org/jabref/gui/StateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import org.jabref.gui.util.OptionalObjectProperty;
import org.jabref.logic.search.SearchQuery;
import org.jabref.logic.search.SearchQueryHighlightListener;
import org.jabref.logic.search.SearchQueryHighlightObservable;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.groups.GroupTreeNode;
Expand All @@ -39,7 +37,6 @@ public class StateManager {
private final ObservableMap<BibDatabaseContext, ObservableList<GroupTreeNode>> selectedGroups = FXCollections.observableHashMap();
private final OptionalObjectProperty<SearchQuery> activeSearchQuery = OptionalObjectProperty.empty();
private final IntegerProperty searchResultSize = new SimpleIntegerProperty();
private final SearchQueryHighlightObservable searchQueryHighlightObservable = new SearchQueryHighlightObservable();

public StateManager() {
activeGroups.bind(Bindings.valueAt(selectedGroups, activeDatabase.orElse(null)));
Expand Down Expand Up @@ -99,24 +96,4 @@ public void setSearchQuery(SearchQuery searchQuery) {
public IntegerProperty searchResultSizeProperty() {
return searchResultSize;
}

public SearchQueryHighlightObservable getSearchQueryHighlightObservable() {
return searchQueryHighlightObservable;
}

public void fireSearchQueryHighlightEvent() {
if (activeSearchQuery.get().isPresent()) {
searchQueryHighlightObservable.fireSearchlistenerEvent(activeSearchQuery.getValue().get());
}
}

public void resetSearchQueryHighlightObservable()
{
searchQueryHighlightObservable.reset();
}

public void addSearchQueryHighlightListener(SearchQueryHighlightListener listener) {
searchQueryHighlightObservable.removeSearchListener(listener);
searchQueryHighlightObservable.addSearchListener(listener);
}
}
8 changes: 0 additions & 8 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.jabref.logic.help.HelpFile;
import org.jabref.logic.importer.EntryBasedFetcher;
import org.jabref.logic.importer.WebFetchers;
import org.jabref.logic.search.SearchQueryHighlightListener;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.util.FileUpdateMonitor;
Expand Down Expand Up @@ -74,7 +73,6 @@ public class EntryEditor extends BorderPane {
private final BibDatabaseContext databaseContext;
private final CountingUndoManager undoManager;
private final BasePanel panel;
private final List<SearchQueryHighlightListener> searchListeners = new ArrayList<>();
private Subscription typeSubscription;
private final List<EntryEditorTab> tabs;
private final FileUpdateMonitor fileMonitor;
Expand Down Expand Up @@ -403,12 +401,6 @@ private void fetchAndMerge(EntryBasedFetcher fetcher) {
new FetchAndMergeEntry(panel, taskExecutor).fetchAndMerge(entry, fetcher);
}

void addSearchListener(SearchQueryHighlightListener listener) {
// TODO: Highlight search text in entry editors
searchListeners.add(listener);

}

public void setFocusToField(String fieldName) {
DefaultTaskExecutor.runInJavaFXThread(() -> {
for (Tab tab : tabbed.getTabs()) {
Expand Down
32 changes: 22 additions & 10 deletions src/main/java/org/jabref/gui/entryeditor/SourceTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
import java.io.StringWriter;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.undo.UndoManager;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ListChangeListener;
import javafx.geometry.Point2D;
import javafx.scene.control.Tooltip;
Expand All @@ -33,6 +37,7 @@
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.importer.fileformat.BibtexParser;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.search.SearchQuery;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.BibDatabaseMode;
Expand Down Expand Up @@ -61,6 +66,7 @@ public class SourceTab extends EntryEditorTab {
private final DialogService dialogService;
private final StateManager stateManager;

private Optional<Pattern> searchHighlightPattern = Optional.empty();
private CodeArea codeArea;

public SourceTab(BibDatabaseContext bibDatabaseContext, CountingUndoManager undoManager, LatexFieldFormatterPreferences fieldFormatterPreferences, ImportFormatPreferences importFormatPreferences, FileUpdateMonitor fileMonitor, DialogService dialogService, StateManager stateManager) {
Expand All @@ -75,17 +81,23 @@ public SourceTab(BibDatabaseContext bibDatabaseContext, CountingUndoManager undo
this.dialogService = dialogService;
this.stateManager = stateManager;

stateManager.addSearchQueryHighlightListener(highlightPattern -> {
if (highlightPattern.isPresent() && codeArea != null) {
codeArea.setStyleClass(0, codeArea.getLength(), "text");
Matcher matcher = highlightPattern.get().matcher(codeArea.getText());
while (matcher.find()) {
for (int i = 0; i <= matcher.groupCount(); i++) {
codeArea.setStyleClass(matcher.start(), matcher.end(), "search");
}
stateManager.activeSearchQueryProperty().addListener((observable, oldValue, newValue) -> {
searchHighlightPattern = newValue.flatMap(SearchQuery::getPatternForWords);
highlightSearchPattern();
});

}

private void highlightSearchPattern() {
if (searchHighlightPattern.isPresent() && codeArea != null) {
codeArea.setStyleClass(0, codeArea.getLength(), "text");
Matcher matcher = searchHighlightPattern.get().matcher(codeArea.getText());
while (matcher.find()) {
for (int i = 0; i <= matcher.groupCount(); i++) {
codeArea.setStyleClass(matcher.start(), matcher.end(), "search");
}
}
});
}
}

private static String getSourceString(BibEntry entry, BibDatabaseMode type, LatexFieldFormatterPreferences fieldFormatterPreferences) throws IOException {
Expand Down Expand Up @@ -170,7 +182,7 @@ protected void bindToEntry(BibEntry entry) {
codeArea.clear();
try {
codeArea.appendText(getSourceString(entry, mode, fieldFormatterPreferences));
stateManager.fireSearchQueryHighlightEvent();
highlightSearchPattern();

} catch (IOException ex) {
codeArea.setEditable(false);
Expand Down
38 changes: 26 additions & 12 deletions src/main/java/org/jabref/gui/preview/PreviewViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;

import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.value.ObservableValue;
import javafx.beans.value.ChangeListener;
import javafx.concurrent.Worker;
import javafx.print.PrinterJob;
import javafx.scene.control.ScrollPane;
Expand All @@ -21,6 +22,7 @@
import org.jabref.logic.citationstyle.PreviewLayout;
import org.jabref.logic.exporter.ExporterFactory;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.search.SearchQuery;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

Expand Down Expand Up @@ -57,6 +59,9 @@ public class PreviewViewer extends ScrollPane implements InvalidationListener {
*/
private Optional<BibEntry> entry = Optional.empty();
private BibDatabaseContext database;
private boolean registered;

private Optional<Pattern> searchHighlightPattern = Optional.empty();

/**
* @param database Used for resolving strings and pdf directories for links.
Expand All @@ -72,22 +77,31 @@ public PreviewViewer(BibDatabaseContext database, DialogService dialogService, S
setContent(previewView);
previewView.setContextMenuEnabled(false);

previewView.getEngine().getLoadWorker().stateProperty().addListener((ObservableValue<? extends Worker.State> observable,
Worker.State oldValue,
Worker.State newValue) -> {
previewView.getEngine().getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {

if (newValue != Worker.State.SUCCEEDED) {
return;
}
stateManager.addSearchQueryHighlightListener(highlightPattern -> {
if (highlightPattern.isPresent()) {
String pattern = highlightPattern.get().pattern().replace("\\Q", "").replace("\\E", "");
if (!registered) {
stateManager.activeSearchQueryProperty().addListener(listener);
registered = true;
}
highlightSearchPattern();
});

}

previewView.getEngine().executeScript("highlight('" + pattern + "');");
private ChangeListener<Optional<SearchQuery>> listener = (queryObservable, queryOldValue, queryNewValue) -> {
searchHighlightPattern = queryNewValue.flatMap(SearchQuery::getPatternForWords);
highlightSearchPattern();
};

}
});
});
private void highlightSearchPattern() {
if (searchHighlightPattern.isPresent()) {
String pattern = searchHighlightPattern.get().pattern().replace("\\Q", "").replace("\\E", "");

previewView.getEngine().executeScript("highlight('" + pattern + "');");
}
}

public void setLayout(PreviewLayout newLayout) {
Expand All @@ -109,8 +123,8 @@ public void setEntry(BibEntry newEntry) {
for (Observable observable : newEntry.getObservables()) {
observable.addListener(this);
}

update();

}

private void update() {
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/org/jabref/gui/search/GlobalSearchBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.search.SearchQuery;
import org.jabref.logic.search.SearchQueryHighlightObservable;
import org.jabref.model.entry.Author;
import org.jabref.preferences.SearchPreferences;

Expand All @@ -73,7 +72,6 @@ public class GlobalSearchBar extends HBox {
private final ToggleButton regularExp;
private final Button searchModeButton = new Button();
private final Label currentResults = new Label("");
private final SearchQueryHighlightObservable searchQueryHighlightObservable = new SearchQueryHighlightObservable();
private SearchDisplayMode searchDisplayMode;

public GlobalSearchBar(JabRefFrame frame) {
Expand Down Expand Up @@ -149,6 +147,16 @@ public GlobalSearchBar(JabRefFrame frame) {
currentResults);

this.setAlignment(Pos.CENTER_LEFT);

//TODO Needs fixing, not yet working correctly
EasyBind.subscribe(Globals.stateManager.searchResultSizeProperty(), matchedResults -> {

Globals.stateManager.activeSearchQueryProperty().getValue().ifPresent(searchQuery -> {
updateResults(matchedResults.intValue(), SearchDescribers.getSearchDescriberFor(searchQuery).getDescription(),
searchQuery.isGrammarBasedSearch());
});

});
}

private void toggleSearchModeAndSearch() {
Expand Down Expand Up @@ -186,7 +194,6 @@ public void focus() {
private void clearSearch() {
currentResults.setText("");
searchField.setText("");
Globals.stateManager.resetSearchQueryHighlightObservable();
Globals.stateManager.clearSearchQuery();
}

Expand All @@ -209,18 +216,11 @@ public void performSearch() {

Globals.stateManager.setSearchQuery(searchQuery);

updateResults(Globals.stateManager.searchResultSizeProperty().get(),
SearchDescribers.getSearchDescriberFor(searchQuery).getDescription(),
searchQuery.isGrammarBasedSearch());


}

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

searchQueryHighlightObservable.reset();

Globals.stateManager.clearSearchQuery();

String illegalSearch = Localization.lang("Search failed: illegal search expression");
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/jabref/logic/search/SearchQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.regex.Pattern;

import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.BibEntry;
Expand Down Expand Up @@ -119,6 +122,28 @@ public List<String> getSearchWords() {
}
}

// Returns a regular expression pattern in the form (w1)|(w2)| ... wi are escaped if no regular expression search is enabled
public Optional<Pattern> getPatternForWords() {
List<String> words = getSearchWords();

if ((words == null) || words.isEmpty() || words.get(0).isEmpty()) {
return Optional.empty();
}

// compile the words to a regular expression in the form (w1)|(w2)|(w3)
StringJoiner joiner = new StringJoiner(")|(", "(", ")");
for (String word : words) {
joiner.add(regularExpression ? word : Pattern.quote(word));
}
String searchPattern = joiner.toString();

if (caseSensitive) {
return Optional.of(Pattern.compile(searchPattern));
} else {
return Optional.of(Pattern.compile(searchPattern, Pattern.CASE_INSENSITIVE));
}
}

public SearchRule getRule() {
return rule;
}
Expand Down

This file was deleted.

Loading

0 comments on commit d0c5f2e

Please sign in to comment.