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

Fix tooltips in CitationsDisplay #5188

Merged
merged 4 commits into from
Aug 18, 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
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) ? "..." : "",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not here, because the getContext() method extracts a substring that always has the \cite command in the middle. That is what is shown into the dashed box.

But is could be useful in the near future. Thanks!

lineText.substring(start, end).trim(),
(end < lineLength) ? " ..." : "");
(end < lineLength) ? "..." : "");
}

@Override
Expand Down