Search in sources :

Example 31 with BibEntry

use of org.jabref.model.entry.BibEntry in project jabref by JabRef.

the class SpecialFieldUpdateListener method listen.

@Subscribe
public void listen(FieldChangedEvent fieldChangedEvent) {
    // only sync if keyword sync is enabled
    if (!Globals.prefs.isKeywordSyncEnabled()) {
        return;
    }
    final BibEntry entry = fieldChangedEvent.getBibEntry();
    final String fieldName = fieldChangedEvent.getFieldName();
    // Source editor cycles through all entries
    // if we immediately updated the fields, the entry editor would detect a subsequent change as a user change
    // and re-fire this event
    // e.g., "keyword = {prio1}, priority = {prio2}" and a change at keyword to prio3 would not succeed.
    SwingUtilities.invokeLater(() -> {
        if (FieldName.KEYWORDS.equals(fieldName)) {
            SpecialFieldsUtils.syncSpecialFieldsFromKeywords(entry, Globals.prefs.getKeywordDelimiter());
        } else if (SpecialField.isSpecialField(fieldName)) {
            SpecialFieldsUtils.syncKeywordsFromSpecialFields(entry, Globals.prefs.getKeywordDelimiter());
        }
        SwingUtilities.invokeLater(() -> JabRefGUI.getMainFrame().getCurrentBasePanel().updateEntryEditorIfShowing());
    });
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) Subscribe(com.google.common.eventbus.Subscribe)

Example 32 with BibEntry

use of org.jabref.model.entry.BibEntry in project jabref by JabRef.

the class ModsExportFormat method performExport.

