use of org.jabref.gui.undo.NamedCompound in project jabref by JabRef.
the class AbbreviateAction method run.
@Override
public void run() {
List<BibEntry> entries = panel.getSelectedEntries();
if (entries == null) {
return;
}
UndoableAbbreviator undoableAbbreviator = new UndoableAbbreviator(Globals.journalAbbreviationLoader.getRepository(Globals.prefs.getJournalAbbreviationPreferences()), iso);
NamedCompound ce = new NamedCompound(Localization.lang("Abbreviate journal names"));
int count = 0;
for (BibEntry entry : entries) {
for (String journalField : InternalBibtexFields.getJournalNameFields()) {
if (undoableAbbreviator.abbreviate(panel.getDatabase(), entry, journalField, ce)) {
count++;
}
}
}
if (count > 0) {
ce.end();
panel.getUndoManager().addEdit(ce);
panel.markBaseChanged();
message = Localization.lang("Abbreviated %0 journal names.", String.valueOf(count));
} else {
message = Localization.lang("No journal names could be abbreviated.");
}
}
use of org.jabref.gui.undo.NamedCompound 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;
}
}
use of org.jabref.gui.undo.NamedCompound in project jabref by JabRef.
the class BasicAction method parseWithFreeCiteAndAddEntries.
/**
* tries to parse the pasted reference with freecite
*
* @return true if successful, false otherwise
*/
private boolean parseWithFreeCiteAndAddEntries() {
FreeCiteImporter fimp = new FreeCiteImporter(Globals.prefs.getImportFormatPreferences());
String text = textPane.getText();
// we have to remove line breaks (but keep empty lines)
// otherwise, the result is broken
text = text.replace(OS.NEWLINE.concat(OS.NEWLINE), "##NEWLINE##");
// possible URL line breaks are removed completely.
text = text.replace("/".concat(OS.NEWLINE), "/");
text = text.replace(OS.NEWLINE, " ");
text = text.replace("##NEWLINE##", OS.NEWLINE);
ParserResult importerResult = fimp.importEntries(text);
if (importerResult.hasWarnings()) {
frame.showMessage(importerResult.getErrorMessage());
}
List<BibEntry> importedEntries = importerResult.getDatabase().getEntries();
if (importedEntries.isEmpty()) {
return false;
} else {
UpdateField.setAutomaticFields(importedEntries, false, false, Globals.prefs.getUpdateFieldPreferences());
boolean markEntries = EntryMarker.shouldMarkEntries();
for (BibEntry e : importedEntries) {
if (markEntries) {
EntryMarker.markEntry(entry, EntryMarker.IMPORT_MARK_LEVEL, false, new NamedCompound(""));
}
frame.getCurrentBasePanel().insertEntry(e);
}
return true;
}
}
use of org.jabref.gui.undo.NamedCompound in project jabref by JabRef.
the class LookupIdentifiersWorker method run.
@Override
public void run() {
BasePanel basePanel = Objects.requireNonNull(frame.getCurrentBasePanel());
List<BibEntry> bibEntries = basePanel.getSelectedEntries();
if (!bibEntries.isEmpty()) {
String totalCount = Integer.toString(bibEntries.size());
NamedCompound namedCompound = new NamedCompound(Localization.lang("Look up %0", fetcher.getIdentifierName()));
int count = 0;
int foundCount = 0;
for (BibEntry bibEntry : bibEntries) {
count++;
frame.output(Localization.lang("Looking up %0... - entry %1 out of %2 - found %3", fetcher.getIdentifierName(), Integer.toString(count), totalCount, Integer.toString(foundCount)));
Optional<T> identifier = Optional.empty();
try {
identifier = fetcher.findIdentifier(bibEntry);
} catch (FetcherException e) {
LOGGER.error("Could not fetch " + fetcher.getIdentifierName(), e);
}
if (identifier.isPresent() && !bibEntry.hasField(identifier.get().getDefaultField())) {
Optional<FieldChange> fieldChange = bibEntry.setField(identifier.get().getDefaultField(), identifier.get().getNormalized());
if (fieldChange.isPresent()) {
namedCompound.addEdit(new UndoableFieldChange(fieldChange.get()));
foundCount++;
frame.output(Localization.lang("Looking up %0... - entry %1 out of %2 - found %3", Integer.toString(count), totalCount, Integer.toString(foundCount)));
}
}
}
namedCompound.end();
if (foundCount > 0) {
basePanel.getUndoManager().addEdit(namedCompound);
basePanel.markBaseChanged();
}
message = Localization.lang("Determined %0 for %1 entries", fetcher.getIdentifierName(), Integer.toString(foundCount));
}
}
use of org.jabref.gui.undo.NamedCompound in project jabref by JabRef.
the class SpecialFieldAction method action.
@Override
public void action() {
try {
List<BibEntry> bes = frame.getCurrentBasePanel().getSelectedEntries();
if ((bes == null) || bes.isEmpty()) {
return;
}
NamedCompound ce = new NamedCompound(undoText);
for (BibEntry be : bes) {
// if (value==null) and then call nullField has been omitted as updatefield also handles value==null
List<FieldChange> changes = SpecialFieldsUtils.updateField(specialField, value, be, nullFieldIfValueIsTheSame, Globals.prefs.isKeywordSyncEnabled(), Globals.prefs.getKeywordDelimiter());
for (FieldChange change : changes) {
ce.addEdit(new UndoableFieldChange(change));
}
}
ce.end();
if (ce.hasEdits()) {
frame.getCurrentBasePanel().getUndoManager().addEdit(ce);
frame.getCurrentBasePanel().markBaseChanged();
frame.getCurrentBasePanel().updateEntryEditorIfShowing();
String outText;
if (nullFieldIfValueIsTheSame || value == null) {
outText = getTextDone(specialField, Integer.toString(bes.size()));
} else {
outText = getTextDone(specialField, value, Integer.toString(bes.size()));
}
frame.output(outText);
} else {
// if user does not change anything with his action, we do not do anything either
// even no output message
}
} catch (Throwable ex) {
LOGGER.error("Problem setting special fields", ex);
}
}
Aggregations