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

Refactor of DOI import failure dialog, import format reader and clipboard manager #8839

Merged
merged 37 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
02481fa
Rename cleanup actions: "Make LaTeX ready" and "Make BibTeX ready"
zkl-ai Apr 30, 2022
07345ea
modify dio exception handler
zkl-ai May 22, 2022
f786412
Merge branch 'JabRef:main' into improve-DOI-import-failure-dialog
zkl-ai May 22, 2022
f06021a
modify dio exception handler change dialog from "No DOI data exits" t…
zkl-ai May 22, 2022
2348a34
Merge remote-tracking branch 'origin/improve-DOI-import-failure-dialo…
zkl-ai May 22, 2022
d32eae2
modify Localization.lang(message)
zkl-ai May 22, 2022
987d5df
Merge branch 'JabRef:main' into improve-DOI-import-failure-dialog
zkl-ai May 22, 2022
a6ec469
modify Localization.lang("Client Error") and
zkl-ai May 22, 2022
b3ac167
pass LocalizationConsistencyTest
zkl-ai May 22, 2022
6bf38c4
pass fetcher test
zkl-ai May 22, 2022
58361cb
Merge branch 'JabRef:main' into improve-DOI-import-failure-dialog
zkl-ai May 24, 2022
8e5b82a
improve the implementation with two add Exception DOIDataNotFoundExce…
zkl-ai May 24, 2022
d406d5a
Merge remote-tracking branch 'origin/improve-DOI-import-failure-dialo…
zkl-ai May 24, 2022
38f3e52
improve the implementation with two add Exception DOIDataNotFoundExce…
zkl-ai May 24, 2022
6b7dfa9
Merge remote-tracking branch 'upstream/main' into improve-DOI-import-…
Siedlerchr Jul 14, 2022
2561d32
Add FetcherClientException and FetcherServerException
Siedlerchr Jul 14, 2022
a02f5d4
rename test
Siedlerchr Jul 14, 2022
5dfdc8e
refactor impor format reader and clipboard manager
Siedlerchr Jul 14, 2022
3c0ee30
Merge remote-tracking branch 'upstream/main' into improve-DOI-import-…
Siedlerchr Jul 15, 2022
9ed8131
Refactor clipboard manager
Siedlerchr Jul 15, 2022
890e623
remove unused prefs
Siedlerchr Jul 15, 2022
c6deb0c
revert style changes
Siedlerchr Jul 15, 2022
4b0aeb2
fix checkstlye
Siedlerchr Jul 15, 2022
a03e25f
do not throw 404 errors from UrlDownload
Siedlerchr Jul 16, 2022
32fd370
better error messages
Siedlerchr Jul 16, 2022
a9c4589
checkstyle
Siedlerchr Jul 16, 2022
0165d14
Merge remote-tracking branch 'upstream/main' into improve-DOI-import-…
Siedlerchr Jul 18, 2022
73a88b7
improve dialog messages
Siedlerchr Jul 18, 2022
5060e34
change l10n
Siedlerchr Jul 19, 2022
b8a83ac
Merge remote-tracking branch 'upstream/main' into improve-DOI-import-…
Siedlerchr Jul 19, 2022
4c0ebd1
fix merge conflicts
Siedlerchr Jul 19, 2022
0cc4826
fix l10n messages
Siedlerchr Jul 19, 2022
369310e
fix checkstyle and fetcher
Siedlerchr Jul 20, 2022
d26e288
Merge remote-tracking branch 'upstream/main' into improve-DOI-import-…
Siedlerchr Aug 5, 2022
c47ea2d
improve error messages
Siedlerchr Aug 5, 2022
be99801
further fixes
Siedlerchr Aug 5, 2022
7c609f3
fix l10n
Siedlerchr Aug 6, 2022
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
93 changes: 7 additions & 86 deletions src/main/java/org/jabref/gui/ClipBoardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

