Search in sources :

Example 6 with NamedCompound

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.");
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) NamedCompound(org.jabref.gui.undo.NamedCompound)

Example 7 with NamedCompound

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;
    }
}
Also used : UndoableKeyChange(org.jabref.gui.undo.UndoableKeyChange) BibEntry(org.jabref.model.entry.BibEntry) BasePanel(org.jabref.gui.BasePanel) BibtexKeyPatternPreferences(org.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences) NamedCompound(org.jabref.gui.undo.NamedCompound)

Example 8 with NamedCompound

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;
    }
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) NamedCompound(org.jabref.gui.undo.NamedCompound) FreeCiteImporter(org.jabref.logic.importer.fileformat.FreeCiteImporter)

Example 9 with NamedCompound

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));
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) BasePanel(org.jabref.gui.BasePanel) FetcherException(org.jabref.logic.importer.FetcherException) NamedCompound(org.jabref.gui.undo.NamedCompound) FieldChange(org.jabref.model.FieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange)

Example 10 with NamedCompound

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);
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) NamedCompound(org.jabref.gui.undo.NamedCompound) FieldChange(org.jabref.model.FieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange)

Aggregations

NamedCompound (org.jabref.gui.undo.NamedCompound)34 BibEntry (org.jabref.model.entry.BibEntry)27 UndoableFieldChange (org.jabref.gui.undo.UndoableFieldChange)11 FieldChange (org.jabref.model.FieldChange)7 ArrayList (java.util.ArrayList)5 BasePanel (org.jabref.gui.BasePanel)5 UndoableInsertEntry (org.jabref.gui.undo.UndoableInsertEntry)4 UndoableKeyChange (org.jabref.gui.undo.UndoableKeyChange)4 UndoableRemoveEntry (org.jabref.gui.undo.UndoableRemoveEntry)3 ButtonBarBuilder (com.jgoodies.forms.builder.ButtonBarBuilder)2 FormLayout (com.jgoodies.forms.layout.FormLayout)2 IOException (java.io.IOException)2 List (java.util.List)2 JButton (javax.swing.JButton)2 JSeparator (javax.swing.JSeparator)2 ExternalFileType (org.jabref.gui.externalfiletype.ExternalFileType)2 UndoableChangeType (org.jabref.gui.undo.UndoableChangeType)2 WindowLocation (org.jabref.gui.util.WindowLocation)2 CheckBoxMessage (org.jabref.gui.util.component.CheckBoxMessage)2 ParserResult (org.jabref.logic.importer.ParserResult)2