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 1 commit
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
48 changes: 44 additions & 4 deletions src/main/java/org/jabref/gui/texparser/CitationsDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
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.ListCell;
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 javafx.util.Callback;

import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.util.ViewModelListCellFactory;
import org.jabref.gui.util.TooltipTextUtil;
import org.jabref.model.strings.LatexToUnicodeAdapter;
import org.jabref.model.texparser.Citation;

Expand All @@ -22,9 +27,7 @@ public class CitationsDisplay extends ListView<Citation> {

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

public ObjectProperty<Path> basePathProperty() {
Expand Down Expand Up @@ -52,4 +55,41 @@ public Node getDisplayGraphic(Citation item) {

return new VBox(contextBox, dataBox);
}

private Tooltip getDisplayTooltip(Citation item) {
TextFlow tooltipText = new TextFlow();
String lineText = item.getLineText();
tooltipText.getChildren().setAll(new Text(lineText.substring(0, item.getColStart())));
tooltipText.getChildren().add(TooltipTextUtil.createText(lineText.substring(item.getColStart(), item.getColEnd()), TooltipTextUtil.TextType.BOLD));
tooltipText.getChildren().add(new Text(lineText.substring(item.getColEnd())));

Tooltip tooltip = new Tooltip();
tooltip.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
tooltip.setGraphic(tooltipText);
tooltip.setMaxHeight(10);
tooltip.maxWidthProperty().bind(this.widthProperty().subtract(85));
tooltip.setWrapText(true);

return tooltip;
}

class ListCitationsCellFactory implements Callback<ListView<Citation>, ListCell<Citation>> {
davidemdot marked this conversation as resolved.
Show resolved Hide resolved
@Override
public ListCell<Citation> call(ListView<Citation> param) {
return new ListCell<Citation>() {
@Override
protected void updateItem(Citation item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setGraphic(null);
setTooltip(null);
} else {
setGraphic(getDisplayGraphic(item));
setTooltip(getDisplayTooltip(item));
}
getListView().refresh();
}
};
}
}
}
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