Skip to content

Commit b411cfb

Browse files
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 <TobiasDiez@gmx.de>
1 parent cf9fbb0 commit b411cfb

20 files changed

+268
-156
lines changed

src/main/java/org/jabref/gui/BasePanel.java

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.jabref.gui.actions.BaseAction;
4242
import org.jabref.gui.actions.CleanupAction;
4343
import org.jabref.gui.actions.CopyBibTeXKeyAndLinkAction;
44+
import org.jabref.gui.actions.GenerateBibtexKeyAction;
4445
import org.jabref.gui.autocompleter.AutoCompletePreferences;
4546
import org.jabref.gui.autocompleter.AutoCompleteUpdater;
4647
import org.jabref.gui.autocompleter.PersonNameSuggestionProvider;
@@ -135,7 +136,6 @@
135136
import org.slf4j.LoggerFactory;
136137

137138
public class BasePanel extends StackPane implements ClipboardOwner {
138-
139139
private static final Logger LOGGER = LoggerFactory.getLogger(BasePanel.class);
140140

141141
private final BibDatabaseContext bibDatabaseContext;
@@ -360,79 +360,7 @@ private void setupActions() {
360360
});
361361

362362
// The action for auto-generating keys.
363-
actions.put(Actions.MAKE_KEY, new AbstractWorker() {
364-
365-
List<BibEntry> entries;
366-
int numSelected;
367-
boolean canceled;
368-
369-
// Run first, in EDT:
370-
@Override
371-
public void init() {
372-
entries = getSelectedEntries();
373-
numSelected = entries.size();
374-
375-
if (entries.isEmpty()) { // None selected. Inform the user to select entries first.
376-
dialogService.showWarningDialogAndWait(Localization.lang("Autogenerate BibTeX keys"),
377-
Localization.lang("First select the entries you want keys to be generated for."));
378-
return;
379-
}
380-
output(formatOutputMessage(Localization.lang("Generating BibTeX key for"), numSelected));
381-
}
382-
383-
// Run second, on a different thread:
384-
@Override
385-
public void run() {
386-
// We don't want to generate keys for entries which already have one thus remove the entries
387-
if (Globals.prefs.getBoolean(JabRefPreferences.AVOID_OVERWRITING_KEY)) {
388-
entries.removeIf(BibEntry::hasCiteKey);
389-
390-
// if we're going to override some cite keys warn the user about it
391-
} else if (Globals.prefs.getBoolean(JabRefPreferences.WARN_BEFORE_OVERWRITING_KEY)) {
392-
if (entries.parallelStream().anyMatch(BibEntry::hasCiteKey)) {
393-
394-
boolean overwriteKeysPressed = dialogService.showConfirmationDialogWithOptOutAndWait(
395-
Localization.lang("Overwrite keys"),
396-
Localization.lang("One or more keys will be overwritten. Continue?"),
397-
Localization.lang("Overwrite keys"),
398-
Localization.lang("Cancel"),
399-
Localization.lang("Disable this confirmation dialog"),
400-
optOut -> Globals.prefs.putBoolean(JabRefPreferences.WARN_BEFORE_OVERWRITING_KEY, !optOut));
401-
402-
// The user doesn't want to overide cite keys
403-
if (!overwriteKeysPressed) {
404-
canceled = true;
405-
return;
406-
}
407-
}
408-
}
409-
410-
// generate the new cite keys for each entry
411-
final NamedCompound ce = new NamedCompound(Localization.lang("Autogenerate BibTeX keys"));
412-
BibtexKeyGenerator keyGenerator = new BibtexKeyGenerator(bibDatabaseContext, Globals.prefs.getBibtexKeyPatternPreferences());
413-
for (BibEntry entry : entries) {
414-
Optional<FieldChange> change = keyGenerator.generateAndSetKey(entry);
415-
change.ifPresent(fieldChange -> ce.addEdit(new UndoableKeyChange(fieldChange)));
416-
}
417-
ce.end();
418-
419-
// register the undo event only if new cite keys were generated
420-
if (ce.hasEdits()) {
421-
getUndoManager().addEdit(ce);
422-
}
423-
}
424-
425-
// Run third, on EDT:
426-
@Override
427-
public void update() {
428-
if (canceled) {
429-
return;
430-
}
431-
markBaseChanged();
432-
numSelected = entries.size();
433-
output(formatOutputMessage(Localization.lang("Generated BibTeX key for"), numSelected));
434-
}
435-
});
363+
actions.put(Actions.MAKE_KEY, new GenerateBibtexKeyAction(this, frame.getDialogService()));
436364

