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

Fix action helper when no library is open #6402

Merged
merged 3 commits into from
May 9, 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
39 changes: 22 additions & 17 deletions src/main/java/org/jabref/gui/actions/ActionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.util.FileHelper;
import org.jabref.preferences.PreferencesService;

import org.fxmisc.easybind.EasyBind;
import org.fxmisc.easybind.monadic.MonadicBinding;
import org.fxmisc.easybind.monadic.MonadicObservableValue;

public class ActionHelper {

public static BooleanExpression needsDatabase(StateManager stateManager) {
return stateManager.activeDatabaseProperty().isPresent();
}
Expand All @@ -30,9 +31,8 @@ public static BooleanExpression needsEntriesSelected(StateManager stateManager)
}

public static BooleanExpression needsEntriesSelected(int numberOfEntries, StateManager stateManager) {
return Bindings.createBooleanBinding(
() -> stateManager.getSelectedEntries().size() == numberOfEntries,
stateManager.getSelectedEntries());
return Bindings.createBooleanBinding(() -> stateManager.getSelectedEntries().size() == numberOfEntries,
stateManager.getSelectedEntries());
}

public static BooleanExpression isFieldSetForSelectedEntry(Field field, StateManager stateManager) {
Expand All @@ -50,18 +50,23 @@ public static BooleanExpression isAnyFieldSetForSelectedEntry(List<Field> fields
}

public static BooleanExpression isFilePresentForSelectedEntry(StateManager stateManager, PreferencesService preferencesService) {
return Bindings.createBooleanBinding(() -> {
List<LinkedFile> files = stateManager.getSelectedEntries().get(0).getFiles();
if ((files.size() > 0) && stateManager.getActiveDatabase().isPresent()) {
Optional<Path> filename = FileHelper.find(
stateManager.getActiveDatabase().get(),
files.get(0).getLink(),
preferencesService.getFilePreferences());
return filename.isPresent();
} else {
return false;
}
}, stateManager.getSelectedEntries(),
stateManager.getSelectedEntries().get(0).getFieldBinding(StandardField.FILE));

ObservableList<BibEntry> selectedEntries = stateManager.getSelectedEntries();
MonadicObservableValue<Boolean> fileIsPresent = EasyBind.monadic(Bindings.valueAt(selectedEntries, 0)).map(entry -> {
List<LinkedFile> files = entry.getFiles();
Copy link
Member

Choose a reason for hiding this comment

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

I think this does not update the status if a file is added to the currently selected entry. For this, the isAnyFieldSetForSelectedEntry method uses flatMap instead of map.

Copy link
Member Author

Choose a reason for hiding this comment

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

I just tested it, it works as well when I add or remove a file to an entry both the context menu and the View menu get updated with the correct status


if ((entry.getFiles().size() > 0) && stateManager.getActiveDatabase().isPresent()) {
Optional<Path> filename = FileHelper.find(
stateManager.getActiveDatabase().get(),
files.get(0).getLink(),
preferencesService.getFilePreferences());
return filename.isPresent();
} else {
return false;
}

}).orElse(false);

return BooleanExpression.booleanExpression(fileIsPresent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.preferences.PreferencesService;

import static org.jabref.gui.actions.ActionHelper.needsEntriesSelected;

public class ShowDocumentViewerAction extends SimpleCommand {

public ShowDocumentViewerAction(StateManager stateManager, PreferencesService preferences) {
this.executable.bind(ActionHelper.isFilePresentForSelectedEntry(stateManager, preferences));
this.executable.bind(needsEntriesSelected(stateManager).and(ActionHelper.isFilePresentForSelectedEntry(stateManager, preferences)));
}

@Override
Expand Down