Search in sources :

Example 26 with BibEntry

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

the class OOBibBase method generateDatabase.

public BibDatabase generateDatabase(List<BibDatabase> databases) throws NoSuchElementException, WrappedTargetException {
    BibDatabase resultDatabase = new BibDatabase();
    List<String> cited = findCitedKeys();
    // For each cited key
    for (String key : cited) {
        // Loop through the available databases
        for (BibDatabase loopDatabase : databases) {
            Optional<BibEntry> entry = loopDatabase.getEntryByKey(key);
            // If entry found
            if (entry.isPresent()) {
                BibEntry clonedEntry = (BibEntry) entry.get().clone();
                // Insert a copy of the entry
                resultDatabase.insertEntry(clonedEntry);
                // Check if the cloned entry has a crossref field
                clonedEntry.getField(FieldName.CROSSREF).ifPresent(crossref -> {
                    if (!resultDatabase.getEntryByKey(crossref).isPresent()) {
                        loopDatabase.getEntryByKey(crossref).ifPresent(resultDatabase::insertEntry);
                    }
                });
                // Be happy with the first found BibEntry and move on to next key
                break;
            }
        }
    }
    return resultDatabase;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) BibDatabase(org.jabref.model.database.BibDatabase)

Example 27 with BibEntry

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

the class OOBibBase method getSortedEntriesFromSortedRefMarks.

private Map<BibEntry, BibDatabase> getSortedEntriesFromSortedRefMarks(List<String> names, Map<String, BibDatabase> linkSourceBase) {
    Map<BibEntry, BibDatabase> newList = new LinkedHashMap<>();
    for (String name : names) {
        Matcher citeMatcher = CITE_PATTERN.matcher(name);
        if (citeMatcher.find()) {
            String[] keys = citeMatcher.group(2).split(",");
            for (String key : keys) {
                BibDatabase database = linkSourceBase.get(key);
                Optional<BibEntry> origEntry = Optional.empty();
                if (database != null) {
                    origEntry = database.getEntryByKey(key);
                }
                if (origEntry.isPresent()) {
                    if (!newList.containsKey(origEntry.get())) {
                        newList.put(origEntry.get(), database);
                    }
                } else {
                    LOGGER.info("BibTeX key not found: '" + key + "'");
                    LOGGER.info("Problem with reference mark: '" + name + "'");
                    newList.put(new UndefinedBibtexEntry(key), null);
                }
            }
        }
    }
    return newList;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) Matcher(java.util.regex.Matcher) UndefinedBibtexEntry(org.jabref.logic.openoffice.UndefinedBibtexEntry) BibDatabase(org.jabref.model.database.BibDatabase) LinkedHashMap(java.util.LinkedHashMap)

Example 28 with BibEntry

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

the class SearchResultFrame method init.

