Skip to content

Commit

Permalink
Custom DOI base address fix (#7569)
Browse files Browse the repository at this point in the history
* using context menu and keyboard shortcut now correctly uses custom DOI

* clicking linked identifier symbol now correctly uses custom DOI

* updated changelog

* removed unneccesary check from LinkedIdentifierColumn since it is already handled by OpenUrlAction

* refactored openCustomDoi into static method of JabRefDesktop
  • Loading branch information
BJaroszkowski authored Apr 8, 2021
1 parent 6837164 commit bc2fc07
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 34 deletions.
21 changes: 18 additions & 3 deletions src/main/java/org/jabref/gui/desktop/JabRefDesktop.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Optional;
import java.util.regex.Pattern;

import org.jabref.gui.DialogService;
import org.jabref.gui.Globals;
import org.jabref.gui.JabRefGUI;
import org.jabref.gui.desktop.os.DefaultDesktop;
Expand All @@ -19,6 +20,7 @@
import org.jabref.gui.desktop.os.Windows;
import org.jabref.gui.externalfiletype.ExternalFileType;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.logic.importer.util.IdentifierParser;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.OS;
import org.jabref.model.database.BibDatabaseContext;
Expand All @@ -27,6 +29,7 @@
import org.jabref.model.entry.identifier.DOI;
import org.jabref.model.entry.identifier.Eprint;
import org.jabref.model.util.FileHelper;
import org.jabref.preferences.PreferencesService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -107,6 +110,19 @@ private static void openDoi(String doi) throws IOException {
openBrowser(link);
}

public static void openCustomDoi(String link, PreferencesService preferences, DialogService dialogService) {
IdentifierParser.parse(StandardField.DOI, link)
.map(identifier -> (DOI) identifier)
.flatMap(doi -> doi.getExternalURIWithCustomBase(preferences.getDOIPreferences().getDefaultBaseURI()))
.ifPresent(uri -> {
try {
JabRefDesktop.openBrowser(uri);
} catch (IOException e) {
dialogService.showErrorDialogAndWait(Localization.lang("Unable to open link."), e);
}
});
}

/**
* Open an external file, attempting to use the correct viewer for it.
*
Expand Down Expand Up @@ -203,9 +219,8 @@ public static void openBrowser(URI url) throws IOException {
}

/**
* Opens the url with the users standard Browser.
* If that fails a popup will be shown to instruct the user to open the link manually
* and the link gets copied to the clipboard
* Opens the url with the users standard Browser. If that fails a popup will be shown to instruct the user to open the link manually and the link gets copied to the clipboard
*
* @param url the URL to open
*/
public static void openBrowserShowPopup(String url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class IdentifierEditorViewModel extends AbstractEditorViewModel {
private final TaskExecutor taskExecutor;
private final DialogService dialogService;
private final Field field;
private PreferencesService preferences;
private final PreferencesService preferences;

public IdentifierEditorViewModel(Field field, SuggestionProvider<?> suggestionProvider, TaskExecutor taskExecutor, DialogService dialogService, FieldCheckers fieldCheckers, PreferencesService preferences) {
super(field, suggestionProvider, fieldCheckers);
Expand Down Expand Up @@ -75,8 +75,8 @@ public BooleanProperty validIdentifierIsNotPresentProperty() {

public void openExternalLink() {
if (field.equals(StandardField.DOI) && preferences.getDOIPreferences().isUseCustom()) {
String baseURI = preferences.getDOIPreferences().getDefaultBaseURI();
openDOIWithCustomBase(baseURI);
identifier.get().map(identifier -> (DOI) identifier).map(DOI::getDOI)
.ifPresent(s -> JabRefDesktop.openCustomDoi(s, preferences, dialogService));
} else {
openExternalLinkDefault();
}
Expand All @@ -94,18 +94,6 @@ public void openExternalLinkDefault() {
);
}

public void openDOIWithCustomBase(String baseURI) {
identifier.get().map(identifier -> (DOI) identifier).flatMap(doi -> doi.getExternalURIWithCustomBase(baseURI)).ifPresent(
uri -> {
try {
JabRefDesktop.openBrowser(uri);
} catch (IOException ex) {
dialogService.showErrorDialogAndWait(Localization.lang("Unable to open link."), ex);
}
}
);
}

public boolean getIdentifierLookupInProgress() {
return identifierLookupInProgress.get();
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public MainTable(MainTableDataModel model,
preferencesService,
externalFileTypes,
libraryTab.getUndoManager(),
dialogService).createColumns());
dialogService,
stateManager).createColumns());

new ViewModelTableRowFactory<BibEntryTableViewModel>()
.withOnMouseClickedEvent((entry, event) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javafx.scene.text.Text;

import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.maintable.columns.FieldColumn;
Expand Down Expand Up @@ -57,19 +58,22 @@ public class MainTableColumnFactory {
private final CellFactory cellFactory;
private final UndoManager undoManager;
private final DialogService dialogService;
private final StateManager stateManager;

public MainTableColumnFactory(BibDatabaseContext database,
PreferencesService preferencesService,
ExternalFileTypes externalFileTypes,
UndoManager undoManager,
DialogService dialogService) {
DialogService dialogService,
StateManager stateManager) {
this.database = Objects.requireNonNull(database);
this.preferencesService = Objects.requireNonNull(preferencesService);
this.columnPreferences = preferencesService.getColumnPreferences();
this.externalFileTypes = Objects.requireNonNull(externalFileTypes);
this.dialogService = dialogService;
this.cellFactory = new CellFactory(externalFileTypes, preferencesService, undoManager);
this.undoManager = undoManager;
this.stateManager = stateManager;
}

public List<TableColumn<BibEntryTableViewModel, ?>> createColumns() {
Expand Down Expand Up @@ -207,7 +211,7 @@ private Node createGroupColorRegion(BibEntryTableViewModel entry, List<AbstractG
* Creates a clickable icons column for DOIs, URLs, URIs and EPrints.
*/
private TableColumn<BibEntryTableViewModel, Map<Field, String>> createIdentifierColumn(MainTableColumnModel columnModel) {
return new LinkedIdentifierColumn(columnModel, cellFactory, database, dialogService);
return new LinkedIdentifierColumn(columnModel, cellFactory, database, dialogService, preferencesService, stateManager);
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/org/jabref/gui/maintable/OpenUrlAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.StandardField;
import org.jabref.preferences.PreferencesService;

public class OpenUrlAction extends SimpleCommand {

private final DialogService dialogService;
private final StateManager stateManager;
private final PreferencesService preferences;

public OpenUrlAction(DialogService dialogService, StateManager stateManager) {
public OpenUrlAction(DialogService dialogService, StateManager stateManager, PreferencesService preferences) {
this.dialogService = dialogService;
this.stateManager = stateManager;
this.preferences = preferences;

BooleanExpression fieldIsSet = ActionHelper.isAnyFieldSetForSelectedEntry(
List.of(StandardField.URL, StandardField.DOI, StandardField.URI, StandardField.EPRINT),
Expand Down Expand Up @@ -62,7 +65,11 @@ public void execute() {

if (link.isPresent()) {
try {
JabRefDesktop.openExternalViewer(databaseContext, link.get(), field);
if (field.equals(StandardField.DOI) && preferences.getDOIPreferences().isUseCustom()) {
JabRefDesktop.openCustomDoi(link.get(), preferences, dialogService);
} else {
JabRefDesktop.openExternalViewer(databaseContext, link.get(), field);
}
} catch (IOException e) {
dialogService.showErrorDialogAndWait(Localization.lang("Unable to open link."), e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/maintable/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static ContextMenu create(BibEntryTableViewModel entry,

contextMenu.getItems().add(factory.createMenuItem(StandardActions.OPEN_FOLDER, new OpenFolderAction(dialogService, stateManager, preferencesService)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.OPEN_EXTERNAL_FILE, new OpenExternalFileAction(dialogService, stateManager, preferencesService)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.OPEN_URL, new OpenUrlAction(dialogService, stateManager)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.OPEN_URL, new OpenUrlAction(dialogService, stateManager, preferencesService)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.SEARCH_SHORTSCIENCE, new SearchShortScienceAction(dialogService, stateManager)));

contextMenu.getItems().add(new SeparatorMenuItem());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@
import javafx.scene.input.MouseButton;

import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
import org.jabref.gui.desktop.JabRefDesktop;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.maintable.BibEntryTableViewModel;
import org.jabref.gui.maintable.CellFactory;
import org.jabref.gui.maintable.ColumnPreferences;
import org.jabref.gui.maintable.MainTableColumnFactory;
import org.jabref.gui.maintable.MainTableColumnModel;
import org.jabref.gui.maintable.OpenUrlAction;
import org.jabref.gui.util.ControlHelper;
import org.jabref.gui.util.ValueTableCellFactory;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.field.Field;
import org.jabref.preferences.PreferencesService;

/**
* A clickable icons column for DOIs, URLs, URIs and EPrints.
Expand All @@ -31,15 +34,21 @@ public class LinkedIdentifierColumn extends MainTableColumn<Map<Field, String>>
private final BibDatabaseContext database;
private final CellFactory cellFactory;
private final DialogService dialogService;
private final PreferencesService preferences;
private final StateManager stateManager;

public LinkedIdentifierColumn(MainTableColumnModel model,
CellFactory cellFactory,
BibDatabaseContext database,
DialogService dialogService) {
DialogService dialogService,
PreferencesService preferences,
StateManager stateManager) {
super(model);
this.database = database;
this.cellFactory = cellFactory;
this.dialogService = dialogService;
this.preferences = preferences;
this.stateManager = stateManager;

Node headerGraphic = IconTheme.JabRefIcons.WWW.getGraphicNode();
Tooltip.install(headerGraphic, new Tooltip(Localization.lang("Linked identifiers")));
Expand All @@ -53,15 +62,8 @@ public LinkedIdentifierColumn(MainTableColumnModel model,
.withTooltip(this::createIdentifierTooltip)
.withMenu(this::createIdentifierMenu)
.withOnMouseClickedEvent((entry, linkedFiles) -> event -> {
if ((event.getButton() == MouseButton.PRIMARY) && (linkedFiles.size() == 1)) {
// Open linked identifier directly only if 1 entry is preset
try {
for (Field field : linkedFiles.keySet()) {
JabRefDesktop.openExternalViewer(database, linkedFiles.get(field), field);
}
} catch (IOException e) {
dialogService.showErrorDialogAndWait(Localization.lang("Unable to open link."), e);
}
if ((event.getButton() == MouseButton.PRIMARY)) {
new OpenUrlAction(dialogService, stateManager, preferences).execute();
}
})
.install(this);
Expand Down

0 comments on commit bc2fc07

Please sign in to comment.