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

Fix generate bibtex key overwrite warning dialog #4418

Merged
merged 3 commits into from
Oct 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions src/main/java/org/jabref/gui/actions/GenerateBibtexKeyAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
import org.jabref.preferences.JabRefPreferences;

public class GenerateBibtexKeyAction implements BaseAction {

private final DialogService dialogService;
private BasePanel basePanel;
private final BasePanel basePanel;
private List<BibEntry> entries;
private boolean isCanceled;

Expand All @@ -29,7 +30,7 @@ public void init() {

if (entries.isEmpty()) {
dialogService.showWarningDialogAndWait(Localization.lang("Autogenerate BibTeX keys"),
Localization.lang("First select the entries you want keys to be generated for."));
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()));
Expand All @@ -38,19 +39,20 @@ public void init() {
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));
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;
}
}

private void generateKeys() {
private void checkOverwriteKeysChosen() {
// 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);
Expand All @@ -64,7 +66,12 @@ private void generateKeys() {
return;
}
}
}

private void generateKeys() {
if (isCanceled) {
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());
Expand All @@ -79,21 +86,19 @@ private void generateKeys() {
basePanel.getUndoManager().addEdit(compound);
}

if (isCanceled) {
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")));
(count > 1 ? Localization.lang("entries") : Localization.lang("entry")));
}

@Override
public void action() {
init();
checkOverwriteKeysChosen();
BackgroundTask.wrap(this::generateKeys)
.executeWith(Globals.TASK_EXECUTOR);
}
Expand Down