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

Truncates the link and/or the link description in the column "linked files" in main table, if too long #6179

Merged
merged 17 commits into from
May 17, 2020
Merged
Show file tree
Hide file tree
Changes from 14 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed an issue where opening a library from the recent libraries menu was not possible. [#5939](https://github.com/JabRef/jabref/issues/5939)
- We fixed an issue with inconsistent capitalization of file extensions when downloading files. [#6115](https://github.com/JabRef/jabref/issues/6115)
- We fixed the display of language and encoding in the preferences dialog. [#6130](https://github.com/JabRef/jabref/pull/6130)
- Now the link and/or the link description in the column "linked files" of the main table gets truncated or wrapped, if too long, otherwise display issues arise. [#6178](https://github.com/JabRef/jabref/issues/6178)
- We fixed an error that sometimes occurred when using the context menu. [#6085](https://github.com/JabRef/jabref/issues/6085)
- We fixed an issue where search full-text documents downloaded files with same name, overwriting existing files. [#6174](https://github.com/JabRef/jabref/pull/6174)
- We fixed an issue when importing into current library an erroneous message "import cancelled" is displayed even though import is successful. [#6266](https://github.com/JabRef/jabref/issues/6266)
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.icon.JabRefIcon;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.ControlHelper;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.externalfiles.LinkedFileHandler;
import org.jabref.logic.l10n.Localization;
Expand Down Expand Up @@ -151,6 +152,18 @@ public String getDescriptionAndLink() {
}
}

public String getTruncatedDescriptionAndLink() {
if (StringUtil.isBlank(linkedFile.getDescription())) {
return ControlHelper.truncateString(linkedFile.getLink(), -1, "...",
ControlHelper.EllipsisPosition.CENTER);
} else {
return ControlHelper.truncateString(linkedFile.getDescription(), -1, "...",
ControlHelper.EllipsisPosition.CENTER) + " (" +
ControlHelper.truncateString(linkedFile.getLink(), -1, "...",
ControlHelper.EllipsisPosition.CENTER) + ")";
}
}

public Optional<Path> findIn(List<Path> directories) {
return linkedFile.findIn(directories);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ private ContextMenu createFileMenu(BibEntryTableViewModel entry, List<LinkedFile
for (LinkedFile linkedFile : linkedFiles) {
LinkedFileViewModel linkedFileViewModel = new LinkedFileViewModel(linkedFile, entry.getEntry(), database, Globals.TASK_EXECUTOR, dialogService, Globals.prefs.getXMPPreferences(), Globals.prefs.getFilePreferences(), externalFileTypes);

MenuItem menuItem = new MenuItem(linkedFileViewModel.getDescriptionAndLink(), linkedFileViewModel.getTypeIcon().getGraphicNode());
MenuItem menuItem = new MenuItem(linkedFileViewModel.getTruncatedDescriptionAndLink(), linkedFileViewModel.getTypeIcon().getGraphicNode());
menuItem.setOnAction(event -> linkedFileViewModel.open());
contextMenu.getItems().add(menuItem);
}
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/org/jabref/gui/util/ControlHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class ControlHelper {
private static PseudoClass dragOverCenter = PseudoClass.getPseudoClass("dragOver-center");
private static PseudoClass dragOverTop = PseudoClass.getPseudoClass("dragOver-top");

public enum EllipsisPosition { BEGINNING, CENTER, ENDING }

public static void setAction(ButtonType buttonType, DialogPane dialogPane, Consumer<Event> consumer) {
Button button = (Button) dialogPane.lookupButton(buttonType);
button.addEventFilter(ActionEvent.ACTION, (event -> {
Expand Down Expand Up @@ -97,4 +99,46 @@ public static void setDroppingPseudoClasses(Cell<?> cell) {
public static void removeDroppingPseudoClasses(Cell<?> cell) {
removePseudoClasses(cell, dragOverBottom, dragOverCenter, dragOverTop);
}

/**
* If needed, truncates a given string to <code>maxCharacters</code>, adding <code>ellipsisString</code> instead.
*
* @param text text which should be truncated, if needed
* @param maxCharacters maximum amount of characters which the resulting text should have, including the
* <code>ellipsisString</code>; if set to -1, then the default length of 75 characters will be
* used
* @param ellipsisString string which should be used for indicating the truncation
* @param ellipsisPosition location in the given text where the truncation should be performed
* @return the new, truncated string
*/
public static String truncateString(String text, int maxCharacters, String ellipsisString, EllipsisPosition ellipsisPosition) {
if (text == null || "".equals(text)) {
return text; // return original
}

if (ellipsisString == null) {
ellipsisString = "";
}

if (maxCharacters == -1) {
maxCharacters = 75; // default
}

maxCharacters = Math.max(ellipsisString.length(), maxCharacters);

if (text.length() > maxCharacters) {
// truncation necessary
switch (ellipsisPosition) {
case BEGINNING:
return ellipsisString + text.substring(text.length() - (maxCharacters - ellipsisString.length()));
case CENTER:
int partialLength = (int) Math.floor((maxCharacters - ellipsisString.length()) / 2f);
return text.substring(0, partialLength) + ellipsisString + text.substring(text.length() - partialLength);
case ENDING:
return text.substring(0, maxCharacters - ellipsisString.length()) + ellipsisString;
}
}

return text;
}
}
9 changes: 8 additions & 1 deletion src/main/java/org/jabref/gui/util/ValueTableCellFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javafx.scene.control.Tooltip;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.stage.Screen;
import javafx.util.Callback;

import org.jabref.model.strings.StringUtil;
Expand Down Expand Up @@ -100,7 +101,13 @@ protected void updateItem(T item, boolean empty) {
if (toTooltip != null) {
String tooltipText = toTooltip.apply(rowItem, item);
if (StringUtil.isNotBlank(tooltipText)) {
setTooltip(new Tooltip(tooltipText));
Screen currentScreen = Screen.getPrimary();
double maxWidth = currentScreen.getBounds().getWidth();
Tooltip tooltip = new Tooltip(tooltipText);
tooltip.setMaxWidth(maxWidth * 2 / 3);
//tooltip.setTextOverrun(OverrunStyle.CENTER_ELLIPSIS); // info: can be enabled instead of wrapping the text
tooltip.setWrapText(true);
setTooltip(tooltip);
}
}

Expand Down