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

Added a download checkbox to the import dialog #6381

Merged
merged 12 commits into from
May 4, 2020
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.ButtonType?>
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.Label?>
Expand Down Expand Up @@ -38,6 +39,7 @@
<Label fx:id="selectedItems" GridPane.rowIndex="1" GridPane.columnIndex="1"/>
</GridPane>
</HBox>
<CheckBox fx:id="downloadLinkedOnlineFiles" text="%Download linked online files"/>
</VBox>
</content>
<ButtonType fx:id="importButton" buttonData="OK_DONE" text="%Import entries"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.Tooltip;
Expand All @@ -38,6 +39,7 @@
import org.jabref.model.entry.types.EntryType;
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.model.util.FileUpdateMonitor;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.views.ViewLoader;
Expand All @@ -50,6 +52,7 @@ public class ImportEntriesDialog extends BaseDialog<Boolean> {
public ButtonType importButton;
public Label totalItems;
public Label selectedItems;
public CheckBox downloadLinkedOnlineFiles;
private final BackgroundTask<ParserResult> task;
private ImportEntriesViewModel viewModel;
@Inject private TaskExecutor taskExecutor;
Expand Down Expand Up @@ -77,9 +80,11 @@ public ImportEntriesDialog(BibDatabaseContext database, BackgroundTask<ParserRes
Button btn = (Button) this.getDialogPane().lookupButton(importButton);
btn.disableProperty().bind(booleanBind);

downloadLinkedOnlineFiles.setSelected(JabRefPreferences.getInstance().getFilePreferences().getDownloadLinkedFiles());
btut marked this conversation as resolved.
Show resolved Hide resolved

setResultConverter(button -> {
if (button == importButton) {
viewModel.importEntries(entriesListView.getCheckModel().getCheckedItems());
viewModel.importEntries(entriesListView.getCheckModel().getCheckedItems(), downloadLinkedOnlineFiles.isSelected());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As JabRef uses the MVVM approach, the ideal solution would be to add a property for the checkbox in the viewmodel and bind that to the checkbox's selected item property. That should have ideally also be already the case for the checkedItems.. Don't know why it's not done.

https://devdocs.jabref.org/readings-on-coding/javafx

} else {
dialogService.notify(Localization.lang("Import canceled"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.jabref.gui.duplicationFinder.DuplicateResolverDialog;
import org.jabref.gui.externalfiles.ImportHandler;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.fieldeditors.LinkedFileViewModel;
import org.jabref.gui.groups.GroupTreeNodeViewModel;
import org.jabref.gui.groups.UndoableAddOrRemoveGroup;
import org.jabref.gui.undo.NamedCompound;
Expand All @@ -31,6 +32,7 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexString;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.groups.GroupTreeNode;
import org.jabref.model.metadata.MetaData;
import org.jabref.model.util.FileUpdateMonitor;
Expand All @@ -44,6 +46,7 @@ public class ImportEntriesViewModel extends AbstractViewModel {
private static final Logger LOGGER = LoggerFactory.getLogger(ImportEntriesViewModel.class);

private final StringProperty message;
private final TaskExecutor taskExecutor;
private final BibDatabaseContext databaseContext;
private final DialogService dialogService;
private final UndoManager undoManager;
Expand All @@ -58,6 +61,7 @@ public class ImportEntriesViewModel extends AbstractViewModel {
* @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) {
this.taskExecutor = taskExecutor;
this.databaseContext = databaseContext;
this.dialogService = dialogService;
this.undoManager = undoManager;
Expand Down Expand Up @@ -99,7 +103,7 @@ public boolean hasDuplicate(BibEntry entry) {
*
* @param entriesToImport subset of the entries contained in parserResult
*/
public void importEntries(List<BibEntry> entriesToImport) {
public void importEntries(List<BibEntry> entriesToImport, boolean downloadFiles) {
// 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 @@ -126,6 +130,15 @@ public void importEntries(List<BibEntry> entriesToImport) {
buildImportHandlerThenImportEntries(entriesToImport);
}

if (downloadFiles) {
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.download();
}
}
}

NamedCompound namedCompound = new NamedCompound(Localization.lang("Import file"));
namedCompound.addEdit(new UndoableInsertEntries(databaseContext.getDatabase(), entriesToImport));

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/jabref/gui/preferences/ImportTab.fxml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?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 @@ -30,5 +31,6 @@
<Label text="%File directory pattern" GridPane.rowIndex="1"/>
<TextField fx:id="fileDirPattern" 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>
3 changes: 3 additions & 0 deletions src/main/java/org/jabref/gui/preferences/ImportTabView.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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,6 +14,7 @@ public class ImportTabView extends AbstractPreferenceTabView<ImportTabViewModel>

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

public ImportTabView(JabRefPreferences preferences) {
this.preferences = preferences;
Expand All @@ -28,6 +30,7 @@ public void initialize() {
fileNamePattern.valueProperty().bindBidirectional(viewModel.fileNamePatternProperty());
fileNamePattern.itemsProperty().bind(viewModel.defaultFileNamePatternsProperty());
fileDirPattern.textProperty().bindBidirectional(viewModel.fileDirPatternProperty());
downloadLinkedFiles.selectedProperty().bindBidirectional(viewModel.downloadLinkedFilesProperty());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
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;
Expand All @@ -19,6 +21,7 @@ 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;
Expand All @@ -32,12 +35,14 @@ public ImportTabViewModel(DialogService dialogService, JabRefPreferences prefere
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 @@ -55,4 +60,6 @@ public List<String> getRestartWarnings() {
public StringProperty fileNamePatternProperty() { return fileNamePatternProperty; }

public StringProperty fileDirPatternProperty() { return fileDirPatternProperty; }

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

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

public String getUser() {
Expand All @@ -48,4 +51,6 @@ public String getFileNamePattern() {
public String getFileDirPattern() {
return fileDirPattern;
}

public boolean getDownloadLinkedFiles() { return downloadLinkedFiles; }
}
6 changes: 5 additions & 1 deletion src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ 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 @@ -632,6 +633,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);

customImports = new CustomImportList(this);

Expand Down Expand Up @@ -1205,7 +1208,8 @@ public FilePreferences getFilePreferences() {
get(MAIN_FILE_DIRECTORY),
getBoolean(BIB_LOC_AS_PRIMARY_DIR),
get(IMPORT_FILENAMEPATTERN),
get(IMPORT_FILEDIRPATTERN));
get(IMPORT_FILEDIRPATTERN),
getBoolean(DOWNLOAD_LINKED_FILES));
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1906,6 +1906,7 @@ Import\ canceled=Import canceled
Select\ all\ new\ entries=Select all new entries
Total\ items\ found\:=Total items found:
Selected\ items\:=Selected items:
Download\ linked\ online\ files=Download linked online files
Select\ the\ entries\ to\ be\ imported\:=Select the entries to be imported\:
Add\ new\ String=Add new String
Remove\ selected\ Strings=Remove selected Strings
Expand Down