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

Implement book cover feature #12198

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

Conversation

damgam0288
Copy link
Contributor

@damgam0288 damgam0288 commented Nov 17, 2024

Description

This PR is a first iteration of the feature request for a book cover in JabRef in issue 10120.

Changes

When users add an image as a File for a Book, In-book or Booklet citation with the description "cover", it will appear on the right side of the Preview panel.

PR-10120-edit-file-dialog-box PR-10120-show-book-cover

Code changes

BibEntry.java

  • getCoverImageFile method - searches the Entry's files for an image with the COVER_TAG in the description
  • COVER_TAG string field - the text that users must include in the file's description to be detected as the book cover
  • isCoverable method - checks if given Entry type can have a cover image COVERABLE_TYPES
  • COVERABLE_TYPES field - a list of BibEntry types that can have a book cover (at the moment, this is Book, In-book and Booklet)

PreviewViewer.java

  • setPreviewText method - edited the HTML to include an img tag for the book cover
  • getBookCoverURI method - supplies the source for the img tag mentioned above

BibEntryTest.java

  • getCoverImageReturnsEmptyIfNoFiles
  • getCoverImageReturnsEmptyIfNoImageFiles
  • getCoverImageReturnsEmptyIfEntryIsNotCoverable
  • getCoverImageDoesNotReturnImagesWithoutCoverDescription
  • getCoverImageDoesNotReturnDocumentsWithCoverDescription
  • getCoverImageReturnsCorrectImage
  • getCoverImageUpdatesWithChangeToDescription

Notes

  • I am not sure how to approach allowing the user to toggle the book cover on/off because it is part of the HTML in setPreviewText method.
  • I have implemented this feature initially by relying on the user to select the image but I would like to use the book cover API as per the issue discussion and would appreciate some advice on how to do this without harming the code-base.

Mandatory checks

  • I own the copyright of the code submitted and I licence 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 (for UI changes)
  • N/A Checked developer's documentation: Is the information available and up to date? If not, I outlined it in this pull request.
  • N/A 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.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Your code currently does not meet JabRef's code guidelines.
We use Checkstyle to identify issues.
Please carefully follow the setup guide for the codestyle.
Afterwards, please run checkstyle locally and fix the issues.

github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Your code currently does not meet JabRef's code guidelines.
We use Checkstyle to identify issues.
Please carefully follow the setup guide for the codestyle.
Afterwards, please run checkstyle locally and fix the issues.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Your code currently does not meet JabRef's code guidelines.
We use OpenRewrite to ensure "modern" Java coding practices.
The issues found can be automatically fixed.
Please execute the gradle task rewriteRun, check the results, commit, and push.

You can check the detailed error output by navigating to your pull request, selecting the tab "Checks", section "Tests" (on the left), subsection "OpenRewrite".

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Your code currently does not meet JabRef's code guidelines.
We use Checkstyle to identify issues.
Please carefully follow the setup guide for the codestyle.
Afterwards, please run checkstyle locally and fix the issues.

@damgam0288
Copy link
Contributor Author

damgam0288 commented Nov 29, 2024

@subhramit May I have your help please - is there anyway to run CheckRewrite without pushing to the branch?

I am not sure what part of my code is causing it to fail - the report on GitHub says BibEntryTest, but when running it in Gradle locally, it says there's an error in Layout.java.

So I am trying to fix any issues in BinEntryTest by pushing to the branch and checking the report, but I think I am cluttering up the commit history unnecessarily in my attempts to find the problem.

Thank-you! :)

@subhramit
Copy link
Member

subhramit commented Nov 29, 2024

@subhramit Can you help please - is there anyway to run CheckRewrite without pushing to the branch? I am not sure what part of my code is causing it to fail - the report says BibEntryTest - I am trying to fix it but I think I am cluttering up the commit history unnecessarily in my attempts to find the problem.

Run the command .\gradlew rewriteRun
Or double click the gradle task as per https://devdocs.jabref.org/code-howtos/faq.html#failing-openrewrite-tests

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Your code currently does not meet JabRef's code guidelines.
We use Checkstyle to identify issues.
Please carefully follow the setup guide for the codestyle.
Afterwards, please run checkstyle locally and fix the issues.

@subhramit
Copy link
Member

subhramit commented Nov 29, 2024

@damgam0288 right now, checkstyle test is red as well. You can take help from the same FAQ page about what to do. You have to have your IntelliJ configured for the local checkstyle warnings to see which part of the code is causing it.
For openrewrite, running the command/task is sufficient to fix it. You can ping on Gitter as well if you have further issues.

@damgam0288
Copy link
Contributor Author

@damgam0288 right now, checkstyle test is red as well. You can take help from the same FAQ page about what to do. You have to have your IntelliJ configured for the local checkstyle warnings to see which part of the code is causing it. For openrewrite, running the command/task is sufficient to fix it.

No worries, that checkstyle error was a result of a previous trial-and-error attempt with OpenRewrite - it's an unused import I will fix with my next commit. Thank you so much for the help with OpenRewrite :) - I was using terminal to run gradle so it wasn't working correctly. I found the errors and I'm working on them now.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Your code currently does not meet JabRef's code guidelines.
We use Checkstyle to identify issues.
Please carefully follow the setup guide for the codestyle.
Afterwards, please run checkstyle locally and fix the issues.

@subhramit
Copy link
Member