437365
// The action for cleaning up entry.
438366
actions.put(Actions.CLEANUP, cleanUpAction);

src/main/java/org/jabref/gui/BasePanelPreferences.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static BasePanelPreferences from(JabRefPreferences preferences) {
3434
BasePanelPreferences basePanelPreferences = new BasePanelPreferences(
3535
preferences.getMainTablePreferences(),
3636
preferences.getAutoCompletePreferences(),
37-
EntryEditorPreferences.from(preferences),
37+
preferences.getEntryEditorPreferences(),
3838
Globals.getKeyPrefs(),
3939
preferences.getPreviewPreferences(),
4040
preferences.getDouble(JabRefPreferences.ENTRY_EDITOR_HEIGHT));

src/main/java/org/jabref/gui/JabRefFrame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ private MenuBar createMenu() {
841841
factory.createMenuItem(StandardActions.RESOLVE_DUPLICATE_KEYS, new OldDatabaseCommandWrapper(Actions.RESOLVE_DUPLICATE_KEYS, this, Globals.stateManager)),
842842
factory.createMenuItem(StandardActions.CHECK_INTEGRITY, new IntegrityCheckAction(this)),
843843
factory.createMenuItem(StandardActions.CLEANUP_ENTRIES, new OldDatabaseCommandWrapper(Actions.CLEANUP, this, Globals.stateManager)),
844-
factory.createMenuItem(StandardActions.GENERATE_CITE_KEY, new OldDatabaseCommandWrapper(Actions.MAKE_KEY, this, Globals.stateManager)),
844+
factory.createMenuItem(StandardActions.GENERATE_CITE_KEYS, new OldDatabaseCommandWrapper(Actions.MAKE_KEY, this, Globals.stateManager)),
845845

846846
new SeparatorMenuItem(),
847847

src/main/java/org/jabref/gui/actions/ActionFactory.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.util.Objects;
44

5+
import javafx.scene.Node;
56
import javafx.scene.control.Button;
7+
import javafx.scene.control.ButtonBase;
68
import javafx.scene.control.Menu;
79
import javafx.scene.control.MenuItem;
810

@@ -57,6 +59,7 @@ public Menu createSubMenu(Action action, MenuItem... children) {
5759

5860
public Button createIconButton(Action action, Command command) {
5961
Button button = ActionUtils.createButton(new JabRefAction(action, command, keyBindingRepository), ActionUtils.ActionTextBehavior.HIDE);
62+
6063
button.getStyleClass().setAll("flatButton");
6164

6265
// For some reason the graphic is not set correctly, so let's fix this
@@ -65,4 +68,24 @@ public Button createIconButton(Action action, Command command) {
6568

6669
return button;
6770
}
71+
72+
public ButtonBase configureIconButton(Action action, Command command, ButtonBase button) {
73+
ActionUtils.configureButton(
74+
new JabRefAction(action, command, keyBindingRepository),
75+
button,
76+
ActionUtils.ActionTextBehavior.HIDE);
77+
78+
button.getStyleClass().add("flatButton");
79+
80+
// For some reason the graphic is not set correctly, so let's fix this
81+
button.graphicProperty().unbind();
82+
action.getIcon().ifPresent(icon -> {
83+
// ToDO: Find a way to reuse JabRefIconView
84+
Node graphicNode = icon.getGraphicNode();
85+
graphicNode.setStyle(String.format("-fx-font-family: %s; -fx-font-size: %s;", icon.fontFamily(), "1em"));
86+
button.setGraphic(graphicNode);
87+
});
88+
89+
return button;
90+
}
6891
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package org.jabref.gui.actions;
2+
3+
import java.util.List;
4+
5+
import org.jabref.Globals;
6+
import org.jabref.gui.BasePanel;
7+
import org.jabref.gui.DialogService;
8+
import org.jabref.gui.undo.NamedCompound;
9+
import org.jabref.gui.undo.UndoableKeyChange;
10+
import org.jabref.gui.worker.AbstractWorker;
11+
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator;
12+
import org.jabref.logic.l10n.Localization;
13+
import org.jabref.model.entry.BibEntry;
14+
import org.jabref.preferences.JabRefPreferences;
15+
16+
public class GenerateBibtexKeyAction extends AbstractWorker {
17+
private final DialogService dialogService;
18+
private BasePanel basePanel;
19+
private List<BibEntry> entries;
20+
private boolean canceled;
21+
22+
public GenerateBibtexKeyAction(BasePanel basePanel, DialogService dialogService) {
23+
this.basePanel = basePanel;
24+
this.dialogService = dialogService;
25+
}
26+
27+
@Override
28+
public void init() {
29+
entries = basePanel.getSelectedEntries();
30+
31+
if (entries.isEmpty()) {
32+
dialogService.showWarningDialogAndWait(Localization.lang("Autogenerate BibTeX keys"),
33+
Localization.lang("First select the entries you want keys to be generated for."));
34+
return;
35+
}
36+
basePanel.output(formatOutputMessage(Localization.lang("Generating BibTeX key for"), entries.size()));
37+
}
38+
39+
public static boolean confirmOverwriteKeys(DialogService dialogService) {
40+
if (Globals.prefs.getBoolean(JabRefPreferences.WARN_BEFORE_OVERWRITING_KEY)) {
41+
return dialogService.showConfirmationDialogWithOptOutAndWait(
42+
Localization.lang("Overwrite keys"),
43+
Localization.lang("One or more keys will be overwritten. Continue?"),
44+
Localization.lang("Overwrite keys"),
45+
Localization.lang("Cancel"),
46+
Localization.lang("Disable this confirmation dialog"),
47+
optOut -> Globals.prefs.putBoolean(JabRefPreferences.WARN_BEFORE_OVERWRITING_KEY, !optOut));
48+
} else {
49+
// Always overwrite keys by default
50+
return true;
51+
}
52+
}
53+
54+
@Override
55+
public void run() {
56+
// We don't want to generate keys for entries which already have one thus remove the entries
57+
if (Globals.prefs.getBoolean(JabRefPreferences.AVOID_OVERWRITING_KEY)) {
58+
entries.removeIf(BibEntry::hasCiteKey);
59+
// if we're going to override some cite keys warn the user about it
60+
} else if (entries.parallelStream().anyMatch(BibEntry::hasCiteKey)) {
61+
boolean overwriteKeys = confirmOverwriteKeys(dialogService);
62+
63+
// The user doesn't want to override cite keys
64+
if (!overwriteKeys) {
65+
canceled = true;
66+
return;
67+
}
68+
}
69+
70+
// generate the new cite keys for each entry
71+
final NamedCompound compound = new NamedCompound(Localization.lang("Autogenerate BibTeX keys"));
72+
BibtexKeyGenerator keyGenerator = new BibtexKeyGenerator(basePanel.getBibDatabaseContext(), Globals.prefs.getBibtexKeyPatternPreferences());
73+
for (BibEntry entry : entries) {
74+
keyGenerator.generateAndSetKey(entry)
75+
.ifPresent(fieldChange -> compound.addEdit(new UndoableKeyChange(fieldChange)));
76+
}
77+
compound.end();
78+
79+
// register the undo event only if new cite keys were generated
80+
if (compound.hasEdits()) {
81+
basePanel.getUndoManager().addEdit(compound);
82+
}
83+
}
84+
85+
@Override
86+
public void update() {
87+
if (canceled) {
88+
return;
89+
}
90+
basePanel.markBaseChanged();
91+
basePanel.output(formatOutputMessage(Localization.lang("Generated BibTeX key for"), entries.size()));
92+
}
93+
94+
private String formatOutputMessage(String start, int count) {
95+
return String.format("%s %d %s.", start, count,
96+
(count > 1 ? Localization.lang("entries") : Localization.lang("entry")));
97+
}
98+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.jabref.gui.actions;
2+
3+
import javax.swing.undo.UndoManager;
4+
5+
import org.jabref.gui.DialogService;
6+
import org.jabref.gui.entryeditor.EntryEditorPreferences;
7+
import org.jabref.gui.undo.UndoableKeyChange;
8+
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator;
9+
import org.jabref.model.database.BibDatabaseContext;
10+
import org.jabref.model.entry.BibEntry;
11+
12+
public class GenerateBibtexKeySingleAction extends SimpleCommand {
13+
14+
private DialogService dialogService;
15+
private BibDatabaseContext databaseContext;
16+
private EntryEditorPreferences preferences;
17+
private BibEntry entry;
18+
private UndoManager undoManager;
19+
20+
public GenerateBibtexKeySingleAction(BibEntry entry, BibDatabaseContext databaseContext, DialogService dialogService, EntryEditorPreferences preferences, UndoManager undoManager) {
21+
this.entry = entry;
22+
this.databaseContext = databaseContext;
23+
this.dialogService = dialogService;
24+
this.preferences = preferences;
25+
this.undoManager = undoManager;
26+
27+
if (preferences.avoidOverwritingCiteKey()) {
28+
// Only make command executable if cite key is empty
29+
this.executable.bind(entry.getCiteKeyBinding().isNull());
30+
}
31+
}
32+
33+
@Override
34+
public void execute() {
35+
if (!entry.hasCiteKey() || GenerateBibtexKeyAction.confirmOverwriteKeys(dialogService)) {
36+
new BibtexKeyGenerator(databaseContext, preferences.getBibtexKeyPatternPreferences())
37+
.generateAndSetKey(entry)
38+
.ifPresent(change -> undoManager.addEdit(new UndoableKeyChange(change)));
39+
}
40+
}
41+
}

src/main/java/org/jabref/gui/actions/JabRefAction.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ public JabRefAction(Action action, KeyBindingRepository keyBindingRepository) {
2424

2525
public JabRefAction(Action action, Command command, KeyBindingRepository keyBindingRepository) {
2626
this(action, keyBindingRepository);
27+
2728
setEventHandler(event -> {
2829
command.execute();
2930
trackExecute();
3031
});
32+
33+
disabledProperty().bind(command.executableProperty().not());
3134
}
3235

3336
private void trackExecute() {

src/main/java/org/jabref/gui/actions/StandardActions.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,12 @@ public enum StandardActions implements Action {
127127
MERGE_ENTRIES(Localization.lang("Merge entries"), IconTheme.JabRefIcons.MERGE_ENTRIES),
128128
RESOLVE_DUPLICATE_KEYS(Localization.lang("Resolve duplicate BibTeX keys"), Localization.lang("Find and remove duplicate BibTeX keys"), KeyBinding.RESOLVE_DUPLICATE_BIBTEX_KEYS),
129129
CHECK_INTEGRITY(Localization.lang("Check integrity"), KeyBinding.CHECK_INTEGRITY),
130-
AUTOGENERATE_KEYS(Localization.lang("Autogenerate BibTeX keys"), IconTheme.JabRefIcons.MAKE_KEY, KeyBinding.AUTOGENERATE_BIBTEX_KEYS),
131130
FIND_UNLINKED_FILES(Localization.lang("Find unlinked files"), Localization.lang("Searches for unlinked PDF files on the file system"), KeyBinding.FIND_UNLINKED_FILES),
132131
AUTO_LINK_FILES(Localization.lang("Automatically set file links"), IconTheme.JabRefIcons.AUTO_FILE_LINK, KeyBinding.AUTOMATICALLY_LINK_FILES),
133132
LOOKUP_DOC_IDENTIFIER(Localization.lang("Look up document identifier")),
134133
LOOKUP_FULLTEXT(Localization.lang("Look up full text documents"), KeyBinding.DOWNLOAD_FULL_TEXT),
135-
136-
GENERATE_CITE_KEY(Localization.lang("Autogenerate BibTeX keys"), IconTheme.JabRefIcons.MAKE_KEY, KeyBinding.AUTOGENERATE_BIBTEX_KEYS),
134+
GENERATE_CITE_KEY(Localization.lang("Generate BibTeX key"), IconTheme.JabRefIcons.MAKE_KEY, KeyBinding.AUTOGENERATE_BIBTEX_KEYS),
135+
GENERATE_CITE_KEYS(Localization.lang("Autogenerate BibTeX keys"), IconTheme.JabRefIcons.MAKE_KEY, KeyBinding.AUTOGENERATE_BIBTEX_KEYS),
137136
DOWNLOAD_FULL_TEXT(Localization.lang("Look up full text documents"), KeyBinding.DOWNLOAD_FULL_TEXT),
138137
CLEANUP_ENTRIES(Localization.lang("Cleanup entries"), IconTheme.JabRefIcons.CLEANUP_ENTRIES, KeyBinding.CLEANUP),
139138
SET_FILE_LINKS(Localization.lang("Automatically set file links"), IconTheme.JabRefIcons.AUTO_FILE_LINK, KeyBinding.AUTOMATICALLY_LINK_FILES),

src/main/java/org/jabref/gui/entryeditor/EntryEditor.fxml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,7 @@
4040
<Tooltip text="%Change entry type"/>
4141
</tooltip>
4242
</Button>
43-
<Button styleClass="flatButton,narrow" onAction="#generateKey">
44-
<graphic>
45-
<JabRefIconView glyphName="MAKE_KEY"/>
46-
</graphic>
47-
<tooltip>
48-
<Tooltip text="%Generate BibTeX key"/>
49-
</tooltip>
50-
</Button>
43+
<Button fx:id="generateCiteKeyButton" styleClass="narrow"/>
5144
<Button fx:id="fetcherButton" styleClass="flatButton,narrow">
5245
<graphic>
5346
<JabRefIconView glyph="REFRESH"/>

0 commit comments

Comments
 (0)