@Override
public void performExport(final BibDatabaseContext databaseContext, final String file, final Charset encoding, List<BibEntry> entries) throws SaveException, IOException {
    Objects.requireNonNull(databaseContext);
    Objects.requireNonNull(entries);
    if (entries.isEmpty()) {
        // Only export if entries exist
        return;
    }
    try {
        ModsCollectionDefinition modsCollection = new ModsCollectionDefinition();
        for (BibEntry bibEntry : entries) {
            ModsDefinition mods = new ModsDefinition();
            bibEntry.getCiteKeyOptional().ifPresent(citeKey -> addIdentifier("citekey", citeKey, mods));
            Map<String, String> fieldMap = bibEntry.getFieldMap();
            addGenre(bibEntry, mods);
            OriginInfoDefinition originInfo = new OriginInfoDefinition();
            PartDefinition partDefinition = new PartDefinition();
            RelatedItemDefinition relatedItem = new RelatedItemDefinition();
            for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                switch(key) {
                    case FieldName.AUTHOR:
                        handleAuthors(mods, value);
                        break;
                    case "affiliation":
                        addAffiliation(mods, value);
                        break;
                    case FieldName.ABSTRACT:
                        addAbstract(mods, value);
                        break;
                    case FieldName.TITLE:
                        addTitle(mods, value);
                        break;
                    case FieldName.LANGUAGE:
                        addLanguage(mods, value);
                        break;
                    case FieldName.LOCATION:
                        addLocation(mods, value);
                        break;
                    case FieldName.URL:
                        addUrl(mods, value);
                        break;
                    case FieldName.NOTE:
                        addNote(mods, value);
                        break;
                    case FieldName.KEYWORDS:
                        addKeyWords(mods, value);
                        break;
                    case FieldName.VOLUME:
                        addDetail(FieldName.VOLUME, value, partDefinition);
                        break;
                    case FieldName.ISSUE:
                        addDetail(FieldName.ISSUE, value, partDefinition);
                        break;
                    case FieldName.PAGES:
                        addPages(partDefinition, value);
                        break;
                    case FieldName.URI:
                        addIdentifier(FieldName.URI, value, mods);
                        break;
                    case FieldName.ISBN:
                        addIdentifier(FieldName.ISBN, value, mods);
                        break;
                    case FieldName.ISSN:
                        addIdentifier(FieldName.ISSN, value, mods);
                        break;
                    case FieldName.DOI:
                        addIdentifier(FieldName.DOI, value, mods);
                        break;
                    case FieldName.PMID:
                        addIdentifier(FieldName.PMID, value, mods);
                        break;
                    case FieldName.JOURNAL:
                        addJournal(value, relatedItem);
                        break;
                    default:
                        break;
                }
                addOriginInformation(key, value, originInfo);
            }
            mods.getModsGroup().add(originInfo);
            addRelatedAndOriginInfoToModsGroup(relatedItem, partDefinition, mods);
            modsCollection.getMods().add(mods);
        }
        JAXBElement<ModsCollectionDefinition> jaxbElement = new JAXBElement<>(new QName(MODS_NAMESPACE_URI, "modsCollection"), ModsCollectionDefinition.class, modsCollection);
        createMarshallerAndWriteToFile(file, jaxbElement);
    } catch (JAXBException ex) {
        throw new SaveException(ex);
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ModsCollectionDefinition(org.jabref.logic.importer.fileformat.mods.ModsCollectionDefinition) ModsDefinition(org.jabref.logic.importer.fileformat.mods.ModsDefinition) QName(javax.xml.namespace.QName) JAXBException(javax.xml.bind.JAXBException) PartDefinition(org.jabref.logic.importer.fileformat.mods.PartDefinition) NamePartDefinition(org.jabref.logic.importer.fileformat.mods.NamePartDefinition) JAXBElement(javax.xml.bind.JAXBElement) OriginInfoDefinition(org.jabref.logic.importer.fileformat.mods.OriginInfoDefinition) RelatedItemDefinition(org.jabref.logic.importer.fileformat.mods.RelatedItemDefinition) Map(java.util.Map)

Example 33 with BibEntry

use of org.jabref.model.entry.BibEntry in project jabref by JabRef.

the class BasePanel method autoGenerateKeysBeforeSaving.

/**
     * If the relevant option is set, autogenerate keys for all entries that are lacking keys.
     */
public void autoGenerateKeysBeforeSaving() {
    if (Globals.prefs.getBoolean(JabRefPreferences.GENERATE_KEYS_BEFORE_SAVING)) {
        NamedCompound ce = new NamedCompound(Localization.lang("Autogenerate BibTeX keys"));
        for (BibEntry bes : bibDatabaseContext.getDatabase().getEntries()) {
            Optional<String> oldKey = bes.getCiteKeyOptional();
            if (!(oldKey.isPresent()) || oldKey.get().isEmpty()) {
                BibtexKeyPatternUtil.makeAndSetLabel(bibDatabaseContext.getMetaData().getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()), bibDatabaseContext.getDatabase(), bes, Globals.prefs.getBibtexKeyPatternPreferences());
                bes.getCiteKeyOptional().ifPresent(newKey -> ce.addEdit(new UndoableKeyChange(bes, oldKey.orElse(""), newKey)));
            }
        }
        // Store undo information, if any:
        if (ce.hasEdits()) {
            ce.end();
            getUndoManager().addEdit(ce);
        }
    }
}
Also used : UndoableKeyChange(org.jabref.gui.undo.UndoableKeyChange) BibEntry(org.jabref.model.entry.BibEntry) NamedCompound(org.jabref.gui.undo.NamedCompound)

Example 34 with BibEntry

use of org.jabref.model.entry.BibEntry in project jabref by JabRef.

the class BasePanel method newEntry.

/**
     * This method is called from JabRefFrame when the user wants to create a new entry. If the argument is null, the
     * user is prompted for an entry type.
     *
     * @param type The type of the entry to create.
     * @return The newly created BibEntry or null the operation was canceled by the user.
     */
