-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Convert absolute paths to relative paths in Recent Files #14464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1796919
6d767ed
c358a9a
1dc7986
3d4e7b3
883c194
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
| } | ||
| Path workingDir = Path.of("").toAbsolutePath(); | ||
| try { | ||
| return workingDir.relativize(absolutePath); | ||
| } catch (Exception e) { | ||
| return absolutePath; // fallback | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. |
||
| return workingDir.resolve(storedPath).normalize(); | ||
| } | ||
| return storedPath; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
| */ | ||
|
Comment on lines
43
to
46
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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())); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, what? |
||
|
|
||
| while (size() > HISTORY_SIZE) { | ||
| history.remove(HISTORY_SIZE); | ||
| } | ||
|
|
@@ -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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>) ? |
||
| } | ||
There was a problem hiding this comment.
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?