Search in sources :

Example 11 with BibEntry

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

the class ManageKeywordsAction method fillKeyWordList.

private void fillKeyWordList() {
    BasePanel bp = frame.getCurrentBasePanel();
    List<BibEntry> entries = bp.getSelectedEntries();
    // fill dialog with values
    keywordListModel.clear();
    sortedKeywordsOfAllEntriesBeforeUpdateByUser.clear();
    if (mergeKeywords.isSelected()) {
        for (BibEntry entry : entries) {
            KeywordList separatedKeywords = entry.getKeywords(Globals.prefs.getKeywordDelimiter());
            sortedKeywordsOfAllEntriesBeforeUpdateByUser.addAll(separatedKeywords);
        }
    } else {
        assert intersectKeywords.isSelected();
        // all keywords from first entry have to be added
        BibEntry firstEntry = entries.get(0);
        KeywordList separatedKeywords = firstEntry.getKeywords(Globals.prefs.getKeywordDelimiter());
        sortedKeywordsOfAllEntriesBeforeUpdateByUser.addAll(separatedKeywords);
        // this approach ensures that one empty keyword list leads to an empty set of common keywords
        for (int i = 1; i < entries.size(); i++) {
            BibEntry entry = entries.get(i);
            separatedKeywords = entry.getKeywords(Globals.prefs.getKeywordDelimiter());
            sortedKeywordsOfAllEntriesBeforeUpdateByUser.retainAll(separatedKeywords);
        }
    }
    for (Keyword keyword : sortedKeywordsOfAllEntriesBeforeUpdateByUser) {
        keywordListModel.addElement(keyword);
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) BasePanel(org.jabref.gui.BasePanel) Keyword(org.jabref.model.entry.Keyword) KeywordList(org.jabref.model.entry.KeywordList)

Example 12 with BibEntry

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

the class ManageKeywordsAction method updateKeywords.

private NamedCompound updateKeywords(List<BibEntry> entries, KeywordList keywordsToAdd, KeywordList keywordsToRemove) {
    NamedCompound ce = new NamedCompound(Localization.lang("Update keywords"));
    for (BibEntry entry : entries) {
        KeywordList keywords = entry.getKeywords(Globals.prefs.getKeywordDelimiter());
        // update keywords
        keywords.removeAll(keywordsToRemove);
        keywords.addAll(keywordsToAdd);
        // put keywords back
        Optional<FieldChange> change = entry.putKeywords(keywords, Globals.prefs.getKeywordDelimiter());
        if (change.isPresent()) {
            ce.addEdit(new UndoableFieldChange(change.get()));
        }
        if (Globals.prefs.isKeywordSyncEnabled()) {
            SpecialFieldsUtils.syncSpecialFieldsFromKeywords(entry, Globals.prefs.getKeywordDelimiter());
        }
    }
    ce.end();
    return ce;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) NamedCompound(org.jabref.gui.undo.NamedCompound) KeywordList(org.jabref.model.entry.KeywordList) FieldChange(org.jabref.model.FieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange)

Example 13 with BibEntry

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

the class SaveDatabaseAction method saveDatabase.

private boolean saveDatabase(File file, boolean selectedOnly, Charset encoding) throws SaveException {
    SaveSession session;
    // block user input
    frame.block();
    try {
        SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs).withEncoding(encoding);
        BibtexDatabaseWriter<SaveSession> databaseWriter = new BibtexDatabaseWriter<>(FileSaveSession::new);
        if (selectedOnly) {
            session = databaseWriter.savePartOfDatabase(panel.getBibDatabaseContext(), panel.getSelectedEntries(), prefs);
        } else {
            session = databaseWriter.saveDatabase(panel.getBibDatabaseContext(), prefs);
        }
        panel.registerUndoableChanges(session);
    } catch (UnsupportedCharsetException ex) {
        JOptionPane.showMessageDialog(frame, Localization.lang("Could not save file.") + Localization.lang("Character encoding '%0' is not supported.", encoding.displayName()), Localization.lang("Save library"), JOptionPane.ERROR_MESSAGE);
        // FIXME: rethrow anti-pattern
        throw new SaveException("rt");
    } catch (SaveException ex) {
        if (ex == SaveException.FILE_LOCKED) {
            throw ex;
        }
        if (ex.specificEntry()) {
            BibEntry entry = ex.getEntry();
            // Error occured during processing of an entry. Highlight it!
            panel.highlightEntry(entry);
        } else {
            LOGGER.error("A problem occured when trying to save the file", ex);
        }
        JOptionPane.showMessageDialog(frame, Localization.lang("Could not save file.") + ".\n" + ex.getMessage(), Localization.lang("Save library"), JOptionPane.ERROR_MESSAGE);
        // FIXME: rethrow anti-pattern
        throw new SaveException("rt");
    } finally {
        // re-enable user input
        frame.unblock();
    }
    // handle encoding problems
    boolean success = true;
    if (!session.getWriter().couldEncodeAll()) {
        FormBuilder builder = FormBuilder.create().layout(new FormLayout("left:pref, 4dlu, fill:pref", "pref, 4dlu, pref"));
        JTextArea ta = new JTextArea(session.getWriter().getProblemCharacters());
        ta.setEditable(false);
        builder.add(Localization.lang("The chosen encoding '%0' could not encode the following characters:", session.getEncoding().displayName())).xy(1, 1);
        builder.add(ta).xy(3, 1);
        builder.add(Localization.lang("What do you want to do?")).xy(1, 3);
        String tryDiff = Localization.lang("Try different encoding");
        int answer = JOptionPane.showOptionDialog(frame, builder.getPanel(), Localization.lang("Save library"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[] { Localization.lang("Save"), tryDiff, Localization.lang("Cancel") }, tryDiff);
        if (answer == JOptionPane.NO_OPTION) {
            // The user wants to use another encoding.
            Object choice = JOptionPane.showInputDialog(frame, Localization.lang("Select encoding"), Localization.lang("Save library"), JOptionPane.QUESTION_MESSAGE, null, Encodings.ENCODINGS_DISPLAYNAMES, encoding);
            if (choice == null) {
                success = false;
            } else {
                Charset newEncoding = Charset.forName((String) choice);
                return saveDatabase(file, selectedOnly, newEncoding);
            }
        } else if (answer == JOptionPane.CANCEL_OPTION) {
            success = false;
        }
    }
    // backup file?
    try {
        if (success) {
            session.commit(file.toPath());
            // Make sure to remember which encoding we used.
            panel.getBibDatabaseContext().getMetaData().setEncoding(encoding, ChangePropagation.DO_NOT_POST_EVENT);
        } else {
            session.cancel();
        }
    } catch (SaveException e) {
        int ans = JOptionPane.showConfirmDialog(null, Localization.lang("Save failed during backup creation") + ". " + Localization.lang("Save without backup?"), Localization.lang("Unable to create backup"), JOptionPane.YES_NO_OPTION);
        if (ans == JOptionPane.YES_OPTION) {
            session.setUseBackup(false);
            session.commit(file.toPath());
            panel.getBibDatabaseContext().getMetaData().setEncoding(encoding, ChangePropagation.DO_NOT_POST_EVENT);
        } else {
            success = false;
        }
    }
    return success;
}
Also used : FormLayout(com.jgoodies.forms.layout.FormLayout) BibEntry(org.jabref.model.entry.BibEntry) FormBuilder(com.jgoodies.forms.builder.FormBuilder) SaveException(org.jabref.logic.exporter.SaveException) JTextArea(javax.swing.JTextArea) BibtexDatabaseWriter(org.jabref.logic.exporter.BibtexDatabaseWriter) FileSaveSession(org.jabref.logic.exporter.FileSaveSession) Charset(java.nio.charset.Charset) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) SavePreferences(org.jabref.logic.exporter.SavePreferences) SaveSession(org.jabref.logic.exporter.SaveSession) FileSaveSession(org.jabref.logic.exporter.FileSaveSession)