public BibEntry newEntry(EntryType type) {
    EntryType actualType = type;
    if (actualType == null) {
        // Find out what type is wanted.
        final EntryTypeDialog etd = new EntryTypeDialog(frame);
        // We want to center the dialog, to make it look nicer.
        etd.setLocationRelativeTo(frame);
        etd.setVisible(true);
        actualType = etd.getChoice();
    }
    if (actualType != null) {
        // Only if the dialog was not canceled.
        final BibEntry be = new BibEntry(actualType.getName());
        try {
            bibDatabaseContext.getDatabase().insertEntry(be);
            // Set owner/timestamp if options are enabled:
            List<BibEntry> list = new ArrayList<>();
            list.add(be);
            UpdateField.setAutomaticFields(list, true, true, Globals.prefs.getUpdateFieldPreferences());
            // Create an UndoableInsertEntry object.
            getUndoManager().addEdit(new UndoableInsertEntry(bibDatabaseContext.getDatabase(), be, BasePanel.this));
            output(Localization.lang("Added new '%0' entry.", actualType.getName().toLowerCase(Locale.ROOT)));
            // and adjustment of the splitter.
            if (mode != BasePanelMode.SHOWING_EDITOR) {
                mode = BasePanelMode.WILL_SHOW_EDITOR;
            }
            highlightEntry(be);
            // The database just changed.
            markBaseChanged();
            final EntryEditor entryEditor = getEntryEditor(be);
            this.showEntryEditor(entryEditor);
            entryEditor.requestFocus();
            return be;
        } catch (KeyCollisionException ex) {
            LOGGER.info(ex.getMessage(), ex);
        }
    }
    return null;
}
Also used : KeyCollisionException(org.jabref.model.database.KeyCollisionException) BibEntry(org.jabref.model.entry.BibEntry) EntryEditor(org.jabref.gui.entryeditor.EntryEditor) EntryType(org.jabref.model.entry.EntryType) ArrayList(java.util.ArrayList) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry)

Example 35 with BibEntry

use of org.jabref.model.entry.BibEntry in project jabref by JabRef.

the class BasePanel method copyTitle.

