Search in sources :

Example 21 with NamedCompound

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

the class FileLinksUpgradeWarning method makeChanges.

/**
     * This method performs the actual changes.
     * @param panel
     * @param pr
     * @param fileDir The path to the file directory to set, or null if it should not be set.
     */
private void makeChanges(BasePanel panel, ParserResult pr, boolean upgradePrefs, boolean upgradeDatabase, String fileDir) {
    if (upgradeDatabase) {
        // Update file links links in the database:
        NamedCompound ce = upgradePdfPsToFile(pr.getDatabase());
        panel.getUndoManager().addEdit(ce);
        panel.markBaseChanged();
    }
    if (fileDir != null) {
        Globals.prefs.put(FieldName.FILE + FileDirectoryPreferences.DIR_SUFFIX, fileDir);
    }
    if (upgradePrefs) {
        // Exchange table columns:
        Globals.prefs.putBoolean(JabRefPreferences.FILE_COLUMN, Boolean.TRUE);
        // If we don't find the file field, insert it at the bottom of the first tab:
        if (!showsFileInGenFields()) {
            String gfs = Globals.prefs.get(JabRefPreferences.CUSTOM_TAB_FIELDS + "0");
            StringBuilder sb = new StringBuilder(gfs);
            if (!gfs.isEmpty()) {
                sb.append(';');
            }
            sb.append(FieldName.FILE);
            Globals.prefs.put(JabRefPreferences.CUSTOM_TAB_FIELDS + "0", sb.toString());
            Globals.prefs.updateEntryEditorTabList();
        }
        panel.frame().setupAllTables();
    }
}
Also used : NamedCompound(org.jabref.gui.undo.NamedCompound)

Example 22 with NamedCompound

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

the class BasePanel method changeType.

private void changeType(List<BibEntry> entries, String newType) {
    if ((entries == null) || (entries.isEmpty())) {
        LOGGER.error("At least one entry must be selected to be able to change the type.");
        return;
    }
    if (entries.size() > 1) {
        int choice = JOptionPane.showConfirmDialog(this, Localization.lang("Multiple entries selected. Do you want to change the type of all these to '%0'?", newType), Localization.lang("Change entry type"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
        if (choice == JOptionPane.NO_OPTION) {
            return;
        }
    }
    NamedCompound compound = new NamedCompound(Localization.lang("Change entry type"));
    for (BibEntry entry : entries) {
        compound.addEdit(new UndoableChangeType(entry, entry.getType(), newType));
        entry.setType(newType);
    }
    output(formatOutputMessage(Localization.lang("Changed type to '%0' for", newType), entries.size()));
    compound.end();
    getUndoManager().addEdit(compound);
    markBaseChanged();
    updateEntryEditorIfShowing();
}
Also used : UndoableChangeType(org.jabref.gui.undo.UndoableChangeType) BibEntry(org.jabref.model.entry.BibEntry) NamedCompound(org.jabref.gui.undo.NamedCompound)

Example 23 with NamedCompound

use of org.jabref.gui.undo.NamedCompound 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)

Example 24 with NamedCompound

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

the class ManageKeywordsAction method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    BasePanel bp = frame.getCurrentBasePanel();
    if (bp == null) {
        return;
    }
    if (bp.getSelectedEntries().isEmpty()) {
        bp.output(Localization.lang("Select at least one entry to manage keywords."));
        return;
    }
    // Lazy creation of the dialog:
    createDialog();
    canceled = true;
    fillKeyWordList();
    diag.pack();
    diag.setLocationRelativeTo(frame);
    diag.setVisible(true);
    if (canceled) {
        return;
    }
    KeywordList keywordsToAdd = new KeywordList();
    KeywordList userSelectedKeywords = new KeywordList();
    // build keywordsToAdd and userSelectedKeywords in parallel
    for (Enumeration<Keyword> keywords = keywordListModel.elements(); keywords.hasMoreElements(); ) {
        Keyword keyword = keywords.nextElement();
        userSelectedKeywords.add(keyword);
        if (!sortedKeywordsOfAllEntriesBeforeUpdateByUser.contains(keyword)) {
            keywordsToAdd.add(keyword);
        }
    }
    KeywordList keywordsToRemove = new KeywordList();
    for (Keyword kword : sortedKeywordsOfAllEntriesBeforeUpdateByUser) {
        if (!userSelectedKeywords.contains(kword)) {
            keywordsToRemove.add(kword);
        }
    }
    if (keywordsToAdd.isEmpty() && keywordsToRemove.isEmpty()) {
        // nothing to be done if nothing is new and nothing is obsolete
        return;
    }
    if (Globals.prefs.isKeywordSyncEnabled() && !keywordsToAdd.isEmpty()) {
        SpecialFieldsUtils.synchronizeSpecialFields(keywordsToAdd, keywordsToRemove);
    }
    NamedCompound ce = updateKeywords(bp.getSelectedEntries(), keywordsToAdd, keywordsToRemove);
    bp.getUndoManager().addEdit(ce);
    bp.markBaseChanged();
}
Also used : BasePanel(org.jabref.gui.BasePanel) Keyword(org.jabref.model.entry.Keyword) NamedCompound(org.jabref.gui.undo.NamedCompound) KeywordList(org.jabref.model.entry.KeywordList)

