Search in sources :

Example 1 with UndoableInsertEntry

use of org.jabref.gui.undo.UndoableInsertEntry in project jabref by JabRef.

the class BasePanel method paste.

private void paste() {
    Collection<BibEntry> bes = new ClipBoardManager().extractBibEntriesFromClipboard();
    // or were parsed from a string
    if (!bes.isEmpty()) {
        NamedCompound ce = new NamedCompound((bes.size() > 1 ? Localization.lang("paste entries") : Localization.lang("paste entry")));
        // Store the first inserted bibtexentry.
        // bes[0] does not work as bes[0] is first clonded,
        // then inserted.
        // This entry is used to open up an entry editor
        // for the first inserted entry.
        BibEntry firstBE = null;
        for (BibEntry be1 : bes) {
            BibEntry be = (BibEntry) be1.clone();
            if (firstBE == null) {
                firstBE = be;
            }
            UpdateField.setAutomaticFields(be, Globals.prefs.getUpdateFieldPreferences());
            // We have to clone the
            // entries, since the pasted
            // entries must exist
            // independently of the copied
            // ones.
            bibDatabaseContext.getDatabase().insertEntry(be);
            ce.addEdit(new UndoableInsertEntry(bibDatabaseContext.getDatabase(), be, BasePanel.this));
        }
        ce.end();
        getUndoManager().addEdit(ce);
        output(formatOutputMessage(Localization.lang("Pasted"), bes.size()));
        markBaseChanged();
        highlightEntry(firstBE);
        mainTable.requestFocus();
        if (Globals.prefs.getBoolean(JabRefPreferences.AUTO_OPEN_FORM)) {
            selectionListener.editSignalled(firstBE);
        }
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) NamedCompound(org.jabref.gui.undo.NamedCompound) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry)

Example 2 with UndoableInsertEntry

use of org.jabref.gui.undo.UndoableInsertEntry 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 3 with UndoableInsertEntry

use of org.jabref.gui.undo.UndoableInsertEntry in project jabref by JabRef.

the class EntryAddChange method makeChange.

@Override
public boolean makeChange(BasePanel panel, BibDatabase secondary, NamedCompound undoEdit) {
    diskEntry.setId(IdGenerator.next());
    panel.getDatabase().insertEntry(diskEntry);
    secondary.insertEntry(diskEntry);
    undoEdit.addEdit(new UndoableInsertEntry(panel.getDatabase(), diskEntry, panel));
    return true;
}
Also used : UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry)

Example 4 with UndoableInsertEntry

use of org.jabref.gui.undo.UndoableInsertEntry in project jabref by JabRef.

the class DroppedFileHandler method tryXmpImport.

// Done by MrDlib
private boolean tryXmpImport(String fileName, ExternalFileType fileType, NamedCompound edits) {
    if (!"pdf".equals(fileType.getExtension())) {
        return false;
    }
    List<BibEntry> xmpEntriesInFile;
    try {
        xmpEntriesInFile = XMPUtil.readXMP(fileName, Globals.prefs.getXMPPreferences());
    } catch (IOException e) {
        LOGGER.warn("Problem reading XMP", e);
        return false;
    }
    if ((xmpEntriesInFile == null) || xmpEntriesInFile.isEmpty()) {
        return false;
    }
    JLabel confirmationMessage = new JLabel(Localization.lang("The PDF contains one or several BibTeX-records.") + "\n" + Localization.lang("Do you want to import these as new entries into the current library?"));
    JPanel entriesPanel = new JPanel();
    entriesPanel.setLayout(new BoxLayout(entriesPanel, BoxLayout.Y_AXIS));
    xmpEntriesInFile.forEach(entry -> {
        JTextArea entryArea = new JTextArea(entry.toString());
        entryArea.setEditable(false);
        entriesPanel.add(entryArea);
    });
    JPanel contentPanel = new JPanel(new BorderLayout());
    contentPanel.add(confirmationMessage, BorderLayout.NORTH);
    contentPanel.add(entriesPanel, BorderLayout.CENTER);
    int reply = JOptionPane.showConfirmDialog(frame, contentPanel, Localization.lang("XMP-metadata found in PDF: %0", fileName), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
    if (reply == JOptionPane.CANCEL_OPTION) {
        // The user canceled thus that we are done.
        return true;
    }
    if (reply == JOptionPane.NO_OPTION) {
        return false;
    }
    // reply == JOptionPane.YES_OPTION)
    /*
         * TODO Extract Import functionality from ImportMenuItem then we could
         * do:
         *
         * ImportMenuItem importer = new ImportMenuItem(frame, (mainTable ==
         * null), new PdfXmpImporter());
         *
         * importer.automatedImport(new String[] { fileName });
         */
    boolean isSingle = xmpEntriesInFile.size() == 1;
    BibEntry single = isSingle ? xmpEntriesInFile.get(0) : null;
    boolean success = true;
    String destFilename;
    if (linkInPlace.isSelected()) {
        destFilename = FileUtil.shortenFileName(Paths.get(fileName), panel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences())).toString();
    } else {
        if (renameCheckBox.isSelected() || (single == null)) {
            destFilename = fileName;
        } else {
            destFilename = single.getCiteKey() + "." + fileType.getExtension();
        }
        if (copyRadioButton.isSelected()) {
            success = doCopy(fileName, destFilename, edits);
        } else if (moveRadioButton.isSelected()) {
            success = doMove(fileName, destFilename, edits);
        }
    }
    if (success) {
        for (BibEntry aXmpEntriesInFile : xmpEntriesInFile) {
            aXmpEntriesInFile.setId(IdGenerator.next());
            edits.addEdit(new UndoableInsertEntry(panel.getDatabase(), aXmpEntriesInFile, panel));
            panel.getDatabase().insertEntry(aXmpEntriesInFile);
            doLink(aXmpEntriesInFile, fileType, destFilename, true, edits);
        }
        panel.markBaseChanged();
        panel.updateEntryEditorIfShowing();
    }
    return true;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) JPanel(javax.swing.JPanel) JTextArea(javax.swing.JTextArea) BorderLayout(java.awt.BorderLayout) BoxLayout(javax.swing.BoxLayout) JLabel(javax.swing.JLabel) IOException(java.io.IOException) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry)

