Search in sources :

Example 6 with UndoableInsertEntry

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

the class EntryFromFileCreatorManager method addEntriesFromFiles.

/**
     * Tries to add a entry for each file in the List.
     *
     * @param files
     * @param database
     * @param panel
     * @param entryType
     * @param generateKeywordsFromPathToFile
     * @param changeListener
     * @param importGUIMessages list of unexpected import event - Messages including
     *         failures
     * @return Returns The number of entries added
     */
public int addEntriesFromFiles(List<File> files, BibDatabase database, BasePanel panel, EntryType entryType, boolean generateKeywordsFromPathToFile, ChangeListener changeListener, List<String> importGUIMessages) {
    int count = 0;
    CompoundEdit ce = new CompoundEdit();
    for (File f : files) {
        EntryFromFileCreator creator = getEntryCreator(f);
        if (creator == null) {
            importGUIMessages.add("Problem importing " + f.getPath() + ": Unknown filetype.");
        } else {
            Optional<BibEntry> entry = creator.createEntry(f, generateKeywordsFromPathToFile);
            if (!entry.isPresent()) {
                importGUIMessages.add("Problem importing " + f.getPath() + ": Entry could not be created.");
                continue;
            }
            if (entryType != null) {
                entry.get().setType(entryType);
            }
            if (entry.get().getId() == null) {
                entry.get().setId(IdGenerator.next());
            }
            /*
                 * TODO: database.insertEntry(BibEntry) is not sensible. Why
                 * does 'true' mean "There were duplicates", while 'false' means
                 * "Everything alright"?
                 */
            if (!database.containsEntryWithId(entry.get().getId())) {
                // Therefore, we only insert the entry if it is not already present
                if (database.insertEntry(entry.get())) {
                    importGUIMessages.add("Problem importing " + f.getPath() + ": Insert into BibDatabase failed.");
                } else {
                    count++;
                    if (panel != null) {
                        ce.addEdit(new UndoableInsertEntry(database, entry.get(), panel));
                    }
                }
            }
        }
        if (changeListener != null) {
            changeListener.stateChanged(new ChangeEvent(this));
        }
    }
    if ((count > 0) && (panel != null)) {
        ce.end();
        panel.getUndoManager().addEdit(ce);
    }
    return count;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ChangeEvent(javax.swing.event.ChangeEvent) CompoundEdit(javax.swing.undo.CompoundEdit) File(java.io.File) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry)

Example 7 with UndoableInsertEntry

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

the class MergeEntriesDialog method init.

/**
     * Sets up the dialog
     *
     * @param selected Selected BibtexEntries
     */
private void init(List<BibEntry> selected) {
    // Check if there are two entries selected
    if (selected.size() != 2) {
        // None selected. Inform the user to select entries first.
        JOptionPane.showMessageDialog(panel.frame(), Localization.lang("You have to choose exactly two entries to merge."), MERGE_ENTRIES, JOptionPane.INFORMATION_MESSAGE);
        this.dispose();
        return;
    }
    // Store the two entries
    BibEntry one = selected.get(0);
    BibEntry two = selected.get(1);
    MergeEntries mergeEntries = new MergeEntries(one, two, panel.getBibDatabaseContext().getMode());
    // Create undo-compound
    NamedCompound ce = new NamedCompound(MERGE_ENTRIES);
    FormLayout layout = new FormLayout("fill:700px:grow", "fill:400px:grow, 4px, p, 5px, p");
    this.setLayout(layout);
    this.add(mergeEntries.getMergeEntryPanel(), cc.xy(1, 1));
    this.add(new JSeparator(), cc.xy(1, 3));
    // Create buttons
    ButtonBarBuilder bb = new ButtonBarBuilder();
    bb.addGlue();
    JButton cancel = new JButton(Localization.lang("Cancel"));
    cancel.setActionCommand("cancel");
    cancel.addActionListener(e -> {
        panel.output(Localization.lang("Canceled merging entries"));
        dispose();
    });
    JButton replaceentries = new JButton(MERGE_ENTRIES);
    replaceentries.setActionCommand("replace");
    replaceentries.addActionListener(e -> {
        BibEntry mergedEntry = mergeEntries.getMergeEntry();
        panel.insertEntry(mergedEntry);
        ce.addEdit(new UndoableInsertEntry(panel.getDatabase(), mergedEntry, panel));
        ce.addEdit(new UndoableRemoveEntry(panel.getDatabase(), one, panel));
        panel.getDatabase().removeEntry(one);
        ce.addEdit(new UndoableRemoveEntry(panel.getDatabase(), two, panel));
        panel.getDatabase().removeEntry(two);
        ce.end();
        panel.getUndoManager().addEdit(ce);
        panel.output(Localization.lang("Merged entries"));
        dispose();
    });
    bb.addButton(new JButton[] { replaceentries, cancel });
    this.add(bb.getPanel(), cc.xy(1, 5));
    // Add some margin around the layout
    layout.appendRow(RowSpec.decode(MARGIN));
    layout.appendColumn(ColumnSpec.decode(MARGIN));
    layout.insertRow(1, RowSpec.decode(MARGIN));
    layout.insertColumn(1, ColumnSpec.decode(MARGIN));
    WindowLocation pw = new WindowLocation(this, JabRefPreferences.MERGEENTRIES_POS_X, JabRefPreferences.MERGEENTRIES_POS_Y, JabRefPreferences.MERGEENTRIES_SIZE_X, JabRefPreferences.MERGEENTRIES_SIZE_Y);
    pw.displayWindowAtStoredLocation();
    // Show what we've got
    setVisible(true);
}
Also used : FormLayout(com.jgoodies.forms.layout.FormLayout) BibEntry(org.jabref.model.entry.BibEntry) NamedCompound(org.jabref.gui.undo.NamedCompound) ButtonBarBuilder(com.jgoodies.forms.builder.ButtonBarBuilder) JButton(javax.swing.JButton) UndoableRemoveEntry(org.jabref.gui.undo.UndoableRemoveEntry) WindowLocation(org.jabref.gui.util.WindowLocation) JSeparator(javax.swing.JSeparator) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry)