Example 14 with BibEntry

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

the class DocumentViewerViewModel method setCurrentEntries.

private void setCurrentEntries(List<BibEntry> entries) {
    if (!entries.isEmpty()) {
        BibEntry firstSelectedEntry = entries.get(0);
        setCurrentEntry(firstSelectedEntry);
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry)

Example 15 with BibEntry

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

the class EntryEditorTabRelatedArticles method setHtmlText.

/**
     * Takes a List of HTML snippets stored in the field "html_representation" of a list of bibentries and sets it in the JEditorPane
     *
     * @param list of bib entries having a field html_representation
     */
public void setHtmlText(List<BibEntry> list) {
    StringBuilder htmlContent = new StringBuilder();
    URL url = IconTheme.getIconUrl("mdlListIcon");
    htmlContent.append("<html><head><title></title></head><body bgcolor='#ffffff'>");
    htmlContent.append("<ul style='list-style-image:(");
    htmlContent.append(url);
    htmlContent.append(")'>");
    list.stream().map(bibEntry -> bibEntry.getField("html_representation")).filter(Optional::isPresent).map(o -> "<li style='margin: 5px'>" + o.get() + "</li>").forEach(html -> htmlContent.append(html));
    htmlContent.append("</ul>");
    htmlContent.append("<br><div style='margin-left: 5px'>");
    htmlContent.append("<a href='http://mr-dlib.org/information-for-users/information-about-mr-dlib-for-jabref-users/#'>");
    htmlContent.append(Localization.lang("What_is_Mr._DLib?"));
    htmlContent.append("</a></div>");
    htmlContent.append("</body></html>");
    this.setText(htmlContent.toString());
}
Also used : HyperlinkEvent(javax.swing.event.HyperlinkEvent) FieldName(org.jabref.model.entry.FieldName) URL(java.net.URL) BibEntry(org.jabref.model.entry.BibEntry) JabRefDesktop(org.jabref.gui.desktop.JabRefDesktop) MrDLibFetcher(org.jabref.logic.importer.fetcher.MrDLibFetcher) IconTheme(org.jabref.gui.IconTheme) IOException(java.io.IOException) JabRefPreferences(org.jabref.preferences.JabRefPreferences) Globals(org.jabref.Globals) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) JEditorPane(javax.swing.JEditorPane) Optional(java.util.Optional) SwingWorker(javax.swing.SwingWorker) Localization(org.jabref.logic.l10n.Localization) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) Optional(java.util.Optional) URL(java.net.URL)

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