use of org.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences in project jabref by JabRef.
the class OpenOfficePanel method checkThatEntriesHaveKeys.
/**
* Check that all entries in the list have BibTeX keys, if not ask if they should be generated
*
* @param entries A list of entries to be checked
* @return true if all entries have BibTeX keys, if it so may be after generating them
*/
private boolean checkThatEntriesHaveKeys(List<BibEntry> entries) {
// Check if there are empty keys
boolean emptyKeys = false;
for (BibEntry entry : entries) {
if (!entry.getCiteKeyOptional().isPresent()) {
// Found one, no need to look further for now
emptyKeys = true;
break;
}
}
// If no empty keys, return true
if (!emptyKeys) {
return true;
}
// Ask if keys should be generated
String[] options = { Localization.lang("Generate keys"), Localization.lang("Cancel") };
int answer = JOptionPane.showOptionDialog(this.frame, Localization.lang("Cannot cite entries without BibTeX keys. Generate keys now?"), Localization.lang("Cite"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, null);
BasePanel panel = frame.getCurrentBasePanel();
if ((answer == JOptionPane.OK_OPTION) && (panel != null)) {
// Generate keys
BibtexKeyPatternPreferences prefs = Globals.prefs.getBibtexKeyPatternPreferences();
NamedCompound undoCompound = new NamedCompound(Localization.lang("Cite"));
for (BibEntry entry : entries) {
if (!entry.getCiteKeyOptional().isPresent()) {
// Generate key
BibtexKeyPatternUtil.makeAndSetLabel(panel.getBibDatabaseContext().getMetaData().getCiteKeyPattern(prefs.getKeyPattern()), panel.getDatabase(), entry, prefs);
// Add undo change
undoCompound.addEdit(new UndoableKeyChange(entry, null, entry.getCiteKeyOptional().get()));
}
}
undoCompound.end();
// Add all undos
panel.getUndoManager().addEdit(undoCompound);
// Now every entry has a key
return true;
} else {
// No, we canceled (or there is no panel to get the database from, highly unlikely)
return false;
}
}
Aggregations