Skip to content

Commit

Permalink
fix: exception when opening already opened files
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-kolb committed Mar 13, 2020
1 parent 23034ce commit 1e78c93
Showing 1 changed file with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<GUIPostOpenAction> 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;
Expand Down Expand Up @@ -84,7 +85,6 @@ public void execute() {
}

/**
*
* @return Path of current panel database directory or the working directory
*/
private Path getInitialDirectory() {
Expand All @@ -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);
}

/**
Expand All @@ -116,12 +116,12 @@ public void openFiles(List<Path> filesToOpen, boolean raisePanel) {
int removed = 0;

// Check if any of the files are already open:
for (Iterator<Path> iterator = filesToOpen.iterator(); iterator.hasNext();) {
for (Iterator<Path> 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
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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;

}
}

0 comments on commit 1e78c93

Please sign in to comment.