Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ShortScience integration #6018

Merged
merged 18 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.StringReader;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -97,6 +98,7 @@
import org.jabref.preferences.JabRefPreferences;

import com.google.common.eventbus.Subscribe;
import org.apache.http.client.utils.URIBuilder;
import org.fxmisc.easybind.EasyBind;
import org.fxmisc.easybind.Subscription;
import org.slf4j.Logger;
Expand Down Expand Up @@ -329,6 +331,8 @@ private void setupActions() {

actions.put(Actions.OPEN_URL, new OpenURLAction());

actions.put(Actions.OPEN_SHORTSCIENCE, new OpenShortScienceAction());

actions.put(Actions.MERGE_WITH_FETCHED_ENTRY, new MergeWithFetchedEntryAction(this, frame.getDialogService()));

actions.put(Actions.REPLACE_ALL, () -> (new ReplaceStringAction(this)).execute());
Expand Down Expand Up @@ -1200,6 +1204,37 @@ public void action() {
}
}

private class OpenShortScienceAction implements BaseAction {
private static final String BASIC_SEARCH_URL = "https://www.shortscience.org/internalsearch";

@Override
public void action() {
final List<BibEntry> bes = mainTable.getSelectedEntries();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please implement a Fetcher (as the DOIFetcher or other fetchers do). --> Then, you can easily write test cases for the different shortscience urls etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is not a fetcher, or I'm mistaken?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really a fetcher as I've understood things, as it is essentially the same as a OpenURLAction with the exception that the link has to be generated. It doesn't fetch any information from the web, but instead opens the user's web browser.

if (bes.size() == 1) {
Optional<String> title = bes.get(0).getField(StandardField.TITLE);
if (title.isPresent()) {
URIBuilder uriBuilder;
try {
uriBuilder = new URIBuilder(BASIC_SEARCH_URL);
} catch (URISyntaxException e) {
// This should never be able to happen as it would require the field to be misconfigured.
throw new AssertionError("ShortScience URL is invalid.");
}
// Direct the user to the search results for the title.
uriBuilder.addParameter("q", title.get());
try {
JabRefDesktop.openExternalViewer(bibDatabaseContext, uriBuilder.toString(), StandardField.URL);
output(Localization.lang("External viewer called") + '.');
} catch (IOException ex) {
output(Localization.lang("Error") + ": " + ex.getMessage());
}
}
} else {
output(Localization.lang("This operation requires exactly one item to be selected."));
}
}
}

private class OpenURLAction implements BaseAction {

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/jabref/gui/actions/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public enum Actions {
OPEN_EXTERNAL_FILE,
OPEN_FOLDER,
OPEN_URL,
OPEN_SHORTSCIENCE,
PASTE,
PREVIOUS_PREVIEW_STYLE,
PULL_CHANGES_FROM_SHARED_DATABASE,
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/jabref/gui/actions/StandardActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum StandardActions implements Action {
SEND_AS_EMAIL(Localization.lang("Send as email"), IconTheme.JabRefIcons.EMAIL),
OPEN_EXTERNAL_FILE(Localization.lang("Open file"), IconTheme.JabRefIcons.FILE, KeyBinding.OPEN_FILE),
OPEN_URL(Localization.lang("Open URL or DOI"), IconTheme.JabRefIcons.WWW, KeyBinding.OPEN_URL_OR_DOI),
OPEN_SHORTSCIENCE(Localization.lang("Search ShortScience")),
stevensdavid marked this conversation as resolved.
Show resolved Hide resolved
MERGE_WITH_FETCHED_ENTRY(Localization.lang("Get BibTeX data from %0", "DOI/ISBN/...")),
ATTACH_FILE(Localization.lang("Attach file"), IconTheme.JabRefIcons.ATTACH_FILE),
PRIORITY(Localization.lang("Priority"), IconTheme.JabRefIcons.PRIORITY),
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/jabref/gui/maintable/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static ContextMenu create(BibEntryTableViewModel entry, KeyBindingReposit
contextMenu.getItems().add(factory.createMenuItem(StandardActions.OPEN_FOLDER, getOpenFolderCommand(panel)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.OPEN_EXTERNAL_FILE, getOpenExternalFileCommand(panel)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.OPEN_URL, getOpenUrlCommand(panel)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.OPEN_SHORTSCIENCE, getOpenShortScienceCommand(panel)));

contextMenu.getItems().add(new SeparatorMenuItem());

Expand Down Expand Up @@ -90,6 +91,12 @@ private static OldCommandWrapper getOpenUrlCommand(BasePanel panel) {
return command;
}

private static OldCommandWrapper getOpenShortScienceCommand(BasePanel panel) {
OldCommandWrapper command = new OldCommandWrapper(Actions.OPEN_SHORTSCIENCE, panel);
command.setExecutable(isFieldSetForSelectedEntry(StandardField.TITLE, panel));
return command;
}

private static OldCommandWrapper getOpenExternalFileCommand(BasePanel panel) {
OldCommandWrapper command = new OldCommandWrapper(Actions.OPEN_EXTERNAL_FILE, panel);
command.setExecutable(isFieldSetForSelectedEntry(StandardField.FILE, panel));
Expand Down