Example 8 with UndoableInsertEntry

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

the class PdfImporter method createNewEntry.

private Optional<BibEntry> createNewEntry() {
    // Find out what type is desired
    EntryTypeDialog etd = new EntryTypeDialog(frame);
    // We want to center the dialog, to make it look nicer.
    etd.setLocationRelativeTo(frame);
    etd.setVisible(true);
    EntryType type = etd.getChoice();
    if (type != null) {
        // Only if the dialog was not canceled.
        final BibEntry bibEntry = new BibEntry(type.getName());
        try {
            panel.getDatabase().insertEntry(bibEntry);
            // Set owner/timestamp if options are enabled:
            List<BibEntry> list = new ArrayList<>();
            list.add(bibEntry);
            UpdateField.setAutomaticFields(list, true, true, Globals.prefs.getUpdateFieldPreferences());
            // Create an UndoableInsertEntry object.
            panel.getUndoManager().addEdit(new UndoableInsertEntry(panel.getDatabase(), bibEntry, panel));
            panel.output(Localization.lang("Added new") + " '" + type.getName().toLowerCase(Locale.ROOT) + "' " + Localization.lang("entry") + ".");
            // and adjustment of the splitter.
            if (panel.getMode() != BasePanelMode.SHOWING_EDITOR) {
                panel.setMode(BasePanelMode.WILL_SHOW_EDITOR);
            }
            SwingUtilities.invokeLater(() -> panel.showEntry(bibEntry));
            // The database just changed.
            panel.markBaseChanged();
            return Optional.of(bibEntry);
        } catch (KeyCollisionException ex) {
            LOGGER.info("Key collision occurred", ex);
        }
    }
    return Optional.empty();
}
Also used : KeyCollisionException(org.jabref.model.database.KeyCollisionException) BibEntry(org.jabref.model.entry.BibEntry) EntryType(org.jabref.model.entry.EntryType) EntryTypeDialog(org.jabref.gui.EntryTypeDialog) ArrayList(java.util.ArrayList) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry)

Example 9 with UndoableInsertEntry

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

the class BasePanel method insertEntry.

/**
     * This method is called from JabRefFrame when the user wants to create a new entry.
     *
     * @param bibEntry The new entry.
     */
public void insertEntry(final BibEntry bibEntry) {
    if (bibEntry != null) {
        try {
            bibDatabaseContext.getDatabase().insertEntry(bibEntry);
            if (Globals.prefs.getBoolean(JabRefPreferences.USE_OWNER)) {
                // Set owner field to default value
                UpdateField.setAutomaticFields(bibEntry, true, true, Globals.prefs.getUpdateFieldPreferences());
            }
            // Create an UndoableInsertEntry object.
            getUndoManager().addEdit(new UndoableInsertEntry(bibDatabaseContext.getDatabase(), bibEntry, BasePanel.this));
            output(Localization.lang("Added new '%0' entry.", bibEntry.getType()));
            // The database just changed.
            markBaseChanged();
            if (Globals.prefs.getBoolean(JabRefPreferences.AUTO_OPEN_FORM)) {
                selectionListener.editSignalled(bibEntry);
            }
            highlightEntry(bibEntry);
        } catch (KeyCollisionException ex) {
            LOGGER.info("Collision for bibtex key" + bibEntry.getId(), ex);
        }
    }
}
Also used : KeyCollisionException(org.jabref.model.database.KeyCollisionException) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry)

