Skip to content

Add support for remembering last-used directory in file attachment dialog#12797

Merged
koppor merged 10 commits into
JabRef:mainfrom
Kaan0029:add-remember-last-used-directory
Apr 10, 2025
Merged

Add support for remembering last-used directory in file attachment dialog#12797
koppor merged 10 commits into
JabRef:mainfrom
Kaan0029:add-remember-last-used-directory

Conversation

@Kaan0029

Copy link
Copy Markdown
Contributor

Previously, the file chooser always opened in the default working directory, even if the user had just selected a different folder. This change stores the last-used folder in preferences and reuses it the next time a file is attached to a BibEntry.

Closes #12554

This change:

  • Adds a lastUsedDirectory field to FilePreferences
  • Modifies AttachFileAction to store and retrieve the last-used directory
  • Improves user experience by remembering recently used folders across attachment actions

Manual Testing

This feature was manually tested:

  • Attached a file → folder was remembered
  • Restarted JabRef → folder persisted and reopened correctly
  • No existing behavior was broken

No test case added because this is a UI feature, and the file dialog is not easily mockable.

Screenshot

Screenshot 2025-03-22 at 10 11 18 AM

Mandatory checks

  • I own the copyright of the code submitted and I license it under the MIT license
  • Change in CHANGELOG.md described in a way that is understandable for the average user (if change is visible to the user)
  • [/] Tests created for changes (if applicable)
  • Manually tested changed features in running JabRef (always required)
  • Screenshots added in PR description (if change is visible to the user)
  • [/] Checked developer's documentation: Is the information available and up to date? If not, I outlined it in this pull request.
  • [/] Checked documentation: Is the information available and up to date? If not, I created an issue at https://github.com/JabRef/user-documentation/issues or, even better, I submitted a pull request to the documentation repository.

Previously, the file chooser always opened in the default working directory,
even if the user had just selected a different folder. This change stores the
last-used folder in preferences and reuses it the next time a file is attached
to a BibEntry.

Fixes: JabRef#12554
@calixtus

Copy link
Copy Markdown
Member

Maybe this should be configurable. If i look at other office applications, like word, it opens the folder always on documents folder or for the working directory. A simple pref option "remember last folder a file was opened" or sthg.
Wdyt @JabRef/developers ?

@Kaan0029

Copy link
Copy Markdown
Contributor Author

@calixtus Thanks for the feedback! That sounds like a really good idea, as I'm sure some users prefer always starting in the same directory, while others would benefit from remembering the last one.

Do you think it might also make sense to add an additional setting to remember the last-used directory per library? For example, when I'm working on two different projects (or using two different .bib libraries simultaneously), it could be more helpful if each library remembered its own last-used folder. Meanwhile, for users who always attach files from the same place (e.g., Downloads), the default behavior would still be sufficient. Curious what you (and others) think!

@koppor

koppor commented Mar 23, 2025

Copy link
Copy Markdown
Member

i think, the setting should go into "Linked files".

grafik


I think, two preferencesd are enough

  • Open file explorer in files directory
  • Open file explorer in last opened directory

Default should be the first one as indicated

For the concrete wording, please check the other strings in JabRef JabRef_en.properties

getBoolean(TRASH_INSTEAD_OF_DELETE, moveToTrashSupported()),
getBoolean(KEEP_DOWNLOAD_URL));
getBoolean(KEEP_DOWNLOAD_URL),
getPath(LAST_USED_DIRECTORY, getDefaultPath()));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Default values are handled in a different way in JabRef.

There is a default mapping somewhere in JabRefCliPreferences.java

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Or you have to specify default there because of some AWT initialization (as described in comment above)?

@subhramit subhramit Apr 3, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes. We do defaults.put(preference, defaultValue) in the constructor for most preferences.
Some of these are using impromptu fallbacks - and their design (for the matter of style/neatness or maybe simply as a result of non-uniformity) is such that we do getProperty(PREFERENCE, defaultFunction()) instead of defaults.put and getProperty(PREFERENCE).
Example - getPath expects a second parameter (default) of type Path (not String), whereas getBoolean has an implementation which does not.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Delayed notice: I implemented the requested changes described here ~3 days ago leveraging defaults.put() as suggested.


Path workingDirectory = databaseContext.getFirstExistingFileDir(filePreferences)
.orElse(filePreferences.getWorkingDirectory());
Path workingDirectory = Optional.ofNullable(filePreferences.getLastUsedDirectory())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why ofNullable is used here?

If last user directory can be empty, then it should be Optional. (BTW, there is a special property class for this, something like OptionalObjectProperty)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here, the preference mentioned in #12797 (comment) comes into play. Thus, the highlighted code should be adapted accordingly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The lastUsedDirectory can fall back to

databaseContext.getFirstExistingFileDir(filePreferences)
                                           .orElse(filePreferences.getWorkingDirectory());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Note: This comment is a delayed response from me (because I implemented the requested changes described here ~3 days ago)
