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

Refactor file preferences #6779

Merged
merged 17 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve

### Changed

- We restructured the 'File' tab and extracted some parts into the 'Linked files' tab [#6779](https://github.com/JabRef/jabref/pull/6779)
- JabRef now offers journal lists from <https://abbrv.jabref.org>. JabRef the lists which use a dot inside the abbreviations. [#5749](https://github.com/JabRef/jabref/pull/5749)

### Fixed
Expand Down
16 changes: 16 additions & 0 deletions docs/adr/0016-mutable-preferences-objects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Mutable preferences objects

## Context and Problem Statement

To create an immutable preferences object every time seems to be a waste of time and computer memory.

## Considered Options

* Create a new object every time a preferences object should be altered by a with*-method, similar to a Builder.
* Alter the existing object and return it.

## Decision Outcome

Chosen option: 1, because the preferences objects are just wrappers around the basic preferences framework of JDK. They
should be mutable on-the-fly similar to objects with a Builder inside and to be stored immediatly again in the
preferences.
7 changes: 6 additions & 1 deletion src/main/java/org/jabref/JabRefMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.jabref.cli.JabRefCLI;
import org.jabref.gui.FXDialog;
import org.jabref.gui.remote.JabRefMessageHandler;
import org.jabref.logic.exporter.ExporterFactory;
import org.jabref.logic.journals.JournalAbbreviationLoader;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.net.ProxyAuthenticator;
Expand Down Expand Up @@ -168,7 +169,11 @@ private static void applyPreferences(JabRefPreferences preferences) {
Globals.prefs.getXMPPreferences(), Globals.getFileUpdateMonitor());
Globals.entryTypesManager.addCustomOrModifiedTypes(preferences.loadBibEntryTypes(BibDatabaseMode.BIBTEX),
preferences.loadBibEntryTypes(BibDatabaseMode.BIBLATEX));
Globals.exportFactory = Globals.prefs.getExporterFactory(Globals.journalAbbreviationRepository);
Globals.exportFactory = ExporterFactory.create(
Globals.prefs.getCustomExportFormats(Globals.journalAbbreviationRepository),
Globals.prefs.getLayoutFormatterPreferences(Globals.journalAbbreviationRepository),
Globals.prefs.getSavePreferencesForExport(),
Globals.prefs.getXMPPreferences());

// Initialize protected terms loader
Globals.protectedTermsLoader = new ProtectedTermsLoader(Globals.prefs.getProtectedTermsPreferences());
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private boolean exportMatches(List<ParserResult> loaded) {

// export new database
Optional<Exporter> exporter = Globals.exportFactory.getExporterByName(formatName);
if (!exporter.isPresent()) {
if (exporter.isEmpty()) {
System.err.println(Localization.lang("Unknown export format") + ": " + formatName);
} else {
// We have an TemplateExporter instance:
Expand Down Expand Up @@ -387,7 +387,7 @@ private boolean generateAux(List<ParserResult> loaded, String[] data) {
private void saveDatabase(BibDatabase newBase, String subName) {
try {
System.out.println(Localization.lang("Saving") + ": " + subName);
SavePreferences prefs = Globals.prefs.loadForSaveFromPreferences();
SavePreferences prefs = Globals.prefs.getSavePreferences();
AtomicFileWriter fileWriter = new AtomicFileWriter(Path.of(subName), prefs.getEncoding());
BibDatabaseWriter databaseWriter = new BibtexDatabaseWriter(fileWriter, prefs, Globals.entryTypesManager);
databaseWriter.saveDatabase(new BibDatabaseContext(newBase));
Expand Down Expand Up @@ -435,7 +435,7 @@ private void exportFile(List<ParserResult> loaded, String[] data) {
.getFileDirectories(Globals.prefs.getFilePreferences());
System.out.println(Localization.lang("Exporting") + ": " + data[0]);
Optional<Exporter> exporter = Globals.exportFactory.getExporterByName(data[1]);
if (!exporter.isPresent()) {
if (exporter.isEmpty()) {
System.err.println(Localization.lang("Unknown export format") + ": " + data[1]);
} else {
// We have an exporter:
Expand All @@ -458,9 +458,9 @@ private void importPreferences() {
Globals.entryTypesManager.addCustomOrModifiedTypes(Globals.prefs.loadBibEntryTypes(BibDatabaseMode.BIBTEX),
Globals.prefs.loadBibEntryTypes(BibDatabaseMode.BIBLATEX));
List<TemplateExporter> customExporters = Globals.prefs.getCustomExportFormats(Globals.journalAbbreviationRepository);
LayoutFormatterPreferences layoutPreferences = Globals.prefs
.getLayoutFormatterPreferences(Globals.journalAbbreviationRepository);
SavePreferences savePreferences = Globals.prefs.loadForExportFromPreferences();
LayoutFormatterPreferences layoutPreferences =
Globals.prefs.getLayoutFormatterPreferences(Globals.journalAbbreviationRepository);
SavePreferences savePreferences = Globals.prefs.getSavePreferencesForExport();
XmpPreferences xmpPreferences = Globals.prefs.getXMPPreferences();
Globals.exportFactory = ExporterFactory.create(customExporters, layoutPreferences, savePreferences, xmpPreferences);
} catch (JabRefException ex) {
Expand Down Expand Up @@ -495,7 +495,11 @@ private void automaticallySetFileLinks(List<ParserResult> loaded) {
for (ParserResult parserResult : loaded) {
BibDatabase database = parserResult.getDatabase();
LOGGER.info(Localization.lang("Automatically setting file links"));
AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(parserResult.getDatabaseContext(), Globals.prefs.getFilePreferences(), Globals.prefs.getAutoLinkPreferences(), ExternalFileTypes.getInstance());
AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(
parserResult.getDatabaseContext(),
Globals.prefs.getFilePreferences(),
Globals.prefs.getAutoLinkPreferences(),
ExternalFileTypes.getInstance());
util.linkAssociatedFiles(database.getEntries(), new NamedCompound(""));
}
}
Expand Down Expand Up @@ -534,7 +538,7 @@ private Optional<ParserResult> fetch(String fetchCommand) {
Optional<SearchBasedFetcher> selectedFetcher = fetchers.stream()
.filter(fetcher -> fetcher.getName().equalsIgnoreCase(engine))
.findFirst();
if (!selectedFetcher.isPresent()) {
if (selectedFetcher.isEmpty()) {
System.out.println(Localization.lang("Could not find fetcher '%0'", engine));

System.out.println(Localization.lang("The following fetchers are available:"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public ExporterViewModel saveExporter() {

// Create a new exporter to be returned to ExportCustomizationDialogViewModel, which requested it
LayoutFormatterPreferences layoutPreferences = preferences.getLayoutFormatterPreferences(repository);
SavePreferences savePreferences = preferences.loadForExportFromPreferences();
SavePreferences savePreferences = preferences.getSavePreferencesForExport();
TemplateExporter format = new TemplateExporter(name.get(), layoutFile.get(), extension.get(),
layoutPreferences, savePreferences);
format.setCustomExport(true);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/exporter/ExportCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ExportCommand(JabRefFrame frame, boolean selectedOnly, JabRefPreferences
public void execute() {
List<TemplateExporter> customExporters = preferences.getCustomExportFormats(Globals.journalAbbreviationRepository);
LayoutFormatterPreferences layoutPreferences = preferences.getLayoutFormatterPreferences(Globals.journalAbbreviationRepository);
SavePreferences savePreferences = preferences.loadForExportFromPreferences();
SavePreferences savePreferences = preferences.getSavePreferencesForExport();
XmpPreferences xmpPreferences = preferences.getXMPPreferences();

// Get list of exporters and sort before adding to file dialog
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private Optional<Path> askForSavePath() {

private boolean save(BibDatabaseContext bibDatabaseContext, SaveDatabaseMode mode) {
Optional<Path> databasePath = bibDatabaseContext.getDatabasePath();
if (!databasePath.isPresent()) {
if (databasePath.isEmpty()) {
Optional<Path> savePath = askForSavePath();
if (!savePath.isPresent()) {
return false;
Expand Down Expand Up @@ -216,10 +216,10 @@ private boolean save(Path targetPath, SaveDatabaseMode mode) {
}

private boolean saveDatabase(Path file, boolean selectedOnly, Charset encoding, SavePreferences.DatabaseSaveType saveType) throws SaveException {
SavePreferences preferences = this.preferences.loadForSaveFromPreferences()
SavePreferences preferences = this.preferences.getSavePreferences()
.withEncoding(encoding)
.withSaveType(saveType);
try (AtomicFileWriter fileWriter = new AtomicFileWriter(file, preferences.getEncoding(), preferences.makeBackup())) {
try (AtomicFileWriter fileWriter = new AtomicFileWriter(file, preferences.getEncoding(), preferences.shouldMakeBackup())) {
BibtexDatabaseWriter databaseWriter = new BibtexDatabaseWriter(fileWriter, preferences, entryTypesManager);

if (selectedOnly) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ public void execute() {
List<BibEntry> entries = stateManager.getSelectedEntries();

final NamedCompound nc = new NamedCompound(Localization.lang("Automatically set file links"));
AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(database, preferences.getFilePreferences(), preferences.getAutoLinkPreferences(), ExternalFileTypes.getInstance());
Task<List<BibEntry>> linkFilesTask = new Task<List<BibEntry>>() {
AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(
database,
preferences.getFilePreferences(),
preferences.getAutoLinkPreferences(),
ExternalFileTypes.getInstance());
Task<List<BibEntry>> linkFilesTask = new Task<>() {

@Override
protected List<BibEntry> call() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ public BackgroundTask<Path> prepareDownloadTask(Path targetDirectory, URLDownloa
String suggestedTypeName = externalFileType.getName();
linkedFile.setFileType(suggestedTypeName);
String suggestedName = linkedFileHandler.getSuggestedFileName(externalFileType.getExtension());
String fulltextDir = FileUtil.createDirNameFromPattern(databaseContext.getDatabase(), entry, filePreferences.getFileDirPattern());
String fulltextDir = FileUtil.createDirNameFromPattern(databaseContext.getDatabase(), entry, filePreferences.getFileDirectoryPattern());
suggestedName = FileNameUniqueness.getNonOverWritingFileName(targetDirectory.resolve(fulltextDir), suggestedName);
return targetDirectory.resolve(fulltextDir).resolve(suggestedName);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,15 @@ public LinkedFileViewModel fromFile(Path file) {
List<Path> fileDirectories = databaseContext.getFileDirectoriesAsPaths(preferences.getFilePreferences());

LinkedFile linkedFile = fromFile(file, fileDirectories, externalFileTypes);
return new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences.getXMPPreferences(), preferences.getFilePreferences(), externalFileTypes);
return new LinkedFileViewModel(
linkedFile,
entry,
databaseContext,
taskExecutor,
dialogService,
preferences.getXMPPreferences(),
preferences.getFilePreferences(),
externalFileTypes);
}

public boolean isFulltextLookupInProgress() {
Expand All @@ -114,7 +122,15 @@ public BooleanProperty fulltextLookupInProgressProperty() {

private List<LinkedFileViewModel> parseToFileViewModel(String stringValue) {
return FileFieldParser.parse(stringValue).stream()
.map(linkedFile -> new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences.getXMPPreferences(), preferences.getFilePreferences(), externalFileTypes))
.map(linkedFile -> new LinkedFileViewModel(
linkedFile,
entry,
databaseContext,
taskExecutor,
dialogService,
preferences.getXMPPreferences(),
preferences.getFilePreferences(),
externalFileTypes))
.collect(Collectors.toList());
}

Expand All @@ -137,7 +153,15 @@ public void addNewFile() {
List<Path> fileDirectories = databaseContext.getFileDirectoriesAsPaths(preferences.getFilePreferences());
dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(newFile -> {
LinkedFile newLinkedFile = fromFile(newFile, fileDirectories, externalFileTypes);
files.add(new LinkedFileViewModel(newLinkedFile, entry, databaseContext, taskExecutor, dialogService, preferences.getXMPPreferences(), preferences.getFilePreferences(), externalFileTypes));
files.add(new LinkedFileViewModel(
newLinkedFile,
entry,
databaseContext,
taskExecutor,
dialogService,
preferences.getXMPPreferences(),
preferences.getFilePreferences(),
externalFileTypes));
});
}

Expand All @@ -159,11 +183,23 @@ public void bindToEntry(BibEntry entry) {
private List<LinkedFileViewModel> findAssociatedNotLinkedFiles(BibEntry entry) {
List<LinkedFileViewModel> result = new ArrayList<>();

AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(databaseContext, preferences.getFilePreferences(), preferences.getAutoLinkPreferences(), ExternalFileTypes.getInstance());
AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(
databaseContext,
preferences.getFilePreferences(),
preferences.getAutoLinkPreferences(),
ExternalFileTypes.getInstance());
try {
List<LinkedFile> linkedFiles = util.findAssociatedNotLinkedFiles(entry);
for (LinkedFile linkedFile : linkedFiles) {
LinkedFileViewModel newLinkedFile = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences.getXMPPreferences(), preferences.getFilePreferences(), externalFileTypes);
LinkedFileViewModel newLinkedFile = new LinkedFileViewModel(
linkedFile,
entry,
databaseContext,
taskExecutor,
dialogService,
preferences.getXMPPreferences(),
preferences.getFilePreferences(),
externalFileTypes);
newLinkedFile.markAsAutomaticallyFound();
result.add(newLinkedFile);
}
Expand Down Expand Up @@ -206,7 +242,15 @@ public void addFromURL() {
}

private void addFromURL(URL url) {
LinkedFileViewModel onlineFile = new LinkedFileViewModel(new LinkedFile(url, ""), entry, databaseContext, taskExecutor, dialogService, preferences.getXMPPreferences(), preferences.getFilePreferences(), externalFileTypes);
LinkedFileViewModel onlineFile = new LinkedFileViewModel(
new LinkedFile(url, ""),
entry,
databaseContext,
taskExecutor,
dialogService,
preferences.getXMPPreferences(),
preferences.getFilePreferences(),
externalFileTypes);
files.add(onlineFile);
onlineFile.download();
}
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/org/jabref/gui/importer/ImportEntriesViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@ public class ImportEntriesViewModel extends AbstractViewModel {
* @param databaseContext the database to import into
* @param task the task executed for parsing the selected files(s).
*/
public ImportEntriesViewModel(BackgroundTask<ParserResult> task, TaskExecutor taskExecutor, BibDatabaseContext databaseContext, DialogService dialogService, UndoManager undoManager, PreferencesService preferences, StateManager stateManager, FileUpdateMonitor fileUpdateMonitor) {
public ImportEntriesViewModel(BackgroundTask<ParserResult> task,
TaskExecutor taskExecutor,
BibDatabaseContext databaseContext,
DialogService dialogService,
UndoManager undoManager,
PreferencesService preferences,
StateManager stateManager,
FileUpdateMonitor fileUpdateMonitor) {
this.taskExecutor = taskExecutor;
this.databaseContext = databaseContext;
this.dialogService = dialogService;
Expand Down Expand Up @@ -94,9 +101,9 @@ public ObservableList<BibEntry> getEntries() {
}

public boolean hasDuplicate(BibEntry entry) {
return findInternalDuplicate(entry).isPresent()
||
new DuplicateCheck(Globals.entryTypesManager).containsDuplicate(databaseContext.getDatabase(), entry, databaseContext.getMode()).isPresent();
return findInternalDuplicate(entry).isPresent() ||
new DuplicateCheck(Globals.entryTypesManager)
.containsDuplicate(databaseContext.getDatabase(), entry, databaseContext.getMode()).isPresent();
}

/**
Expand Down Expand Up @@ -132,8 +139,8 @@ public void importEntries(List<BibEntry> entriesToImport, boolean shouldDownload
}

// Remember the selection in the dialog
FilePreferences filePreferences = preferences.getFilePreferences();
filePreferences.setShouldDownloadLinkedFiles(shouldDownloadFiles);
FilePreferences filePreferences = preferences.getFilePreferences()
.withShouldDownloadLinkedFiles(shouldDownloadFiles);
preferences.storeFilePreferences(filePreferences);

if (shouldDownloadFiles) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/gui/menus/FileHistoryMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
import org.jabref.gui.importer.actions.OpenDatabaseAction;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.io.FileHistory;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreferencesService;

public class FileHistoryMenu extends Menu {

private final FileHistory history;
private final JabRefPreferences preferences;
private final PreferencesService preferences;
private final DialogService dialogService;
private final OpenDatabaseAction openDatabaseAction;

public FileHistoryMenu(JabRefPreferences preferences, DialogService dialogService, OpenDatabaseAction openDatabaseAction) {
public FileHistoryMenu(PreferencesService preferences, DialogService dialogService, OpenDatabaseAction openDatabaseAction) {
setText(Localization.lang("Recent libraries"));

this.preferences = preferences;
Expand Down
Loading