Search in sources :

Example 1 with CallBack

use of org.jabref.gui.worker.CallBack in project jabref by JabRef.

the class BasePanel method runWorker.

public static void runWorker(AbstractWorker worker) throws Exception {
    // This part uses Spin's features:
    Runnable wrk = worker.getWorker();
    // The Worker returned by getWorker() has been wrapped
    // by Spin.off(), which makes its methods be run in
    // a different thread from the EDT.
    CallBack clb = worker.getCallBack();
    // This method runs in this same thread, the EDT.
    worker.init();
    // Useful for initial GUI actions, like printing a message.
    // The CallBack returned by getCallBack() has been wrapped
    // by Spin.over(), which makes its methods be run on
    // the EDT.
    // Runs the potentially time-consuming action
    wrk.run();
    // without freezing the GUI. The magic is that THIS line
    // of execution will not continue until run() is finished.
    // Runs the update() method on the EDT.
    clb.update();
}
Also used : CallBack(org.jabref.gui.worker.CallBack)

Example 2 with CallBack

use of org.jabref.gui.worker.CallBack 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

CallBack (org.jabref.gui.worker.CallBack)2 ArrayList (java.util.ArrayList)1 DuplicateResolverResult (org.jabref.gui.DuplicateResolverDialog.DuplicateResolverResult)1 NamedCompound (org.jabref.gui.undo.NamedCompound)1 UndoableInsertEntry (org.jabref.gui.undo.UndoableInsertEntry)1 UndoableRemoveEntry (org.jabref.gui.undo.UndoableRemoveEntry)1 BibEntry (org.jabref.model.entry.BibEntry)1