Skip to content

Commit

Permalink
fix search highlighting in source editor
Browse files Browse the repository at this point in the history
fix listeners
  • Loading branch information
Siedlerchr committed May 12, 2019
1 parent 29155ef commit cd6d181
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#bibtexSourceCodeArea .search {
-fx-fill: red;
}

.bibtexSourceCodeArea .text {
-fx-fill: -fx-text-background-color;
}
27 changes: 14 additions & 13 deletions src/main/java/org/jabref/gui/entryeditor/SourceTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ public class SourceTab extends EntryEditorTab {
private final BibDatabaseMode mode;
private final UndoManager undoManager;
private final ObjectProperty<ValidationMessage> sourceIsValid = new SimpleObjectProperty<>();
private final ObservableRuleBasedValidator sourceValidator = new ObservableRuleBasedValidator(sourceIsValid);
@SuppressWarnings("unchecked") private final ObservableRuleBasedValidator sourceValidator = new ObservableRuleBasedValidator(sourceIsValid);
private final ImportFormatPreferences importFormatPreferences;
private final FileUpdateMonitor fileMonitor;
private final DialogService dialogService;
private CodeArea codeArea;

public SourceTab(BibDatabaseContext bibDatabaseContext, CountingUndoManager undoManager, LatexFieldFormatterPreferences fieldFormatterPreferences, ImportFormatPreferences importFormatPreferences, FileUpdateMonitor fileMonitor, DialogService dialogService) {
this.mode = bibDatabaseContext.getMode();
Expand All @@ -71,6 +72,17 @@ public SourceTab(BibDatabaseContext bibDatabaseContext, CountingUndoManager undo
this.fileMonitor = fileMonitor;
this.dialogService = dialogService;

Globals.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");
}
}
}
});
}

private static String getSourceString(BibEntry entry, BibDatabaseMode type, LatexFieldFormatterPreferences fieldFormatterPreferences) throws IOException {
Expand Down Expand Up @@ -145,18 +157,7 @@ protected void bindToEntry(BibEntry entry) {
}
});
this.setContent(codeArea);

Globals.stateManager.addSearchQueryHighlightListener(highlightPattern -> {
if (highlightPattern.isPresent()) {
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");
}
}
}
});

this.codeArea = codeArea;
// Store source for on focus out event in the source code (within its text area)
// and update source code for every change of entry field values
BindingsHelper.bindContentBidirectional(entry.getFieldsObservable(), codeArea.focusedProperty(), onFocus -> {
Expand Down
32 changes: 17 additions & 15 deletions src/main/java/org/jabref/gui/preview/PreviewViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ public PreviewViewer(BibDatabaseContext database, DialogService dialogService) {
previewView = new WebView();
setContent(previewView);
previewView.setContextMenuEnabled(false);

previewView.getEngine().getLoadWorker().stateProperty().addListener((ObservableValue<? extends Worker.State> observable,
Worker.State oldValue,
Worker.State newValue) -> {
if (newValue != Worker.State.SUCCEEDED) {
return;
}
Globals.stateManager.addSearchQueryHighlightListener(highlightPattern -> {
if (highlightPattern.isPresent()) {
String pattern = highlightPattern.get().pattern().replace("\\Q", "").replace("\\E", "");

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

}
});
});

}

public void setLayout(PreviewLayout newLayout) {
Expand Down Expand Up @@ -119,21 +136,6 @@ private void setPreviewText(String text) {
previewView.getEngine().setJavaScriptEnabled(true);
previewView.getEngine().loadContent(myText);

previewView.getEngine().getLoadWorker().stateProperty().addListener((ObservableValue<? extends Worker.State> observable,
Worker.State oldValue,
Worker.State newValue) -> {
if (newValue != Worker.State.SUCCEEDED) {
return;
}
Globals.stateManager.addSearchQueryHighlightListener(highlightPattern -> {
if (highlightPattern.isPresent()) {
String pattern = highlightPattern.get().pattern().replace("\\Q", "").replace("\\E", "");

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

}
});
});
this.setHvalue(0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public void addSearchListener(SearchQueryHighlightListener newListener) {

eventBus.register(newListener);
newListener.highlightPattern(pattern);

}

public void removeSearchListener(SearchQueryHighlightListener listener) {
Expand Down

0 comments on commit cd6d181

Please sign in to comment.