Please revert your last commit, and use the gradle sidebar (double click on the task) to fix it. Your JDK is outdated, so using rewriteRun is affecting multiple classes.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Your code currently does not meet JabRef's code guidelines.
We use Checkstyle to identify issues.
Please carefully follow the setup guide for the codestyle.
Afterwards, please run checkstyle locally and fix the issues.

@subhramit subhramit marked this pull request as ready for review November 30, 2024 06:56
@subhramit
Copy link
Member

I removed it from the drafts, hope that's fine?

@damgam0288
Copy link
Contributor Author

I removed it from the drafts, hope that's fine?

Of course, no problem 👍

Sorry I missed your last message but I think I fixed the rewrite issues? Or is it still a problem?

@subhramit
Copy link
Member

Sorry I missed your last message but I think I fixed the rewrite issues? Or is it still a problem?

No, all good now. Thanks.

@damgam0288
Copy link
Contributor Author

I am re-writing my tests to suit OpenRewrite, then will re-request review

@subhramit
Copy link
Member

I am re-writing my tests to suit OpenRewrite, then will re-request review

You don't have to manually "suit" openrewrite when coding. As long as your code works, running the task will refactor it as per openrewrite. You can then just commit and push changes.

@damgam0288
Copy link
Contributor Author

damgam0288 commented Nov 30, 2024

@subhramit The following test was causing an IndexOutOfBoundsException from OpenRewrite despite executing successfully. I tried my best but I couldn't fix it; I have omitted it for now. It might be a bug because I found this issue on OpenRewrite's repository. Any suggestions on what to do? Thank-you.

@Test
  void getCoverImageUpdatesWithChangeToDescription() {
      List<LinkedFile> files = new ArrayList<>();

      files.add(new LinkedFile("cover", Paths.get("JabRef-icon-128.png"), "PNG image"));
      files.add(new LinkedFile("", Paths.get("JabRef-icon-64.png"), "PNG image"));
      LinkedFile cover1 = files.get(0);
      LinkedFile cover2 = files.get(1);

      BibEntry entry = new BibEntry(StandardEntryType.Book).withField(StandardField.AUTHOR, "value");
      entry.setFiles(files);

      assertEquals(Optional.of(cover1), entry.getCoverImageFile());

      cover1.setDescription("");
      cover2.setDescription("cover");
      entry.setFiles(files);

      assertEquals(Optional.of(cover2), entry.getCoverImageFile());
  }

The error:

Error while visiting src\test\java\org\jabref\model\entry\BibEntryTest.java: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
  java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:100)
  java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
  java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
  java.base/java.util.Objects.checkIndex(Objects.java:365)
  java.base/java.util.ArrayList.get(ArrayList.java:428)
  org.openrewrite.staticanalysis.EqualsAvoidsNullVisitor.visitMethodInvocation(EqualsAvoidsNullVisitor.java:62)
  org.openrewrite.java.tree.J$MethodInvocation.acceptJava(J.java:3918)
  org.openrewrite.java.tree.J.accept(J.java:59)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
  org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:320)
  org.openrewrite.java.JavaVisitor.visitLeftPadded(JavaVisitor.java:1395)
  org.openrewrite.java.JavaVisitor.visitVariable(JavaVisitor.java:1301)
  org.openrewrite.java.tree.J$VariableDeclarations$NamedVariable.acceptJava(J.java:5914)
  org.openrewrite.java.tree.J.accept(J.java:59)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
  org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:320)
  ...

Copy link
Member

@koppor koppor left a comment

Choose a reason for hiding this comment

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

While reviewing, it came clear to me that the "architecture" of the solution is wrong.

isImage is good, the other things maybe not. We need to try out. In your solution, the types with a cover are hard-coded. I would go the extreme other way and show the first image as cover in all cases... Let me describe below:

JabRef offers configurability. For the preview, this is https://docs.jabref.org/setup/preview Read about the syntax at https://docs.jabref.org/collaborative-work/export/customexports.

JabRef should also be "knobless". It should "magically" do something if possible - and not force the user.

Steps:

  1. Understand FileLink(filetype). Implementation is at org.jabref.logic.layout.format.FileLink#format
  2. Add a jpg image to an entry
  3. Adapt JabRef prview preferences to use filelink to show that image
  4. Add a png image to another entry
  5. Adapt JabReff preview preferences to use filelink+png to show that image
  6. Adapt FileLink to support "image" as general file type -- it then searches for all image types and returns the first hit. This makes JabRef "knobless", because JabRef automatically finds the image
  7. Adapt defaults.put(PREVIEW_STYLE, in org.jabref.gui.preferences.JabRefGuiPreferences#JabRefGuiPreferences to contain your adaption
  8. Adapt org.jabref.migrations.PreferencesMigrations#upgradePreviewStyle to migrate to your new style

I think, step 3 is the most challenging


Screenshot of the preferences:

image

highlightLayoutText();
this.setHvalue(0);
}

private String getBookCoverURI() {
if (entry != null) {
Copy link
Member

Choose a reason for hiding this comment

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

Is this check really necessary? I would convert it into assert entry != null;

@@ -218,14 +218,35 @@ private void setPreviewText(String text) {
layoutText = """
<html>
<body id="previewBody">
<div id="content"> %s </div>
Copy link
Member

Choose a reason for hiding this comment

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

No. See general review comments.

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

Successfully merging this pull request may close these issues.

5 participants