Skip to content

Commit 9fad2cc

Browse files
committed
feat: support searching via google scholar
1 parent 3c97e33 commit 9fad2cc

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

jabgui/src/main/java/org/jabref/gui/actions/StandardActions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public enum StandardActions implements Action {
3737
EXTRACT_FILE_REFERENCES_OFFLINE(Localization.lang("Extract references from file (offline)"), IconTheme.JabRefIcons.FILE_STAR),
3838
OPEN_URL(Localization.lang("Open URL or DOI"), IconTheme.JabRefIcons.WWW, KeyBinding.OPEN_URL_OR_DOI),
3939
SEARCH_SHORTSCIENCE(Localization.lang("Search ShortScience")),
40+
SEARCH_GOOGLE_SCHOLAR(Localization.lang("Search Google Scholar")),
4041
MERGE_WITH_FETCHED_ENTRY(Localization.lang("Get bibliographic data from %0", "DOI/ISBN/..."), KeyBinding.MERGE_WITH_FETCHED_ENTRY),
4142
ATTACH_FILE(Localization.lang("Attach file"), IconTheme.JabRefIcons.ATTACH_FILE),
4243
ATTACH_FILE_FROM_URL(Localization.lang("Attach file from URL"), IconTheme.JabRefIcons.DOWNLOAD_FILE),

jabgui/src/main/java/org/jabref/gui/maintable/RightClickMenu.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public static ContextMenu create(BibEntryTableViewModel entry,
102102

103103
factory.createMenuItem(StandardActions.OPEN_URL, new OpenUrlAction(dialogService, stateManager, preferences)),
104104
factory.createMenuItem(StandardActions.SEARCH_SHORTSCIENCE, new SearchShortScienceAction(dialogService, stateManager, preferences)),
105+
factory.createMenuItem(StandardActions.SEARCH_GOOGLE_SCHOLAR, new SearchGoogleScholarAction(dialogService, stateManager, preferences)),
105106

106107
new SeparatorMenuItem(),
107108

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.jabref.gui.maintable;
2+
3+
import java.io.IOException;
4+
import java.util.List;
5+
6+
import org.jabref.gui.DialogService;
7+
import org.jabref.gui.StateManager;
8+
import static org.jabref.gui.actions.ActionHelper.isFieldSetForSelectedEntry;
9+
import static org.jabref.gui.actions.ActionHelper.needsEntriesSelected;
10+
import org.jabref.gui.actions.SimpleCommand;
11+
import org.jabref.gui.desktop.os.NativeDesktop;
12+
import org.jabref.gui.preferences.GuiPreferences;
13+
import org.jabref.logic.l10n.Localization;
14+
import org.jabref.logic.util.ExternalLinkCreator;
15+
import org.jabref.model.entry.BibEntry;
16+
import org.jabref.model.entry.field.StandardField;
17+
18+
import javafx.beans.binding.BooleanExpression;
19+
20+
public class SearchGoogleScholarAction extends SimpleCommand {
21+
private final DialogService dialogService;
22+
private final StateManager stateManager;
23+
private final GuiPreferences preferences;
24+
25+
public SearchGoogleScholarAction(DialogService dialogService, StateManager stateManager, GuiPreferences preferences) {
26+
this.dialogService = dialogService;
27+
this.stateManager = stateManager;
28+
this.preferences = preferences;
29+
30+
BooleanExpression fieldIsSet = isFieldSetForSelectedEntry(StandardField.TITLE, stateManager);
31+
this.executable.bind(needsEntriesSelected(1, stateManager).and(fieldIsSet));
32+
}
33+
34+
@Override
35+
public void execute() {
36+
stateManager.getActiveDatabase().ifPresent(databaseContext -> {
37+
final List<BibEntry> bibEntries = stateManager.getSelectedEntries();
38+
39+
if (bibEntries.size() != 1) {
40+
dialogService.notify(Localization.lang("This operation requires exactly one item to be selected."));
41+
return;
42+
}
43+
ExternalLinkCreator.getGoogleScholarSearchURL(bibEntries.getFirst()).ifPresent(url -> {
44+
try {
45+
NativeDesktop.openExternalViewer(databaseContext, preferences, url, StandardField.URL, dialogService, bibEntries.getFirst());
46+
} catch (IOException ex) {
47+
dialogService.showErrorDialogAndWait(Localization.lang("Unable to open Google Scholar."), ex);
48+
}
49+
});
50+
});
51+
}
52+
}

jablib/src/main/java/org/jabref/logic/util/ExternalLinkCreator.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
public class ExternalLinkCreator {
1212
private static final String SHORTSCIENCE_SEARCH_URL = "https://www.shortscience.org/internalsearch";
13+
private static final String GOOGLE_SCHOLAR_SEARCH_URL = "https://scholar.google.com/scholar";
1314

1415
/**
1516
* Get a URL to the search results of ShortScience for the BibEntry's title
@@ -31,4 +32,25 @@ public static Optional<String> getShortScienceSearchURL(BibEntry entry) {
3132
return uriBuilder.toString();
3233
});
3334
}
35+
36+
/**
37+
* Get a URL to the search results of Google Scholar for the BibEntry's title
38+
*
39+
* @param entry The entry to search for. Expects the BibEntry's title to be set for successful return.
40+
* @return The URL if it was successfully created
41+
*/
42+
public static Optional<String> getGoogleScholarSearchURL(BibEntry entry) {
43+
return entry.getField(StandardField.TITLE).map(title -> {
44+
URIBuilder uriBuilder;
45+
try {
46+
uriBuilder = new URIBuilder(GOOGLE_SCHOLAR_SEARCH_URL);
47+
} catch (URISyntaxException e) {
48+
// This should never be able to happen as it would require the field to be misconfigured.
49+
throw new AssertionError("Google Scholar URL is invalid.", e);
50+
}
51+
// Direct the user to the search results for the title.
52+
uriBuilder.addParameter("q", title);
53+
return uriBuilder.toString();
54+
});
55+
}
3456
}

jablib/src/main/resources/l10n/JabRef_en.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,6 +2198,9 @@ Text\ editor=Text editor
21982198
Search\ ShortScience=Search ShortScience
21992199
Unable\ to\ open\ ShortScience.=Unable to open ShortScience.
22002200
2201+
Search\ Google\ Scholar=Search Google Scholar
2202+
Unable\ to\ open\ Google\ Scholar.=Unable to open Google Scholar.
2203+
22012204
Shared\ database=Shared database
22022205
Lookup=Lookup
22032206

0 commit comments

Comments
 (0)