Example 5 with UndoableInsertEntry

use of org.jabref.gui.undo.UndoableInsertEntry in project jabref by JabRef.

the class AppendDatabaseAction method mergeFromBibtex.

private static void mergeFromBibtex(BasePanel panel, ParserResult parserResult, boolean importEntries, boolean importStrings, boolean importGroups, boolean importSelectorWords) throws KeyCollisionException {
    BibDatabase fromDatabase = parserResult.getDatabase();
    List<BibEntry> appendedEntries = new ArrayList<>();
    List<BibEntry> originalEntries = new ArrayList<>();
    BibDatabase database = panel.getDatabase();
    NamedCompound ce = new NamedCompound(Localization.lang("Append library"));
    MetaData meta = parserResult.getMetaData();
    if (importEntries) {
        // Add entries
        boolean overwriteOwner = Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_OWNER);
        boolean overwriteTimeStamp = Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_TIME_STAMP);
        for (BibEntry originalEntry : fromDatabase.getEntries()) {
            BibEntry entry = (BibEntry) originalEntry.clone();
            UpdateField.setAutomaticFields(entry, overwriteOwner, overwriteTimeStamp, Globals.prefs.getUpdateFieldPreferences());
            database.insertEntry(entry);
            appendedEntries.add(entry);
            originalEntries.add(originalEntry);
            ce.addEdit(new UndoableInsertEntry(database, entry, panel));
        }
    }
    if (importStrings) {
        for (BibtexString bs : fromDatabase.getStringValues()) {
            if (!database.hasStringLabel(bs.getName())) {
                database.addString(bs);
                ce.addEdit(new UndoableInsertString(panel, database, bs));
            }
        }
    }
    if (importGroups) {
        meta.getGroups().ifPresent(newGroups -> {
            if (newGroups.getGroup() instanceof AllEntriesGroup) {
                try {
                    ExplicitGroup group = new ExplicitGroup("Imported", GroupHierarchyType.INDEPENDENT, Globals.prefs.getKeywordDelimiter());
                    newGroups.setGroup(group);
                    group.add(appendedEntries);
                } catch (IllegalArgumentException e) {
                    LOGGER.error(e);
                }
            }
            addGroups(newGroups, ce);
        });
    }
    if (importSelectorWords) {
        for (ContentSelector selector : meta.getContentSelectorList()) {
            panel.getBibDatabaseContext().getMetaData().addContentSelector(selector);
        }
    }
    ce.end();
    panel.getUndoManager().addEdit(ce);
    panel.markBaseChanged();
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ArrayList(java.util.ArrayList) BibtexString(org.jabref.model.entry.BibtexString) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry) ExplicitGroup(org.jabref.model.groups.ExplicitGroup) AllEntriesGroup(org.jabref.model.groups.AllEntriesGroup) UndoableInsertString(org.jabref.gui.undo.UndoableInsertString) NamedCompound(org.jabref.gui.undo.NamedCompound) MetaData(org.jabref.model.metadata.MetaData) ContentSelector(org.jabref.model.metadata.ContentSelector) BibDatabase(org.jabref.model.database.BibDatabase)

Aggregations

UndoableInsertEntry (org.jabref.gui.undo.UndoableInsertEntry)10 BibEntry (org.jabref.model.entry.BibEntry)8 ArrayList (java.util.ArrayList)4 NamedCompound (org.jabref.gui.undo.NamedCompound)4 KeyCollisionException (org.jabref.model.database.KeyCollisionException)3 UndoableRemoveEntry (org.jabref.gui.undo.UndoableRemoveEntry)2 EntryType (org.jabref.model.entry.EntryType)2 ButtonBarBuilder (com.jgoodies.forms.builder.ButtonBarBuilder)1 FormLayout (com.jgoodies.forms.layout.FormLayout)1 BorderLayout (java.awt.BorderLayout)1 File (java.io.File)1 IOException (java.io.IOException)1 BoxLayout (javax.swing.BoxLayout)1 JButton (javax.swing.JButton)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1 JSeparator (javax.swing.JSeparator)1 JTextArea (javax.swing.JTextArea)1 ChangeEvent (javax.swing.event.ChangeEvent)1 CompoundEdit (javax.swing.undo.CompoundEdit)1