private void init(String title) {
    searchResultFrame = new JFrame();
    searchResultFrame.setTitle(title);
    searchResultFrame.setIconImages(IconTheme.getLogoSet());
    preview = new PreviewPanel(null, null);
    sortedEntries = new SortedList<>(entries, new EntryComparator(false, true, FieldName.AUTHOR));
    model = (DefaultEventTableModel<BibEntry>) GlazedListsSwing.eventTableModelWithThreadProxyList(sortedEntries, new EntryTableFormat());
    entryTable = new JTable(model);
    GeneralRenderer renderer = new GeneralRenderer(Color.white);
    entryTable.setDefaultRenderer(JLabel.class, renderer);
    entryTable.setDefaultRenderer(String.class, renderer);
    setWidths();
    TableComparatorChooser<BibEntry> tableSorter = TableComparatorChooser.install(entryTable, sortedEntries, AbstractTableComparatorChooser.MULTIPLE_COLUMN_KEYBOARD);
    setupComparatorChooser(tableSorter);
    JScrollPane sp = new JScrollPane(entryTable);
    final DefaultEventSelectionModel<BibEntry> selectionModel = (DefaultEventSelectionModel<BibEntry>) GlazedListsSwing.eventSelectionModelWithThreadProxyList(sortedEntries);
    entryTable.setSelectionModel(selectionModel);
    selectionModel.getSelected().addListEventListener(new EntrySelectionListener());
    entryTable.addMouseListener(new TableClickListener());
    contentPane.setTopComponent(sp);
    contentPane.setBottomComponent(preview);
    // Key bindings:
    AbstractAction closeAction = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    };
    ActionMap actionMap = contentPane.getActionMap();
    InputMap inputMap = contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.CLOSE_DIALOG), "close");
    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.CLOSE_DATABASE), "close");
    actionMap.put("close", closeAction);
    actionMap = entryTable.getActionMap();
    inputMap = entryTable.getInputMap();
    //Override 'selectNextColumnCell' and 'selectPreviousColumnCell' to move rows instead of cells on TAB
    actionMap.put("selectNextColumnCell", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            selectNextEntry();
        }
    });
    actionMap.put("selectPreviousColumnCell", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            selectPreviousEntry();
        }
    });
    actionMap.put("selectNextRow", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            selectNextEntry();
        }
    });
    actionMap.put("selectPreviousRow", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            selectPreviousEntry();
        }
    });
    String selectFirst = "selectFirst";
    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.SELECT_FIRST_ENTRY), selectFirst);
    actionMap.put(selectFirst, new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent event) {
            selectFirstEntry();
        }
    });
    String selectLast = "selectLast";
    inputMap.put(Globals.getKeyPrefs().getKey(KeyBinding.SELECT_LAST_ENTRY), selectLast);
    actionMap.put(selectLast, new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent event) {
            selectLastEntry();
        }
    });
    actionMap.put("copy", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (!selectionModel.getSelected().isEmpty()) {
                List<BibEntry> bes = selectionModel.getSelected();
                TransferableBibtexEntry trbe = new TransferableBibtexEntry(bes);
                // ! look at ClipBoardManager
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trbe, frame.getCurrentBasePanel());
                frame.output(Localization.lang("Copied") + ' ' + (bes.size() > 1 ? bes.size() + " " + Localization.lang("entries") : "1 " + Localization.lang("entry") + '.'));
            }
        }
    });
    // override standard enter-action; enter opens the selected entry
    entryTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter");
    actionMap.put("Enter", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            BibEntry entry = sortedEntries.get(entryTable.getSelectedRow());
            selectEntryInBasePanel(entry);
        }
    });
    searchResultFrame.addWindowListener(new WindowAdapter() {

        @Override
        public void windowOpened(WindowEvent e) {
            contentPane.setDividerLocation(0.5f);
        }

        @Override
        public void windowClosing(WindowEvent event) {
            dispose();
        }
    });
    searchResultFrame.getContentPane().add(contentPane, BorderLayout.CENTER);
    // Remember and default to last size:
    SearchPreferences searchPreferences = new SearchPreferences(Globals.prefs);
    searchResultFrame.setSize(searchPreferences.getSeachDialogWidth(), searchPreferences.getSeachDialogHeight());
    searchResultFrame.setLocation(searchPreferences.getSearchDialogPosX(), searchPreferences.getSearchDialogPosY());
    searchResultFrame.addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent e) {
            new SearchPreferences(Globals.prefs).setSearchDialogWidth(searchResultFrame.getSize().width).setSearchDialogHeight(searchResultFrame.getSize().height);
        }

        @Override
        public void componentMoved(ComponentEvent e) {
            new SearchPreferences(Globals.prefs).setSearchDialogPosX(searchResultFrame.getLocation().x).setSearchDialogPosY(searchResultFrame.getLocation().y);
        }
    });
}
Also used : ActionEvent(java.awt.event.ActionEvent) GeneralRenderer(org.jabref.gui.renderer.GeneralRenderer) WindowAdapter(java.awt.event.WindowAdapter) PreviewPanel(org.jabref.gui.PreviewPanel) JFrame(javax.swing.JFrame) SortedList(ca.odell.glazedlists.SortedList) List(java.util.List) BasicEventList(ca.odell.glazedlists.BasicEventList) EventList(ca.odell.glazedlists.EventList) AbstractAction(javax.swing.AbstractAction) ComponentAdapter(java.awt.event.ComponentAdapter) JScrollPane(javax.swing.JScrollPane) BibEntry(org.jabref.model.entry.BibEntry) ActionMap(javax.swing.ActionMap) TransferableBibtexEntry(org.jabref.gui.TransferableBibtexEntry) DefaultEventSelectionModel(ca.odell.glazedlists.swing.DefaultEventSelectionModel) SearchPreferences(org.jabref.preferences.SearchPreferences) EntryComparator(org.jabref.logic.bibtex.comparator.EntryComparator) JTable(javax.swing.JTable) WindowEvent(java.awt.event.WindowEvent) InputMap(javax.swing.InputMap) ComponentEvent(java.awt.event.ComponentEvent)

Example 29 with BibEntry

use of org.jabref.model.entry.BibEntry 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 30 with BibEntry

use of org.jabref.model.entry.BibEntry 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

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