-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add auto-renaming of linked files on entry data change #13295
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
base: main
Are you sure you want to change the base?
Conversation
- Implemented a preference option under Linked files -> Linked file name conventions to enable auto-renaming of files when entry data changes (default: false). - Added functionality to listen for entry change events and rename files if the preference is enabled and the file name matches the defined pattern. - Ensured that no action is taken if the pattern is empty, as the file name would not match. - Considered scenarios where an entry has multiple files attached and handled them appropriately. - Added test cases to verify the new functionality.
jabgui/src/main/java/org/jabref/gui/externalfiles/AutoRenameFileOnEntryChange.java
Show resolved
Hide resolved
jabgui/src/main/java/org/jabref/gui/externalfiles/AutoRenameFileOnEntryChange.java
Show resolved
Hide resolved
jabgui/src/main/java/org/jabref/gui/preferences/linkedfiles/LinkedFilesTab.java
Show resolved
Hide resolved
jabgui/src/main/java/org/jabref/gui/preferences/linkedfiles/LinkedFilesTab.java
Show resolved
Hide resolved
jabgui/src/main/java/org/jabref/gui/preferences/linkedfiles/LinkedFilesTabViewModel.java
Show resolved
Hide resolved
jabgui/src/main/java/org/jabref/gui/preferences/linkedfiles/LinkedFilesTabViewModel.java
Show resolved
Hide resolved
jabgui/src/test/java/org/jabref/gui/externalfiles/AutoRenameFileOnEntryChangeTest.java
Show resolved
Hide resolved
@Test | ||
void multipleFilesRenameOnEntryChange() throws IOException { | ||
// create multiple entries | ||
List<String> fileNames = Arrays.asList( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modern Java data structures should be used. Instead of Arrays.asList, consider using List.of for better performance and readability.
@@ -106,6 +109,18 @@ public void setStoreFilesRelativeToBibFile(boolean shouldStoreFilesRelativeToBib | |||
this.storeFilesRelativeToBibFile.set(shouldStoreFilesRelativeToBibFile); | |||
} | |||
|
|||
public boolean shouldAutoRenameFilesOnChange() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method should return an Optional instead of a primitive boolean to avoid returning null and to align with modern Java practices.
@@ -203,6 +204,7 @@ private void initializeComponentsAndListeners(boolean isDummyContext) { | |||
|
|||
bibDatabaseContext.getDatabase().registerListener(this); | |||
bibDatabaseContext.getMetaData().registerListener(this); | |||
new AutoRenameFileOnEntryChange(bibDatabaseContext, preferences); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The instantiation of AutoRenameFileOnEntryChange should be moved to org.jabref.logic package as it involves non-GUI operations, adhering to the layered architecture principle.
} | ||
|
||
private boolean relatesToFilePattern(String fileNamePattern, FieldChangedEvent event) { | ||
Set<String> extractedFields = new HashSet<>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using 'new HashSet<>()' is not optimal. Consider using 'Set.of()' for better readability and performance as per modern Java practices.
// change year only | ||
entry.setField(StandardField.YEAR, "2082"); | ||
assertEquals("newKey2082.pdf", entry.getFiles().getFirst().getLink()); | ||
assertTrue(Files.exists(tempDir.resolve("newKey2082.pdf"))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The assertion checks for a boolean condition instead of asserting the expected content of the object. Use assertEquals to compare expected and actual values.
@trag-bot didn't find any issues in the code! ✅✨ |
Closes #11316
This PR introduces an option to automatically rename linked files when the associated entry data is modified. The feature is configurable in Preferences under Linked files -> Linked file name conventions, with a default setting of false. It also includes handling for entries with multiple attached files and adds relevant test cases.
Steps to test
Mandatory checks
CHANGELOG.md
described in a way that is understandable for the average user (if change is visible to the user)