Skip to content

Commit

Permalink
Reimplement tooltips for file and identifier columns (#4011)
Browse files Browse the repository at this point in the history
* Reimplement tooltips for file and identifier column

* Extract not translatable strings out of localization
  • Loading branch information
florian-beetz authored and Siedlerchr committed May 7, 2018
1 parent 8a8e09a commit 35b0683
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
34 changes: 28 additions & 6 deletions src/main/java/org/jabref/gui/maintable/MainTableColumnFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public MainTableColumnFactory(BibDatabaseContext database, ColumnPreferences pre

// Add column for eprints
if (preferences.showEprintColumn()) {
columns.add(createIconColumn(IconTheme.JabRefIcons.WWW, FieldName.EPRINT));
columns.add(createEprintColumn(IconTheme.JabRefIcons.WWW, FieldName.EPRINT));
}

// Add columns for other file types
Expand Down Expand Up @@ -245,6 +245,7 @@ private TableColumn<BibEntryTableViewModel, List<LinkedFile>> createFileColumn()
column.setCellValueFactory(cellData -> cellData.getValue().getLinkedFiles());
new ValueTableCellFactory<BibEntryTableViewModel, List<LinkedFile>>()
.withGraphic(this::createFileIcon)
.withTooltip(this::createFileTooltip)
.withMenu(this::createFileMenu)
.withOnMouseClickedEvent((entry, linkedFiles) -> event -> {
if ((event.getButton() == MouseButton.PRIMARY) && (linkedFiles.size() == 1)) {
Expand All @@ -257,6 +258,13 @@ private TableColumn<BibEntryTableViewModel, List<LinkedFile>> createFileColumn()
return column;
}

private String createFileTooltip(List<LinkedFile> linkedFiles) {
if (linkedFiles.size() > 0) {
return Localization.lang("Open file %0", linkedFiles.get(0).getLink());
}
return null;
}

private ContextMenu createFileMenu(BibEntryTableViewModel entry, List<LinkedFile> linkedFiles) {
if (linkedFiles.size() <= 1) {
return null;
Expand Down Expand Up @@ -285,9 +293,10 @@ private TableColumn<BibEntryTableViewModel, String> createUrlOrDoiColumn(JabRefI
column.getStyleClass().add(ICON_COLUMN);
setExactWidth(column, GUIGlobals.WIDTH_ICON_COL);
// icon is chosen based on field name in cell, so map fields to its names
column.setCellValueFactory(cellData -> EasyBind.map(cellData.getValue().getField(firstField), x -> firstField).orElse(EasyBind.map(cellData.getValue().getField(secondField), x -> secondField)));
column.setCellValueFactory(cellData -> EasyBind.monadic(cellData.getValue().getField(firstField)).map(x -> firstField).orElse(EasyBind.monadic(cellData.getValue().getField(secondField)).map(x -> secondField)));
new ValueTableCellFactory<BibEntryTableViewModel, String>()
.withGraphic(cellFactory::getTableIcon)
.withTooltip(this::createIdentifierTooltip)
.withOnMouseClickedEvent((BibEntryTableViewModel entry, String content) -> (MouseEvent event) -> openUrlOrDoi(event, entry, content))
.install(column);
return column;
Expand All @@ -313,16 +322,29 @@ private void openUrlOrDoi(MouseEvent event, BibEntryTableViewModel entry, String
event.consume();
}

private TableColumn<BibEntryTableViewModel, String> createIconColumn(JabRefIcon icon, String field) {
private String createIdentifierTooltip(BibEntryTableViewModel entry, String content) {
Optional<String> field = entry.getEntry().getField(content);
if (field.isPresent()) {
if (FieldName.DOI.equals(content)) {
return Localization.lang("Open %0 URL (%1)", "DOI", field.get());
} else if (FieldName.URL.equals(content)) {
return Localization.lang("Open URL (%0)", field.get());
} else if (FieldName.EPRINT.equals(content)) {
return Localization.lang("Open %0 URL (%1)", "ArXiv", field.get());
}
}
return null;
}

private TableColumn<BibEntryTableViewModel, String> createEprintColumn(JabRefIcon icon, String field) {
TableColumn<BibEntryTableViewModel, String> column = new TableColumn<>();
column.setGraphic(icon.getGraphicNode());
column.getStyleClass().add(ICON_COLUMN);
setExactWidth(column, GUIGlobals.WIDTH_ICON_COL);
column.setCellValueFactory(cellData -> {
return EasyBind.map(cellData.getValue().getField(field), x -> field);
});
column.setCellValueFactory(cellData -> EasyBind.monadic(cellData.getValue().getField(field)).map(x -> field));
new ValueTableCellFactory<BibEntryTableViewModel, String>()
.withGraphic(cellFactory::getTableIcon)
.withTooltip(this::createIdentifierTooltip)
.withOnMouseClickedEvent((BibEntryTableViewModel entry, String content) -> (MouseEvent event) -> openUrlOrDoi(event, entry, field))
.install(column);
return column;
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/org/jabref/gui/util/ValueTableCellFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ValueTableCellFactory<S, T> implements Callback<TableColumn<S, T>,
private Function<T, String> toText;
private BiFunction<S, T, Node> toGraphic;
private BiFunction<S, T, EventHandler<? super MouseEvent>> toOnMouseClickedEvent;
private Function<T, String> toTooltip;
private BiFunction<S, T, String> toTooltip;
private Function<T, ContextMenu> contextMenuFactory;
private BiFunction<S, T, ContextMenu> menuFactory;

Expand All @@ -46,11 +46,16 @@ public ValueTableCellFactory<S, T> withGraphic(BiFunction<S, T, Node> toGraphic)
return this;
}

public ValueTableCellFactory<S, T> withTooltip(Function<T, String> toTooltip) {
public ValueTableCellFactory<S, T> withTooltip(BiFunction<S, T, String> toTooltip) {
this.toTooltip = toTooltip;
return this;
}

public ValueTableCellFactory<S, T> withTooltip(Function<T, String> toTooltip) {
this.toTooltip = (rowItem, value) -> toTooltip.apply(value);
return this;
}

public ValueTableCellFactory<S, T> withOnMouseClickedEvent(BiFunction<S, T, EventHandler<? super MouseEvent>> toOnMouseClickedEvent) {
this.toOnMouseClickedEvent = toOnMouseClickedEvent;
return this;
Expand Down Expand Up @@ -95,7 +100,7 @@ protected void updateItem(T item, boolean empty) {
setGraphic(toGraphic.apply(rowItem, item));
}
if (toTooltip != null) {
String tooltipText = toTooltip.apply(item);
String tooltipText = toTooltip.apply(rowItem, item);
if (StringUtil.isNotBlank(tooltipText)) {
setTooltip(new Tooltip(tooltipText));
}
Expand Down

0 comments on commit 35b0683

Please sign in to comment.