Skip to content

Commit

Permalink
[feat] Implement mockup of ShortScience integration
Browse files Browse the repository at this point in the history
This commit adds a simple integration of ShortScience.org
in the form of an additional context menu action when an
entry is selected. This action opens the user's web browser
to the search page of ShortScience with the title of the
paper entered. Essentially, this is our MVP for the
integration towards ShortScience bar some testing.

[Issue: #3]
  • Loading branch information
stevensdavid committed Feb 25, 2020
1 parent cc94cca commit 531b7f3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
36 changes: 36 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,9 @@

import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -329,6 +332,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 +1205,37 @@ public void action() {
}
}

private class OpenShortScienceAction implements BaseAction {

@Override
public void action() {
final List<BibEntry> bes = mainTable.getSelectedEntries();
if (bes.size() == 1) {
Optional<String> title = bes.get(0).getField(StandardField.TITLE);
if (title.isPresent()) {
String link;
try {
link = String.format("https://www.shortscience.org/internalsearch?q=%s", URLEncoder.encode(title.get(), StandardCharsets.UTF_8.name()));
} catch (UnsupportedEncodingException ex) {
output(Localization.lang("Error") + ": " + ex.getMessage());
return;
}
try {
JabRefDesktop.openExternalViewer(bibDatabaseContext, link, StandardField.URL);
output(Localization.lang("External viewer called") + '.');
} catch (IOException ex) {
output(Localization.lang("Error") + ": " + ex.getMessage());
}
} else {
//No title entered, we shouldn't be able to get here
throw new AssertionError("OpenShortScienceAction called without title.");
}
} 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("Open ShortScience"), IconTheme.JabRefIcons.WWW),
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

0 comments on commit 531b7f3

Please sign in to comment.