From 654ee2004a63af40896c7c45bc1aa6959093b039 Mon Sep 17 00:00:00 2001 From: Stefan Kolb Date: Fri, 13 Mar 2020 22:05:19 +0100 Subject: [PATCH] fix: exception when opening already opened files --- CHANGELOG.md | 3 +- .../importer/actions/OpenDatabaseAction.java | 28 ++++++++----------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bfd09f6aaa0..19ab61071a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve ### Fixed +- We fixed an issue where opening a library from the recent libraries menu was not possible [#5939](https://github.com/JabRef/jabref/issues/5939) + ### Removed ## [5.0] – 2020-03-06 @@ -47,7 +49,6 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We fixed an issue where entries containing Unicode charaters were not parsed correctly [#5899](https://github.com/JabRef/jabref/issues/5899) - We fixed an issue where an entry containing an external filename with curly braces could not be saved. Curly braces are now longer allowed in filenames. [#5899](https://github.com/JabRef/jabref/issues/5899) - We fixed an issue where changing the type of an entry did not update the main table [#5906](https://github.com/JabRef/jabref/issues/5906) -- We fixed an issue where opening a library from the recent libraries menu was not possible [#5939](https://github.com/JabRef/jabref/issues/5939) - We fixed an issue in the optics of the library properties, that cropped the dialog on scaled displays. [#5969](https://github.com/JabRef/jabref/issues/5969) - We fixed an issue where changing the type of an entry did not update the main table. [#5906](https://github.com/JabRef/jabref/issues/5906) - We fixed an issue where opening a library from the recent libraries menu was not possible. [#5939](https://github.com/JabRef/jabref/issues/5939) diff --git a/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java b/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java index 50717a536e3..a9701ce1b90 100644 --- a/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java +++ b/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java @@ -3,6 +3,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; @@ -42,11 +43,11 @@ public class OpenDatabaseAction extends SimpleCommand { // List of actions that may need to be called after opening the file. Such as // upgrade actions etc. that may depend on the JabRef version that wrote the file: private static final List POST_OPEN_ACTIONS = Arrays.asList( - // Migrations: - // Warning for migrating the Review into the Comment field - new MergeReviewIntoCommentAction(), - // Check for new custom entry types loaded from the BIB file: - new CheckForNewEntryTypesAction()); + // Migrations: + // Warning for migrating the Review into the Comment field + new MergeReviewIntoCommentAction(), + // Check for new custom entry types loaded from the BIB file: + new CheckForNewEntryTypesAction()); private final JabRefFrame frame; private final DialogService dialogService; @@ -84,7 +85,6 @@ public void execute() { } /** - * * @return Path of current panel database directory or the working directory */ private Path getInitialDirectory() { @@ -102,7 +102,7 @@ private Path getInitialDirectory() { * @param file the file, may be null or not existing */ public void openFile(Path file, boolean raisePanel) { - openFiles(Arrays.asList(file), raisePanel); + openFiles(new ArrayList<>(List.of(file)), raisePanel); } /** @@ -116,12 +116,12 @@ public void openFiles(List filesToOpen, boolean raisePanel) { int removed = 0; // Check if any of the files are already open: - for (Iterator iterator = filesToOpen.iterator(); iterator.hasNext();) { + for (Iterator iterator = filesToOpen.iterator(); iterator.hasNext(); ) { Path file = iterator.next(); for (int i = 0; i < frame.getTabbedPane().getTabs().size(); i++) { BasePanel basePanel = frame.getBasePanelAt(i); if ((basePanel.getBibDatabaseContext().getDatabasePath().isPresent()) - && basePanel.getBibDatabaseContext().getDatabasePath().get().equals(file)) { + && basePanel.getBibDatabaseContext().getDatabasePath().get().equals(file)) { iterator.remove(); removed++; // See if we removed the final one. If so, we must perhaps @@ -169,10 +169,9 @@ private void openTheFile(Path file, boolean raisePanel) { OpenDatabaseAction.performPostOpenActions(panel, result); }) .onFailure(ex -> dialogService.showErrorDialogAndWait(Localization.lang("Connection error"), - ex.getMessage() + "\n\n" + Localization.lang("A local copy will be opened."))) + ex.getMessage() + "\n\n" + Localization.lang("A local copy will be opened."))) .executeWith(Globals.TASK_EXECUTOR); } - } private ParserResult loadDatabase(Path file) throws Exception { @@ -187,23 +186,21 @@ private ParserResult loadDatabase(Path file) throws Exception { } ParserResult result = OpenDatabase.loadDatabase(fileToLoad.toString(), - Globals.prefs.getImportFormatPreferences(), Globals.getFileUpdateMonitor()); + Globals.prefs.getImportFormatPreferences(), Globals.getFileUpdateMonitor()); if (result.getDatabase().isShared()) { try { new SharedDatabaseUIManager(frame).openSharedDatabaseFromParserResult(result); } catch (SQLException | DatabaseNotSupportedException | InvalidDBMSConnectionPropertiesException | - NotASharedDatabaseException e) { + NotASharedDatabaseException e) { result.getDatabaseContext().clearDatabaseFile(); // do not open the original file result.getDatabase().clearSharedDatabaseID(); LOGGER.error("Connection error", e); throw e; - } } return result; - } private BasePanel addNewDatabase(ParserResult result, final Path file, boolean raisePanel) { @@ -214,6 +211,5 @@ private BasePanel addNewDatabase(ParserResult result, final Path file, boolean r BasePanel basePanel = new BasePanel(frame, BasePanelPreferences.from(Globals.prefs), result.getDatabaseContext(), ExternalFileTypes.getInstance()); frame.addTab(basePanel, raisePanel); return basePanel; - } }