From 531b7f33e620ba4eb3a96a6eae86f9f64e3fe9ff Mon Sep 17 00:00:00 2001 From: David Stevens Date: Tue, 25 Feb 2020 13:48:24 +0100 Subject: [PATCH] [feat] Implement mockup of ShortScience integration 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] --- src/main/java/org/jabref/gui/BasePanel.java | 36 +++++++++++++++++++ .../java/org/jabref/gui/actions/Actions.java | 1 + .../jabref/gui/actions/StandardActions.java | 1 + .../jabref/gui/maintable/RightClickMenu.java | 7 ++++ 4 files changed, 45 insertions(+) diff --git a/src/main/java/org/jabref/gui/BasePanel.java b/src/main/java/org/jabref/gui/BasePanel.java index efec6542ab0..1925e62eed5 100644 --- a/src/main/java/org/jabref/gui/BasePanel.java +++ b/src/main/java/org/jabref/gui/BasePanel.java @@ -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; @@ -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()); @@ -1200,6 +1205,37 @@ public void action() { } } + private class OpenShortScienceAction implements BaseAction { + + @Override + public void action() { + final List bes = mainTable.getSelectedEntries(); + if (bes.size() == 1) { + Optional 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 diff --git a/src/main/java/org/jabref/gui/actions/Actions.java b/src/main/java/org/jabref/gui/actions/Actions.java index 49040b8e00b..3d462bb4c8f 100644 --- a/src/main/java/org/jabref/gui/actions/Actions.java +++ b/src/main/java/org/jabref/gui/actions/Actions.java @@ -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, diff --git a/src/main/java/org/jabref/gui/actions/StandardActions.java b/src/main/java/org/jabref/gui/actions/StandardActions.java index c1b7a2dfa7c..f9c978d8931 100644 --- a/src/main/java/org/jabref/gui/actions/StandardActions.java +++ b/src/main/java/org/jabref/gui/actions/StandardActions.java @@ -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), diff --git a/src/main/java/org/jabref/gui/maintable/RightClickMenu.java b/src/main/java/org/jabref/gui/maintable/RightClickMenu.java index 7029810a2fa..cf24697e5e5 100644 --- a/src/main/java/org/jabref/gui/maintable/RightClickMenu.java +++ b/src/main/java/org/jabref/gui/maintable/RightClickMenu.java @@ -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()); @@ -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));