private void copyTitle() {
    List<BibEntry> selectedBibEntries = mainTable.getSelectedEntries();
    if (!selectedBibEntries.isEmpty()) {
        storeCurrentEdit();
        // Collect all non-null titles.
        List<String> titles = selectedBibEntries.stream().filter(bibEntry -> bibEntry.getTitle().isPresent()).map(bibEntry -> bibEntry.getTitle().get()).collect(Collectors.toList());
        if (titles.isEmpty()) {
            output(Localization.lang("None of the selected entries have titles."));
            return;
        }
        StringSelection ss = new StringSelection(String.join("\n", titles));
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, BasePanel.this);
        if (titles.size() == selectedBibEntries.size()) {
            // All entries had titles.
            output((selectedBibEntries.size() > 1 ? Localization.lang("Copied titles") : Localization.lang("Copied title")) + '.');
        } else {
            output(Localization.lang("Warning: %0 out of %1 entries have undefined title.", Integer.toString(selectedBibEntries.size() - titles.size()), Integer.toString(selectedBibEntries.size())));
        }
    }
}
Also used : CitationStyleToClipboardWorker(org.jabref.gui.worker.CitationStyleToClipboardWorker) BibDatabase(org.jabref.model.database.BibDatabase) CheckBoxMessage(org.jabref.gui.util.component.CheckBoxMessage) TextInputDialog(org.jabref.gui.plaintextimport.TextInputDialog) Map(java.util.Map) EntryChangedEvent(org.jabref.model.entry.event.EntryChangedEvent) Path(java.nio.file.Path) ExternalFileType(org.jabref.gui.externalfiletype.ExternalFileType) ClipboardOwner(java.awt.datatransfer.ClipboardOwner) FieldEditor(org.jabref.gui.fieldeditors.FieldEditor) SaveDatabaseAction(org.jabref.gui.exporter.SaveDatabaseAction) DBMSSynchronizer(org.jabref.shared.DBMSSynchronizer) Set(java.util.Set) KeyEvent(java.awt.event.KeyEvent) InvocationTargetException(java.lang.reflect.InvocationTargetException) Platform(javafx.application.Platform) AbstractAction(javax.swing.AbstractAction) BibtexKeyPatternUtil(org.jabref.logic.bibtexkeypattern.BibtexKeyPatternUtil) FormLayout(com.jgoodies.forms.layout.FormLayout) CallBack(org.jabref.gui.worker.CallBack) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) LogFactory(org.apache.commons.logging.LogFactory) UndoableKeyChange(org.jabref.gui.undo.UndoableKeyChange) ExternalFileMenuItem(org.jabref.gui.externalfiletype.ExternalFileMenuItem) JPanel(javax.swing.JPanel) Actions(org.jabref.gui.actions.Actions) PreviewPreferences(org.jabref.preferences.PreviewPreferences) JSplitPane(javax.swing.JSplitPane) NamedCompound(org.jabref.gui.undo.NamedCompound) AutoCompleter(org.jabref.logic.autocompleter.AutoCompleter) MergeEntriesDialog(org.jabref.gui.mergeentries.MergeEntriesDialog) JabRefDesktop(org.jabref.gui.desktop.JabRefDesktop) SpecialFieldDatabaseChangeListener(org.jabref.gui.specialfields.SpecialFieldDatabaseChangeListener) MainTableDataModel(org.jabref.gui.maintable.MainTableDataModel) KeyCollisionException(org.jabref.model.database.KeyCollisionException) KeyAdapter(java.awt.event.KeyAdapter) SpecialField(org.jabref.model.entry.specialfields.SpecialField) AbbreviateAction(org.jabref.gui.journals.AbbreviateAction) SpecialFieldValue(org.jabref.model.entry.specialfields.SpecialFieldValue) ArrayList(java.util.ArrayList) CitationStyleCache(org.jabref.logic.citationstyle.CitationStyleCache) Layout(org.jabref.logic.layout.Layout) SpecialFieldValueViewModel(org.jabref.gui.specialfields.SpecialFieldValueViewModel) StringSelection(java.awt.datatransfer.StringSelection) ContentAutoCompleters(org.jabref.logic.autocompleter.ContentAutoCompleters) SaveException(org.jabref.logic.exporter.SaveException) ContentSelectorDialog(org.jabref.gui.contentselector.ContentSelectorDialog) MarkEntriesAction(org.jabref.gui.worker.MarkEntriesAction) FindFullTextAction(org.jabref.gui.externalfiles.FindFullTextAction) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry) AbstractBibtexKeyPattern(org.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern) IOException(java.io.IOException) EntryEditor(org.jabref.gui.entryeditor.EntryEditor) MainTable(org.jabref.gui.maintable.MainTable) File(java.io.File) Globals(org.jabref.Globals) FileListEntry(org.jabref.gui.filelist.FileListEntry) StringReader(java.io.StringReader) UndoableChangeType(org.jabref.gui.undo.UndoableChangeType) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) WriteXMPAction(org.jabref.gui.externalfiles.WriteXMPAction) GroupAddRemoveDialog(org.jabref.gui.groups.GroupAddRemoveDialog) FileSaveSession(org.jabref.logic.exporter.FileSaveSession) JTextArea(javax.swing.JTextArea) CopyBibTeXKeyAndLinkAction(org.jabref.gui.actions.CopyBibTeXKeyAndLinkAction) EntryRemovedEvent(org.jabref.model.database.event.EntryRemovedEvent) ChangeScanner(org.jabref.collab.ChangeScanner) UnabbreviateAction(org.jabref.gui.journals.UnabbreviateAction) CannotUndoException(javax.swing.undo.CannotUndoException) AbstractWorker(org.jabref.gui.worker.AbstractWorker) SearchQuery(org.jabref.logic.search.SearchQuery) JabRefPreferences(org.jabref.preferences.JabRefPreferences) AppendDatabaseAction(org.jabref.gui.importer.actions.AppendDatabaseAction) AutoCompletePreferences(org.jabref.logic.autocompleter.AutoCompletePreferences) EntryAddedEvent(org.jabref.model.database.event.EntryAddedEvent) FileAnnotationCache(org.jabref.logic.pdf.FileAnnotationCache) CannotRedoException(javax.swing.undo.CannotRedoException) Encodings(org.jabref.logic.l10n.Encodings) Locale(java.util.Locale) CountingUndoManager(org.jabref.gui.undo.CountingUndoManager) AutoCompleterFactory(org.jabref.logic.autocompleter.AutoCompleterFactory) UpdateField(org.jabref.logic.util.UpdateField) UndoableRemoveEntry(org.jabref.gui.undo.UndoableRemoveEntry) BorderLayout(java.awt.BorderLayout) SearchFixDuplicateLabels(org.jabref.gui.bibtexkeypattern.SearchFixDuplicateLabels) Clipboard(java.awt.datatransfer.Clipboard) FileFinders(org.jabref.logic.util.io.FileFinders) Collection(java.util.Collection) BibEntry(org.jabref.model.entry.BibEntry) FileListTableModel(org.jabref.gui.filelist.FileListTableModel) BorderFactory(javax.swing.BorderFactory) Collectors(java.util.stream.Collectors) DefaultTaskExecutor(org.jabref.gui.util.DefaultTaskExecutor) FileFinder(org.jabref.logic.util.io.FileFinder) Objects(java.util.Objects) FieldChange(org.jabref.model.FieldChange) List(java.util.List) FileUpdateListener(org.jabref.collab.FileUpdateListener) ExportToClipboardAction(org.jabref.gui.exporter.ExportToClipboardAction) LayoutHelper(org.jabref.logic.layout.LayoutHelper) Optional(java.util.Optional) JabRefExecutorService(org.jabref.JabRefExecutorService) KeyBinding(org.jabref.gui.keyboard.KeyBinding) Toolkit(java.awt.Toolkit) SavePreferences(org.jabref.logic.exporter.SavePreferences) BibtexDatabaseWriter(org.jabref.logic.exporter.BibtexDatabaseWriter) FieldName(org.jabref.model.entry.FieldName) Transferable(java.awt.datatransfer.Transferable) HashMap(java.util.HashMap) CitationStyleOutputFormat(org.jabref.logic.citationstyle.CitationStyleOutputFormat) CleanupAction(org.jabref.gui.actions.CleanupAction) SwingUtilities(javax.swing.SwingUtilities) FileExtensions(org.jabref.logic.util.FileExtensions) SpecialFieldViewModel(org.jabref.gui.specialfields.SpecialFieldViewModel) Charset(java.nio.charset.Charset) BaseAction(org.jabref.gui.actions.BaseAction) FileUpdatePanel(org.jabref.collab.FileUpdatePanel) MainTableFormat(org.jabref.gui.maintable.MainTableFormat) Localization(org.jabref.logic.l10n.Localization) Subscribe(com.google.common.eventbus.Subscribe) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) MainTableSelectionListener(org.jabref.gui.maintable.MainTableSelectionListener) JComponent(javax.swing.JComponent) FormBuilder(com.jgoodies.forms.builder.FormBuilder) FileBasedLock(org.jabref.logic.util.io.FileBasedLock) FileUtil(org.jabref.logic.util.io.FileUtil) SaveSession(org.jabref.logic.exporter.SaveSession) FileDialogConfiguration(org.jabref.gui.util.FileDialogConfiguration) EntryEventSource(org.jabref.model.entry.event.EntryEventSource) JOptionPane(javax.swing.JOptionPane) ActionEvent(java.awt.event.ActionEvent) SynchronizeFileField(org.jabref.gui.externalfiles.SynchronizeFileField) SendAsEMailAction(org.jabref.gui.worker.SendAsEMailAction) AttachFileAction(org.jabref.gui.filelist.AttachFileAction) DatabaseLocation(org.jabref.model.database.DatabaseLocation) MergeWithFetchedEntryAction(org.jabref.gui.mergeentries.MergeWithFetchedEntryAction) Log(org.apache.commons.logging.Log) ExternalFileTypes(org.jabref.gui.externalfiletype.ExternalFileTypes) EntryType(org.jabref.model.entry.EntryType) Collections(java.util.Collections) BibEntry(org.jabref.model.entry.BibEntry) StringSelection(java.awt.datatransfer.StringSelection)

Aggregations

BibEntry (org.jabref.model.entry.BibEntry)716 Test (org.junit.Test)466 ParserResult (org.jabref.logic.importer.ParserResult)131 StringReader (java.io.StringReader)107 ArrayList (java.util.ArrayList)75 BibDatabase (org.jabref.model.database.BibDatabase)63 Path (java.nio.file.Path)52 IOException (java.io.IOException)43 HashMap (java.util.HashMap)37 Before (org.junit.Before)36 NamedCompound (org.jabref.gui.undo.NamedCompound)30 BibtexParser (org.jabref.logic.importer.fileformat.BibtexParser)28 BibtexString (org.jabref.model.entry.BibtexString)28 List (java.util.List)23 File (java.io.File)21 StringWriter (java.io.StringWriter)19 Optional (java.util.Optional)19 BasePanel (org.jabref.gui.BasePanel)19 FieldChange (org.jabref.model.FieldChange)18 InputStream (java.io.InputStream)16