From 3e640f57add685e8d7df7e68fba80fba57578cf5 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Fri, 7 Oct 2022 18:01:21 +0000 Subject: [PATCH 01/18] Save files again relative to bib file --- CHANGELOG.md | 2 -- .../architecture/AllowedToUseSwing.java | 10 ---------- .../org/jabref/gui/desktop/JabRefDesktop.java | 19 ------------------ .../jabref/logic/layout/format/FileLink.java | 2 +- .../layout/format/FileLinkPreferences.java | 7 ++++--- .../logic/layout/format/WrapFileLinks.java | 2 +- .../jabref/preferences/FilePreferences.java | 19 +++++++++--------- .../jabref/preferences/JabRefPreferences.java | 20 +++---------------- .../architecture/MainArchitectureTests.java | 3 +-- .../org/jabref/logic/layout/LayoutTest.java | 3 ++- .../layout/format/WrapFileLinksTest.java | 9 +++++---- .../database/BibDatabaseContextTest.java | 8 +++----- 12 files changed, 30 insertions(+), 74 deletions(-) delete mode 100644 src/main/java/org/jabref/architecture/AllowedToUseSwing.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cdaae90658..dd397e66907 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,8 +36,6 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We removed "last-search-date" from the SLR feature, because the last-search-date can be deducted from the git logs. [#9116](https://github.com/JabRef/jabref/pull/9116) - A user can now add arbitrary data into `study.yml`. JabRef just ignores this data. [#9124](https://github.com/JabRef/jabref/pull/9124) - We reworked the External Changes Resolver dialog. [#9021](https://github.com/JabRef/jabref/pull/9021) -- The fallback directory of the file folder now is the general file directory. In case there was a directory configured for a library and this directory was not found, JabRef placed the PDF next to the .bib file and not into the general file directory. -- The global default directory for storing PDFs is now the subdirectory "JabRef" in the user's home. - We reworked the Define study parameters dialog. [#9123](https://github.com/JabRef/jabref/pull/9123) - We simplified the actions to fast-resolve duplicates to 'Keep Left', 'Keep Right', 'Keep Both' and 'Keep Merged'. [#9056](https://github.com/JabRef/jabref/issues/9056) - We fixed an issue where a message about changed metadata would occur on saving although nothing changed. [#9159](https://github.com/JabRef/jabref/issues/9159) diff --git a/src/main/java/org/jabref/architecture/AllowedToUseSwing.java b/src/main/java/org/jabref/architecture/AllowedToUseSwing.java deleted file mode 100644 index 99a40a4bade..00000000000 --- a/src/main/java/org/jabref/architecture/AllowedToUseSwing.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.jabref.architecture; - -/** - * Annotation to indicate that this logic class can access swing - */ -public @interface AllowedToUseSwing { - - // The rationale - String value(); -} diff --git a/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java b/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java index b74fea558d4..517ffc1689d 100644 --- a/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java +++ b/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java @@ -10,9 +10,6 @@ import java.util.Optional; import java.util.regex.Pattern; -import javax.swing.filechooser.FileSystemView; - -import org.jabref.architecture.AllowedToUseSwing; import org.jabref.gui.DialogService; import org.jabref.gui.Globals; import org.jabref.gui.desktop.os.DefaultDesktop; @@ -40,7 +37,6 @@ * TODO: Replace by http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html * http://stackoverflow.com/questions/18004150/desktop-api-is-not-supported-on-the-current-platform */ -@AllowedToUseSwing("Needs access to swing for the user's os dependent file chooser path") public class JabRefDesktop { private static final Logger LOGGER = LoggerFactory.getLogger(JabRefDesktop.class); @@ -292,21 +288,6 @@ public static void openConsole(File file, PreferencesService preferencesService, } } - /** - * Get the user's default file chooser directory - * - * @return The path to the directory - */ - public static String getDefaultFileChooserDirectory() { - // Property "user.home" might be "difficult" on Windows - // See https://stackoverflow.com/a/586917/873282 for a longer discussion - // The proposed solution is to use Swing's FileSystemView - // See https://stackoverflow.com/a/32914568/873282 - // As of 2022, System.getProperty("user.home") returns c:\Users\USERNAME on Windows 10, whereas - // the FileSystemView returns C:\Users\USERNAME\Documents, which is the "better" directory - return FileSystemView.getFileSystemView().getDefaultDirectory().getPath(); - } - // TODO: Move to OS.java public static NativeDesktop getNativeDesktop() { if (OS.WINDOWS) { diff --git a/src/main/java/org/jabref/logic/layout/format/FileLink.java b/src/main/java/org/jabref/logic/layout/format/FileLink.java index 79a5b4b31fc..f53d803644f 100644 --- a/src/main/java/org/jabref/logic/layout/format/FileLink.java +++ b/src/main/java/org/jabref/logic/layout/format/FileLink.java @@ -55,7 +55,7 @@ public String format(String field) { // ugly hack, the export routine has set a global variable before // starting the export, which contains the database's file directory: if (prefs.getFileDirForDatabase() == null) { - dirs = Collections.singletonList(Path.of(prefs.getMainFileDirectory())); + dirs = prefs.getMainFileDirectory().map(List::of).orElse(Collections.emptyList()); } else { dirs = prefs.getFileDirForDatabase(); } diff --git a/src/main/java/org/jabref/logic/layout/format/FileLinkPreferences.java b/src/main/java/org/jabref/logic/layout/format/FileLinkPreferences.java index 20b9dcd067e..93a46ee7db5 100644 --- a/src/main/java/org/jabref/logic/layout/format/FileLinkPreferences.java +++ b/src/main/java/org/jabref/logic/layout/format/FileLinkPreferences.java @@ -2,18 +2,19 @@ import java.nio.file.Path; import java.util.List; +import java.util.Optional; public class FileLinkPreferences { - private final String mainFileDirectory; + private final Optional mainFileDirectory; private final List fileDirForDatabase; - public FileLinkPreferences(String mainFileDirectory, List fileDirForDatabase) { + public FileLinkPreferences(Optional mainFileDirectory, List fileDirForDatabase) { this.mainFileDirectory = mainFileDirectory; this.fileDirForDatabase = fileDirForDatabase; } - public String getMainFileDirectory() { + public Optional getMainFileDirectory() { return mainFileDirectory; } diff --git a/src/main/java/org/jabref/logic/layout/format/WrapFileLinks.java b/src/main/java/org/jabref/logic/layout/format/WrapFileLinks.java index f93c3e097b3..d2a881d921d 100644 --- a/src/main/java/org/jabref/logic/layout/format/WrapFileLinks.java +++ b/src/main/java/org/jabref/logic/layout/format/WrapFileLinks.java @@ -199,7 +199,7 @@ public String format(String field) { // ugly hack, the export routine has set a global variable before // starting the export, which contains the database's file directory: if ((prefs.getFileDirForDatabase() == null) || prefs.getFileDirForDatabase().isEmpty()) { - dirs = Collections.singletonList(Path.of(prefs.getMainFileDirectory())); + dirs = prefs.getMainFileDirectory().map(List::of).orElse(Collections.emptyList()); } else { dirs = prefs.getFileDirForDatabase(); } diff --git a/src/main/java/org/jabref/preferences/FilePreferences.java b/src/main/java/org/jabref/preferences/FilePreferences.java index 219fe1c9d49..b4c6cbd5b98 100644 --- a/src/main/java/org/jabref/preferences/FilePreferences.java +++ b/src/main/java/org/jabref/preferences/FilePreferences.java @@ -16,6 +16,7 @@ import javafx.collections.ObservableSet; import org.jabref.gui.externalfiletype.ExternalFileType; +import org.jabref.gui.util.OptionalObjectProperty; import org.jabref.model.strings.StringUtil; public class FilePreferences { @@ -23,7 +24,7 @@ public class FilePreferences { public static final String[] DEFAULT_FILENAME_PATTERNS = new String[] {"[bibtexkey]", "[bibtexkey] - [title]"}; private final StringProperty user = new SimpleStringProperty(); - private final SimpleStringProperty mainFileDirectory = new SimpleStringProperty(); + private final OptionalObjectProperty mainFileDirectory = OptionalObjectProperty.empty(); private final BooleanProperty storeFilesRelativeToBibFile = new SimpleBooleanProperty(); private final StringProperty fileNamePattern = new SimpleStringProperty(); private final StringProperty fileDirectoryPattern = new SimpleStringProperty(); @@ -42,7 +43,7 @@ public FilePreferences(String user, Path workingDirectory, Set externalFileTypes) { this.user.setValue(user); - this.mainFileDirectory.setValue(mainFileDirectory); + this.setMainFileDirectory(mainFileDirectory); this.storeFilesRelativeToBibFile.setValue(storeFilesRelativeToBibFile); this.fileNamePattern.setValue(fileNamePattern); this.fileDirectoryPattern.setValue(fileDirectoryPattern); @@ -57,19 +58,19 @@ public String getUser() { } public Optional getFileDirectory() { - if (StringUtil.isBlank(mainFileDirectory.getValue())) { - return Optional.empty(); - } else { - return Optional.of(Path.of(mainFileDirectory.getValue())); - } + return mainFileDirectory.getValue(); } - public StringProperty mainFileDirectoryProperty() { + public OptionalObjectProperty mainFileDirectoryProperty() { return mainFileDirectory; } public void setMainFileDirectory(String mainFileDirectory) { - this.mainFileDirectory.set(mainFileDirectory); + if (StringUtil.isBlank(mainFileDirectory)) { + this.mainFileDirectory.setValue(Optional.empty()); + } else { + this.mainFileDirectory.setValue(Optional.of(Path.of(mainFileDirectory))); + } } public boolean shouldStoreFilesRelativeToBibFile() { diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 166e5d9f187..80dba5c9cf1 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -1109,7 +1109,7 @@ public void importPreferences(Path file) throws JabRefException { @Override public FileLinkPreferences getFileLinkPreferences() { return new FileLinkPreferences( - getFilePreferences().mainFileDirectoryProperty().get(), + getFilePreferences().getFileDirectory(), fileDirForDatabase); } @@ -2179,20 +2179,6 @@ public FieldWriterPreferences getFieldWriterPreferences() { getFieldContentParserPreferences()); } - /** - * Ensures that the main file directory is a non-empty String. - * The directory is NOT created, because creation of the directory is the task of the respective methods. - * - * @param originalDirectory the directory as configured - */ - private String determineMainFileDirectory(String originalDirectory) { - if ((originalDirectory != null) && !originalDirectory.isEmpty()) { - // A non-empty directory is kept - return originalDirectory; - } - return JabRefDesktop.getDefaultFileChooserDirectory(); - } - @Override public FilePreferences getFilePreferences() { if (Objects.nonNull(filePreferences)) { @@ -2201,7 +2187,7 @@ public FilePreferences getFilePreferences() { filePreferences = new FilePreferences( getInternalPreferences().getUser(), - determineMainFileDirectory(get(MAIN_FILE_DIRECTORY)), + get(MAIN_FILE_DIRECTORY), getBoolean(STORE_RELATIVE_TO_BIB), get(IMPORT_FILENAMEPATTERN), get(IMPORT_FILEDIRPATTERN), @@ -2211,7 +2197,7 @@ public FilePreferences getFilePreferences() { ExternalFileTypes.fromString(get(EXTERNAL_FILE_TYPES)) ); - EasyBind.listen(filePreferences.mainFileDirectoryProperty(), (obs, oldValue, newValue) -> put(MAIN_FILE_DIRECTORY, newValue)); + EasyBind.listen(filePreferences.mainFileDirectoryProperty(), (obs, oldValue, newValue) -> put(MAIN_FILE_DIRECTORY, newValue.map(Path::toString).orElse(""))); EasyBind.listen(filePreferences.storeFilesRelativeToBibFileProperty(), (obs, oldValue, newValue) -> putBoolean(STORE_RELATIVE_TO_BIB, newValue)); EasyBind.listen(filePreferences.fileNamePatternProperty(), (obs, oldValue, newValue) -> put(IMPORT_FILENAMEPATTERN, newValue)); EasyBind.listen(filePreferences.fileDirectoryPatternProperty(), (obs, oldValue, newValue) -> put(IMPORT_FILEDIRPATTERN, newValue)); diff --git a/src/test/java/org/jabref/architecture/MainArchitectureTests.java b/src/test/java/org/jabref/architecture/MainArchitectureTests.java index 5f67761f064..91301adca91 100644 --- a/src/test/java/org/jabref/architecture/MainArchitectureTests.java +++ b/src/test/java/org/jabref/architecture/MainArchitectureTests.java @@ -32,8 +32,7 @@ public static void doNotUseApacheCommonsLang3(JavaClasses classes) { @ArchTest public static void doNotUseSwing(JavaClasses classes) { // This checks for all Swing packages, but not the UndoManager - noClasses().that().areNotAnnotatedWith(AllowedToUseSwing.class) - .should().accessClassesThat() + noClasses().should().accessClassesThat() .resideInAnyPackage("javax.swing", "javax.swing.border..", "javax.swing.colorchooser..", diff --git a/src/test/java/org/jabref/logic/layout/LayoutTest.java b/src/test/java/org/jabref/logic/layout/LayoutTest.java index b9700d9faf5..b5e82ad1b13 100644 --- a/src/test/java/org/jabref/logic/layout/LayoutTest.java +++ b/src/test/java/org/jabref/logic/layout/LayoutTest.java @@ -5,6 +5,7 @@ import java.io.StringReader; import java.nio.file.Path; import java.util.Collections; +import java.util.Optional; import org.jabref.logic.layout.format.FileLinkPreferences; import org.jabref.logic.layout.format.NameFormatterPreferences; @@ -135,7 +136,7 @@ void beginConditionals() throws IOException { @Test void wrapFileLinksExpandFile() throws IOException { when(layoutFormatterPreferences.getFileLinkPreferences()).thenReturn( - new FileLinkPreferences("", Collections.singletonList(Path.of("src/test/resources/pdfs/")))); + new FileLinkPreferences(Optional.empty(), Collections.singletonList(Path.of("src/test/resources/pdfs/")))); BibEntry entry = new BibEntry(StandardEntryType.Article); entry.addFile(new LinkedFile("Test file", Path.of("encrypted.pdf"), "PDF")); diff --git a/src/test/java/org/jabref/logic/layout/format/WrapFileLinksTest.java b/src/test/java/org/jabref/logic/layout/format/WrapFileLinksTest.java index aad1356ab1d..4137e54bcdf 100644 --- a/src/test/java/org/jabref/logic/layout/format/WrapFileLinksTest.java +++ b/src/test/java/org/jabref/logic/layout/format/WrapFileLinksTest.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.nio.file.Path; import java.util.Collections; +import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -17,7 +18,7 @@ class WrapFileLinksTest { @BeforeEach void setUp() { - FileLinkPreferences preferences = new FileLinkPreferences("", Collections.emptyList()); + FileLinkPreferences preferences = new FileLinkPreferences(Optional.empty(), Collections.emptyList()); formatter = new WrapFileLinks(preferences); } @@ -97,7 +98,7 @@ void testEndingBracket() { @Test void testPath() throws IOException { - FileLinkPreferences preferences = new FileLinkPreferences("", + FileLinkPreferences preferences = new FileLinkPreferences(Optional.empty(), Collections.singletonList(Path.of("src/test/resources/pdfs/"))); formatter = new WrapFileLinks(preferences); formatter.setArgument("\\p"); @@ -107,7 +108,7 @@ void testPath() throws IOException { @Test void testPathFallBackToGeneratedDir() throws IOException { - FileLinkPreferences preferences = new FileLinkPreferences("src/test/resources/pdfs/", + FileLinkPreferences preferences = new FileLinkPreferences(Optional.of(Path.of("src/test/resources/pdfs/")), Collections.emptyList()); formatter = new WrapFileLinks(preferences); formatter.setArgument("\\p"); @@ -117,7 +118,7 @@ void testPathFallBackToGeneratedDir() throws IOException { @Test void testPathReturnsRelativePathIfNotFound() { - FileLinkPreferences preferences = new FileLinkPreferences("", + FileLinkPreferences preferences = new FileLinkPreferences(Optional.empty(), Collections.singletonList(Path.of("src/test/resources/pdfs/"))); formatter = new WrapFileLinks(preferences); formatter.setArgument("\\p"); diff --git a/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java b/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java index 181117f7dac..f3483d791e1 100644 --- a/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java +++ b/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java @@ -5,7 +5,6 @@ import java.util.List; import java.util.Optional; -import org.jabref.gui.desktop.JabRefDesktop; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.types.IEEETranEntryType; import org.jabref.model.metadata.MetaData; @@ -94,13 +93,12 @@ void getFileDirectoriesWithMetadata() { @Test void getUserFileDirectoryIfAllAreEmpty() { when(fileDirPrefs.shouldStoreFilesRelativeToBibFile()).thenReturn(false); + Path userDirGlobal = Path.of("some random path").toAbsolutePath(); + when(fileDirPrefs.getFileDirectory()).thenReturn(Optional.of(userDirGlobal)); - Path userDirJabRef = Path.of(JabRefDesktop.getDefaultFileChooserDirectory(), "JabRef"); - - when(fileDirPrefs.getFileDirectory()).thenReturn(Optional.of(userDirJabRef)); BibDatabaseContext database = new BibDatabaseContext(); database.setDatabasePath(Path.of("biblio.bib")); - assertEquals(Collections.singletonList(userDirJabRef), database.getFileDirectories(fileDirPrefs)); + assertEquals(Collections.singletonList(userDirGlobal), database.getFileDirectories(fileDirPrefs)); } @Test From de27af59300ba02be29af75d89200fb1a5813613 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Wed, 12 Oct 2022 17:09:40 +0200 Subject: [PATCH 02/18] Reworded --- .../gui/preferences/linkedfiles/LinkedFilesTabViewModel.java | 2 +- .../java/org/jabref/model/database/BibDatabaseContext.java | 2 +- src/main/java/org/jabref/preferences/FilePreferences.java | 2 +- src/main/java/org/jabref/preferences/JabRefPreferences.java | 2 +- .../jabref/logic/pdf/search/indexing/DocumentReaderTest.java | 5 ++--- .../org/jabref/model/database/BibDatabaseContextTest.java | 2 +- 6 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/jabref/gui/preferences/linkedfiles/LinkedFilesTabViewModel.java b/src/main/java/org/jabref/gui/preferences/linkedfiles/LinkedFilesTabViewModel.java index d04d90e8f54..ea38ed9f858 100644 --- a/src/main/java/org/jabref/gui/preferences/linkedfiles/LinkedFilesTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/linkedfiles/LinkedFilesTabViewModel.java @@ -74,7 +74,7 @@ public LinkedFilesTabViewModel(DialogService dialogService, PreferencesService p @Override public void setValues() { // External files preferences / Attached files preferences / File preferences - mainFileDirectoryProperty.setValue(filePreferences.getFileDirectory().orElse(Path.of("")).toString()); + mainFileDirectoryProperty.setValue(filePreferences.getMainFileDirectory().orElse(Path.of("")).toString()); useMainFileDirectoryProperty.setValue(!filePreferences.shouldStoreFilesRelativeToBibFile()); useBibLocationAsPrimaryProperty.setValue(filePreferences.shouldStoreFilesRelativeToBibFile()); fulltextIndex.setValue(filePreferences.shouldFulltextIndexLinkedFiles()); diff --git a/src/main/java/org/jabref/model/database/BibDatabaseContext.java b/src/main/java/org/jabref/model/database/BibDatabaseContext.java index 4331f66d90b..436f2b070c7 100644 --- a/src/main/java/org/jabref/model/database/BibDatabaseContext.java +++ b/src/main/java/org/jabref/model/database/BibDatabaseContext.java @@ -171,7 +171,7 @@ public List getFileDirectories(FilePreferences preferences) { }); } else { // Main file directory - preferences.getFileDirectory().ifPresent(fileDirs::add); + preferences.getMainFileDirectory().ifPresent(fileDirs::add); } return fileDirs.stream().map(Path::toAbsolutePath).collect(Collectors.toList()); diff --git a/src/main/java/org/jabref/preferences/FilePreferences.java b/src/main/java/org/jabref/preferences/FilePreferences.java index b4c6cbd5b98..98dc9c25c17 100644 --- a/src/main/java/org/jabref/preferences/FilePreferences.java +++ b/src/main/java/org/jabref/preferences/FilePreferences.java @@ -57,7 +57,7 @@ public String getUser() { return user.getValue(); } - public Optional getFileDirectory() { + public Optional getMainFileDirectory() { return mainFileDirectory.getValue(); } diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 80dba5c9cf1..32da0dd16d0 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -1109,7 +1109,7 @@ public void importPreferences(Path file) throws JabRefException { @Override public FileLinkPreferences getFileLinkPreferences() { return new FileLinkPreferences( - getFilePreferences().getFileDirectory(), + getFilePreferences().getMainFileDirectory(), fileDirForDatabase); } diff --git a/src/test/java/org/jabref/logic/pdf/search/indexing/DocumentReaderTest.java b/src/test/java/org/jabref/logic/pdf/search/indexing/DocumentReaderTest.java index 27cdb271844..047689eb543 100644 --- a/src/test/java/org/jabref/logic/pdf/search/indexing/DocumentReaderTest.java +++ b/src/test/java/org/jabref/logic/pdf/search/indexing/DocumentReaderTest.java @@ -1,6 +1,5 @@ package org.jabref.logic.pdf.search.indexing; -import java.io.IOException; import java.nio.file.Path; import java.util.Collections; import java.util.List; @@ -35,12 +34,12 @@ public void setup() { when(databaseContext.getFileDirectories(Mockito.any())).thenReturn(Collections.singletonList(Path.of("src/test/resources/pdfs"))); this.filePreferences = mock(FilePreferences.class); when(filePreferences.getUser()).thenReturn("test"); - when(filePreferences.getFileDirectory()).thenReturn(Optional.empty()); + when(filePreferences.getMainFileDirectory()).thenReturn(Optional.empty()); when(filePreferences.shouldStoreFilesRelativeToBibFile()).thenReturn(true); } @Test - public void unknownFileTestShouldReturnEmptyList() throws IOException { + public void unknownFileTestShouldReturnEmptyList() { // given BibEntry entry = new BibEntry(); entry.setFiles(Collections.singletonList(new LinkedFile("Wrong path", "NOT_PRESENT.pdf", "Type"))); diff --git a/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java b/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java index f3483d791e1..5a793167be8 100644 --- a/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java +++ b/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java @@ -94,7 +94,7 @@ void getFileDirectoriesWithMetadata() { void getUserFileDirectoryIfAllAreEmpty() { when(fileDirPrefs.shouldStoreFilesRelativeToBibFile()).thenReturn(false); Path userDirGlobal = Path.of("some random path").toAbsolutePath(); - when(fileDirPrefs.getFileDirectory()).thenReturn(Optional.of(userDirGlobal)); + when(fileDirPrefs.getMainFileDirectory()).thenReturn(Optional.of(userDirGlobal)); BibDatabaseContext database = new BibDatabaseContext(); database.setDatabasePath(Path.of("biblio.bib")); From 07491286e61d4873e7045bdbdd2038819a4ef493 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Wed, 12 Oct 2022 18:07:46 +0200 Subject: [PATCH 03/18] Reintroduced default file chooser directory without swing --- src/main/java/module-info.java | 1 + .../jabref/gui/desktop/os/NativeDesktop.java | 10 +++++++- .../org/jabref/gui/desktop/os/Windows.java | 24 +++++++++++++++++++ .../jabref/preferences/JabRefPreferences.java | 4 +++- 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 4edbf562d8c..fbc9f463f2d 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -114,6 +114,7 @@ requires com.fasterxml.jackson.dataformat.yaml; requires com.fasterxml.jackson.datatype.jsr310; requires net.harawata.appdirs; + requires com.sun.jna.platform; requires org.eclipse.jgit; uses org.eclipse.jgit.transport.SshSessionFactory; diff --git a/src/main/java/org/jabref/gui/desktop/os/NativeDesktop.java b/src/main/java/org/jabref/gui/desktop/os/NativeDesktop.java index 349368ef3a6..c255fa50fb5 100644 --- a/src/main/java/org/jabref/gui/desktop/os/NativeDesktop.java +++ b/src/main/java/org/jabref/gui/desktop/os/NativeDesktop.java @@ -14,7 +14,6 @@ public interface NativeDesktop { * * @param filePath The filename. * @param application Link to the app that opens the file. - * @throws IOException */ void openFileWithApplication(String filePath, String application) throws IOException; @@ -31,6 +30,15 @@ public interface NativeDesktop { */ Path getApplicationDirectory(); + /** + * Get the user's default file chooser directory + * + * @return The path to the directory + */ + default Path getDefaultFileChooserDirectory() { + return Path.of(System.getProperty("user.home")); + } + /** * Returns the path to the system's user directory. * diff --git a/src/main/java/org/jabref/gui/desktop/os/Windows.java b/src/main/java/org/jabref/gui/desktop/os/Windows.java index d85722740d6..f0446012d79 100644 --- a/src/main/java/org/jabref/gui/desktop/os/Windows.java +++ b/src/main/java/org/jabref/gui/desktop/os/Windows.java @@ -11,7 +11,16 @@ import org.jabref.gui.externalfiletype.ExternalFileType; import org.jabref.gui.externalfiletype.ExternalFileTypes; +import com.sun.jna.platform.win32.KnownFolders; +import com.sun.jna.platform.win32.Shell32Util; +import com.sun.jna.platform.win32.ShlObj; +import com.sun.jna.platform.win32.Win32Exception; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class Windows implements NativeDesktop { + private static final Logger LOGGER = LoggerFactory.getLogger(Windows.class); + private static final String DEFAULT_EXECUTABLE_EXTENSION = ".exe"; @Override @@ -70,6 +79,21 @@ public Path getApplicationDirectory() { return getUserDirectory(); } + @Override + public Path getDefaultFileChooserDirectory() { + try { + try { + return Path.of(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_Documents)); + } catch (UnsatisfiedLinkError e) { + // Windows Vista or earlier + return Path.of(Shell32Util.getFolderPath(ShlObj.CSIDL_MYDOCUMENTS)); + } + } catch (Win32Exception e) { + LOGGER.error(e.getMessage()); + return Path.of(System.getProperty("user.home")); + } + } + @Override public void openFileWithApplication(String filePath, String application) throws IOException { new ProcessBuilder(Path.of(application).toString(), Path.of(filePath).toString()).start(); diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 32da0dd16d0..69323cce9f9 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -2187,7 +2187,9 @@ public FilePreferences getFilePreferences() { filePreferences = new FilePreferences( getInternalPreferences().getUser(), - get(MAIN_FILE_DIRECTORY), + StringUtil.isNotBlank(get(MAIN_FILE_DIRECTORY)) + ? get(MAIN_FILE_DIRECTORY) + : JabRefDesktop.getNativeDesktop().getDefaultFileChooserDirectory().toString(), getBoolean(STORE_RELATIVE_TO_BIB), get(IMPORT_FILENAMEPATTERN), get(IMPORT_FILEDIRPATTERN), From 7dc926eef412122bceadb991d175d9605607ad82 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Wed, 12 Oct 2022 18:20:43 +0200 Subject: [PATCH 04/18] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2220e09ba1a..c8a9dc97afe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,8 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We removed "last-search-date" from the SLR feature, because the last-search-date can be deducted from the git logs. [#9116](https://github.com/JabRef/jabref/pull/9116) - A user can now add arbitrary data into `study.yml`. JabRef just ignores this data. [#9124](https://github.com/JabRef/jabref/pull/9124) - We reworked the External Changes Resolver dialog. [#9021](https://github.com/JabRef/jabref/pull/9021) +- The fallback directory of the file folder now is the general file directory. In case there was a directory configured for a library and this directory was not found, JabRef placed the PDF next to the .bib file and not into the general file directory. +- The global default directory for storing PDFs is now the subdirectory "JabRef" in the user's home. - We reworked the Define study parameters dialog. [#9123](https://github.com/JabRef/jabref/pull/9123) - We simplified the actions to fast-resolve duplicates to 'Keep Left', 'Keep Right', 'Keep Both' and 'Keep Merged'. [#9056](https://github.com/JabRef/jabref/issues/9056) - We fixed an issue where a message about changed metadata would occur on saving although nothing changed. [#9159](https://github.com/JabRef/jabref/issues/9159) From 6cb05d33d005190e5c0f111981bdeb513d0619ce Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Wed, 12 Oct 2022 18:22:21 +0200 Subject: [PATCH 05/18] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8a9dc97afe..f0db46ad3dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,7 +37,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We removed "last-search-date" from the SLR feature, because the last-search-date can be deducted from the git logs. [#9116](https://github.com/JabRef/jabref/pull/9116) - A user can now add arbitrary data into `study.yml`. JabRef just ignores this data. [#9124](https://github.com/JabRef/jabref/pull/9124) - We reworked the External Changes Resolver dialog. [#9021](https://github.com/JabRef/jabref/pull/9021) -- The fallback directory of the file folder now is the general file directory. In case there was a directory configured for a library and this directory was not found, JabRef placed the PDF next to the .bib file and not into the general file directory. +- The fallback directory of the file folder now is the general file directory. In case there was a directory configured for a library and this directory was not found, JabRef placed the PDF next to the .bib file and not into the general file directory. [#9222](https://github.com/JabRef/jabref/pull/9222) - The global default directory for storing PDFs is now the subdirectory "JabRef" in the user's home. - We reworked the Define study parameters dialog. [#9123](https://github.com/JabRef/jabref/pull/9123) - We simplified the actions to fast-resolve duplicates to 'Keep Left', 'Keep Right', 'Keep Both' and 'Keep Merged'. [#9056](https://github.com/JabRef/jabref/issues/9056) From dfd64114a609a522e995e0859d45cf127ab31999 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Wed, 12 Oct 2022 19:02:38 +0200 Subject: [PATCH 06/18] Update NativeDesktop.java --- src/main/java/org/jabref/gui/desktop/os/NativeDesktop.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/jabref/gui/desktop/os/NativeDesktop.java b/src/main/java/org/jabref/gui/desktop/os/NativeDesktop.java index c255fa50fb5..9ce8c37193f 100644 --- a/src/main/java/org/jabref/gui/desktop/os/NativeDesktop.java +++ b/src/main/java/org/jabref/gui/desktop/os/NativeDesktop.java @@ -36,6 +36,10 @@ public interface NativeDesktop { * @return The path to the directory */ default Path getDefaultFileChooserDirectory() { + // We do not return a subdirectory "JabRef", because + // - the directory might not exist at this point of the method + // - we might not have the rights to create a directory + // - getters should not have any side effect return Path.of(System.getProperty("user.home")); } From 28624ffdab89bd4a49cf5226e4b3b0c3db8f57b1 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Wed, 12 Oct 2022 19:22:06 +0200 Subject: [PATCH 07/18] Reworked ADR --- ...0026-use-jna-to-determine-default-directory.md} | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) rename docs/decisions/{0026-use-swings-file-chooser-to-determine-default-directory.md => 0026-use-jna-to-determine-default-directory.md} (77%) diff --git a/docs/decisions/0026-use-swings-file-chooser-to-determine-default-directory.md b/docs/decisions/0026-use-jna-to-determine-default-directory.md similarity index 77% rename from docs/decisions/0026-use-swings-file-chooser-to-determine-default-directory.md rename to docs/decisions/0026-use-jna-to-determine-default-directory.md index 127067b12f8..40b76f03e39 100644 --- a/docs/decisions/0026-use-swings-file-chooser-to-determine-default-directory.md +++ b/docs/decisions/0026-use-jna-to-determine-default-directory.md @@ -2,7 +2,7 @@ nav_order: 26 parent: Decision Records --- -# Use Swing's FileChooser to Determine Default Directory +# Use Java Native Access to Determine Default Directory ## Context and Problem Statement @@ -20,11 +20,11 @@ How to determine the "best" directory native for the OS the user runs. * Use Swing's FileChooser to Determine Default Directory * Use `user.home` * [AppDirs](https://github.com/harawata/appdirs) +* [Java Native Access](https://github.com/java-native-access/jna) ## Decision Outcome -Chosen option: "Use Swing's FileChooser to Determine Default Directory", because -comes out best (see below). +Chosen option: "Java Native Access", because comes out best (see below). ## Pros and Cons of the Options @@ -51,7 +51,13 @@ There is `System.getProperty("user.home");`. > AppDirs is a small java library which provides a path to the platform dependent special folder/directory. * Good, because already used in JabRef -* Bad, because does not use "MyDocuments" on Windows, but rather `C:\Users\\AppData\\` as basis +* Bad, because does not use `Documents` on Windows, but rather `C:\Users\\AppData\\` as basis + +### Java Native Access + +* Good, because no additional dependency required, as it is already loaded by AppDirs +* Good, because it is well maintained and widely used +* Good, because it provides direct access to `Documents` and other system variables ## More Information From 1026011c10dbde634c043ab905e88056bf18b417 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Wed, 12 Oct 2022 19:26:43 +0200 Subject: [PATCH 08/18] Added mac directory --- src/main/java/org/jabref/gui/desktop/os/OSX.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/org/jabref/gui/desktop/os/OSX.java b/src/main/java/org/jabref/gui/desktop/os/OSX.java index d7a3c6cd710..aee59fd2254 100644 --- a/src/main/java/org/jabref/gui/desktop/os/OSX.java +++ b/src/main/java/org/jabref/gui/desktop/os/OSX.java @@ -52,4 +52,13 @@ public String detectProgramPath(String programName, String directoryName) { public Path getApplicationDirectory() { return Path.of("/Applications"); } + + @Override + public Path getDefaultFileChooserDirectory() { + // We do not return a subdirectory "JabRef", because + // - the directory might not exist at this point of the method + // - we might not have the rights to create a directory + // - getters should not have any side effect + return Path.of(System.getProperty("user.home") + "/Documents"); + } } From b95b021eb3d8cd1415f945cea9c27e8aa7dc7484 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Wed, 12 Oct 2022 19:28:48 +0200 Subject: [PATCH 09/18] Removed comments --- src/main/java/org/jabref/gui/desktop/os/OSX.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/org/jabref/gui/desktop/os/OSX.java b/src/main/java/org/jabref/gui/desktop/os/OSX.java index aee59fd2254..ece1d553d56 100644 --- a/src/main/java/org/jabref/gui/desktop/os/OSX.java +++ b/src/main/java/org/jabref/gui/desktop/os/OSX.java @@ -55,10 +55,6 @@ public Path getApplicationDirectory() { @Override public Path getDefaultFileChooserDirectory() { - // We do not return a subdirectory "JabRef", because - // - the directory might not exist at this point of the method - // - we might not have the rights to create a directory - // - getters should not have any side effect return Path.of(System.getProperty("user.home") + "/Documents"); } } From eab433c510b7cd3877f9e34d72e988a8394a13b0 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Wed, 12 Oct 2022 20:15:50 +0200 Subject: [PATCH 10/18] Added linux support --- src/main/java/org/jabref/gui/desktop/os/Linux.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/gui/desktop/os/Linux.java b/src/main/java/org/jabref/gui/desktop/os/Linux.java index 289efb4f576..a354423ee04 100644 --- a/src/main/java/org/jabref/gui/desktop/os/Linux.java +++ b/src/main/java/org/jabref/gui/desktop/os/Linux.java @@ -8,6 +8,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Locale; +import java.util.Objects; import java.util.Optional; import org.jabref.architecture.AllowedToUseAwt; @@ -134,7 +135,7 @@ public void openConsole(String absolutePath, DialogService dialogService) throws if (emulatorName != null) { emulatorName = emulatorName.substring(emulatorName.lastIndexOf(File.separator) + 1); - String[] cmd = {}; + String[] cmd; if (emulatorName.contains("gnome")) { cmd = new String[] {"gnome-terminal", "--working-directory=", absolutePath}; } else if (emulatorName.contains("xfce4")) { @@ -167,4 +168,12 @@ public String detectProgramPath(String programName, String directoryName) { public Path getApplicationDirectory() { return Path.of("/usr/lib/"); } + + @Override + public Path getDefaultFileChooserDirectory() { + return Path.of(Objects.requireNonNullElse( + System.getenv("XDG_DOCUMENTS_DIR"), + System.getProperty("users.home") + "/Documents") + ); + } } From c90c543162a971a615a2095cc8d4bf2eba2caf98 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Mon, 17 Oct 2022 20:09:39 +0200 Subject: [PATCH 11/18] Revert "Save files again relative to bib file" This reverts commit 3e640f57add685e8d7df7e68fba80fba57578cf5. # Conflicts: # CHANGELOG.md # src/main/java/org/jabref/preferences/FilePreferences.java # src/main/java/org/jabref/preferences/JabRefPreferences.java # src/test/java/org/jabref/model/database/BibDatabaseContextTest.java --- CHANGELOG.md | 4 ++-- .../architecture/AllowedToUseSwing.java | 10 +++++++++ .../org/jabref/gui/desktop/JabRefDesktop.java | 19 ++++++++++++++++ .../jabref/logic/layout/format/FileLink.java | 2 +- .../layout/format/FileLinkPreferences.java | 7 +++--- .../logic/layout/format/WrapFileLinks.java | 2 +- .../jabref/preferences/FilePreferences.java | 21 +++++++++--------- .../jabref/preferences/JabRefPreferences.java | 22 ++++++++++++++----- .../architecture/MainArchitectureTests.java | 3 ++- .../org/jabref/logic/layout/LayoutTest.java | 3 +-- .../layout/format/WrapFileLinksTest.java | 9 ++++---- .../database/BibDatabaseContextTest.java | 7 +++--- 12 files changed, 74 insertions(+), 35 deletions(-) create mode 100644 src/main/java/org/jabref/architecture/AllowedToUseSwing.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 748589ad3b7..830fb823352 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,8 +37,8 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We changed the button label from "Return to JabRef" to "Return to library" to better indicate the purpose of the action. - A user can now add arbitrary data into `study.yml`. JabRef just ignores this data. [#9124](https://github.com/JabRef/jabref/pull/9124) - We reworked the External Changes Resolver dialog. [#9021](https://github.com/JabRef/jabref/pull/9021) -- The fallback directory of the file folder now is the general file directory. In case there was a directory configured for a library and this directory was not found, JabRef placed the PDF next to the .bib file and not into the general file directory. [#9222](https://github.com/JabRef/jabref/pull/9222) -- The global default directory for storing PDFs is now the subdirectory "JabRef" in the user's home. +- The fallback directory of the file folder now is the general file directory. In case there was a directory configured for a library and this directory was not found, JabRef placed the PDF next to the .bib file and not into the general file directory. +- The global default directory for storing PDFs is now the documents folder in the user's home. - We reworked the Define study parameters dialog. [#9123](https://github.com/JabRef/jabref/pull/9123) - We simplified the actions to fast-resolve duplicates to 'Keep Left', 'Keep Right', 'Keep Both' and 'Keep Merged'. [#9056](https://github.com/JabRef/jabref/issues/9056) - We fixed an issue where a message about changed metadata would occur on saving although nothing changed. [#9159](https://github.com/JabRef/jabref/issues/9159) diff --git a/src/main/java/org/jabref/architecture/AllowedToUseSwing.java b/src/main/java/org/jabref/architecture/AllowedToUseSwing.java new file mode 100644 index 00000000000..99a40a4bade --- /dev/null +++ b/src/main/java/org/jabref/architecture/AllowedToUseSwing.java @@ -0,0 +1,10 @@ +package org.jabref.architecture; + +/** + * Annotation to indicate that this logic class can access swing + */ +public @interface AllowedToUseSwing { + + // The rationale + String value(); +} diff --git a/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java b/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java index 517ffc1689d..b74fea558d4 100644 --- a/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java +++ b/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java @@ -10,6 +10,9 @@ import java.util.Optional; import java.util.regex.Pattern; +import javax.swing.filechooser.FileSystemView; + +import org.jabref.architecture.AllowedToUseSwing; import org.jabref.gui.DialogService; import org.jabref.gui.Globals; import org.jabref.gui.desktop.os.DefaultDesktop; @@ -37,6 +40,7 @@ * TODO: Replace by http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html * http://stackoverflow.com/questions/18004150/desktop-api-is-not-supported-on-the-current-platform */ +@AllowedToUseSwing("Needs access to swing for the user's os dependent file chooser path") public class JabRefDesktop { private static final Logger LOGGER = LoggerFactory.getLogger(JabRefDesktop.class); @@ -288,6 +292,21 @@ public static void openConsole(File file, PreferencesService preferencesService, } } + /** + * Get the user's default file chooser directory + * + * @return The path to the directory + */ + public static String getDefaultFileChooserDirectory() { + // Property "user.home" might be "difficult" on Windows + // See https://stackoverflow.com/a/586917/873282 for a longer discussion + // The proposed solution is to use Swing's FileSystemView + // See https://stackoverflow.com/a/32914568/873282 + // As of 2022, System.getProperty("user.home") returns c:\Users\USERNAME on Windows 10, whereas + // the FileSystemView returns C:\Users\USERNAME\Documents, which is the "better" directory + return FileSystemView.getFileSystemView().getDefaultDirectory().getPath(); + } + // TODO: Move to OS.java public static NativeDesktop getNativeDesktop() { if (OS.WINDOWS) { diff --git a/src/main/java/org/jabref/logic/layout/format/FileLink.java b/src/main/java/org/jabref/logic/layout/format/FileLink.java index f53d803644f..79a5b4b31fc 100644 --- a/src/main/java/org/jabref/logic/layout/format/FileLink.java +++ b/src/main/java/org/jabref/logic/layout/format/FileLink.java @@ -55,7 +55,7 @@ public String format(String field) { // ugly hack, the export routine has set a global variable before // starting the export, which contains the database's file directory: if (prefs.getFileDirForDatabase() == null) { - dirs = prefs.getMainFileDirectory().map(List::of).orElse(Collections.emptyList()); + dirs = Collections.singletonList(Path.of(prefs.getMainFileDirectory())); } else { dirs = prefs.getFileDirForDatabase(); } diff --git a/src/main/java/org/jabref/logic/layout/format/FileLinkPreferences.java b/src/main/java/org/jabref/logic/layout/format/FileLinkPreferences.java index 93a46ee7db5..20b9dcd067e 100644 --- a/src/main/java/org/jabref/logic/layout/format/FileLinkPreferences.java +++ b/src/main/java/org/jabref/logic/layout/format/FileLinkPreferences.java @@ -2,19 +2,18 @@ import java.nio.file.Path; import java.util.List; -import java.util.Optional; public class FileLinkPreferences { - private final Optional mainFileDirectory; + private final String mainFileDirectory; private final List fileDirForDatabase; - public FileLinkPreferences(Optional mainFileDirectory, List fileDirForDatabase) { + public FileLinkPreferences(String mainFileDirectory, List fileDirForDatabase) { this.mainFileDirectory = mainFileDirectory; this.fileDirForDatabase = fileDirForDatabase; } - public Optional getMainFileDirectory() { + public String getMainFileDirectory() { return mainFileDirectory; } diff --git a/src/main/java/org/jabref/logic/layout/format/WrapFileLinks.java b/src/main/java/org/jabref/logic/layout/format/WrapFileLinks.java index d2a881d921d..f93c3e097b3 100644 --- a/src/main/java/org/jabref/logic/layout/format/WrapFileLinks.java +++ b/src/main/java/org/jabref/logic/layout/format/WrapFileLinks.java @@ -199,7 +199,7 @@ public String format(String field) { // ugly hack, the export routine has set a global variable before // starting the export, which contains the database's file directory: if ((prefs.getFileDirForDatabase() == null) || prefs.getFileDirForDatabase().isEmpty()) { - dirs = prefs.getMainFileDirectory().map(List::of).orElse(Collections.emptyList()); + dirs = Collections.singletonList(Path.of(prefs.getMainFileDirectory())); } else { dirs = prefs.getFileDirForDatabase(); } diff --git a/src/main/java/org/jabref/preferences/FilePreferences.java b/src/main/java/org/jabref/preferences/FilePreferences.java index 98dc9c25c17..219fe1c9d49 100644 --- a/src/main/java/org/jabref/preferences/FilePreferences.java +++ b/src/main/java/org/jabref/preferences/FilePreferences.java @@ -16,7 +16,6 @@ import javafx.collections.ObservableSet; import org.jabref.gui.externalfiletype.ExternalFileType; -import org.jabref.gui.util.OptionalObjectProperty; import org.jabref.model.strings.StringUtil; public class FilePreferences { @@ -24,7 +23,7 @@ public class FilePreferences { public static final String[] DEFAULT_FILENAME_PATTERNS = new String[] {"[bibtexkey]", "[bibtexkey] - [title]"}; private final StringProperty user = new SimpleStringProperty(); - private final OptionalObjectProperty mainFileDirectory = OptionalObjectProperty.empty(); + private final SimpleStringProperty mainFileDirectory = new SimpleStringProperty(); private final BooleanProperty storeFilesRelativeToBibFile = new SimpleBooleanProperty(); private final StringProperty fileNamePattern = new SimpleStringProperty(); private final StringProperty fileDirectoryPattern = new SimpleStringProperty(); @@ -43,7 +42,7 @@ public FilePreferences(String user, Path workingDirectory, Set externalFileTypes) { this.user.setValue(user); - this.setMainFileDirectory(mainFileDirectory); + this.mainFileDirectory.setValue(mainFileDirectory); this.storeFilesRelativeToBibFile.setValue(storeFilesRelativeToBibFile); this.fileNamePattern.setValue(fileNamePattern); this.fileDirectoryPattern.setValue(fileDirectoryPattern); @@ -57,20 +56,20 @@ public String getUser() { return user.getValue(); } - public Optional getMainFileDirectory() { - return mainFileDirectory.getValue(); + public Optional getFileDirectory() { + if (StringUtil.isBlank(mainFileDirectory.getValue())) { + return Optional.empty(); + } else { + return Optional.of(Path.of(mainFileDirectory.getValue())); + } } - public OptionalObjectProperty mainFileDirectoryProperty() { + public StringProperty mainFileDirectoryProperty() { return mainFileDirectory; } public void setMainFileDirectory(String mainFileDirectory) { - if (StringUtil.isBlank(mainFileDirectory)) { - this.mainFileDirectory.setValue(Optional.empty()); - } else { - this.mainFileDirectory.setValue(Optional.of(Path.of(mainFileDirectory))); - } + this.mainFileDirectory.set(mainFileDirectory); } public boolean shouldStoreFilesRelativeToBibFile() { diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 69323cce9f9..166e5d9f187 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -1109,7 +1109,7 @@ public void importPreferences(Path file) throws JabRefException { @Override public FileLinkPreferences getFileLinkPreferences() { return new FileLinkPreferences( - getFilePreferences().getMainFileDirectory(), + getFilePreferences().mainFileDirectoryProperty().get(), fileDirForDatabase); } @@ -2179,6 +2179,20 @@ public FieldWriterPreferences getFieldWriterPreferences() { getFieldContentParserPreferences()); } + /** + * Ensures that the main file directory is a non-empty String. + * The directory is NOT created, because creation of the directory is the task of the respective methods. + * + * @param originalDirectory the directory as configured + */ + private String determineMainFileDirectory(String originalDirectory) { + if ((originalDirectory != null) && !originalDirectory.isEmpty()) { + // A non-empty directory is kept + return originalDirectory; + } + return JabRefDesktop.getDefaultFileChooserDirectory(); + } + @Override public FilePreferences getFilePreferences() { if (Objects.nonNull(filePreferences)) { @@ -2187,9 +2201,7 @@ public FilePreferences getFilePreferences() { filePreferences = new FilePreferences( getInternalPreferences().getUser(), - StringUtil.isNotBlank(get(MAIN_FILE_DIRECTORY)) - ? get(MAIN_FILE_DIRECTORY) - : JabRefDesktop.getNativeDesktop().getDefaultFileChooserDirectory().toString(), + determineMainFileDirectory(get(MAIN_FILE_DIRECTORY)), getBoolean(STORE_RELATIVE_TO_BIB), get(IMPORT_FILENAMEPATTERN), get(IMPORT_FILEDIRPATTERN), @@ -2199,7 +2211,7 @@ public FilePreferences getFilePreferences() { ExternalFileTypes.fromString(get(EXTERNAL_FILE_TYPES)) ); - EasyBind.listen(filePreferences.mainFileDirectoryProperty(), (obs, oldValue, newValue) -> put(MAIN_FILE_DIRECTORY, newValue.map(Path::toString).orElse(""))); + EasyBind.listen(filePreferences.mainFileDirectoryProperty(), (obs, oldValue, newValue) -> put(MAIN_FILE_DIRECTORY, newValue)); EasyBind.listen(filePreferences.storeFilesRelativeToBibFileProperty(), (obs, oldValue, newValue) -> putBoolean(STORE_RELATIVE_TO_BIB, newValue)); EasyBind.listen(filePreferences.fileNamePatternProperty(), (obs, oldValue, newValue) -> put(IMPORT_FILENAMEPATTERN, newValue)); EasyBind.listen(filePreferences.fileDirectoryPatternProperty(), (obs, oldValue, newValue) -> put(IMPORT_FILEDIRPATTERN, newValue)); diff --git a/src/test/java/org/jabref/architecture/MainArchitectureTests.java b/src/test/java/org/jabref/architecture/MainArchitectureTests.java index 02e44dd62ab..25b60cea46c 100644 --- a/src/test/java/org/jabref/architecture/MainArchitectureTests.java +++ b/src/test/java/org/jabref/architecture/MainArchitectureTests.java @@ -32,7 +32,8 @@ public static void doNotUseApacheCommonsLang3(JavaClasses classes) { @ArchTest public static void doNotUseSwing(JavaClasses classes) { // This checks for all Swing packages, but not the UndoManager - noClasses().should().accessClassesThat() + noClasses().that().areNotAnnotatedWith(AllowedToUseSwing.class) + .should().accessClassesThat() .resideInAnyPackage("javax.swing", "javax.swing.border..", "javax.swing.colorchooser..", diff --git a/src/test/java/org/jabref/logic/layout/LayoutTest.java b/src/test/java/org/jabref/logic/layout/LayoutTest.java index b5e82ad1b13..b9700d9faf5 100644 --- a/src/test/java/org/jabref/logic/layout/LayoutTest.java +++ b/src/test/java/org/jabref/logic/layout/LayoutTest.java @@ -5,7 +5,6 @@ import java.io.StringReader; import java.nio.file.Path; import java.util.Collections; -import java.util.Optional; import org.jabref.logic.layout.format.FileLinkPreferences; import org.jabref.logic.layout.format.NameFormatterPreferences; @@ -136,7 +135,7 @@ void beginConditionals() throws IOException { @Test void wrapFileLinksExpandFile() throws IOException { when(layoutFormatterPreferences.getFileLinkPreferences()).thenReturn( - new FileLinkPreferences(Optional.empty(), Collections.singletonList(Path.of("src/test/resources/pdfs/")))); + new FileLinkPreferences("", Collections.singletonList(Path.of("src/test/resources/pdfs/")))); BibEntry entry = new BibEntry(StandardEntryType.Article); entry.addFile(new LinkedFile("Test file", Path.of("encrypted.pdf"), "PDF")); diff --git a/src/test/java/org/jabref/logic/layout/format/WrapFileLinksTest.java b/src/test/java/org/jabref/logic/layout/format/WrapFileLinksTest.java index 4137e54bcdf..aad1356ab1d 100644 --- a/src/test/java/org/jabref/logic/layout/format/WrapFileLinksTest.java +++ b/src/test/java/org/jabref/logic/layout/format/WrapFileLinksTest.java @@ -4,7 +4,6 @@ import java.io.IOException; import java.nio.file.Path; import java.util.Collections; -import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -18,7 +17,7 @@ class WrapFileLinksTest { @BeforeEach void setUp() { - FileLinkPreferences preferences = new FileLinkPreferences(Optional.empty(), Collections.emptyList()); + FileLinkPreferences preferences = new FileLinkPreferences("", Collections.emptyList()); formatter = new WrapFileLinks(preferences); } @@ -98,7 +97,7 @@ void testEndingBracket() { @Test void testPath() throws IOException { - FileLinkPreferences preferences = new FileLinkPreferences(Optional.empty(), + FileLinkPreferences preferences = new FileLinkPreferences("", Collections.singletonList(Path.of("src/test/resources/pdfs/"))); formatter = new WrapFileLinks(preferences); formatter.setArgument("\\p"); @@ -108,7 +107,7 @@ void testPath() throws IOException { @Test void testPathFallBackToGeneratedDir() throws IOException { - FileLinkPreferences preferences = new FileLinkPreferences(Optional.of(Path.of("src/test/resources/pdfs/")), + FileLinkPreferences preferences = new FileLinkPreferences("src/test/resources/pdfs/", Collections.emptyList()); formatter = new WrapFileLinks(preferences); formatter.setArgument("\\p"); @@ -118,7 +117,7 @@ void testPathFallBackToGeneratedDir() throws IOException { @Test void testPathReturnsRelativePathIfNotFound() { - FileLinkPreferences preferences = new FileLinkPreferences(Optional.empty(), + FileLinkPreferences preferences = new FileLinkPreferences("", Collections.singletonList(Path.of("src/test/resources/pdfs/"))); formatter = new WrapFileLinks(preferences); formatter.setArgument("\\p"); diff --git a/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java b/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java index 5a793167be8..73f2689be05 100644 --- a/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java +++ b/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.Optional; +import org.jabref.gui.desktop.JabRefDesktop; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.types.IEEETranEntryType; import org.jabref.model.metadata.MetaData; @@ -93,12 +94,12 @@ void getFileDirectoriesWithMetadata() { @Test void getUserFileDirectoryIfAllAreEmpty() { when(fileDirPrefs.shouldStoreFilesRelativeToBibFile()).thenReturn(false); - Path userDirGlobal = Path.of("some random path").toAbsolutePath(); - when(fileDirPrefs.getMainFileDirectory()).thenReturn(Optional.of(userDirGlobal)); + Path userDirJabRef = Path.of(JabRefDesktop.getDefaultFileChooserDirectory()); + when(fileDirPrefs.getFileDirectory()).thenReturn(Optional.of(userDirJabRef)); BibDatabaseContext database = new BibDatabaseContext(); database.setDatabasePath(Path.of("biblio.bib")); - assertEquals(Collections.singletonList(userDirGlobal), database.getFileDirectories(fileDirPrefs)); + assertEquals(Collections.singletonList(userDirJabRef), database.getFileDirectories(fileDirPrefs)); } @Test From 6b28d086a80de9914e4214a916fdab7b373e086d Mon Sep 17 00:00:00 2001 From: Christoph Date: Mon, 17 Oct 2022 20:10:28 +0200 Subject: [PATCH 12/18] Update src/main/java/org/jabref/gui/desktop/os/Windows.java --- src/main/java/org/jabref/gui/desktop/os/Windows.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/gui/desktop/os/Windows.java b/src/main/java/org/jabref/gui/desktop/os/Windows.java index f0446012d79..eb973986e0e 100644 --- a/src/main/java/org/jabref/gui/desktop/os/Windows.java +++ b/src/main/java/org/jabref/gui/desktop/os/Windows.java @@ -89,7 +89,7 @@ public Path getDefaultFileChooserDirectory() { return Path.of(Shell32Util.getFolderPath(ShlObj.CSIDL_MYDOCUMENTS)); } } catch (Win32Exception e) { - LOGGER.error(e.getMessage()); + LOGGER.error("Error accessing folder", e); return Path.of(System.getProperty("user.home")); } } From 10823443e0d7b2c961aa5737f799f3cbc8902c16 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Mon, 17 Oct 2022 20:16:59 +0200 Subject: [PATCH 13/18] fix merge errors --- src/main/java/org/jabref/preferences/FilePreferences.java | 2 +- .../java/org/jabref/model/database/BibDatabaseContextTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jabref/preferences/FilePreferences.java b/src/main/java/org/jabref/preferences/FilePreferences.java index 219fe1c9d49..264d778aeda 100644 --- a/src/main/java/org/jabref/preferences/FilePreferences.java +++ b/src/main/java/org/jabref/preferences/FilePreferences.java @@ -56,7 +56,7 @@ public String getUser() { return user.getValue(); } - public Optional getFileDirectory() { + public Optional getMainFileDirectory() { if (StringUtil.isBlank(mainFileDirectory.getValue())) { return Optional.empty(); } else { diff --git a/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java b/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java index 73f2689be05..3b8d60b734d 100644 --- a/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java +++ b/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java @@ -96,7 +96,7 @@ void getUserFileDirectoryIfAllAreEmpty() { when(fileDirPrefs.shouldStoreFilesRelativeToBibFile()).thenReturn(false); Path userDirJabRef = Path.of(JabRefDesktop.getDefaultFileChooserDirectory()); - when(fileDirPrefs.getFileDirectory()).thenReturn(Optional.of(userDirJabRef)); + when(fileDirPrefs.getMainFileDirectory()).thenReturn(Optional.of(userDirJabRef)); BibDatabaseContext database = new BibDatabaseContext(); database.setDatabasePath(Path.of("biblio.bib")); assertEquals(Collections.singletonList(userDirJabRef), database.getFileDirectories(fileDirPrefs)); From a51570d73fbb2486d3251fd3999bb686091b7f8c Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Mon, 17 Oct 2022 20:40:46 +0200 Subject: [PATCH 14/18] fix wrong method call --- .../org/jabref/gui/desktop/JabRefDesktop.java | 19 +------------------ .../jabref/preferences/JabRefPreferences.java | 17 +++-------------- .../database/BibDatabaseContextTest.java | 2 +- 3 files changed, 5 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java b/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java index b74fea558d4..d5ca7d1cc18 100644 --- a/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java +++ b/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java @@ -10,9 +10,6 @@ import java.util.Optional; import java.util.regex.Pattern; -import javax.swing.filechooser.FileSystemView; - -import org.jabref.architecture.AllowedToUseSwing; import org.jabref.gui.DialogService; import org.jabref.gui.Globals; import org.jabref.gui.desktop.os.DefaultDesktop; @@ -40,7 +37,6 @@ * TODO: Replace by http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html * http://stackoverflow.com/questions/18004150/desktop-api-is-not-supported-on-the-current-platform */ -@AllowedToUseSwing("Needs access to swing for the user's os dependent file chooser path") public class JabRefDesktop { private static final Logger LOGGER = LoggerFactory.getLogger(JabRefDesktop.class); @@ -292,20 +288,7 @@ public static void openConsole(File file, PreferencesService preferencesService, } } - /** - * Get the user's default file chooser directory - * - * @return The path to the directory - */ - public static String getDefaultFileChooserDirectory() { - // Property "user.home" might be "difficult" on Windows - // See https://stackoverflow.com/a/586917/873282 for a longer discussion - // The proposed solution is to use Swing's FileSystemView - // See https://stackoverflow.com/a/32914568/873282 - // As of 2022, System.getProperty("user.home") returns c:\Users\USERNAME on Windows 10, whereas - // the FileSystemView returns C:\Users\USERNAME\Documents, which is the "better" directory - return FileSystemView.getFileSystemView().getDefaultDirectory().getPath(); - } + // TODO: Move to OS.java public static NativeDesktop getNativeDesktop() { diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 166e5d9f187..47eb12ee9e0 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -2179,19 +2179,6 @@ public FieldWriterPreferences getFieldWriterPreferences() { getFieldContentParserPreferences()); } - /** - * Ensures that the main file directory is a non-empty String. - * The directory is NOT created, because creation of the directory is the task of the respective methods. - * - * @param originalDirectory the directory as configured - */ - private String determineMainFileDirectory(String originalDirectory) { - if ((originalDirectory != null) && !originalDirectory.isEmpty()) { - // A non-empty directory is kept - return originalDirectory; - } - return JabRefDesktop.getDefaultFileChooserDirectory(); - } @Override public FilePreferences getFilePreferences() { @@ -2201,7 +2188,9 @@ public FilePreferences getFilePreferences() { filePreferences = new FilePreferences( getInternalPreferences().getUser(), - determineMainFileDirectory(get(MAIN_FILE_DIRECTORY)), + StringUtil.isNotBlank(get(MAIN_FILE_DIRECTORY)) + ? get(MAIN_FILE_DIRECTORY) + : JabRefDesktop.getNativeDesktop().getDefaultFileChooserDirectory().toString(), getBoolean(STORE_RELATIVE_TO_BIB), get(IMPORT_FILENAMEPATTERN), get(IMPORT_FILEDIRPATTERN), diff --git a/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java b/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java index 3b8d60b734d..4f6a3335f48 100644 --- a/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java +++ b/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java @@ -94,7 +94,7 @@ void getFileDirectoriesWithMetadata() { @Test void getUserFileDirectoryIfAllAreEmpty() { when(fileDirPrefs.shouldStoreFilesRelativeToBibFile()).thenReturn(false); - Path userDirJabRef = Path.of(JabRefDesktop.getDefaultFileChooserDirectory()); + Path userDirJabRef = JabRefDesktop.getNativeDesktop().getDefaultFileChooserDirectory(); when(fileDirPrefs.getMainFileDirectory()).thenReturn(Optional.of(userDirJabRef)); BibDatabaseContext database = new BibDatabaseContext(); From 5dd4bd85b5bb7429e7268328a59d1074a6d270c3 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Mon, 17 Oct 2022 20:48:52 +0200 Subject: [PATCH 15/18] remove defautl fix checkstyle --- src/main/java/org/jabref/gui/desktop/JabRefDesktop.java | 2 -- .../java/org/jabref/gui/desktop/os/DefaultDesktop.java | 5 +++++ .../java/org/jabref/gui/desktop/os/NativeDesktop.java | 8 +------- .../java/org/jabref/preferences/JabRefPreferences.java | 1 - 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java b/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java index d5ca7d1cc18..517ffc1689d 100644 --- a/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java +++ b/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java @@ -288,8 +288,6 @@ public static void openConsole(File file, PreferencesService preferencesService, } } - - // TODO: Move to OS.java public static NativeDesktop getNativeDesktop() { if (OS.WINDOWS) { diff --git a/src/main/java/org/jabref/gui/desktop/os/DefaultDesktop.java b/src/main/java/org/jabref/gui/desktop/os/DefaultDesktop.java index ec0805b641f..86d97fb45ae 100644 --- a/src/main/java/org/jabref/gui/desktop/os/DefaultDesktop.java +++ b/src/main/java/org/jabref/gui/desktop/os/DefaultDesktop.java @@ -45,4 +45,9 @@ public String detectProgramPath(String programName, String directoryName) { public Path getApplicationDirectory() { return getUserDirectory(); } + + @Override + public Path getDefaultFileChooserDirectory() { + return Path.of(System.getProperty("user.home")); + } } diff --git a/src/main/java/org/jabref/gui/desktop/os/NativeDesktop.java b/src/main/java/org/jabref/gui/desktop/os/NativeDesktop.java index 9ce8c37193f..74341d10e8a 100644 --- a/src/main/java/org/jabref/gui/desktop/os/NativeDesktop.java +++ b/src/main/java/org/jabref/gui/desktop/os/NativeDesktop.java @@ -35,13 +35,7 @@ public interface NativeDesktop { * * @return The path to the directory */ - default Path getDefaultFileChooserDirectory() { - // We do not return a subdirectory "JabRef", because - // - the directory might not exist at this point of the method - // - we might not have the rights to create a directory - // - getters should not have any side effect - return Path.of(System.getProperty("user.home")); - } + Path getDefaultFileChooserDirectory(); /** * Returns the path to the system's user directory. diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 47eb12ee9e0..2d2c656a652 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -2179,7 +2179,6 @@ public FieldWriterPreferences getFieldWriterPreferences() { getFieldContentParserPreferences()); } - @Override public FilePreferences getFilePreferences() { if (Objects.nonNull(filePreferences)) { From aa7e7a9248dec5259a4bd06e29875f36d569c913 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Mon, 17 Oct 2022 21:14:59 +0200 Subject: [PATCH 16/18] reintroduce method to avoid calling get two times --- .../org/jabref/preferences/JabRefPreferences.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 2d2c656a652..adb03c06aa2 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -2187,9 +2187,7 @@ public FilePreferences getFilePreferences() { filePreferences = new FilePreferences( getInternalPreferences().getUser(), - StringUtil.isNotBlank(get(MAIN_FILE_DIRECTORY)) - ? get(MAIN_FILE_DIRECTORY) - : JabRefDesktop.getNativeDesktop().getDefaultFileChooserDirectory().toString(), + determineMainFileDirectory(get(MAIN_FILE_DIRECTORY)), getBoolean(STORE_RELATIVE_TO_BIB), get(IMPORT_FILENAMEPATTERN), get(IMPORT_FILEDIRPATTERN), @@ -2212,6 +2210,13 @@ public FilePreferences getFilePreferences() { return filePreferences; } + private String determineMainFileDirectory(String mainFileDirecotry) { + if (StringUtil.isNotBlank(mainFileDirecotry)) { + return mainFileDirecotry; + } + return JabRefDesktop.getNativeDesktop().getDefaultFileChooserDirectory().toString(); + } + @Override public AutoLinkPreferences getAutoLinkPreferences() { if (Objects.nonNull(autoLinkPreferences)) { From 44aa9dc1302d5b58c68014b3a56810625fd7cf0d Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Mon, 17 Oct 2022 21:38:53 +0200 Subject: [PATCH 17/18] Introduced getPath method --- .../jabref/preferences/JabRefPreferences.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index adb03c06aa2..f4ddffdb5bf 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -944,6 +944,14 @@ public List getStringList(String key) { return convertStringToList(get(key)); } + /** + * Returns a Path + */ + private Path getPath(String key, Path defaultValue) { + String rawPath = get(key); + return StringUtil.isNotBlank(rawPath) ? Path.of(rawPath) : defaultValue; + } + /** * Clear all preferences. * @@ -2187,7 +2195,7 @@ public FilePreferences getFilePreferences() { filePreferences = new FilePreferences( getInternalPreferences().getUser(), - determineMainFileDirectory(get(MAIN_FILE_DIRECTORY)), + getPath(MAIN_FILE_DIRECTORY, JabRefDesktop.getNativeDesktop().getDefaultFileChooserDirectory()).toString(), getBoolean(STORE_RELATIVE_TO_BIB), get(IMPORT_FILENAMEPATTERN), get(IMPORT_FILEDIRPATTERN), @@ -2210,13 +2218,6 @@ public FilePreferences getFilePreferences() { return filePreferences; } - private String determineMainFileDirectory(String mainFileDirecotry) { - if (StringUtil.isNotBlank(mainFileDirecotry)) { - return mainFileDirecotry; - } - return JabRefDesktop.getNativeDesktop().getDefaultFileChooserDirectory().toString(); - } - @Override public AutoLinkPreferences getAutoLinkPreferences() { if (Objects.nonNull(autoLinkPreferences)) { From 486b8b27877ff3adab61d59ed77812803c9027d8 Mon Sep 17 00:00:00 2001 From: Christoph Date: Mon, 17 Oct 2022 22:18:36 +0200 Subject: [PATCH 18/18] Update src/main/java/org/jabref/gui/desktop/os/Linux.java fix env var --- src/main/java/org/jabref/gui/desktop/os/Linux.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/gui/desktop/os/Linux.java b/src/main/java/org/jabref/gui/desktop/os/Linux.java index a354423ee04..c08ca68aa2c 100644 --- a/src/main/java/org/jabref/gui/desktop/os/Linux.java +++ b/src/main/java/org/jabref/gui/desktop/os/Linux.java @@ -173,7 +173,7 @@ public Path getApplicationDirectory() { public Path getDefaultFileChooserDirectory() { return Path.of(Objects.requireNonNullElse( System.getenv("XDG_DOCUMENTS_DIR"), - System.getProperty("users.home") + "/Documents") + System.getProperty("user.home") + "/Documents") ); } }