|
102 | 102 | import org.jabref.gui.worker.CitationStyleToClipboardWorker; |
103 | 103 | import org.jabref.gui.worker.MarkEntriesAction; |
104 | 104 | import org.jabref.gui.worker.SendAsEMailAction; |
105 | | -import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternUtil; |
| 105 | +import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator; |
106 | 106 | import org.jabref.logic.citationstyle.CitationStyleCache; |
107 | 107 | import org.jabref.logic.citationstyle.CitationStyleOutputFormat; |
108 | 108 | import org.jabref.logic.exporter.BibtexDatabaseWriter; |
|
122 | 122 | import org.jabref.logic.util.io.FileFinders; |
123 | 123 | import org.jabref.logic.util.io.FileUtil; |
124 | 124 | import org.jabref.model.FieldChange; |
125 | | -import org.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern; |
126 | 125 | import org.jabref.model.database.BibDatabase; |
127 | 126 | import org.jabref.model.database.BibDatabaseContext; |
128 | 127 | import org.jabref.model.database.KeyCollisionException; |
|
140 | 139 | import org.jabref.model.entry.event.EntryEventSource; |
141 | 140 | import org.jabref.model.entry.specialfields.SpecialField; |
142 | 141 | import org.jabref.model.entry.specialfields.SpecialFieldValue; |
| 142 | +import org.jabref.model.strings.StringUtil; |
143 | 143 | import org.jabref.preferences.JabRefPreferences; |
144 | 144 | import org.jabref.preferences.PreviewPreferences; |
145 | 145 |
|
@@ -489,16 +489,10 @@ public void run() { |
489 | 489 |
|
490 | 490 | // generate the new cite keys for each entry |
491 | 491 | final NamedCompound ce = new NamedCompound(Localization.lang("Autogenerate BibTeX keys")); |
492 | | - AbstractBibtexKeyPattern citeKeyPattern = bibDatabaseContext.getMetaData() |
493 | | - .getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()); |
| 492 | + BibtexKeyGenerator keyGenerator = new BibtexKeyGenerator(bibDatabaseContext, Globals.prefs.getBibtexKeyPatternPreferences()); |
494 | 493 | for (BibEntry entry : entries) { |
495 | | - String oldCiteKey = entry.getCiteKeyOptional().orElse(""); |
496 | | - BibtexKeyPatternUtil.makeAndSetLabel(citeKeyPattern, bibDatabaseContext.getDatabase(), |
497 | | - entry, Globals.prefs.getBibtexKeyPatternPreferences()); |
498 | | - String newCiteKey = entry.getCiteKeyOptional().orElse(""); |
499 | | - if (!oldCiteKey.equals(newCiteKey)) { |
500 | | - ce.addEdit(new UndoableKeyChange(entry, oldCiteKey, newCiteKey)); |
501 | | - } |
| 494 | + Optional<FieldChange> change = keyGenerator.generateAndSetKey(entry); |
| 495 | + change.ifPresent(fieldChange -> ce.addEdit(new UndoableKeyChange(fieldChange))); |
502 | 496 | } |
503 | 497 | ce.end(); |
504 | 498 |
|
@@ -1754,15 +1748,12 @@ public void autoGenerateKeysBeforeSaving() { |
1754 | 1748 | if (Globals.prefs.getBoolean(JabRefPreferences.GENERATE_KEYS_BEFORE_SAVING)) { |
1755 | 1749 | NamedCompound ce = new NamedCompound(Localization.lang("Autogenerate BibTeX keys")); |
1756 | 1750 |
|
| 1751 | + BibtexKeyGenerator keyGenerator = new BibtexKeyGenerator(bibDatabaseContext, Globals.prefs.getBibtexKeyPatternPreferences()); |
1757 | 1752 | for (BibEntry bes : bibDatabaseContext.getDatabase().getEntries()) { |
1758 | 1753 | Optional<String> oldKey = bes.getCiteKeyOptional(); |
1759 | | - if (!(oldKey.isPresent()) || oldKey.get().isEmpty()) { |
1760 | | - BibtexKeyPatternUtil.makeAndSetLabel(bibDatabaseContext.getMetaData() |
1761 | | - .getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()), |
1762 | | - bibDatabaseContext.getDatabase(), |
1763 | | - bes, Globals.prefs.getBibtexKeyPatternPreferences()); |
1764 | | - bes.getCiteKeyOptional().ifPresent( |
1765 | | - newKey -> ce.addEdit(new UndoableKeyChange(bes, oldKey.orElse(""), newKey))); |
| 1754 | + if (StringUtil.isBlank(oldKey)) { |
| 1755 | + Optional<FieldChange> change = keyGenerator.generateAndSetKey(bes); |
| 1756 | + change.ifPresent(fieldChange -> ce.addEdit(new UndoableKeyChange(fieldChange))); |
1766 | 1757 | } |
1767 | 1758 | } |
1768 | 1759 |
|
|
0 commit comments