Skip to content

Commit dc49e45

Browse files
fix: address static analysis, i18n, and concurrency review findings
1 parent 7d2494e commit dc49e45

3 files changed

Lines changed: 42 additions & 10 deletions

File tree

jabgui/src/main/java/org/jabref/gui/preview/PreviewViewer.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ private void update() {
211211
databaseContext == null ? "null" : databaseContext,
212212
entry == null ? "null" : entry,
213213
layout == null ? "null" : layout);
214+
previewThrottler.cancel();
215+
latestRequestId.incrementAndGet();
214216
setPreviewText("");
215217
return;
216218
}
@@ -224,10 +226,19 @@ private void update() {
224226

225227
BackgroundTask.wrap(() -> generatePreviewWithAnnotations(currentEntry, filePreferences))
226228
.onSuccess(htmlComplete -> {
227-
if (requestId == latestRequestId.get() && currentEntry.equals(this.entry) && currentDatabaseContext.equals(this.databaseContext) && (currentLayout == null || currentLayout.equals(this.layout))) {
229+
if (requestId == latestRequestId.get()
230+
&& Objects.equals(currentEntry, this.entry)
231+
&& Objects.equals(currentDatabaseContext, this.databaseContext)
232+
&& (currentLayout == null || Objects.equals(currentLayout, this.layout))) {
233+
228234
setPreviewText(htmlComplete);
235+
236+
if (preferences.getPreviewPreferences().shouldDownloadCovers()) {
237+
downloadCoverAndRefresh(currentEntry, htmlComplete);
238+
}
229239
} else {
230-
LOGGER.debug("Stale preview task discarded for entry: {}", currentEntry.getCitationKey().orElse(""));
240+
LOGGER.debug("Stale preview task discarded for entry: {}",
241+
currentEntry != null ? currentEntry.getCitationKey().orElse("") : "");
231242
}
232243
})
233244
.onFailure(exception -> LOGGER.error("Error generating preview text", exception))
@@ -438,8 +449,9 @@ private String generatePreviewWithAnnotations(BibEntry currentEntry, FilePrefere
438449
String annotationsHtml = FileAnnotationPreview.render(annotationsMap);
439450
previewHtml.append(annotationsHtml);
440451
}
441-
} catch (RuntimeException e) {
442-
LOGGER.error("Failed to generate preview with annotations due to a runtime error", e);
452+
} catch (Exception e) {
453+
LOGGER.error("Failed to read PDF annotations for preview", e);
454+
throw new RuntimeException("Error loading PDF annotations", e);
443455
}
444456

445457
return previewHtml.toString();

jablib/src/main/java/org/jabref/logic/pdf/FileAnnotationPreview.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
import java.util.Map;
77
import java.util.Objects;
88

9+
import org.jspecify.annotations.Nullable;
910
import org.jabref.logic.l10n.Localization;
1011
import org.jabref.logic.util.strings.StringUtil;
1112
import org.jabref.model.pdf.FileAnnotation;
1213

1314
import static org.jabref.logic.util.strings.StringUtil.quoteForHTML;
1415

1516
public class FileAnnotationPreview {
16-
public static String render(Map<Path, List<FileAnnotation>> annotations) {
17+
public static String render(@Nullable Map<Path, List<FileAnnotation>> annotations) {
1718
if (annotations == null || annotations.isEmpty()) {
1819
return "";
1920
}
@@ -45,12 +46,27 @@ public static String render(Map<Path, List<FileAnnotation>> annotations) {
4546
}
4647

4748
private static void renderAnnotation(StringBuilder html, FileAnnotation annotation) {
48-
String type = annotation.getAnnotationType() != null ? annotation.getAnnotationType().toString() : Localization.lang("Unknown");
49+
String typeStr;
50+
51+
if (annotation.getAnnotationType() != null) {
52+
String typeName = annotation.getAnnotationType().toString();
53+
54+
if ("HIGHLIGHT".equalsIgnoreCase(typeName)) {
55+
typeStr = Localization.lang("highlight");
56+
} else if ("UNDERLINE".equalsIgnoreCase(typeName)) {
57+
typeStr = Localization.lang("underline");
58+
} else if ("STRIKEOUT".equalsIgnoreCase(typeName)) {
59+
typeStr = Localization.lang("strikeout");
60+
} else {
61+
typeStr = typeName.toLowerCase();
62+
}
63+
} else {
64+
typeStr = Localization.lang("unknown");
65+
}
66+
4967
int page = annotation.getPage();
5068
String content = annotation.getContent();
51-
52-
String pageLabel = Localization.lang("Page");
53-
String headerLabel = "%s (%s %d):".formatted(type, pageLabel, page);
69+
String headerLabel = Localization.lang("%0 (page %1)", typeStr, String.valueOf(page));
5470

5571
html.append("<b>")
5672
.append(quoteForHTML(headerLabel))

jablib/src/main/resources/l10n/JabRef_en.properties

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3592,4 +3592,8 @@ The\ format\ option\ must\ contain\ either\ 'xmp'\ or\ 'bibtex-attachment'.=The
35923592

35933593
Note=Note
35943594
PDF\ Annotations=PDF Annotations
3595-
Unknown=Unknown
3595+
unknown=unknown
3596+
highlight=highlight
3597+
strikeout=strikeout
3598+
underline=underline
3599+
%0\ (page\ %1)=%0 (page %1)

0 commit comments

Comments
 (0)