Skip to content

Commit

Permalink
Remember last choice of download files in import dialog (#6761)
Browse files Browse the repository at this point in the history
* Remember last choice of download files in import dialog

* Fix checkstyle issues
  • Loading branch information
tobiasdiez authored Aug 18, 2020
1 parent ec53712 commit 40d2834
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ImportEntriesDialog(BibDatabaseContext database, BackgroundTask<ParserRes
Button btn = (Button) this.getDialogPane().lookupButton(importButton);
btn.disableProperty().bind(booleanBind);

downloadLinkedOnlineFiles.setSelected(preferences.getFilePreferences().getDownloadLinkedFiles());
downloadLinkedOnlineFiles.setSelected(preferences.getFilePreferences().shouldDownloadLinkedFiles());

setResultConverter(button -> {
if (button == importButton) {
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/org/jabref/gui/importer/ImportEntriesViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.jabref.model.entry.BibtexString;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.groups.GroupTreeNode;
import org.jabref.model.metadata.FilePreferences;
import org.jabref.model.metadata.MetaData;
import org.jabref.model.util.FileUpdateMonitor;
import org.jabref.preferences.PreferencesService;
Expand All @@ -53,8 +54,8 @@ public class ImportEntriesViewModel extends AbstractViewModel {
private final StateManager stateManager;
private final FileUpdateMonitor fileUpdateMonitor;
private ParserResult parserResult = null;
private ObservableList<BibEntry> entries;
private PreferencesService preferences;
private final ObservableList<BibEntry> entries;
private final PreferencesService preferences;

/**
* @param databaseContext the database to import into
Expand Down Expand Up @@ -103,7 +104,7 @@ public boolean hasDuplicate(BibEntry entry) {
*
* @param entriesToImport subset of the entries contained in parserResult
*/
public void importEntries(List<BibEntry> entriesToImport, boolean downloadFiles) {
public void importEntries(List<BibEntry> entriesToImport, boolean shouldDownloadFiles) {
// Check if we are supposed to warn about duplicates.
// If so, then see if there are duplicates, and warn if yes.
if (preferences.shouldWarnAboutDuplicatesForImport()) {
Expand All @@ -130,10 +131,15 @@ public void importEntries(List<BibEntry> entriesToImport, boolean downloadFiles)
buildImportHandlerThenImportEntries(entriesToImport);
}

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

if (shouldDownloadFiles) {
for (BibEntry bibEntry : entriesToImport) {
for (LinkedFile linkedFile : bibEntry.getFiles()) {
LinkedFileViewModel linkedFileViewModel = new LinkedFileViewModel(linkedFile, bibEntry, databaseContext, taskExecutor, dialogService, preferences.getXMPPreferences(), preferences.getFilePreferences(), ExternalFileTypes.getInstance());
LinkedFileViewModel linkedFileViewModel = new LinkedFileViewModel(linkedFile, bibEntry, databaseContext, taskExecutor, dialogService, preferences.getXMPPreferences(), filePreferences, ExternalFileTypes.getInstance());
linkedFileViewModel.download();
}
}
Expand Down Expand Up @@ -163,7 +169,7 @@ public void importEntries(List<BibEntry> entriesToImport, boolean downloadFiles)
MetaData targetMetada = databaseContext.getMetaData();
parserResult.getMetaData()
.getContentSelectorList()
.forEach(contentSelector -> targetMetada.addContentSelector(contentSelector));
.forEach(targetMetada::addContentSelector);
// TODO undo of content selectors (currently not implemented)

// copy groups to target database
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/jabref/gui/preferences/ImportTab.fxml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
Expand Down Expand Up @@ -28,8 +27,7 @@
prefWidth="300" minWidth="300" maxWidth="300"/>

<Label text="%File directory pattern" GridPane.rowIndex="1"/>
<TextField fx:id="fileDirPattern" GridPane.columnIndex="1" GridPane.rowIndex="1"
<TextField fx:id="fileDirectoryPattern" GridPane.columnIndex="1" GridPane.rowIndex="1"
prefWidth="300" minWidth="300" maxWidth="300"/>
<CheckBox fx:id="downloadLinkedFiles" text="%Download linked online files" GridPane.rowIndex="2"/>
</GridPane>
</fx:root>
9 changes: 3 additions & 6 deletions src/main/java/org/jabref/gui/preferences/ImportTabView.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jabref.gui.preferences;

import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;

Expand All @@ -13,8 +12,7 @@
public class ImportTabView extends AbstractPreferenceTabView<ImportTabViewModel> implements PreferencesTab {

@FXML private ComboBox<String> fileNamePattern;
@FXML private TextField fileDirPattern;
@FXML private CheckBox downloadLinkedFiles;
@FXML private TextField fileDirectoryPattern;

public ImportTabView(JabRefPreferences preferences) {
this.preferences = preferences;
Expand All @@ -25,12 +23,11 @@ public ImportTabView(JabRefPreferences preferences) {
}

public void initialize() {
this.viewModel = new ImportTabViewModel(dialogService, preferences);
this.viewModel = new ImportTabViewModel(preferences);

fileNamePattern.valueProperty().bindBidirectional(viewModel.fileNamePatternProperty());
fileNamePattern.itemsProperty().bind(viewModel.defaultFileNamePatternsProperty());
fileDirPattern.textProperty().bindBidirectional(viewModel.fileDirPatternProperty());
downloadLinkedFiles.selectedProperty().bindBidirectional(viewModel.downloadLinkedFilesProperty());
fileDirectoryPattern.textProperty().bindBidirectional(viewModel.fileDirPatternProperty());
}

@Override
Expand Down
14 changes: 1 addition & 13 deletions src/main/java/org/jabref/gui/preferences/ImportTabViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
import java.util.ArrayList;
import java.util.List;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;

import org.jabref.gui.DialogService;
import org.jabref.preferences.JabRefPreferences;

public class ImportTabViewModel implements PreferenceTabViewModel {
Expand All @@ -21,28 +18,23 @@ public class ImportTabViewModel implements PreferenceTabViewModel {
private final ListProperty<String> defaultFileNamePatternsProperty = new SimpleListProperty<>(FXCollections.observableArrayList(DEFAULT_FILENAME_PATTERNS));
private final StringProperty fileNamePatternProperty = new SimpleStringProperty();
private final StringProperty fileDirPatternProperty = new SimpleStringProperty();
private final BooleanProperty downloadLinkedFilesProperty = new SimpleBooleanProperty();

private final DialogService dialogService;
private final JabRefPreferences preferences;

public ImportTabViewModel(DialogService dialogService, JabRefPreferences preferences) {
this.dialogService = dialogService;
public ImportTabViewModel(JabRefPreferences preferences) {
this.preferences = preferences;
}

@Override
public void setValues() {
fileNamePatternProperty.setValue(preferences.get(JabRefPreferences.IMPORT_FILENAMEPATTERN));
fileDirPatternProperty.setValue(preferences.get(JabRefPreferences.IMPORT_FILEDIRPATTERN));
downloadLinkedFilesProperty.setValue(preferences.getBoolean(JabRefPreferences.DOWNLOAD_LINKED_FILES));
}

@Override
public void storeSettings() {
preferences.put(JabRefPreferences.IMPORT_FILENAMEPATTERN, fileNamePatternProperty.getValue());
preferences.put(JabRefPreferences.IMPORT_FILEDIRPATTERN, fileDirPatternProperty.getValue());
preferences.putBoolean(JabRefPreferences.DOWNLOAD_LINKED_FILES, downloadLinkedFilesProperty.getValue());
}

@Override
Expand All @@ -66,8 +58,4 @@ public StringProperty fileNamePatternProperty() {
public StringProperty fileDirPatternProperty() {
return fileDirPatternProperty;
}

public BooleanProperty downloadLinkedFilesProperty() {
return downloadLinkedFilesProperty;
}
}
14 changes: 9 additions & 5 deletions src/main/java/org/jabref/model/metadata/FilePreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ public class FilePreferences {
private final boolean bibLocationAsPrimary;
private final String fileNamePattern;
private final String fileDirPattern;
private final boolean downloadLinkedFiles;
private boolean shouldDownloadLinkedFiles;

public FilePreferences(String user,
String mainFileDirectory,
boolean bibLocationAsPrimary,
String fileNamePattern,
String fileDirPattern,
boolean downloadLinkedFiles) {
boolean shouldDownloadLinkedFiles) {
this.user = user;
this.mainFileDirectory = mainFileDirectory;
this.bibLocationAsPrimary = bibLocationAsPrimary;
this.fileNamePattern = fileNamePattern;
this.fileDirPattern = fileDirPattern;
this.downloadLinkedFiles = downloadLinkedFiles;
this.shouldDownloadLinkedFiles = shouldDownloadLinkedFiles;
}

public String getUser() {
Expand All @@ -52,7 +52,11 @@ public String getFileDirPattern() {
return fileDirPattern;
}

public boolean getDownloadLinkedFiles() {
return downloadLinkedFiles;
public boolean shouldDownloadLinkedFiles() {
return shouldDownloadLinkedFiles;
}

public void setShouldDownloadLinkedFiles(boolean shouldDownloadLinkedFiles) {
this.shouldDownloadLinkedFiles = shouldDownloadLinkedFiles;
}
}
15 changes: 12 additions & 3 deletions src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ public class JabRefPreferences implements PreferencesService {
public static final String CLEANUP_FORMATTERS = "CleanUpFormatters";
public static final String IMPORT_FILENAMEPATTERN = "importFileNamePattern";
public static final String IMPORT_FILEDIRPATTERN = "importFileDirPattern";
public static final String DOWNLOAD_LINKED_FILES = "downloadLinkedFiles";
public static final String NAME_FORMATTER_VALUE = "nameFormatterFormats";
public static final String NAME_FORMATER_KEY = "nameFormatterNames";
public static final String PUSH_TO_APPLICATION = "pushToApplication";
Expand Down Expand Up @@ -363,6 +362,7 @@ public class JabRefPreferences implements PreferencesService {

// Dialog states
private static final String PREFS_EXPORT_PATH = "prefsExportPath";
private static final String DOWNLOAD_LINKED_FILES = "downloadLinkedFiles";

// Helper string
private static final String USER_HOME = System.getProperty("user.home");
Expand Down Expand Up @@ -654,8 +654,8 @@ private JabRefPreferences() {
defaults.put(IMPORT_FILENAMEPATTERN, ImportTabViewModel.DEFAULT_FILENAME_PATTERNS[1]);
// Default empty String to be backwards compatible
defaults.put(IMPORT_FILEDIRPATTERN, "");
// Don't download files by default
defaults.put(DOWNLOAD_LINKED_FILES, false);
// Download files by default
defaults.put(DOWNLOAD_LINKED_FILES, true);

customImports = new CustomImportList(this);

Expand Down Expand Up @@ -1189,6 +1189,15 @@ public FilePreferences getFilePreferences() {
getBoolean(DOWNLOAD_LINKED_FILES));
}

@Override
public void storeFilePreferences(FilePreferences filePreferences) {
put(JabRefPreferences.MAIN_FILE_DIRECTORY, filePreferences.getFileDirectory().map(Path::toString).orElse(""));
putBoolean(JabRefPreferences.BIB_LOC_AS_PRIMARY_DIR, filePreferences.isBibLocationAsPrimary());
put(JabRefPreferences.IMPORT_FILENAMEPATTERN, filePreferences.getFileNamePattern());
put(JabRefPreferences.IMPORT_FILEDIRPATTERN, filePreferences.getFileDirPattern());
putBoolean(JabRefPreferences.DOWNLOAD_LINKED_FILES, filePreferences.shouldDownloadLinkedFiles());
}

@Override
public FieldWriterPreferences getFieldWriterPreferences() {
return new FieldWriterPreferences(
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/jabref/preferences/PreferencesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public interface PreferencesService {

FilePreferences getFilePreferences();

void storeFilePreferences(FilePreferences filePreferences);

FieldWriterPreferences getFieldWriterPreferences();

FieldContentFormatterPreferences getFieldContentParserPreferences();
Expand Down

0 comments on commit 40d2834

Please sign in to comment.