Skip to content

Commit 723c35f

Browse files
authored
Fix no such element exception in copy to (#12488)
* Fix np such element exception in copy tp Fixes JabRef/jabref-issue-melting-pot#768 Follow up to #12374 * fix checkstyle
1 parent f7b2ac0 commit 723c35f

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.jabref.gui.maintable;
22

3-
import java.nio.file.Path;
43
import java.util.Optional;
54

65
import javax.swing.undo.UndoManager;
@@ -120,27 +119,31 @@ private static Menu createCopyToMenu(ActionFactory factory,
120119
StateManager stateManager,
121120
GuiPreferences preferences,
122121
LibraryTab libraryTab,
123-
ImportHandler importHandler
124-
) {
122+
ImportHandler importHandler) {
125123
Menu copyToMenu = factory.createMenu(StandardActions.COPY_TO);
126124

127125
ObservableList<BibDatabaseContext> openDatabases = stateManager.getOpenDatabases();
128126

129127
BibDatabaseContext sourceDatabaseContext = libraryTab.getBibDatabaseContext();
130128

131-
Optional<Path> sourcePath = libraryTab.getBibDatabaseContext().getDatabasePath();
132-
String sourceDatabaseName = FileUtil.getUniquePathFragment(stateManager.collectAllDatabasePaths(), sourcePath.get()).get();
129+
Optional<String> sourceDatabaseName = libraryTab
130+
.getBibDatabaseContext().getDatabasePath().stream()
131+
.flatMap(path -> FileUtil.getUniquePathFragment(stateManager.collectAllDatabasePaths(), path).stream())
132+
.findFirst();
133133

134134
if (!openDatabases.isEmpty()) {
135135
openDatabases.forEach(bibDatabaseContext -> {
136136
Optional<String> destinationPath = Optional.empty();
137137
String destinationDatabaseName = "";
138138

139139
if (bibDatabaseContext.getDatabasePath().isPresent()) {
140-
destinationDatabaseName = FileUtil.getUniquePathFragment(stateManager.collectAllDatabasePaths(), bibDatabaseContext.getDatabasePath().get()).get();
141-
if (destinationDatabaseName.equals(sourceDatabaseName)) {
140+
Optional<String> uniqueFilePathFragment = FileUtil.getUniquePathFragment(stateManager.collectAllDatabasePaths(), bibDatabaseContext.getDatabasePath().get());
141+
if (uniqueFilePathFragment.equals(sourceDatabaseName)) {
142142
return;
143143
}
144+
if (uniqueFilePathFragment.isPresent()) {
145+
destinationDatabaseName = uniqueFilePathFragment.get();
146+
}
144147
} else if (bibDatabaseContext.getLocation() == DatabaseLocation.SHARED) {
145148
destinationDatabaseName = bibDatabaseContext.getDBMSSynchronizer().getDBName() + " [" + Localization.lang("shared") + "]";
146149
} else {

0 commit comments

Comments
 (0)