-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Restore auto rename feature 2 #12604
base: main
Are you sure you want to change the base?
Changes from all commits
358b764
65895ee
2ec61ab
6c3e5cf
e77da98
690af63
6b786ed
2795286
b1a41b5
7ed45f4
5d9f31a
575aa58
28b80e1
1f3c1d1
c93e2b4
9f81a8e
8771120
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,12 @@ | |
|
||
### Added | ||
|
||
|
||
Check failure on line 14 in CHANGELOG.md
|
||
|
||
Check failure on line 15 in CHANGELOG.md
|
||
- We added automatic file renaming functionality: when entry data is changed, associated filenames are updated automatically without requiring manual clicks on the "Generate" button. [#12604](https://github.com/JabRef/jabref/pull/12604) | ||
|
||
- We added <kbd>F5</kbd> as a shortcut key for fetching data and <kbd>Alt+F</kbd> as a shortcut for looking up data using DOI. [#11802](https://github.com/JabRef/jabref/issues/11802) | ||
|
||
- We added a feature to rename the subgroup, with the keybinding (<kbd>F2</kbd>) for quick access. [#11896](https://github.com/JabRef/jabref/issues/11896) | ||
- We added a new functionality that displays a drop-down list of matching suggestions when typing a citation key pattern. [#12502](https://github.com/JabRef/jabref/issues/12502) | ||
- We added a new CLI that supports txt, csv, and console-based output for consistency in BibTeX entries. [#11984](https://github.com/JabRef/jabref/issues/11984) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.jabref.logic.citationkeypattern; | ||
|
||
import org.jabref.logic.FilePreferences; | ||
import org.jabref.model.database.BibDatabaseContext; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.event.FieldChangedEvent; | ||
import org.jabref.model.entry.field.Field; | ||
import org.jabref.model.entry.field.StandardField; | ||
|
||
import com.google.common.eventbus.Subscribe; | ||
|
||
public class AutomaticCitationKeyGenerator { | ||
private final CitationKeyGenerator keyGenerator; | ||
private final BibDatabaseContext databaseContext; | ||
private final FilePreferences filePreferences; | ||
|
||
public AutomaticCitationKeyGenerator(BibDatabaseContext databaseContext, | ||
CitationKeyPatternPreferences citationKeyPatternPreferences, | ||
FilePreferences filePreferences) { | ||
this.databaseContext = databaseContext; | ||
this.keyGenerator = new CitationKeyGenerator(databaseContext, citationKeyPatternPreferences); | ||
this.filePreferences = filePreferences; | ||
} | ||
|
||
/** | ||
* Listens for field changes and regenerates the citation key if a relevant field changed. | ||
*/ | ||
@Subscribe | ||
public void listen(FieldChangedEvent event) { | ||
BibEntry entry = event.getBibEntry(); | ||
Field field = event.getField(); | ||
|
||
// Check if the changed field affects the citation key generation | ||
// Typically includes: author, year, title, etc. | ||
if (isRelevantField(field)) { | ||
// Regenerate the citation key | ||
keyGenerator.generateAndSetKey(entry); | ||
} | ||
} | ||
|
||
/** | ||
* Determines if a field is relevant for citation key generation. | ||
* | ||
* @param field The field to check | ||
* @return true if the field affects citation key generation | ||
*/ | ||
private boolean isRelevantField(Field field) { | ||
// Determine which fields affect the citation key based on the pattern | ||
return field == StandardField.AUTHOR || | ||
field == StandardField.YEAR || | ||
field == StandardField.TITLE; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package org.jabref.logic.externalfiles; | ||
|
||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import org.jabref.logic.FilePreferences; | ||
import org.jabref.model.database.BibDatabaseContext; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.LinkedFile; | ||
import org.jabref.model.entry.event.FieldChangedEvent; | ||
|
||
import com.google.common.eventbus.Subscribe; | ||
|
||
public class AutomaticFileRenamer { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use org.jabref.logic.cleanup.RenamePdfCleanup#cleanup instead of self-written class. If this is not possible, comment why. |
||
|
||
private final BibDatabaseContext databaseContext; | ||
private final FilePreferences filePreferences; | ||
private final AtomicBoolean isCurrentlyRenamingFile = new AtomicBoolean(false); | ||
|
||
public AutomaticFileRenamer(BibDatabaseContext databaseContext, FilePreferences filePreferences) { | ||
this.databaseContext = databaseContext; | ||
this.filePreferences = filePreferences; | ||
} | ||
|
||
@Subscribe | ||
public void listen(FieldChangedEvent event) { | ||
if (!filePreferences.shouldAutoRenameFilesOnEntryChange()) { | ||
return; | ||
} | ||
|
||
if (isCurrentlyRenamingFile.get()) { | ||
return; | ||
} | ||
|
||
BibEntry entry = event.getBibEntry(); | ||
|
||
if (entry.getFiles().isEmpty()) { | ||
return; | ||
} | ||
|
||
new Thread(() -> { | ||
try { | ||
Thread.sleep(500); // avoid renaming the file too frequently | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use background task infrasturcture |
||
|
||
if (!isCurrentlyRenamingFile.compareAndSet(false, true)) { | ||
return; | ||
} // make sure only one thread is renaming the file at a time - Thread Safety | ||
|
||
try { | ||
if (entry.getCitationKey().isEmpty()) { | ||
return; | ||
} // make sure the entry has a citation key | ||
|
||
List<LinkedFile> updatedFiles = new ArrayList<>(); | ||
boolean anyFileRenamed = false; | ||
|
||
// handling every file in the entry | ||
for (LinkedFile linkedFile : entry.getFiles()) { | ||
if (linkedFile.isOnlineLink()) { | ||
updatedFiles.add(linkedFile); | ||
continue; | ||
} | ||
|
||
Optional<Path> filePath = linkedFile.findIn(databaseContext, filePreferences); | ||
if (filePath.isEmpty()) { | ||
updatedFiles.add(linkedFile); | ||
continue; | ||
} | ||
|
||
LinkedFileHandler fileHandler = new LinkedFileHandler(linkedFile, entry, databaseContext, filePreferences); | ||
try { | ||
boolean renamed = fileHandler.renameToSuggestedName(); | ||
if (renamed) { | ||
LinkedFile updatedFile = fileHandler.refreshFileLink(); | ||
updatedFiles.add(updatedFile); | ||
anyFileRenamed = true; | ||
} else { | ||
updatedFiles.add(linkedFile); | ||
} | ||
} catch (Exception e) { | ||
updatedFiles.add(linkedFile); | ||
} | ||
} | ||
|
||
if (anyFileRenamed) { | ||
entry.setFiles(updatedFiles); | ||
} | ||
} finally { | ||
isCurrentlyRenamingFile.set(false); // Make sure the flag is set to false after the renamer is done | ||
} | ||
} catch (Exception e) { | ||
// DO NOTHING FOR NOW | ||
} | ||
}).start(); | ||
} | ||
} |
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.
here lacks one parameter it should be @see #bootstrap(String[], Path) otherwise the project doesn't compile
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.
I really don't see why openoffice gui files need to be touched here.
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.
Side track: Git Butler is a neat git client for submitting unrelated pull requests while keeping all changes locally merged. - However, this for advanced users, because one will get feedback on the pull requests and need to handle them.