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
@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

@@ -35,6 +38,7 @@ public class DocumentViewerViewModel extends AbstractViewModel {
private final BooleanProperty liveMode = new SimpleBooleanProperty(true);
private final IntegerProperty currentPage = new SimpleIntegerProperty();
private final StringProperty highlightText = new SimpleStringProperty();
private final Logger LOGGER = LoggerFactory.getLogger(DocumentViewerViewModel.class);
Copy link
Member

Choose a reason for hiding this comment

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

for the sake of consistency with other classes, please put LOGGER to the top of field definitions above stateManager.

Set<LinkedFile> pdfFiles = entries.stream()
.map(BibEntry::getFiles)
.flatMap(List::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.

Please only make english comments

try {
Path filePath = Path.of(file.getLink());
return FileUtil.isPDFFile(filePath);
} catch (Exception e) {
Copy link
Member

Choose a reason for hiding this comment

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

do not catch generic exceptions, be very specific. You don't know then why sthg failed? 1000% sure it is because it is not a pdf file?


// Create placeholder label
placeholderLabel = new Label("No PDF available for preview");
placeholderLabel.setStyle("-fx-text-fill: #666666; -fx-font-size: 16px;");
Copy link
Member

Choose a reason for hiding this comment

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

no random style definitions in code. Put everything in base.css !

getChildren().add(pdfView);

// Create placeholder label
placeholderLabel = new Label("No PDF available for preview");
Copy link
Member

Choose a reason for hiding this comment

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

Localization

Comment on lines +42 to +50
// Initially show placeholder
pdfView.setVisible(false);
placeholderLabel.setVisible(true);

EasyBind.subscribe(currentPage, current -> pdfView.setPage(current.intValue()));
// We can only set the search query at the moment not the results or mark them in the text
EasyBind.subscribe(highlightText, pdfView::setSearchText);
// Initially hide PDFView until a document is loaded
pdfView.setVisible(false);
Copy link
Member

Choose a reason for hiding this comment

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

duplication

Comment on lines +66 to +68
// Show PDF and hide placeholder
pdfView.setVisible(true);
placeholderLabel.setVisible(false);
Copy link
Member

@calixtus calixtus Jun 11, 2025

Choose a reason for hiding this comment

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

meaningless comment. superfluous. But i know. AI loves this stupid comments.
What LLM do you use?

Comment on lines +72 to +75
// Show error message
pdfView.setVisible(false);
placeholderLabel.setText("Could not load PDF: " + document.getFileName());
placeholderLabel.setVisible(true);
Copy link
Member

Choose a reason for hiding this comment

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

Localization

Copy link
Member

Choose a reason for hiding this comment

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

undo

Copy link
Member

Choose a reason for hiding this comment

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

undo

Comment on lines +102 to +103
// Always call show(), even when newDocument is null
viewer.show(newDocument);
Copy link
Member

Choose a reason for hiding this comment

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

meaningless comment

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
3 participants