Example 10 with UndoableInsertEntry

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

the class DuplicateSearch method run.

@Override
public void run() {
    panel.output(Localization.lang("Searching for duplicates..."));
    bes = panel.getDatabase().getEntries();
    if (bes.size() < 2) {
        return;
    }
    SearcherRunnable st = new SearcherRunnable();
    JabRefExecutorService.INSTANCE.executeInterruptableTask(st, "DuplicateSearcher");
    int current = 0;
    final List<BibEntry> toRemove = new ArrayList<>();
    final List<BibEntry> toAdd = new ArrayList<>();
    int duplicateCounter = 0;
    boolean autoRemoveExactDuplicates = false;
    synchronized (duplicates) {
        while (!st.finished() || (current < duplicates.size())) {
            if (current >= duplicates.size()) {
                try {
                    duplicates.wait();
                } catch (InterruptedException ignored) {
                // Ignore
                }
            } else {
                // duplicates found
                List<BibEntry> be = duplicates.get(current);
                current++;
                if (!toRemove.contains(be.get(0)) && !toRemove.contains(be.get(1))) {
                    // Check if they are exact duplicates:
                    boolean askAboutExact = false;
                    if (DuplicateCheck.compareEntriesStrictly(be.get(0), be.get(1)) > 1) {
                        if (autoRemoveExactDuplicates) {
                            toRemove.add(be.get(1));
                            duplicateCounter++;
                            continue;
                        }
                        askAboutExact = true;
                    }
                    DuplicateCallBack cb = new DuplicateCallBack(JabRefGUI.getMainFrame(), be.get(0), be.get(1), askAboutExact ? DuplicateResolverType.DUPLICATE_SEARCH_WITH_EXACT : DuplicateResolverType.DUPLICATE_SEARCH);
                    ((CallBack) Spin.over(cb)).update();
                    duplicateCounter++;
                    DuplicateResolverResult answer = cb.getSelected();
                    if ((answer == DuplicateResolverResult.KEEP_LEFT) || (answer == DuplicateResolverResult.AUTOREMOVE_EXACT)) {
                        toRemove.add(be.get(1));
                        if (answer == DuplicateResolverResult.AUTOREMOVE_EXACT) {
                            // Remember choice
                            autoRemoveExactDuplicates = true;
                        }
                    } else if (answer == DuplicateResolverResult.KEEP_RIGHT) {
                        toRemove.add(be.get(0));
                    } else if (answer == DuplicateResolverResult.BREAK) {
                        // thread killing
                        st.setFinished();
                        current = Integer.MAX_VALUE;
                        // correct counter
                        duplicateCounter--;
                    } else if (answer == DuplicateResolverResult.KEEP_MERGE) {
                        toRemove.addAll(be);
                        toAdd.add(cb.getMergedEntry());
                    }
                }
            }
        }
    }
    final NamedCompound ce = new NamedCompound(Localization.lang("duplicate removal"));
    final int dupliC = duplicateCounter;
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            // Now, do the actual removal:
            if (!toRemove.isEmpty()) {
                for (BibEntry entry : toRemove) {
                    panel.getDatabase().removeEntry(entry);
                    ce.addEdit(new UndoableRemoveEntry(panel.getDatabase(), entry, panel));
                }
                panel.markBaseChanged();
            }
            // and adding merged entries:
            if (!toAdd.isEmpty()) {
                for (BibEntry entry : toAdd) {
                    panel.getDatabase().insertEntry(entry);
                    ce.addEdit(new UndoableInsertEntry(panel.getDatabase(), entry, panel));
                }
                panel.markBaseChanged();
            }
            synchronized (duplicates) {
                panel.output(Localization.lang("Duplicates found") + ": " + duplicates.size() + ' ' + Localization.lang("pairs processed") + ": " + dupliC);
            }
            ce.end();
            panel.getUndoManager().addEdit(ce);
        }
    });
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) CallBack(org.jabref.gui.worker.CallBack) ArrayList(java.util.ArrayList) UndoableRemoveEntry(org.jabref.gui.undo.UndoableRemoveEntry) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry) NamedCompound(org.jabref.gui.undo.NamedCompound) DuplicateResolverResult(org.jabref.gui.DuplicateResolverDialog.DuplicateResolverResult)

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