diff --git a/jablib/src/main/java/org/jabref/logic/preferences/LastFilesOpenedPreferences.java b/jablib/src/main/java/org/jabref/logic/preferences/LastFilesOpenedPreferences.java index f136c0aa64a..b5e69988ba3 100644 --- a/jablib/src/main/java/org/jabref/logic/preferences/LastFilesOpenedPreferences.java +++ b/jablib/src/main/java/org/jabref/logic/preferences/LastFilesOpenedPreferences.java @@ -21,13 +21,17 @@ public class LastFilesOpenedPreferences { private final FileHistory fileHistory; public LastFilesOpenedPreferences(List lastFilesOpened, Path lastFocusedFile, FileHistory fileHistory) { - this.lastFilesOpened = FXCollections.observableArrayList(lastFilesOpened); - this.lastFocusedFile = new SimpleObjectProperty<>(lastFocusedFile); + this.lastFilesOpened = FXCollections.observableArrayList( + lastFilesOpened.stream().map(this::toRelative).toList() + ); + this.lastFocusedFile = new SimpleObjectProperty<>(toRelative(lastFocusedFile)); this.fileHistory = fileHistory; } public ObservableList getLastFilesOpened() { - return lastFilesOpened; + return FXCollections.observableArrayList( + lastFilesOpened.stream().map(this::toAbsolute).toList() + ); } public void setLastFilesOpened(List files) { @@ -35,7 +39,7 @@ public void setLastFilesOpened(List files) { } public Path getLastFocusedFile() { - return lastFocusedFile.get(); + return toAbsolute(lastFocusedFile.get()); } public ObjectProperty lastFocusedFileProperty() { @@ -43,10 +47,33 @@ public ObjectProperty lastFocusedFileProperty() { } public void setLastFocusedFile(Path lastFocusedFile) { - this.lastFocusedFile.set(lastFocusedFile); + this.lastFocusedFile.set(toRelative(lastFocusedFile)); } public FileHistory getFileHistory() { return fileHistory; } + + private Path toRelative(Path absolutePath) { + if (absolutePath == null) { + return null; + } + Path workingDir = Path.of("").toAbsolutePath(); + try { + return workingDir.relativize(absolutePath); + } catch (Exception e) { + return absolutePath; // fallback + } + } + + private Path toAbsolute(Path storedPath) { + if (storedPath == null) { + return null; + } + Path workingDir = Path.of("").toAbsolutePath(); + if (!storedPath.isAbsolute()) { + return workingDir.resolve(storedPath).normalize(); + } + return storedPath; + } } diff --git a/jablib/src/main/java/org/jabref/logic/util/io/FileHistory.java b/jablib/src/main/java/org/jabref/logic/util/io/FileHistory.java index 7ccbcadc88d..d80e13e1701 100644 --- a/jablib/src/main/java/org/jabref/logic/util/io/FileHistory.java +++ b/jablib/src/main/java/org/jabref/logic/util/io/FileHistory.java @@ -27,7 +27,7 @@ public int size() { @Override protected void doAdd(int index, Path element) { - history.add(index, element); + history.add(index, toRelative(element.toAbsolutePath())); } @Override @@ -41,11 +41,13 @@ protected Path doRemove(int index) { } /** - * Adds the file to the top of the list. If it already is in the list, it is merely moved to the top. + * Adds the file to the top of the list. If it already is in the list, it is + * merely moved to the top. */ public void newFile(Path file) { removeItem(file); - this.addFirst(file); + this.addFirst(toRelative(file.toAbsolutePath())); + while (size() > HISTORY_SIZE) { history.remove(HISTORY_SIZE); } @@ -58,4 +60,21 @@ public void removeItem(Path file) { public static FileHistory of(List list) { return new FileHistory(new ArrayList<>(list)); } + + private Path toRelative(Path absolutePath) { + Path workingDir = Path.of("").toAbsolutePath(); + try { + return workingDir.relativize(absolutePath); + } catch (Exception e) { + return absolutePath; // fallback + } + } + + private Path toAbsolute(Path storedPath) { + Path workingDir = Path.of("").toAbsolutePath(); + if (!storedPath.isAbsolute()) { + return workingDir.resolve(storedPath).normalize(); + } + return storedPath; + } }