Example 25 with NamedCompound

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

the class MassSetFieldAction method massRenameField.

/**
     * Move contents from one field to another for a Collection of entries.
     *
     * @param entries         The entries to do this operation for.
     * @param field           The field to move contents from.
     * @param newField        The field to move contents into.
     * @param overwriteValues If true, overwrites any existing values in the new field. If false, makes no change for
     *                        entries with existing value in the new field.
     * @return A CompoundEdit for the entire operation.
     */
private static UndoableEdit massRenameField(Collection<BibEntry> entries, String field, String newField, boolean overwriteValues) {
    NamedCompound ce = new NamedCompound(Localization.lang("Rename field"));
    for (BibEntry entry : entries) {
        Optional<String> valToMove = entry.getField(field);
        // If there is no value, do nothing:
        if ((!valToMove.isPresent()) || valToMove.get().isEmpty()) {
            continue;
        }
        // If we are not allowed to overwrite values, check if there is a
        // non-empty value already for this entry for the new field:
        Optional<String> valInNewField = entry.getField(newField);
        if (!overwriteValues && (valInNewField.isPresent()) && !valInNewField.get().isEmpty()) {
            continue;
        }
        entry.setField(newField, valToMove.get());
        ce.addEdit(new UndoableFieldChange(entry, newField, valInNewField.orElse(null), valToMove.get()));
        entry.clearField(field);
        ce.addEdit(new UndoableFieldChange(entry, field, valToMove.get(), null));
    }
    ce.end();
    return ce;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) NamedCompound(org.jabref.gui.undo.NamedCompound) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange)

Aggregations

NamedCompound (org.jabref.gui.undo.NamedCompound)34 BibEntry (org.jabref.model.entry.BibEntry)27 UndoableFieldChange (org.jabref.gui.undo.UndoableFieldChange)11 FieldChange (org.jabref.model.FieldChange)7 ArrayList (java.util.ArrayList)5 BasePanel (org.jabref.gui.BasePanel)5 UndoableInsertEntry (org.jabref.gui.undo.UndoableInsertEntry)4 UndoableKeyChange (org.jabref.gui.undo.UndoableKeyChange)4 UndoableRemoveEntry (org.jabref.gui.undo.UndoableRemoveEntry)3 ButtonBarBuilder (com.jgoodies.forms.builder.ButtonBarBuilder)2 FormLayout (com.jgoodies.forms.layout.FormLayout)2 IOException (java.io.IOException)2 List (java.util.List)2 JButton (javax.swing.JButton)2 JSeparator (javax.swing.JSeparator)2 ExternalFileType (org.jabref.gui.externalfiletype.ExternalFileType)2 UndoableChangeType (org.jabref.gui.undo.UndoableChangeType)2 WindowLocation (org.jabref.gui.util.WindowLocation)2 CheckBoxMessage (org.jabref.gui.util.component.CheckBoxMessage)2 ParserResult (org.jabref.logic.importer.ParserResult)2