Expand All @@ -22,19 +19,8 @@
import org.jabref.architecture.AllowedToUseAwt;
import org.jabref.logic.bibtex.BibEntryWriter;
import org.jabref.logic.bibtex.FieldWriter;
import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.ImportException;
import org.jabref.logic.importer.ImportFormatReader;
import org.jabref.logic.importer.ImportFormatReader.UnknownFormatImport;
import org.jabref.logic.importer.ParseException;
import org.jabref.logic.importer.fetcher.ArXiv;
import org.jabref.logic.importer.fetcher.DoiFetcher;
import org.jabref.logic.importer.fileformat.BibtexParser;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.identifier.ArXivIdentifier;
import org.jabref.model.entry.identifier.DOI;
import org.jabref.model.util.OptionalUtil;
import org.jabref.preferences.PreferencesService;

import org.slf4j.Logger;
Expand All @@ -49,18 +35,16 @@ public class ClipBoardManager {

private static Clipboard clipboard;
private static java.awt.datatransfer.Clipboard primary;
private static ImportFormatReader importFormatReader;

private final PreferencesService preferencesService;

public ClipBoardManager(PreferencesService preferencesService) {
this(Clipboard.getSystemClipboard(), Toolkit.getDefaultToolkit().getSystemSelection(), Globals.IMPORT_FORMAT_READER, preferencesService);
this(Clipboard.getSystemClipboard(), Toolkit.getDefaultToolkit().getSystemSelection(), preferencesService);
}

public ClipBoardManager(Clipboard clipboard, java.awt.datatransfer.Clipboard primary, ImportFormatReader importFormatReader, PreferencesService preferencesService) {
public ClipBoardManager(Clipboard clipboard, java.awt.datatransfer.Clipboard primary, PreferencesService preferencesService) {
ClipBoardManager.clipboard = clipboard;
ClipBoardManager.primary = primary;
ClipBoardManager.importFormatReader = importFormatReader;

this.preferencesService = preferencesService;
}

Expand Down Expand Up @@ -103,6 +87,10 @@ public static String getContents() {
return result;
}

public Optional<String> getBibTeXEntriesFromClipbaord() {
return Optional.ofNullable(clipboard.getContent(DragAndDropDataFormats.ENTRIES)).map(String.class::cast);
}

/**
* Get the String residing on the primary clipboard (if it exists).
*
Expand Down Expand Up @@ -167,71 +155,4 @@ public void setContent(List<BibEntry> entries) throws IOException {
clipboard.setContent(content);
setPrimaryClipboardContent(content);
}

public List<BibEntry> extractData() {
Object entries = clipboard.getContent(DragAndDropDataFormats.ENTRIES);

if (entries == null) {
return handleStringData(clipboard.getString());
}
return handleBibTeXData((String) entries);
}

private List<BibEntry> handleBibTeXData(String entries) {
BibtexParser parser = new BibtexParser(preferencesService.getImportFormatPreferences(), Globals.getFileUpdateMonitor());
try {
return parser.parseEntries(new ByteArrayInputStream(entries.getBytes(StandardCharsets.UTF_8)));
} catch (ParseException ex) {
LOGGER.error("Could not paste", ex);
return Collections.emptyList();
}
}

private List<BibEntry> handleStringData(String data) {
if ((data == null) || data.isEmpty()) {
return Collections.emptyList();
}

Optional<DOI> doi = DOI.parse(data);
if (doi.isPresent()) {
return fetchByDOI(doi.get());
}
Optional<ArXivIdentifier> arXiv = ArXivIdentifier.parse(data);
if (arXiv.isPresent()) {
return fetchByArXiv(arXiv.get());
}

return tryImportFormats(data);
}

private List<BibEntry> tryImportFormats(String data) {
try {
UnknownFormatImport unknownFormatImport = importFormatReader.importUnknownFormat(data);
return unknownFormatImport.parserResult.getDatabase().getEntries();
} catch (ImportException ignored) {
return Collections.emptyList();
}
}

private List<BibEntry> fetchByDOI(DOI doi) {
LOGGER.info("Found DOI in clipboard");
try {
Optional<BibEntry> entry = new DoiFetcher(preferencesService.getImportFormatPreferences()).performSearchById(doi.getDOI());
return OptionalUtil.toList(entry);
} catch (FetcherException ex) {
LOGGER.error("Error while fetching", ex);
return Collections.emptyList();
}
}

private List<BibEntry> fetchByArXiv(ArXivIdentifier arXivIdentifier) {
LOGGER.info("Found arxiv identifier in clipboard");
try {
Optional<BibEntry> entry = new ArXiv(preferencesService.getImportFormatPreferences()).performSearchById(arXivIdentifier.getNormalizedWithoutVersion());
return OptionalUtil.toList(entry);
} catch (FetcherException ex) {
LOGGER.error("Error while fetching", ex);
return Collections.emptyList();
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/org/jabref/gui/DefaultInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jabref.gui.keyboard.KeyBindingRepository;
import org.jabref.gui.theme.ThemeManager;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.importer.ImportFormatReader;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.protectedterms.ProtectedTermsLoader;
import org.jabref.model.entry.BibEntryTypesManager;
Expand Down Expand Up @@ -52,6 +53,8 @@ private static Object createDependency(Class<?> clazz) {
return Globals.undoManager;
} else if (clazz == BibEntryTypesManager.class) {
return Globals.entryTypesManager;
} else if (clazz == ImportFormatReader.class) {
return Globals.IMPORT_FORMAT_READER;
} else {
try {
return clazz.newInstance();
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/jabref/gui/EntryTypeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jabref.gui.util.IconValidationDecorator;
import org.jabref.gui.util.ViewModelListCellFactory;
import org.jabref.logic.importer.IdBasedFetcher;
import org.jabref.logic.importer.ImportFormatReader;
import org.jabref.logic.importer.WebFetcher;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseMode;
Expand All @@ -47,6 +48,7 @@
public class EntryTypeView extends BaseDialog<EntryType> {

@Inject StateManager stateManager;
@Inject ImportFormatReader importFormatReader;

@FXML private ButtonType generateButton;
@FXML private TextField idTextField;
Expand Down Expand Up @@ -118,7 +120,7 @@ private void addEntriesToPane(FlowPane pane, Collection<? extends BibEntryType>
@FXML
public void initialize() {
visualizer.setDecoration(new IconValidationDecorator());
viewModel = new EntryTypeViewModel(preferencesService, libraryTab, dialogService, stateManager);
viewModel = new EntryTypeViewModel(preferencesService, libraryTab, dialogService, stateManager, importFormatReader);

idBasedFetchers.itemsProperty().bind(viewModel.fetcherItemsProperty());
idTextField.textProperty().bindBidirectional(viewModel.idTextProperty());
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/jabref/gui/EntryTypeViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.jabref.gui.importer.NewEntryAction;
import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.IdBasedFetcher;
import org.jabref.logic.importer.ImportFormatReader;
import org.jabref.logic.importer.WebFetchers;
import org.jabref.logic.importer.fetcher.DoiFetcher;
import org.jabref.logic.l10n.Localization;
Expand Down Expand Up @@ -50,15 +51,19 @@ public class EntryTypeViewModel {
private final DialogService dialogService;
private final Validator idFieldValidator;
private final StateManager stateManager;
private final ImportFormatReader importFormatReader;

public EntryTypeViewModel(PreferencesService preferences,
LibraryTab libraryTab,
DialogService dialogService,
StateManager stateManager) {
StateManager stateManager,
ImportFormatReader importFormatReader) {
this.libraryTab = libraryTab;
this.preferencesService = preferences;
this.dialogService = dialogService;
this.stateManager = stateManager;
this.importFormatReader = importFormatReader;

fetchers.addAll(WebFetchers.getIdBasedFetchers(
preferences.getImportFormatPreferences(),
preferences.getImporterPreferences()));
Expand Down Expand Up @@ -164,7 +169,8 @@ public void runFetcherWorker() {
Globals.getFileUpdateMonitor(),
libraryTab.getUndoManager(),
stateManager,
dialogService);
dialogService,
importFormatReader);
handler.importEntryWithDuplicateCheck(libraryTab.getBibDatabaseContext(), entry);

searchSuccesfulProperty.set(true);
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
import org.jabref.logic.help.HelpFile;
import org.jabref.logic.importer.IdFetcher;
import org.jabref.logic.importer.ImportCleanup;
import org.jabref.logic.importer.ImportFormatReader;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.importer.WebFetchers;
import org.jabref.logic.l10n.Localization;
Expand Down Expand Up @@ -183,6 +184,7 @@ public class JabRefFrame extends BorderPane {
private Subscription dividerSubscription;

private final TaskExecutor taskExecutor;
private final ImportFormatReader importFormatReader;

public JabRefFrame(Stage mainStage) {
this.mainStage = mainStage;
Expand All @@ -194,6 +196,7 @@ public JabRefFrame(Stage mainStage) {
this.globalSearchBar = new GlobalSearchBar(this, stateManager, prefs, undoManager);
this.fileHistory = new FileHistoryMenu(prefs, dialogService, getOpenDatabaseAction());
this.taskExecutor = Globals.TASK_EXECUTOR;
this.importFormatReader = Globals.IMPORT_FORMAT_READER;
this.setOnKeyTyped(key -> {
if (this.fileHistory.isShowing()) {
if (this.fileHistory.openFileByKey(key)) {
Expand Down Expand Up @@ -1092,7 +1095,7 @@ private void trackOpenNewDatabase(LibraryTab libraryTab) {
public LibraryTab addTab(BibDatabaseContext databaseContext, boolean raisePanel) {
Objects.requireNonNull(databaseContext);

LibraryTab libraryTab = new LibraryTab(this, prefs, stateManager, themeManager, databaseContext, ExternalFileTypes.getInstance());
LibraryTab libraryTab = new LibraryTab(this, prefs, stateManager, themeManager, databaseContext, ExternalFileTypes.getInstance(), importFormatReader);
addTab(libraryTab, raisePanel);
return libraryTab;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/JabRefMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private static void applyPreferences(PreferencesService preferences) {

// Build list of Import and Export formats
Globals.IMPORT_FORMAT_READER.resetImportFormats(preferences.getImporterPreferences(),
preferences.getGeneralPreferences(), preferences.getImportFormatPreferences(),
preferences.getImportFormatPreferences(),
preferences.getXmpPreferences(), Globals.getFileUpdateMonitor());
Globals.entryTypesManager.addCustomOrModifiedTypes(preferences.getBibEntryTypes(BibDatabaseMode.BIBTEX),
preferences.getBibEntryTypes(BibDatabaseMode.BIBLATEX));
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.jabref.logic.autosaveandbackup.AutosaveManager;
import org.jabref.logic.autosaveandbackup.BackupManager;
import org.jabref.logic.citationstyle.CitationStyleCache;
import org.jabref.logic.importer.ImportFormatReader;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.importer.util.FileFieldParser;
import org.jabref.logic.l10n.Localization;
Expand Down Expand Up @@ -113,13 +114,15 @@ public class LibraryTab extends Tab {
private BackgroundTask<ParserResult> dataLoadingTask = BackgroundTask.wrap(() -> null);

private final IndexingTaskManager indexingTaskManager = new IndexingTaskManager(Globals.TASK_EXECUTOR);
private final ImportFormatReader importFormatReader;

public LibraryTab(JabRefFrame frame,
PreferencesService preferencesService,
StateManager stateManager,
ThemeManager themeManager,
BibDatabaseContext bibDatabaseContext,
ExternalFileTypes externalFileTypes) {
ExternalFileTypes externalFileTypes,
ImportFormatReader importFormatReader) {
this.frame = Objects.requireNonNull(frame);
this.bibDatabaseContext = Objects.requireNonNull(bibDatabaseContext);
this.externalFileTypes = Objects.requireNonNull(externalFileTypes);
Expand All @@ -128,6 +131,7 @@ public LibraryTab(JabRefFrame frame,
this.preferencesService = Objects.requireNonNull(preferencesService);
this.stateManager = Objects.requireNonNull(stateManager);
this.themeManager = Objects.requireNonNull(themeManager);
this.importFormatReader = importFormatReader;

bibDatabaseContext.getDatabase().registerListener(this);
bibDatabaseContext.getMetaData().registerListener(this);
Expand Down Expand Up @@ -480,7 +484,9 @@ private void createMainTable() {
dialogService,
stateManager,
externalFileTypes,
Globals.getKeyPrefs());
Globals.getKeyPrefs(),
Globals.getClipboardManager(),
Globals.IMPORT_FORMAT_READER);

// Add the listener that binds selection to state manager (TODO: should be replaced by proper JavaFX binding as soon as table is implemented in JavaFX)
mainTable.addSelectionListener(listEvent -> stateManager.setSelectedEntries(mainTable.getSelectedEntries()));
Expand Down Expand Up @@ -789,11 +795,11 @@ public void resetChangedProperties() {
}

public static class Factory {
public LibraryTab createLibraryTab(JabRefFrame frame, PreferencesService preferencesService, StateManager stateManager, ThemeManager themeManager, Path file, BackgroundTask<ParserResult> dataLoadingTask) {
public LibraryTab createLibraryTab(JabRefFrame frame, PreferencesService preferencesService, StateManager stateManager, ThemeManager themeManager, Path file, BackgroundTask<ParserResult> dataLoadingTask, ImportFormatReader importFormatReader) {
BibDatabaseContext context = new BibDatabaseContext();
context.setDatabasePath(file);

LibraryTab newTab = new LibraryTab(frame, preferencesService, stateManager, themeManager, context, ExternalFileTypes.getInstance());
LibraryTab newTab = new LibraryTab(frame, preferencesService, stateManager, themeManager, context, ExternalFileTypes.getInstance(), importFormatReader);
newTab.setDataLoadingTask(dataLoadingTask);

dataLoadingTask.onRunning(newTab::onDatabaseLoadingStarted)
Expand Down Expand Up @@ -929,7 +935,7 @@ public void notify(Node graphic, String text, List<Action> actions, Duration dur
this.setText(text);
this.getActions().setAll(actions);
this.show();
if (duration != null && !duration.equals(Duration.ZERO)) {
if ((duration != null) && !duration.equals(Duration.ZERO)) {
PauseTransition delay = new PauseTransition(duration);
delay.setOnFinished(e -> this.hide());
delay.play();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.ImportFormatReader;
import org.jabref.logic.importer.fetcher.GrobidCitationFetcher;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
Expand All @@ -43,7 +44,8 @@ public BibtexExtractorViewModel(BibDatabaseContext bibdatabaseContext,
FileUpdateMonitor fileUpdateMonitor,
TaskExecutor taskExecutor,
UndoManager undoManager,
StateManager stateManager) {
StateManager stateManager,
ImportFormatReader importFormatReader) {

this.dialogService = dialogService;
this.preferencesService = preferencesService;
Expand All @@ -55,7 +57,8 @@ public BibtexExtractorViewModel(BibDatabaseContext bibdatabaseContext,
fileUpdateMonitor,
undoManager,
stateManager,
dialogService);
dialogService,
importFormatReader);
}

public StringProperty inputTextProperty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jabref.gui.StateManager;
import org.jabref.gui.util.BaseDialog;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.importer.ImportFormatReader;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.util.FileUpdateMonitor;
Expand All @@ -35,6 +36,7 @@ public class ExtractBibtexDialog extends BaseDialog<Void> {
@Inject private TaskExecutor taskExecutor;
@Inject private UndoManager undoManager;
@Inject private PreferencesService preferencesService;
@Inject private ImportFormatReader importFormatReader;

public ExtractBibtexDialog() {
ViewLoader.view(this)
Expand All @@ -53,7 +55,7 @@ public ExtractBibtexDialog() {
@FXML
private void initialize() {
BibDatabaseContext database = stateManager.getActiveDatabase().orElseThrow(() -> new NullPointerException("Database null"));
this.viewModel = new BibtexExtractorViewModel(database, dialogService, preferencesService, fileUpdateMonitor, taskExecutor, undoManager, stateManager);
this.viewModel = new BibtexExtractorViewModel(database, dialogService, preferencesService, fileUpdateMonitor, taskExecutor, undoManager, stateManager, importFormatReader);
input.textProperty().bindBidirectional(viewModel.inputTextProperty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import java.io.IOException;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
Expand All @@ -30,8 +27,6 @@
public class ErrorConsoleViewModel extends AbstractViewModel {
private static final Logger LOGGER = LoggerFactory.getLogger(ErrorConsoleViewModel.class);

private final DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
private final Date date = new Date();
private final DialogService dialogService;
private final ClipBoardManager clipBoardManager;
private final BuildInfo buildInfo;
Expand Down
Loading