Skip to content

Commit

Permalink
Fix generate bibtex key overwrite warning dialog (JabRef#4418)
Browse files Browse the repository at this point in the history
* Fix generate bibtex key overwrite warning dialog


Fixes JabRef#4417

* split up confirm of overwrite and generate keys
put only generate action in background task

* fix checkstyle
  • Loading branch information
Siedlerchr authored and tobiasdiez committed Oct 27, 2018
1 parent f5a8f00 commit b09973f
Showing 1 changed file with 18 additions and 13 deletions.
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

0 comments on commit b09973f

Please sign in to comment.