Skip to content

Commit

Permalink
Observable Preferences H (WorkingDir, getUser, NameFormatter, Version…
Browse files Browse the repository at this point in the history
…, SpecialFields) (#8260)

* Refactored setWorkingDirectory

* Refactored getWorkingDirectory and getUser and fixed minor ide suggestions

* Refactored NameFormatterPreferences

* Refactored VersionPreferences

* Refactored SpecialFieldsPreferences, removed orphaned autosync prefs

* Fixed Checkstyle and tests

* Fixed l10n

* Fixed test
  • Loading branch information
calixtus authored Nov 22, 2021
1 parent 66667f3 commit ab13b2a
Show file tree
Hide file tree
Showing 59 changed files with 337 additions and 423 deletions.
24 changes: 12 additions & 12 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,28 +310,28 @@ private void writeMetadatatoPDFsOfEntry(BibDatabaseContext databaseContext, Stri
try {
if (writeXMP) {
if (xmpPdfExporter.exportToAllFilesOfEntry(databaseContext, encoding, filePreferences, entry, List.of(entry))) {
System.out.println(String.format("Successfully written XMP metadata on at least one linked file of %s", citeKey));
System.out.printf("Successfully written XMP metadata on at least one linked file of %s%n", citeKey);
} else {
System.err.println(String.format("Cannot write XMP metadata on any linked files of %s. Make sure there is at least one linked file and the path is correct.", citeKey));
System.err.printf("Cannot write XMP metadata on any linked files of %s. Make sure there is at least one linked file and the path is correct.%n", citeKey);
}
}
if (embeddBibfile) {
if (embeddedBibFilePdfExporter.exportToAllFilesOfEntry(databaseContext, encoding, filePreferences, entry, List.of(entry))) {
System.out.println(String.format("Successfully embedded metadata on at least one linked file of %s", citeKey));
System.out.printf("Successfully embedded metadata on at least one linked file of %s%n", citeKey);
} else {
System.out.println(String.format("Cannot embedd metadata on any linked files of %s. Make sure there is at least one linked file and the path is correct.", citeKey));
System.out.printf("Cannot embedd metadata on any linked files of %s. Make sure there is at least one linked file and the path is correct.%n", citeKey);
}
}
} catch (Exception e) {
LOGGER.error(String.format("Failed writing metadata on a linked file of %s.", citeKey));
LOGGER.error("Failed writing metadata on a linked file of {}.", citeKey);
}
}

private void writeMetadatatoPdfByCitekey(BibDatabaseContext databaseContext, BibDatabase dataBase, Vector<String> citeKeys, Charset encoding, FilePreferences filePreferences, XmpPdfExporter xmpPdfExporter, EmbeddedBibFilePdfExporter embeddedBibFilePdfExporter, boolean writeXMP, boolean embeddBibfile) {
for (String citeKey : citeKeys) {
List<BibEntry> bibEntryList = dataBase.getEntriesByCitationKey(citeKey);
if (bibEntryList.isEmpty()) {
System.err.println(String.format("Skipped - Cannot find %s in library.", citeKey));
System.err.printf("Skipped - Cannot find %s in library.%n", citeKey);
continue;
}
for (BibEntry entry : bibEntryList) {
Expand All @@ -350,25 +350,25 @@ private void writeMetadatatoPdfByFileNames(BibDatabaseContext databaseContext, B
try {
if (writeXMP) {
if (xmpPdfExporter.exportToFileByPath(databaseContext, dataBase, encoding, filePreferences, filePath)) {
System.out.println(String.format("Successfully written XMP metadata of at least one entry to %s", fileName));
System.out.printf("Successfully written XMP metadata of at least one entry to %s%n", fileName);
} else {
System.out.println(String.format("File %s is not linked to any entry in database.", fileName));
System.out.printf("File %s is not linked to any entry in database.%n", fileName);
}
}
if (embeddBibfile) {
if (embeddedBibFilePdfExporter.exportToFileByPath(databaseContext, dataBase, encoding, filePreferences, filePath)) {
System.out.println(String.format("Successfully embedded XMP metadata of at least one entry to %s", fileName));
System.out.printf("Successfully embedded XMP metadata of at least one entry to %s%n", fileName);
} else {
System.out.println(String.format("File %s is not linked to any entry in database.", fileName));
System.out.printf("File %s is not linked to any entry in database.%n", fileName);
}
}
} catch (IOException e) {
LOGGER.error("Error accessing file '{}'.", fileName);
} catch (Exception e) {
LOGGER.error(String.format("Error writing entry to %s.", fileName));
LOGGER.error("Error writing entry to {}.", fileName);
}
} else {
LOGGER.error(String.format("Skipped - PDF %s does not exist", fileName));
LOGGER.error("Skipped - PDF {} does not exist", fileName);
}
}
}
Expand Down
29 changes: 15 additions & 14 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -754,19 +754,20 @@ private MenuBar createMenu() {
factory.createMenuItem(StandardActions.MASS_SET_FIELDS, new MassSetFieldsAction(stateManager, dialogService, undoManager))
);

if (prefs.getSpecialFieldsPreferences().isSpecialFieldsEnabled()) {
edit.getItems().addAll(
new SeparatorMenuItem(),
// ToDo: SpecialField needs the active BasePanel to mark it as changed.
// Refactor BasePanel, should mark the BibDatabaseContext or the UndoManager as dirty instead!
SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.RANKING, factory, this, dialogService, prefs, undoManager, stateManager),
SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.RELEVANCE, factory, this, dialogService, prefs, undoManager, stateManager),
SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.QUALITY, factory, this, dialogService, prefs, undoManager, stateManager),
SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.PRINTED, factory, this, dialogService, prefs, undoManager, stateManager),
SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.PRIORITY, factory, this, dialogService, prefs, undoManager, stateManager),
SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.READ_STATUS, factory, this, dialogService, prefs, undoManager, stateManager)
);
}
SeparatorMenuItem specialFieldsSeparator = new SeparatorMenuItem();
specialFieldsSeparator.visibleProperty().bind(prefs.getSpecialFieldsPreferences().specialFieldsEnabledProperty());

