Skip to content

Commit

Permalink
refactoring, extension
Browse files Browse the repository at this point in the history
  • Loading branch information
systemoperator committed Feb 24, 2020
1 parent 32d7400 commit 4325489
Show file tree
Hide file tree
Showing 6 changed files with 370 additions and 169 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
import org.jabref.gui.protectedterms.ManageProtectedTermsAction;
import org.jabref.gui.push.PushToApplicationAction;
import org.jabref.gui.push.PushToApplicationsManager;
import org.jabref.gui.referencemetadata.FetchReferenceMetadata;
import org.jabref.gui.referencemetadata.ReferenceMetadataFetcher;
import org.jabref.gui.search.GlobalSearchBar;
import org.jabref.gui.shared.ConnectToSharedDatabaseCommand;
import org.jabref.gui.specialfields.SpecialFieldMenuItemFactory;
Expand Down Expand Up @@ -784,7 +784,7 @@ private MenuBar createMenu() {
lookupIdentifiers,
factory.createMenuItem(StandardActions.DOWNLOAD_FULL_TEXT, new OldDatabaseCommandWrapper(Actions.DOWNLOAD_FULL_TEXT, this, stateManager)),

factory.createMenuItem(StandardActions.FETCH_REFERENCE_METADATA, new FetchReferenceMetadata(this, prefs, stateManager, undoManager, Globals.TASK_EXECUTOR)),
factory.createMenuItem(StandardActions.FETCH_REFERENCE_METADATA, new ReferenceMetadataFetcher(this, prefs, stateManager, undoManager, Globals.TASK_EXECUTOR)),

new SeparatorMenuItem(),

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.jabref.gui.referencemetadata;

import java.util.ArrayList;
import java.util.List;

import javax.swing.undo.UndoManager;

import javafx.collections.ObservableList;
import javafx.concurrent.Task;

import org.jabref.gui.DialogService;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.util.BindingsHelper;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.preferences.JabRefPreferences;

import static org.jabref.gui.actions.ActionHelper.needsDatabase;
import static org.jabref.gui.actions.ActionHelper.needsEntriesSelected;

/**
* This class allows fetching the reference metadata (e.g. citation counts) from the web for the currently selected entries in a library.
*/
public class ReferenceMetadataFetcher extends SimpleCommand {

private static boolean USE_REFERENCE_METADATA_FETCHER_GOOGLE_SCHOLAR = true;

private final DialogService dialogService;
private final JabRefPreferences preferences;
private final StateManager stateManager;
private UndoManager undoManager;
private TaskExecutor taskExecutor;

public ReferenceMetadataFetcher(JabRefFrame frame, JabRefPreferences preferences, StateManager stateManager, UndoManager undoManager, TaskExecutor taskExecutor) {
this.dialogService = frame.getDialogService();
this.preferences = preferences;
this.stateManager = stateManager;
this.undoManager = undoManager;
this.taskExecutor = taskExecutor;

this.executable.bind(needsDatabase(this.stateManager).and(needsEntriesSelected(stateManager)));
this.statusMessage.bind(BindingsHelper.ifThenElse(executable, Localization.lang("This operation fetches reference metadata for the currently selected entries online."), Localization.lang("This operation requires one or more entries to be selected.")));
}

@Override
public void execute() {
BibDatabaseContext database = stateManager.getActiveDatabase().orElseThrow(() -> new NullPointerException("Database null"));
ObservableList<BibEntry> entries = stateManager.getSelectedEntries();

final NamedCompound nc = new NamedCompound(Localization.lang("Fetch reference metadata"));

Task<List<BibEntry>> fetchReferenceMetadataTask = new Task<List<BibEntry>>() {

@Override
protected List<BibEntry> call() {

if (USE_REFERENCE_METADATA_FETCHER_GOOGLE_SCHOLAR) {
ReferenceMetadataFetcherGoogleScholar rmfgs = new ReferenceMetadataFetcherGoogleScholar();
rmfgs.fetchFor(database, entries, dialogService);
}

return entries;
}

@Override
protected void succeeded() {
if (!getValue().isEmpty()) {
if (nc.hasEdits()) {
nc.end();
undoManager.addEdit(nc);
}
dialogService.notify(Localization.lang("Finished fetching reference metadata."));
} else {
dialogService.notify(Localization.lang("Finished fetching reference metadata.") + " " + Localization.lang("There was nothing to do."));
}
}
};

dialogService.showProgressDialogAndWait(
Localization.lang("Fetching reference metadata online"),
Localization.lang("Querying reference metadata..."),
fetchReferenceMetadataTask);
taskExecutor.execute(fetchReferenceMetadataTask);
}
}
Loading

0 comments on commit 4325489

Please sign in to comment.