Skip to content

Commit

Permalink
Fix tooltips in CitationsDisplay (#5188)
Browse files Browse the repository at this point in the history
* Fix tooltips in CitationsDisplay

* Rename withTooltip(Callback<T, String>) to withStringTooltip() in ViewModelListCellFactory

* Add withTooltip(Callback<T, Tooltip>) to ViewModelListCellFactory

* Update CitationsDisplay for using ViewModelListCellFactory
  • Loading branch information
davidemdot authored and Siedlerchr committed Aug 18, 2019
1 parent 0255dda commit 0316d76
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private void buildLayout() {
actionsList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
new ViewModelListCellFactory<FieldFormatterCleanup>()
.withText(action -> action.getField() + ": " + action.getFormatter().getName())
.withTooltip(action -> action.getFormatter().getDescription())
.withStringTooltip(action -> action.getFormatter().getDescription())
.install(actionsList);
add(actionsList, 1, 1, 3, 1);

Expand Down Expand Up @@ -171,7 +171,7 @@ private GridPane getSelectorPanel() {
formattersCombobox = new ComboBox<>(FXCollections.observableArrayList(availableFormatters));
new ViewModelListCellFactory<Formatter>()
.withText(Formatter::getName)
.withTooltip(Formatter::getDescription)
.withStringTooltip(Formatter::getDescription)
.install(formattersCombobox);
EasyBind.subscribe(formattersCombobox.valueProperty(), e -> updateDescription());
builder.add(formattersCombobox, 3, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public LinkedFilesEditor(Field field, DialogService dialogService, BibDatabaseCo
.load();

ViewModelListCellFactory<LinkedFileViewModel> cellFactory = new ViewModelListCellFactory<LinkedFileViewModel>()
.withTooltip(LinkedFileViewModel::getDescription)
.withStringTooltip(LinkedFileViewModel::getDescription)
.withGraphic(LinkedFilesEditor::createFileDisplay)
.withContextMenu(this::createContextMenuForFile)
.withOnMouseClickedEvent(this::handleItemMouseClick)
Expand Down
44 changes: 41 additions & 3 deletions src/main/java/org/jabref/gui/texparser/CitationsDisplay.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package org.jabref.gui.texparser;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.Node;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;

import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.util.ViewModelListCellFactory;
Expand All @@ -18,20 +23,20 @@

public class CitationsDisplay extends ListView<Citation> {

private ObjectProperty<Path> basePath;
private final ObjectProperty<Path> basePath;

public CitationsDisplay() {
this.basePath = new SimpleObjectProperty<>(null);
new ViewModelListCellFactory<Citation>().withGraphic(this::getDisplayGraphic)
.withTooltip(Citation::getLineText)
.withTooltip(this::getDisplayTooltip)
.install(this);
}

public ObjectProperty<Path> basePathProperty() {
return basePath;
}

public Node getDisplayGraphic(Citation item) {
private Node getDisplayGraphic(Citation item) {
if (basePath.get() == null) {
basePath.set(item.getPath().getRoot());
}
Expand All @@ -52,4 +57,37 @@ public Node getDisplayGraphic(Citation item) {

return new VBox(contextBox, dataBox);
}

private Tooltip getDisplayTooltip(Citation item) {
String line = item.getLineText();
int start = item.getColStart();
int end = item.getColEnd();

List<Text> texts = new ArrayList<>(3);

// Text before the citation.
if (start > 0) {
texts.add(new Text(line.substring(0, start)));
}

// Citation text (highlighted).
Text citation = new Text(line.substring(start, end));
citation.getStyleClass().setAll("tooltip-text-bold");
texts.add(citation);

// Text after the citation.
if (end < line.length()) {
texts.add(new Text(line.substring(end)));
}

Tooltip tooltip = new Tooltip();
tooltip.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
tooltip.setGraphic(new TextFlow(texts.toArray(new Text[0])));
tooltip.setMaxHeight(10);
tooltip.setMinWidth(200);
tooltip.maxWidthProperty().bind(this.widthProperty().subtract(85));
tooltip.setWrapText(true);

return tooltip;
}
}
26 changes: 16 additions & 10 deletions src/main/java/org/jabref/gui/util/ViewModelListCellFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ViewModelListCellFactory<T> implements Callback<ListView<T>, ListCe

private Callback<T, String> toText;
private Callback<T, Node> toGraphic;
private Callback<T, String> toTooltip;
private Callback<T, Tooltip> toTooltip;
private BiConsumer<T, ? super MouseEvent> toOnMouseClickedEvent;
private Callback<T, String> toStyleClass;
private Callback<T, ContextMenu> toContextMenu;
Expand All @@ -58,9 +58,8 @@ public ViewModelListCellFactory<T> withIcon(Callback<T, GlyphIcons> toIcon) {
GlyphIcons icon = toIcon.call(viewModel);
if (icon != null) {
return MaterialDesignIconFactory.get().createIcon(icon);
} else {
return null;
}
return null;
};
return this;
}
Expand All @@ -74,7 +73,18 @@ public ViewModelListCellFactory<T> withIcon(Callback<T, GlyphIcons> toIcon, Call
return this;
}

public ViewModelListCellFactory<T> withTooltip(Callback<T, String> toTooltip) {
public ViewModelListCellFactory<T> withStringTooltip(Callback<T, String> toStringTooltip) {
this.toTooltip = viewModel -> {
String tooltipText = toStringTooltip.call(viewModel);
if (StringUtil.isNotBlank(tooltipText)) {
return new Tooltip(tooltipText);
}
return null;
};
return this;
}

public ViewModelListCellFactory<T> withTooltip(Callback<T, Tooltip> toTooltip) {
this.toTooltip = toTooltip;
return this;
}
Expand All @@ -89,8 +99,7 @@ public ViewModelListCellFactory<T> withStyleClass(Callback<T, String> toStyleCla
return this;
}

public ViewModelListCellFactory<T> withOnMouseClickedEvent(
BiConsumer<T, ? super MouseEvent> toOnMouseClickedEvent) {
public ViewModelListCellFactory<T> withOnMouseClickedEvent(BiConsumer<T, ? super MouseEvent> toOnMouseClickedEvent) {
this.toOnMouseClickedEvent = toOnMouseClickedEvent;
return this;
}
Expand Down Expand Up @@ -163,10 +172,7 @@ protected void updateItem(T item, boolean empty) {
getStyleClass().setAll(toStyleClass.call(viewModel));
}
if (toTooltip != null) {
String tooltipText = toTooltip.call(viewModel);
if (StringUtil.isNotBlank(tooltipText)) {
setTooltip(new Tooltip(tooltipText));
}
setTooltip(toTooltip.call(viewModel));
}
if (toContextMenu != null) {
setContextMenu(toContextMenu.call(viewModel));
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/model/texparser/Citation.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Citation {
/**
* The total number of characters that are shown around a cite (cite width included).
*/
private static final int CONTEXT_WIDTH = 200;
private static final int CONTEXT_WIDTH = 300;

private final Path path;
private final int line;
Expand Down Expand Up @@ -66,9 +66,9 @@ public String getContext() {

// Add three dots when the string does not contain all the line.
return String.format("%s%s%s",
(start > 0) ? "... " : "",
(start > 0) ? "..." : "",
lineText.substring(start, end).trim(),
(end < lineLength) ? " ..." : "");
(end < lineLength) ? "..." : "");
}

@Override
Expand Down

0 comments on commit 0316d76

Please sign in to comment.