-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract old Bibtex key generation action (#4056)
* Extract old Bibtex key generation action * Add Todos * Imports * Merge properly * Unify key generation code * Hide text from icon button * Fix build Co-authored-by: Tobias Diez <[email protected]>
- Loading branch information
1 parent
cf9fbb0
commit b411cfb
Showing
20 changed files
with
268 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/main/java/org/jabref/gui/actions/GenerateBibtexKeyAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package org.jabref.gui.actions; | ||
|
||
import java.util.List; | ||
|
||
import org.jabref.Globals; | ||
import org.jabref.gui.BasePanel; | ||
import org.jabref.gui.DialogService; | ||
import org.jabref.gui.undo.NamedCompound; | ||
import org.jabref.gui.undo.UndoableKeyChange; | ||
import org.jabref.gui.worker.AbstractWorker; | ||
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.preferences.JabRefPreferences; | ||
|
||
public class GenerateBibtexKeyAction extends AbstractWorker { | ||
private final DialogService dialogService; | ||
private BasePanel basePanel; | ||
private List<BibEntry> entries; | ||
private boolean canceled; | ||
|
||
public GenerateBibtexKeyAction(BasePanel basePanel, DialogService dialogService) { | ||
this.basePanel = basePanel; | ||
this.dialogService = dialogService; | ||
} | ||
|
||
@Override | ||
public void init() { | ||
entries = basePanel.getSelectedEntries(); | ||
|
||
if (entries.isEmpty()) { | ||
dialogService.showWarningDialogAndWait(Localization.lang("Autogenerate BibTeX keys"), | ||
Localization.lang("First select the entries you want keys to be generated for.")); | ||
return; | ||
} | ||
basePanel.output(formatOutputMessage(Localization.lang("Generating BibTeX key for"), entries.size())); | ||
} | ||
|
||
public static boolean confirmOverwriteKeys(DialogService dialogService) { | ||
if (Globals.prefs.getBoolean(JabRefPreferences.WARN_BEFORE_OVERWRITING_KEY)) { | ||
return dialogService.showConfirmationDialogWithOptOutAndWait( | ||
Localization.lang("Overwrite keys"), | ||
Localization.lang("One or more keys will be overwritten. Continue?"), | ||
Localization.lang("Overwrite keys"), | ||
Localization.lang("Cancel"), | ||
Localization.lang("Disable this confirmation dialog"), | ||
optOut -> Globals.prefs.putBoolean(JabRefPreferences.WARN_BEFORE_OVERWRITING_KEY, !optOut)); | ||
} else { | ||
// Always overwrite keys by default | ||
return true; | ||
} | ||
} | ||
|
||
@Override | ||
public void run() { | ||
// We don't want to generate keys for entries which already have one thus remove the entries | ||
if (Globals.prefs.getBoolean(JabRefPreferences.AVOID_OVERWRITING_KEY)) { | ||
entries.removeIf(BibEntry::hasCiteKey); | ||
// if we're going to override some cite keys warn the user about it | ||
} else if (entries.parallelStream().anyMatch(BibEntry::hasCiteKey)) { | ||
boolean overwriteKeys = confirmOverwriteKeys(dialogService); | ||
|
||
// The user doesn't want to override cite keys | ||
if (!overwriteKeys) { | ||
canceled = true; | ||
return; | ||
} | ||
} | ||
|
||
// generate the new cite keys for each entry | ||
final NamedCompound compound = new NamedCompound(Localization.lang("Autogenerate BibTeX keys")); | ||
BibtexKeyGenerator keyGenerator = new BibtexKeyGenerator(basePanel.getBibDatabaseContext(), Globals.prefs.getBibtexKeyPatternPreferences()); | ||
for (BibEntry entry : entries) { | ||
keyGenerator.generateAndSetKey(entry) | ||
.ifPresent(fieldChange -> compound.addEdit(new UndoableKeyChange(fieldChange))); | ||
} | ||
compound.end(); | ||
|
||
// register the undo event only if new cite keys were generated | ||
if (compound.hasEdits()) { | ||
basePanel.getUndoManager().addEdit(compound); | ||
} | ||
} | ||
|
||
@Override | ||
public void update() { | ||
if (canceled) { | ||
return; | ||
} | ||
basePanel.markBaseChanged(); | ||
basePanel.output(formatOutputMessage(Localization.lang("Generated BibTeX key for"), entries.size())); | ||
} | ||
|
||
private String formatOutputMessage(String start, int count) { | ||
return String.format("%s %d %s.", start, count, | ||
(count > 1 ? Localization.lang("entries") : Localization.lang("entry"))); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/org/jabref/gui/actions/GenerateBibtexKeySingleAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.jabref.gui.actions; | ||
|
||
import javax.swing.undo.UndoManager; | ||
|
||
import org.jabref.gui.DialogService; | ||
import org.jabref.gui.entryeditor.EntryEditorPreferences; | ||
import org.jabref.gui.undo.UndoableKeyChange; | ||
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator; | ||
import org.jabref.model.database.BibDatabaseContext; | ||
import org.jabref.model.entry.BibEntry; | ||
|
||
public class GenerateBibtexKeySingleAction extends SimpleCommand { | ||
|
||
private DialogService dialogService; | ||
private BibDatabaseContext databaseContext; | ||
private EntryEditorPreferences preferences; | ||
private BibEntry entry; | ||
private UndoManager undoManager; | ||
|
||
public GenerateBibtexKeySingleAction(BibEntry entry, BibDatabaseContext databaseContext, DialogService dialogService, EntryEditorPreferences preferences, UndoManager undoManager) { | ||
this.entry = entry; | ||
this.databaseContext = databaseContext; | ||
this.dialogService = dialogService; | ||
this.preferences = preferences; | ||
this.undoManager = undoManager; | ||
|
||
if (preferences.avoidOverwritingCiteKey()) { | ||
// Only make command executable if cite key is empty | ||
this.executable.bind(entry.getCiteKeyBinding().isNull()); | ||
} | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
if (!entry.hasCiteKey() || GenerateBibtexKeyAction.confirmOverwriteKeys(dialogService)) { | ||
new BibtexKeyGenerator(databaseContext, preferences.getBibtexKeyPatternPreferences()) | ||
.generateAndSetKey(entry) | ||
.ifPresent(change -> undoManager.addEdit(new UndoableKeyChange(change))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.