edit.getItems().addAll(
specialFieldsSeparator,
// ToDo: SpecialField needs the active BasePanel to mark it as changed.
// Refactor BasePanel, should mark the BibDatabaseContext or the UndoManager as dirty instead!
SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.RANKING, factory, this, dialogService, prefs, undoManager, stateManager),
SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.RELEVANCE, factory, this, dialogService, prefs, undoManager, stateManager),
SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.QUALITY, factory, this, dialogService, prefs, undoManager, stateManager),
SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.PRINTED, factory, this, dialogService, prefs, undoManager, stateManager),
SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.PRIORITY, factory, this, dialogService, prefs, undoManager, stateManager),
SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.READ_STATUS, factory, this, dialogService, prefs, undoManager, stateManager)
);

// @formatter:off
library.getItems().addAll(
Expand Down Expand Up @@ -1192,7 +1193,7 @@ private Boolean confirmEmptyEntry(LibraryTab libraryTab, BibDatabaseContext cont
if (response.isPresent() && response.get().equals(deleteEmptyEntries)) {
// The user wants to delete.
try {
for (BibEntry currentEntry : new ArrayList<BibEntry>(context.getEntries())) {
for (BibEntry currentEntry : new ArrayList<>(context.getEntries())) {
if (currentEntry.getFields().isEmpty()) {
context.getDatabase().removeEntries(Collections.singletonList(currentEntry));
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public JabRefGUI(Stage mainStage, List<ParserResult> databases, boolean isBlank,
openWindow(mainStage);

new VersionWorker(Globals.BUILD_INFO.version,
preferencesService.getVersionPreferences().getIgnoredVersion(),
mainFrame.getDialogService(),
Globals.TASK_EXECUTOR)
Globals.TASK_EXECUTOR,
preferencesService.getVersionPreferences())
.checkForNewVersionDelayed();
}

Expand Down
3 changes: 0 additions & 3 deletions src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.jabref.gui.importer.actions.OpenDatabaseAction;
import org.jabref.gui.maintable.MainTable;
import org.jabref.gui.maintable.MainTableDataModel;
import org.jabref.gui.specialfields.SpecialFieldDatabaseChangeListener;
import org.jabref.gui.undo.CountingUndoManager;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableFieldChange;
Expand Down Expand Up @@ -465,8 +464,6 @@ public void editEntryAndFocusField(BibEntry entry, Field field) {
}

private void createMainTable() {
bibDatabaseContext.getDatabase().registerListener(SpecialFieldDatabaseChangeListener.INSTANCE);

mainTable = new MainTable(tableModel,
this,
bibDatabaseContext,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/auximport/FromAuxDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private void browseButtonClicked() {
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.addExtensionFilter(StandardFileType.AUX)
.withDefaultExtension(StandardFileType.AUX)
.withInitialDirectory(preferences.getWorkingDir()).build();
.withInitialDirectory(preferences.getFilePreferences().getWorkingDirectory()).build();
dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(file -> auxFileField.setText(file.toAbsolutePath().toString()));
}
}
14 changes: 7 additions & 7 deletions src/main/java/org/jabref/gui/copyfiles/CopySingleFileAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,40 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.util.OptionalUtil;
import org.jabref.preferences.PreferencesService;
import org.jabref.preferences.FilePreferences;

public class CopySingleFileAction extends SimpleCommand {

private final LinkedFile linkedFile;
private final DialogService dialogService;
private final BibDatabaseContext databaseContext;
private final PreferencesService preferencesService;
private final FilePreferences filePreferences;

private final BiFunction<Path, Path, Path> resolvePathFilename = (path, file) -> path.resolve(file.getFileName());

public CopySingleFileAction(LinkedFile linkedFile, DialogService dialogService, BibDatabaseContext databaseContext, PreferencesService preferencesService) {
public CopySingleFileAction(LinkedFile linkedFile, DialogService dialogService, BibDatabaseContext databaseContext, FilePreferences filePreferences) {
this.linkedFile = linkedFile;
this.dialogService = dialogService;
this.databaseContext = databaseContext;
this.preferencesService = preferencesService;
this.filePreferences = filePreferences;

this.executable.bind(Bindings.createBooleanBinding(
() -> !linkedFile.isOnlineLink()
&& linkedFile.findIn(databaseContext, preferencesService.getFilePreferences()).isPresent(),
&& linkedFile.findIn(databaseContext, this.filePreferences).isPresent(),
linkedFile.linkProperty()));
}

@Override
public void execute() {
DirectoryDialogConfiguration dirDialogConfiguration = new DirectoryDialogConfiguration.Builder()
.withInitialDirectory(preferencesService.getWorkingDir())
.withInitialDirectory(filePreferences.getWorkingDirectory())
.build();
Optional<Path> exportPath = dialogService.showDirectorySelectionDialog(dirDialogConfiguration);
exportPath.ifPresent(this::copyFileToDestination);
}

private void copyFileToDestination(Path exportPath) {
Optional<Path> fileToExport = linkedFile.findIn(databaseContext, preferencesService.getFilePreferences());
Optional<Path> fileToExport = linkedFile.findIn(databaseContext, filePreferences);
Optional<Path> newPath = OptionalUtil.combine(Optional.of(exportPath), fileToExport, resolvePathFilename);

if (newPath.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ enum Status {
private LatexParserResult latexParserResult;
private BibEntry currentEntry;

public LatexCitationsTabViewModel(BibDatabaseContext databaseContext, PreferencesService preferencesService,
TaskExecutor taskExecutor, DialogService dialogService) {
public LatexCitationsTabViewModel(BibDatabaseContext databaseContext,
PreferencesService preferencesService,
TaskExecutor taskExecutor,
DialogService dialogService) {
this.databaseContext = databaseContext;
this.preferencesService = preferencesService;
this.taskExecutor = taskExecutor;
this.dialogService = dialogService;
this.directory = new SimpleObjectProperty(databaseContext.getMetaData().getLatexFileDirectory(preferencesService.getUser())
.orElse(FileUtil.getInitialDirectory(databaseContext, preferencesService)));
this.directory = new SimpleObjectProperty<>(databaseContext.getMetaData().getLatexFileDirectory(preferencesService.getFilePreferences().getUser())
.orElse(FileUtil.getInitialDirectory(databaseContext, preferencesService.getFilePreferences().getWorkingDirectory())));
this.citationList = FXCollections.observableArrayList();
this.status = new SimpleObjectProperty<>(Status.IN_PROGRESS);
this.searchError = new SimpleStringProperty("");
Expand Down Expand Up @@ -128,8 +130,8 @@ private void cancelSearch() {

private Collection<Citation> searchAndParse(String citeKey) throws IOException {
// we need to check whether the user meanwhile set the LaTeX file directory or the database changed locations
Path newDirectory = databaseContext.getMetaData().getLatexFileDirectory(preferencesService.getUser())
.orElse(FileUtil.getInitialDirectory(databaseContext, preferencesService));
Path newDirectory = databaseContext.getMetaData().getLatexFileDirectory(preferencesService.getFilePreferences().getUser())
.orElse(FileUtil.getInitialDirectory(databaseContext, preferencesService.getFilePreferences().getWorkingDirectory()));

if (latexParserResult == null || !newDirectory.equals(directory.get())) {
directory.set(newDirectory);
Expand Down Expand Up @@ -170,7 +172,7 @@ public void setLatexDirectory() {
.withInitialDirectory(directory.get()).build();

dialogService.showDirectorySelectionDialog(directoryDialogConfiguration).ifPresent(selectedDirectory ->
databaseContext.getMetaData().setLatexFileDirectory(preferencesService.getUser(), selectedDirectory.toAbsolutePath()));
databaseContext.getMetaData().setLatexFileDirectory(preferencesService.getFilePreferences().getUser(), selectedDirectory.toAbsolutePath()));

init(currentEntry);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ private Optional<Path> askForSavePath() {
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.addExtensionFilter(StandardFileType.BIBTEX_DB)
.withDefaultExtension(StandardFileType.BIBTEX_DB)
.withInitialDirectory(preferences.getWorkingDir())
.withInitialDirectory(preferences.getFilePreferences().getWorkingDirectory())
.build();
Optional<Path> selectedPath = dialogService.showFileSaveDialog(fileDialogConfiguration);
selectedPath.ifPresent(path -> preferences.setWorkingDirectory(path.getParent()));
selectedPath.ifPresent(path -> preferences.getFilePreferences().setWorkingDirectory(path.getParent()));
if (selectedPath.isPresent()) {
Path savePath = selectedPath.get();
// Workaround for linux systems not adding file extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ public class UnlinkedFilesDialogViewModel {

private final FunctionBasedValidator<String> scanDirectoryValidator;

public UnlinkedFilesDialogViewModel(DialogService dialogService, ExternalFileTypes externalFileTypes, UndoManager undoManager,
FileUpdateMonitor fileUpdateMonitor, PreferencesService preferences, StateManager stateManager, TaskExecutor taskExecutor) {
public UnlinkedFilesDialogViewModel(DialogService dialogService,
ExternalFileTypes externalFileTypes,
UndoManager undoManager,
FileUpdateMonitor fileUpdateMonitor,
PreferencesService preferences,
StateManager stateManager,
TaskExecutor taskExecutor) {
this.preferences = preferences;
this.dialogService = dialogService;
this.taskExecutor = taskExecutor;
Expand All @@ -95,9 +100,9 @@ public UnlinkedFilesDialogViewModel(DialogService dialogService, ExternalFileTyp
stateManager);

this.fileFilterList = FXCollections.observableArrayList(
new FileExtensionViewModel(StandardFileType.ANY_FILE, externalFileTypes),
new FileExtensionViewModel(StandardFileType.BIBTEX_DB, externalFileTypes),
new FileExtensionViewModel(StandardFileType.PDF, externalFileTypes));
new FileExtensionViewModel(StandardFileType.ANY_FILE, externalFileTypes),
new FileExtensionViewModel(StandardFileType.BIBTEX_DB, externalFileTypes),
new FileExtensionViewModel(StandardFileType.PDF, externalFileTypes));

this.dateFilterList = FXCollections.observableArrayList(DateRange.values());

Expand Down Expand Up @@ -175,7 +180,7 @@ public void startExport() {
}

FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.withInitialDirectory(preferences.getWorkingDir())
.withInitialDirectory(preferences.getFilePreferences().getWorkingDirectory())
.addExtensionFilter(StandardFileType.TXT)
.withDefaultExtension(StandardFileType.TXT)
.build();
Expand Down Expand Up @@ -218,12 +223,12 @@ public void cancelTasks() {

public void browseFileDirectory() {
DirectoryDialogConfiguration directoryDialogConfiguration = new DirectoryDialogConfiguration.Builder()
.withInitialDirectory(preferences.getWorkingDir()).build();
.withInitialDirectory(preferences.getFilePreferences().getWorkingDirectory()).build();

dialogService.showDirectorySelectionDialog(directoryDialogConfiguration)
.ifPresent(selectedDirectory -> {
directoryPath.setValue(selectedDirectory.toAbsolutePath().toString());
preferences.setWorkingDirectory(selectedDirectory.toAbsolutePath());
preferences.getFilePreferences().setWorkingDirectory(selectedDirectory.toAbsolutePath());
});
}

Expand Down
Loading

0 comments on commit ab13b2a

Please sign in to comment.