Skip to content
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

Fix for issue 571: File folders: Move between global and user-specific #12059

Closed
wants to merge 70 commits into from
Closed
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
98d19dc
Add a feature for having dynamic move funciton(files): Working for li…
Officialshubham Oct 7, 2024
5db6d07
Change the the retrieval of main file directory
Officialshubham Oct 8, 2024
cc328a6
Merge branch 'JabRef:main' into fix-for-issue-571
rl712 Oct 8, 2024
f42850d
Merge branch 'JabRef:main' into fix-for-issue-571
rl712 Oct 9, 2024
2a2a632
Merge branch 'JabRef:main' into fix-for-issue-571
rl712 Oct 10, 2024
acc484f
Merge branch 'JabRef:main' into fix-for-issue-571
rl712 Oct 13, 2024
bc9e5e6
Merge branch 'JabRef:main' into fix-for-issue-571
rl712 Oct 14, 2024
96a7172
Merge branch 'JabRef:main' into fix-for-issue-571
rl712 Oct 14, 2024
de22fab
Merge branch 'JabRef:main' into fix-for-issue-571
rl712 Oct 15, 2024
f62b498
Add debugging statements
Officialshubham Oct 13, 2024
b22c7de
Move files to diff directories
Officialshubham Oct 13, 2024
58ca163
Solve changing the directory after first move
Officialshubham Oct 15, 2024
1b497a1
Merge branch 'JabRef:main' into fix-for-issue-571
Officialshubham Oct 16, 2024
7e74446
Create the test for FileDirectoryHandlerTest.java
heartofiro Oct 16, 2024
03c185b
Merge branch 'JabRef:main' into fix-for-issue-571
rl712 Oct 17, 2024
1c3428e
Merge branch 'JabRef:main' into fix-for-issue-571
rl712 Oct 17, 2024
9c9ecf5
Merge branch 'JabRef:main' into fix-for-issue-571
rl712 Oct 18, 2024
a50bf02
Merge branch 'JabRef:main' into fix-for-issue-571
rl712 Oct 18, 2024
65b6738
Add rename logic for move
Officialshubham Oct 19, 2024
7df6f97
Add logic for concurrent change of label
Officialshubham Oct 19, 2024
c2aefe5
Working for 2 directory structure now
Officialshubham Oct 19, 2024
61113bf
Remove junk code: cleanup
Officialshubham Oct 20, 2024
5234308
Add funcitonality when directories are 0
Officialshubham Oct 20, 2024
8711341
Change the ordering of menuItem: remove wrong target
Officialshubham Oct 20, 2024
99b7b90
Test for FileDIrectoryHander is working
Officialshubham Oct 20, 2024
0acd0ab
Remove junk comments: cleanup
Officialshubham Oct 20, 2024
da29975
Add more test cases
Officialshubham Oct 22, 2024
cc9d491
Preserve sub dir structure when moving
Officialshubham Oct 22, 2024
5b1a517
Add and remove comments
Officialshubham Oct 22, 2024
2c5e559
Add changes to CHANGELOG.md: Fixed Section
Officialshubham Oct 22, 2024
76f67df
Add comments for user and library: FileDirectoryHandler
Officialshubham Oct 22, 2024
e0dc17f
Fix submodule error for csl-styles
Officialshubham Oct 23, 2024
dcb631c
Merge branch 'main' into fix-for-issue-571
Officialshubham Oct 23, 2024
fa5e0a5
Change by openrewrite
Officialshubham Oct 23, 2024
126bf41
Fix bare URL and trailing spaces error: CHANGELOG.md
Officialshubham Oct 23, 2024
f08db39
Add incomplete link in CHANGELOG.md
Officialshubham Oct 23, 2024
c0ae669
Update CHANGELOG.md: fix-for-issue-571
Officialshubham Oct 23, 2024
eb25e9f
Merge branch 'JabRef:main' into fix-for-issue-571
Officialshubham Oct 23, 2024
9ed5d7e
Add "world + pdf" and "person + pdf" icons
u7646387 Oct 24, 2024
4445c11
Merge branch 'JabRef:main' into fix-icons
rl712 Oct 24, 2024
38e9a52
Merge branch 'JabRef:main' into fix-for-issue-571
Officialshubham Oct 24, 2024
517b393
Add getDirectoryInfo for dirs and implemet changes from PR
Officialshubham Oct 24, 2024
efe96f3
Change cleanup position after update menu
Officialshubham Oct 24, 2024
c6f36d9
Remove unnecessary comment
Officialshubham Oct 24, 2024
3ff5918
Change default to library directory name
Officialshubham Oct 24, 2024
6abe046
Rename default to library specific in FileDirectoryHandlerTest
Officialshubham Oct 24, 2024
17ff80e
Remove cleanup directories stream code: Not required
Officialshubham Oct 24, 2024
b514108
Improve the logic of getTargetDirectory
Officialshubham Oct 24, 2024
2b8e8c4
Add language keys
Officialshubham Oct 24, 2024
290968c
Change tests based on directoriesInfo
Officialshubham Oct 24, 2024
865fcf7
Fix language key error
Officialshubham Oct 24, 2024
e911b52
Fix Standard Streams Usage Violation
Officialshubham Oct 24, 2024
b0f519e
Fix layered architecture violation
Officialshubham Oct 24, 2024
8089323
Merge branch 'main' into fix-for-issue-571
Officialshubham Oct 24, 2024
5638259
Merge branch 'JabRef:main' into fix-icons
rl712 Oct 27, 2024
af67c43
Merge branch 'fix-for-issue-571' of https://github.com/rl712/jabref i…
rl712 Oct 27, 2024
c4f9212
Implemented code for world + pdf icon to be displayed for main file d…
rl712 Oct 27, 2024
3af150b
Merge remote-tracking branch 'origin/fix-icons' into fix-icons
rl712 Oct 27, 2024
216ba2d
Revert "Implemented code for world + pdf icon to be displayed for mai…
rl712 Oct 27, 2024
1cc2586
Revert "Revert "Implemented code for world + pdf icon to be displayed…
rl712 Oct 27, 2024
4f97f2c
Merge branch 'JabRef:main' into fix-icons
rl712 Oct 27, 2024
16f8c74
Merged updates from the latest branch
rl712 Oct 27, 2024
3a8b29e
add conditional logic.
tomeg09 Oct 27, 2024
8dcd163
commented out code that was causing error.
tomeg09 Oct 27, 2024
0ee186e
Merge branch 'main' of https://github.com/rl712/jabref into fix-for-i…
rl712 Oct 27, 2024
4eed640
Implemented file icons to be displayed in menu item for move file
rl712 Oct 27, 2024
82d69b7
Implemented file icons to be displayed in menu item for move and rena…
rl712 Oct 27, 2024
27fe09a
Uncommented setDisable(true) for moveFileItem and moveAndRenameFileItem
rl712 Oct 27, 2024
38afcc1
Merge branch 'fix-icons' of https://github.com/rl712/jabref into fix-…
rl712 Oct 27, 2024
187c932
Updated CHANGELOG.md with addition of custom icons to menu item such …
rl712 Oct 27, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We fixed an issue where <kbd>Tab</kbd> cannot be used to jump to next field in some single-line fields. [#11785](https://github.com/JabRef/jabref/issues/11785)
- We fixed an issue where it was not possible to select selecting content of other user's comments.[#11106](https://github.com/JabRef/jabref/issues/11106)
- We fixed an issue where web search preferences "Custom API key" table modifications not discarded. [#11925](https://github.com/JabRef/jabref/issues/11925)
- We fixed an issue where user has an ability to move between global and user-specific folders. [koppor#571](https://github.com/koppor/jabref/issues/571)
- We fixed an issue where trying to open a library from a failed mounted directory on Mac would cause an error. [#10548](https://github.com/JabRef/jabref/issues/10548)

### Removed
Expand Down
161 changes: 161 additions & 0 deletions src/main/java/org/jabref/gui/fieldeditors/FileDirectoryHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package org.jabref.gui.fieldeditors;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.jabref.gui.DialogService;
import org.jabref.logic.FilePreferences;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.metadata.MetaData;

/*
Responsible for handling file movement across directories
*/
public class FileDirectoryHandler {
private final BibDatabaseContext databaseContext;
private final FilePreferences filePreferences;
private final DialogService dialogService;

public FileDirectoryHandler(BibDatabaseContext databaseContext, FilePreferences filePreferences, DialogService dialogService) {
this.databaseContext = databaseContext;
this.filePreferences = filePreferences;
this.dialogService = dialogService;
}

public record DirectoryInfo(
String label,
Path path,
DirectoryType type) {
}

public enum DirectoryType {
MAIN,
GENERAL,
USER_SPECIFIC
}

public Optional<DirectoryInfo> determineTargetDirectory(Path currentFilePath) {
List<DirectoryInfo> availableDirectories = getAvailableDirectories();
if (availableDirectories.isEmpty()) {
return Optional.empty();
}

Optional<DirectoryType> currentDirectory = determineCurrentDirectory(currentFilePath);
if (availableDirectories.size() == 1) {
if (currentDirectory.isEmpty()) {
return Optional.of(availableDirectories.getFirst());
}
} else if (availableDirectories.size() == 2) {
return handleTwoDirectoriesCase(currentDirectory, availableDirectories);
} else if (availableDirectories.size() == 3) {
return handleThreeDirectoriesCase(currentDirectory, availableDirectories);
}

return Optional.empty();
}

private List<DirectoryInfo> getAvailableDirectories() {
List<DirectoryInfo> directories = new ArrayList<>();
MetaData metaData = databaseContext.getMetaData();

filePreferences.getMainFileDirectory().ifPresent(mainFilePath -> directories.add(new DirectoryInfo(Localization.lang("main file directory"), Path.of(mainFilePath.toUri()), DirectoryType.MAIN)));
// For library specific dir
metaData.getDefaultFileDirectory().ifPresent(path ->
directories.add(new DirectoryInfo(
Localization.lang("library-specific file directory"),
Path.of(path),
DirectoryType.GENERAL
))
);

// For user-specific dir
metaData.getUserFileDirectory(filePreferences.getUserAndHost()).ifPresent(path ->
directories.add(new DirectoryInfo(
Localization.lang("user-specific file directory"),
Path.of(path),
DirectoryType.USER_SPECIFIC
))
);

return directories;
}

private Optional<DirectoryType> determineCurrentDirectory(Path filePath) {
MetaData metaData = databaseContext.getMetaData();
// Check main directory - If file is in MAIN
Optional<Path> mainFilePath = filePreferences.getMainFileDirectory();
if (mainFilePath.isPresent() && filePath.startsWith(mainFilePath.get())) {
return Optional.of(DirectoryType.MAIN);
}

// Check general directory - If file is in Library
if (metaData.getDefaultFileDirectory()
.map(dir -> filePath.startsWith(Path.of(dir)))
.orElse(false)) {
return Optional.of(DirectoryType.GENERAL);
}

// Check user specific directory - If file is in user
if (metaData.getUserFileDirectory(filePreferences.getUserAndHost())
.map(dir -> filePath.startsWith(Path.of(dir)))
.orElse(false)) {
return Optional.of(DirectoryType.USER_SPECIFIC);
}

return Optional.empty();
}

private Optional<DirectoryInfo> handleTwoDirectoriesCase(Optional<DirectoryType> currentDirectory, List<DirectoryInfo> availableDirectories) {
String mainFilePath = String.valueOf(filePreferences.getMainFileDirectory());
if (currentDirectory.isEmpty()) {
// File outside both directories - prefer general, then user-specific, then main
return availableDirectories.stream()
.filter(dir -> dir.type == DirectoryType.GENERAL)
.findFirst()
.or(() -> availableDirectories.stream()
.filter(dir -> dir.type == DirectoryType.USER_SPECIFIC)
.findFirst())
.or(() -> mainFilePath != null ? availableDirectories.stream()
.filter(dir -> dir.type == DirectoryType.MAIN && dir.path.startsWith(mainFilePath))
.findFirst() : Optional.empty());
}

DirectoryType current = currentDirectory.get();
if (current == DirectoryType.MAIN) {
return availableDirectories.stream()
.filter(dir -> dir.type == DirectoryType.GENERAL || dir.type == DirectoryType.USER_SPECIFIC)
.findFirst();
} else if (current == DirectoryType.GENERAL) {
return availableDirectories.stream()
.filter(dir -> dir.type == DirectoryType.MAIN)
.findFirst();
} else {
return availableDirectories.stream()
.filter(dir -> dir.type == DirectoryType.MAIN)
.findFirst();
}
}

private Optional<DirectoryInfo> handleThreeDirectoriesCase(Optional<DirectoryType> currentDirectory, List<DirectoryInfo> availableDirectories) {
String mainFilePath = String.valueOf(filePreferences.getMainFileDirectory());

if (currentDirectory.isEmpty()) {
return availableDirectories.stream()
.filter(dir -> dir.type == DirectoryType.GENERAL)
.findFirst();
}

DirectoryType current = currentDirectory.get();
return switch (current) {
case MAIN, USER_SPECIFIC -> availableDirectories.stream()
.filter(dir -> dir.type == DirectoryType.GENERAL)
.findFirst();
case GENERAL -> availableDirectories.stream()
.filter(dir -> dir.type == DirectoryType.USER_SPECIFIC)
.findFirst();
};
}
}
Loading
Loading