Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,59 @@ public class LastFilesOpenedPreferences {
private final FileHistory fileHistory;

public LastFilesOpenedPreferences(List<Path> 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<Path> getLastFilesOpened() {
return lastFilesOpened;
return FXCollections.observableArrayList(
lastFilesOpened.stream().map(this::toAbsolute).toList()
);
}

public void setLastFilesOpened(List<Path> files) {
lastFilesOpened.setAll(files);
}

public Path getLastFocusedFile() {
return lastFocusedFile.get();
return toAbsolute(lastFocusedFile.get());
}

public ObjectProperty<Path> lastFocusedFileProperty() {
return lastFocusedFile;
}

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;
}
Comment on lines +58 to +60
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When can this happen? Maybe annotate paramter as @NonNull?

Path workingDir = Path.of("").toAbsolutePath();
try {
return workingDir.relativize(absolutePath);
} catch (Exception e) {
return absolutePath; // fallback
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do a LOGGER.error, because this should be fixed maybe?

}
}

private Path toAbsolute(Path storedPath) {
if (storedPath == null) {
return null;
}
Path workingDir = Path.of("").toAbsolutePath();
if (!storedPath.isAbsolute()) {
Comment on lines +69 to +74
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

return workingDir.resolve(storedPath).normalize();
}
return storedPath;
}
}
25 changes: 22 additions & 3 deletions jablib/src/main/java/org/jabref/logic/util/io/FileHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*/
Comment on lines 43 to 46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why the reformatting here?

public void newFile(Path file) {
removeItem(file);
this.addFirst(file);
this.addFirst(toRelative(file.toAbsolutePath()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, what?


while (size() > HISTORY_SIZE) {
history.remove(HISTORY_SIZE);
}
Expand All @@ -58,4 +60,21 @@ public void removeItem(Path file) {
public static FileHistory of(List<Path> list) {
return new FileHistory(new ArrayList<>(list));
}

private Path toRelative(Path absolutePath) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definetitely needs a test

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;
}
Comment on lines +64 to +79
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is duplicated code.

Strong indication that used at too many places.

Maybe you can use org.jabref.logic.util.io.FileUtil#relativize(java.nio.file.Path, java.util.List<java.nio.file.Path>) ?

}
Loading