I tackled the issues suggested here. Since a preference determined by user was suggested, I added conditionals. ofNullable initially solved a bug, so I had added it. Should have only been a provisional solution but I changed that now.

- Added preference options to choose between opening file explorer in:
  * Files directory (default)
  * Last opened directory
- Updated AttachFileAction to use the selected preference
- Added localization strings for the new UI options
- Added necessary view model bindings

This enhancement gives users control over where the file dialog opens
when attaching files, making the workflow more customizable.

See: [#XXXX](https://github.com/JabRef/jabref/issues/XXXX)
Comment thread src/main/java/org/jabref/gui/linkedfile/AttachFileAction.java

@subhramit subhramit left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please address the redundancy in defaults.

Comment on lines +1576 to +1577
getBoolean(OPEN_FILE_EXPLORER_IN_FILE_DIRECTORY, Boolean.TRUE),
getBoolean(OPEN_FILE_EXPLORER_IN_LAST_USED_DIRECTORY, Boolean.FALSE));

@subhramit subhramit Apr 7, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You have already put defaults for these two, so no utility of passing them here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the notice! I deleted them. (as part of a commit from around 3 days ago so this is a delayed comment response by me)

Comment on lines +36 to +37
private final BooleanProperty openFileExplorerInFileDirectory = new SimpleBooleanProperty(true);
private final BooleanProperty openFileExplorerInLastUsedDirectory = new SimpleBooleanProperty(false);

@subhramit subhramit Apr 7, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Again, no need of passing defaults in the constructor here, you have already handled them in JabRefCliPreferences.
See how the other properties are handled ^

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I got rid of the parameters!

@subhramit

Copy link
Copy Markdown
Member

GitHub keeps messing up the order of review comments somehow these days. Fixed them via edits.

Comment on lines +229 to +231
public Path getLastUsedDirectory() {
return lastUsedDirectory.get();
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

New public methods should not return null. Consider using Optional to avoid returning null and improve null safety.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

False positive

@Siedlerchr

Copy link
Copy Markdown
Member

Please fix the submodules, then it's fine https://devdocs.jabref.org/code-howtos/faq.html#submodules

Siedlerchr
Siedlerchr previously approved these changes Apr 9, 2025
@Siedlerchr Siedlerchr added the status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers label Apr 9, 2025
Comment on lines +1552 to +1560
getInternalPreferences().getUserAndHost(),
getInternalPreferences().getUserAndHost(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, this indent is off - I will fix it.

subhramit
subhramit previously approved these changes Apr 10, 2025
@trag-bot

trag-bot Bot commented Apr 10, 2025

Copy link
Copy Markdown

@trag-bot didn't find any issues in the code! ✅✨

@koppor koppor added this pull request to the merge queue Apr 10, 2025
Merged via the queue into JabRef:main with commit b610ec9 Apr 10, 2025
krishnagjsForGit pushed a commit to krishnagjsForGit/jabref that referenced this pull request May 2, 2025
…alog (JabRef#12797)

* Add support for remembering last-used directory for file attachments

Previously, the file chooser always opened in the default working directory,
even if the user had just selected a different folder. This change stores the
last-used folder in preferences and reuses it the next time a file is attached
to a BibEntry.

Fixes: JabRef#12554

* Add option to configure file explorer directory in preferences

- Added preference options to choose between opening file explorer in:
  * Files directory (default)
  * Last opened directory
- Updated AttachFileAction to use the selected preference
- Added localization strings for the new UI options
- Added necessary view model bindings

This enhancement gives users control over where the file dialog opens
when attaching files, making the workflow more customizable.

See: [#XXXX](https://github.com/JabRef/jabref/issues/XXXX)

* Remove redundant default values after merging with main

* Fix submodules

* Fix csl-styles

* Fix indent

---------

Co-authored-by: Oliver Kopp <kopp.dev@gmail.com>
krishnagjsForGit pushed a commit to krishnagjsForGit/jabref that referenced this pull request May 2, 2025
…alog (JabRef#12797)

* Add support for remembering last-used directory for file attachments

Previously, the file chooser always opened in the default working directory,
even if the user had just selected a different folder. This change stores the
last-used folder in preferences and reuses it the next time a file is attached
to a BibEntry.

Fixes: JabRef#12554

* Add option to configure file explorer directory in preferences

- Added preference options to choose between opening file explorer in:
  * Files directory (default)
  * Last opened directory
- Updated AttachFileAction to use the selected preference
- Added localization strings for the new UI options
- Added necessary view model bindings

This enhancement gives users control over where the file dialog opens
when attaching files, making the workflow more customizable.

See: [#XXXX](https://github.com/JabRef/jabref/issues/XXXX)

* Remove redundant default values after merging with main

* Fix submodules

* Fix csl-styles

* Fix indent

---------

Co-authored-by: Oliver Kopp <kopp.dev@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Save the last directory from which files were added

6 participants