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

[WIP] File link deletion dialog improvements #3690

Merged
merged 5 commits into from
Feb 5, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ For more details refer to the [field mapping help page](http://help.jabref.org/e
- We added Facebook and Twitter icons in the toolbar to link to our [Facebook](https://www.facebook.com/JabRef/) and [Twitter](https://twitter.com/jabref_org) pages.
- We no longer print empty lines when exporting an entry in RIS format [#3634](https://github.com/JabRef/jabref/issues/3634)
- We improved file saving so that hard links are now preserved when a save is performed [#2633](https://github.com/JabRef/jabref/issues/2633)
- We changed the default dialog option when removing a [file link](http://help.jabref.org/en/FileLinks#adding-external-links-to-an-entry) from an entry.
The new default removes the linked file from the entry instead of deleting the file from disk. [#3679](https://github.com/JabRef/jabref/issues/3679)

### Fixed
- We fixed the missing dot in the name of an exported file. [#3576](https://github.com/JabRef/jabref/issues/3576)
- Autocompletion in the search bar can now be disabled via the preferences. [#3598](https://github.com/JabRef/jabref/issues/3598)
- We fixed and extended the RIS import functionality to cover more fields. [#3634](https://github.com/JabRef/jabref/issues/3634) [#2607](https://github.com/JabRef/jabref/issues/2607)
- Chaining modifiers in BibTeX key pattern now works as described in the documentation. [#3648](https://github.com/JabRef/jabref/issues/3648)
- We fixed an issue where not all bibtex/biblatex fields would be exported as latex-free to MS-Office XML [koppor#284](https://github.com/koppor/jabref/issues/284)
- We fixed an issue where linked files would be deleted from bibliography entries despite choosing the "Cancel" option in the dialog menu.

### Removed
- We removed the [Look and Feels from JGoodies](http://www.jgoodies.com/freeware/libraries/looks/), because the open source version is not compatible with Java 9.
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static javafx.scene.control.ButtonBar.ButtonData;

public class LinkedFileViewModel extends AbstractViewModel {

private static final Logger LOGGER = LoggerFactory.getLogger(LinkedFileViewModel.class);
Expand All @@ -54,15 +56,21 @@ public class LinkedFileViewModel extends AbstractViewModel {
private final BooleanProperty downloadOngoing = new SimpleBooleanProperty(false);
private final BooleanProperty isAutomaticallyFound = new SimpleBooleanProperty(false);
private final BooleanProperty canWriteXMPMetadata = new SimpleBooleanProperty(false);
private final DialogService dialogService = new FXDialogService();
private final DialogService dialogService;
private final BibEntry entry;
private final TaskExecutor taskExecutor;

public LinkedFileViewModel(LinkedFile linkedFile, BibEntry entry, BibDatabaseContext databaseContext, TaskExecutor taskExecutor) {
this(linkedFile, entry, databaseContext, taskExecutor, new FXDialogService());
}

protected LinkedFileViewModel(LinkedFile linkedFile, BibEntry entry, BibDatabaseContext databaseContext,
TaskExecutor taskExecutor, DialogService dialogService) {
this.linkedFile = linkedFile;
this.databaseContext = databaseContext;
this.entry = entry;
this.taskExecutor = taskExecutor;
this.dialogService = dialogService;

downloadOngoing.bind(downloadProgress.greaterThanOrEqualTo(0).and(downloadProgress.lessThan(100)));
canWriteXMPMetadata.setValue(!linkedFile.isOnlineLink() && linkedFile.getFileType().equalsIgnoreCase("pdf"));
Expand Down Expand Up @@ -274,14 +282,14 @@ public void moveToDefaultDirectory() {

public boolean delete() {
Optional<Path> file = linkedFile.findIn(databaseContext, Globals.prefs.getFileDirectoryPreferences());
ButtonType removeFromEntry = new ButtonType(Localization.lang("Remove from entry"));
ButtonType removeFromEntry = new ButtonType(Localization.lang("Remove from entry"), ButtonData.YES);

if (file.isPresent()) {
ButtonType deleteFromEntry = new ButtonType(Localization.lang("Delete from disk"));
Optional<ButtonType> buttonType = dialogService.showCustomButtonDialogAndWait(AlertType.INFORMATION,
Localization.lang("Delete '%0'", file.get().toString()),
Localization.lang("Delete the selected file permanently from disk, or just remove the file from the entry? Pressing Delete will delete the file permanently from disk."),
deleteFromEntry, removeFromEntry, ButtonType.CANCEL);
removeFromEntry, deleteFromEntry, ButtonType.CANCEL);
Copy link
Member

@tobiasdiez tobiasdiez Feb 3, 2018

Choose a reason for hiding this comment

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

This change is fine. Could you please also add ButtonData.Yes as a second constructor argument for the removeFromEntry button. With this the buttons are automatically ordered correctly according to the current OS specifications.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, not a problem. Would you like to have the change rebased into the first commit, or is a new one on top of the current two acceptable?

Copy link
Member

Choose a reason for hiding this comment

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

Just add them in a new commit. We usually squash all commits on merge


if (buttonType.isPresent()) {
if (buttonType.get().equals(removeFromEntry)) {
Expand All @@ -304,7 +312,7 @@ public boolean delete() {
} else {
LOGGER.warn("Could not find file " + linkedFile.getLink());
}
return true;
return false;
}

public void edit() {
Expand Down
154 changes: 154 additions & 0 deletions src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package org.jabref.gui.fieldeditors;

import java.io.File;
import java.io.IOException;
import java.util.Optional;

import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;

import org.jabref.gui.DialogService;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.metadata.FileDirectoryPreferences;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

public class LinkedFileViewModelTest {

@Rule public TemporaryFolder tempFolder = new TemporaryFolder();
private LinkedFile linkedFile;
private BibEntry entry;
private BibDatabaseContext databaseContext;
private TaskExecutor taskExecutor;
private DialogService dialogService;

@Before
public void setUp() {
entry = new BibEntry();
databaseContext = new BibDatabaseContext();
taskExecutor = mock(TaskExecutor.class);
dialogService = mock(DialogService.class);
}

@Test
public void deleteFilePathNotPresent() {
// Making this a spy, so we can inject an empty optional without digging into the implementation
linkedFile = spy(new LinkedFile("", "", ""));
Copy link
Member

Choose a reason for hiding this comment

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

You could also pass "non-existent file" as the path as you do below. This might be a bit easier to understand.

doReturn(Optional.empty()).when(linkedFile).findIn(
any(BibDatabaseContext.class), any(FileDirectoryPreferences.class)
);

LinkedFileViewModel viewModel = new LinkedFileViewModel(
linkedFile, entry, databaseContext, taskExecutor, dialogService
);
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure, but I think our style conventions say that the closing parenthesis has to go on the same line. Right, @koppor ?

boolean removed = viewModel.delete();

assertFalse(removed);
verifyZeroInteractions(dialogService); // dialog was never shown
}

@Test
public void deleteDialogRemoveFromEntry() throws IOException {
File tempFile = tempFolder.newFile();
linkedFile = new LinkedFile("", tempFile.getAbsolutePath(), "");
when(dialogService.showCustomButtonDialogAndWait(
any(AlertType.class),
anyString(),
anyString(),
any(ButtonType.class),
any(ButtonType.class),
any(ButtonType.class)
)).thenAnswer(invocation -> Optional.of(invocation.getArgument(3))); // first vararg - remove button
Copy link
Contributor Author

@bdach bdach Feb 4, 2018

Choose a reason for hiding this comment

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

To explain myself with this -- the implementation uses strict reference equality for checking user choices (at least for the first two), so I thought about either getting out the parameter from the invocation like this, or modifying the equality checks to use the ButtonData field in the button types, which made the implementation very weird looking.


LinkedFileViewModel viewModel = new LinkedFileViewModel(
linkedFile, entry, databaseContext, taskExecutor, dialogService
);
boolean removed = viewModel.delete();

assertTrue(removed);
assertTrue(tempFile.exists());
}

@Test
public void deleteDialogDeleteFromEntry() throws IOException {
File tempFile = tempFolder.newFile();
linkedFile = new LinkedFile("", tempFile.getAbsolutePath(), "");
when(dialogService.showCustomButtonDialogAndWait(
any(AlertType.class),
anyString(),
anyString(),
any(ButtonType.class),
any(ButtonType.class),
any(ButtonType.class)
)).thenAnswer(invocation -> Optional.of(invocation.getArgument(4))); // second vararg - delete button

LinkedFileViewModel viewModel = new LinkedFileViewModel(
linkedFile, entry, databaseContext, taskExecutor, dialogService
);
boolean removed = viewModel.delete();

assertTrue(removed);
assertFalse(tempFile.exists());
}


@Test
public void deleteDialogDeleteFromEntryException() throws IOException {
linkedFile = new LinkedFile("", "!!nonexistent file!!", "");
when(dialogService.showCustomButtonDialogAndWait(
any(AlertType.class),
anyString(),
anyString(),
any(ButtonType.class),
any(ButtonType.class),
any(ButtonType.class)
)).thenAnswer(invocation -> Optional.of(invocation.getArgument(4))); // second vararg - delete button

LinkedFileViewModel viewModel = new LinkedFileViewModel(
linkedFile, entry, databaseContext, taskExecutor, dialogService
);
boolean removed = viewModel.delete();

verify(dialogService).showErrorDialogAndWait(anyString(), anyString());
assertFalse(removed);
}

@Test
public void deleteDialogCanceled() throws IOException {
Copy link
Member

Choose a reason for hiding this comment

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

In general we try to name the tests according to the scheme methodUnderTest_scenario_expectedBehavior (without the underscore). So for this test here, it would be e.g. deleteWhenDialogCancelledReturnsFalse.

File tempFile = tempFolder.newFile();
linkedFile = new LinkedFile("desc", tempFile.getAbsolutePath(), "pdf");
when(dialogService.showCustomButtonDialogAndWait(
any(AlertType.class),
anyString(),
anyString(),
any(ButtonType.class),
any(ButtonType.class),
any(ButtonType.class)
)).thenAnswer(invocation -> Optional.of(invocation.getArgument(5))); // third vararg - cancel button

LinkedFileViewModel viewModel = new LinkedFileViewModel(
linkedFile, entry, databaseContext, taskExecutor, dialogService
);
boolean removed = viewModel.delete();

assertFalse(removed);
assertTrue(tempFile.exists());
}
}