Skip to content

Commit

Permalink
Rework actions on bindings instead of using directly change listener …
Browse files Browse the repository at this point in the history
…- review changes
  • Loading branch information
ricantech authored and ricantech committed Feb 17, 2019
1 parent e4f3ea3 commit 205170c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.function.Supplier;

import javafx.beans.property.ListProperty;
import javafx.beans.value.ChangeListener;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
Expand All @@ -20,6 +19,7 @@
import org.jabref.model.metadata.MetaData;

import com.airhacks.afterburner.views.ViewLoader;
import org.fxmisc.easybind.EasyBind;

public class ContentSelectorDialogView extends BaseDialog<Void> {

Expand Down Expand Up @@ -65,8 +65,22 @@ public ContentSelectorDialogView(JabRefFrame jabRefFrame) {
public void initialize() {
viewModel = new ContentSelectorDialogViewModel(metaData, basePanel, dialogService);

initListView(fieldNamesListView, viewModel::getFieldNamesBackingList, (observable, oldValue, newValue) -> onFieldNameSelected(newValue));
initListView(keywordsListView, viewModel::getKeywordsBackingList, (observable, oldValue, newValue) -> onKeywordSelected());
initFieldNameComponents();
initKeywordsComponents();
}

private void initFieldNameComponents() {
initListView(fieldNamesListView, viewModel::getFieldNamesBackingList);
viewModel.selectedFieldNameProperty().bind(fieldNamesListView.getSelectionModel().selectedItemProperty());
removeFieldNameButton.disableProperty().bind(viewModel.isNoFieldNameSelected());
EasyBind.subscribe(viewModel.selectedFieldNameProperty(), viewModel::populateKeywords);
}

private void initKeywordsComponents() {
initListView(keywordsListView, viewModel::getKeywordsBackingList);
viewModel.selectedKeywordProperty().bind(keywordsListView.getSelectionModel().selectedItemProperty());
addKeywordButton.disableProperty().bind(viewModel.isFieldNameListEmpty());
removeKeywordButton.disableProperty().bind(viewModel.isNoKeywordSelected());
}

@FXML
Expand All @@ -93,23 +107,11 @@ private void removeKeyword() {
}
}

private void initListView(ListView<String> listViewToInit, Supplier<ListProperty<String>> backingList, ChangeListener<String> onSelectedListener) {
private void initListView(ListView<String> listViewToInit, Supplier<ListProperty<String>> backingList) {
listViewToInit.itemsProperty().bind(backingList.get());
listViewToInit.getSelectionModel().selectedItemProperty().addListener(onSelectedListener);
listViewToInit.getSelectionModel().select(FIRST_ELEMENT);
}

private void onFieldNameSelected(String newValue) {
removeKeywordButton.setDisable(!getSelectedKeyword().isPresent());
removeFieldNameButton.setDisable(!getSelectedFieldName().isPresent());
addKeywordButton.setDisable(newValue == null);
viewModel.populateKeywordsFor((newValue));
}

private void onKeywordSelected() {
removeKeywordButton.setDisable(!getSelectedKeyword().isPresent());
}

private Optional<String> getSelectedFieldName() {
return Optional.of(fieldNamesListView.getSelectionModel()).map(SelectionModel::getSelectedItem);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
import java.util.Set;
import java.util.stream.Collectors;

import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;

import org.jabref.gui.AbstractViewModel;
Expand All @@ -34,6 +38,8 @@ class ContentSelectorDialogViewModel extends AbstractViewModel {

private ListProperty<String> fieldNames = new SimpleListProperty<>(FXCollections.observableArrayList());
private ListProperty<String> keywords = new SimpleListProperty<>(FXCollections.observableArrayList());
private StringProperty selectedFieldName = new SimpleStringProperty();
private StringProperty selectedKeyword = new SimpleStringProperty();

ContentSelectorDialogViewModel(MetaData metaData, BasePanel basePanel, DialogService dialogService) {
this.metaData = metaData;
Expand Down Expand Up @@ -63,10 +69,30 @@ ListProperty<String> getFieldNamesBackingList() {
return fieldNames;
}

StringProperty selectedFieldNameProperty() {
return selectedFieldName;
}

BooleanBinding isFieldNameListEmpty() {
return Bindings.isEmpty(fieldNames);
}

BooleanBinding isNoFieldNameSelected() {
return Bindings.isEmpty(selectedFieldName);
}

ListProperty<String> getKeywordsBackingList() {
return keywords;
}

StringProperty selectedKeywordProperty() {
return selectedKeyword;
}

BooleanBinding isNoKeywordSelected() {
return Bindings.isEmpty(selectedKeyword);
}

void showInputFieldNameDialog() {
dialogService.showInputDialogAndWait(Localization.lang("Add new field name"), Localization.lang("Field name:"))
.ifPresent(this::addFieldNameIfUnique);
Expand Down Expand Up @@ -104,20 +130,19 @@ private void removeFieldName(String fieldNameToRemove) {
fieldNames.remove(fieldNameToRemove);
}

void populateKeywordsFor(String selectedFieldName) {
void populateKeywords(String selectedFieldName) {
keywords.clear();
if (selectedFieldName != null) {
keywords.addAll(fieldNameKeywordsMap.get(selectedFieldName));
}

}

void showInputKeywordDialog(String selectedFieldName) {
dialogService.showInputDialogAndWait(Localization.lang("Add new keyword"), Localization.lang("Keyword:"))
.ifPresent(newKeyword -> addKeyword(selectedFieldName, newKeyword));
.ifPresent(newKeyword -> addKeywordIfUnique(selectedFieldName, newKeyword));
}

private void addKeyword(String fieldName, String keywordToAdd) {
private void addKeywordIfUnique(String fieldName, String keywordToAdd) {
boolean exists = fieldNameKeywordsMap.get(fieldName).contains(keywordToAdd);
if (exists) {
dialogService.showErrorDialogAndWait(Localization.lang("Keyword \"%0\" already exists", keywordToAdd));
Expand All @@ -128,7 +153,7 @@ private void addKeyword(String fieldName, String keywordToAdd) {
existingKeywords.add(keywordToAdd);
fieldNameKeywordsMap.put(fieldName, existingKeywords);
keywords.add(keywordToAdd);
populateKeywordsFor(fieldName);
populateKeywords(fieldName);
}

void showRemoveKeywordConfirmationDialog(String fieldName, String keywordToRemove) {
Expand Down

0 comments on commit 205170c

Please sign in to comment.