Skip to content

Improve Document Viewer error handling and state management #13298

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

FlyJoanne
Copy link
Contributor

  • Add user-friendly messages when no PDF files are available
  • Fix state management to properly clear content when switching entries
  • Replace technical exceptions with helpful placeholder text
  • Improve user experience for entries with non-PDF files only

Closes #13198

Improved the Document Viewer's handling of entries that contain only non-PDF files by replacing technical error messages with user-friendly placeholders and fixing state management issues that caused stale content to be displayed when switching between entries.

Steps to test

  1. Test entries with non-PDF files only:
    • Select a bibliography entry that has only .docx, .md, or other non-PDF files attached
    • Open Document Viewer
    • Expected: Should display "No PDF available for preview" message instead of technical exceptions

image

  1. Test normal PDF functionality:
    • Select an entry with PDF files attached
    • Open Document Viewer
    • Expected: PDF should display normally with dropdown showing only PDF files
image
  1. Test state management:
    • First open Document Viewer with an entry containing PDF files
    • Then switch to an entry with only non-PDF files
    • Expected: Should show placeholder message, not the previous PDF content

image

Mandatory checks

  • I own the copyright of the code submitted and I license it under the MIT license
  • Change in CHANGELOG.md described in a way that is understandable for the average user (if change is visible to the user)
  • [/] Tests created for changes (if applicable)
  • Manually tested changed features in running JabRef (always required)
  • Screenshots added in PR description (if change is visible to the user)
  • [/] Checked developer's documentation: Is the information available and up to date? If not, I outlined it in this pull request.
  • [/] Checked documentation: Is the information available and up to date? If not, I created an issue at https://github.com/JabRef/user-documentation/issues or, even better, I submitted a pull request to the documentation repository.

- Add user-friendly messages when no PDF files are available
- Fix state management to properly clear content when switching entries
- Replace technical exceptions with helpful placeholder text
- Improve user experience for entries with non-PDF files only

Closes JabRef#13198

// Filter to include only PDF files
Set<LinkedFile> pdfFiles = linkedFiles.stream()
.filter(this::isPdfFile)
Copy link
Member

Choose a reason for hiding this comment

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

Move idPdfFile filter in the stream above so you can directlry filter

* @param file the LinkedFile to check
* @return true if the file is a PDF, false otherwise
*/
private boolean isPdfFile(LinkedFile file) {
Copy link
Member

Choose a reason for hiding this comment

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

You can use the existing method already

public static boolean isPDFFile(Path file) {

- Remove warning symbol from error message text for better formatting
- Optimize PDF filtering to use single stream operation
- Simplify isPdfFile method to use existing FileUtil directly
- Fix checkstyle issues
@@ -89,11 +108,34 @@ private void setCurrentDocument(Path path) {
}
}

private boolean isPdfFile(LinkedFile file) {
if (file == null || file.getLink() == null || file.getLink().trim().isEmpty()) {
Copy link

Choose a reason for hiding this comment

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

The method isPdfFile should not use exceptions for control flow. Instead, it should handle null checks and invalid states more gracefully.

LOGGER.debug("No document provided to viewer, showing placeholder");
// Show placeholder and hide PDF
pdfView.setVisible(false);
placeholderLabel.setText("No PDF available for preview");
Copy link

Choose a reason for hiding this comment

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

The label text should be in sentence case and consistent with other strings. Consider using 'No PDF available for preview.' to maintain consistency and proper sentence structure.

}
} else {
LOGGER.error("Could not load PDF document: no document found");
LOGGER.debug("No document provided to viewer, showing placeholder");
Copy link

Choose a reason for hiding this comment

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

The log message should end with a period instead of a comma for proper sentence structure.

@FlyJoanne
Copy link
Contributor Author

@Siedlerchr Thanks for the feedback! I've addressed all the review comments:

  • Removed the warning symbol from error messages
  • Optimized the PDF filtering to use a single stream operation as suggested
  • Simplified the isPdfFile method to directly use existing FileUtil
  • Fixed all checkstyle issues

The functionality has been tested and works as expected.

if (pdfFiles.isEmpty()) {
// No PDF files found - clear the list and current document
files.clear();
currentDocument.set(null);
Copy link
Member

@Siedlerchr Siedlerchr Jun 10, 2025

Choose a reason for hiding this comment

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

I htink it would be good to display a notification: dialogService.notify(Localization.lang("No pdf files available"))

@Siedlerchr
Copy link
Member

Just one little addition and I think it's ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Filter out only PDF files in Document Viewer
2 participants