Skip to content

Commit

Permalink
feat: refactor the way how category show/hide in navigation tree
Browse files Browse the repository at this point in the history
  • Loading branch information
aldanchenko committed Aug 2, 2022
1 parent 4fb7eae commit d61e804
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*******************************************************************************/
package com.dlsc.preferencesfx.view;

import java.util.HashMap;
import java.util.Map;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
Expand Down Expand Up @@ -42,7 +44,7 @@ public class FilterableTreeItem<T> extends CheckBoxTreeItem<T> {
private final FilteredList<FilterableTreeItem<T>> filteredList =new FilteredList<>(sourceList);
private final ObjectProperty<TreeItemPredicate<T>> predicate = new SimpleObjectProperty<>();

private boolean isVisible = true;
private Map<FilterableTreeItem<T>, Integer> childItemIndexesMap = new HashMap<>();

/**
* Creates a new {@link TreeItem} with sorted children.
Expand All @@ -52,6 +54,10 @@ public class FilterableTreeItem<T> extends CheckBoxTreeItem<T> {
public FilterableTreeItem(T value) {
super(value);

setupFilteredListPredicateBindings();
}

private void setupFilteredListPredicateBindings() {
filteredList.predicateProperty().bind(Bindings.createObjectBinding(() -> {
Predicate<FilterableTreeItem<T>> treeItemPredicate = child -> {
// Set the predicate of child items to force filtering
Expand All @@ -60,10 +66,6 @@ public FilterableTreeItem(T value) {
filterableChild.setPredicate(predicate.get());
}

if (!child.isVisible) {
return false;
}

// If there is no predicate, keep this tree item
if (predicate.get() == null) {
return true;
Expand All @@ -82,7 +84,7 @@ public FilterableTreeItem(T value) {

Bindings.bindContent(getChildren(), getBackingList());
}

/**
* @return the backing list
*/
Expand Down Expand Up @@ -120,11 +122,20 @@ public final void setPredicate(TreeItemPredicate<T> predicate) {
this.predicate.set(predicate);
}

public boolean isVisible() {
return isVisible;
}

public void setVisible(boolean visible) {
isVisible = visible;
/**
* Remove child item from {@link #sourceList} and save item position if visibility is false.
* If visibility is true it restore (add item to {@link #sourceList}) to same position using index.
*
* @param childItem - child item object
* @param visible - visibility of this child item
*/
public void changeChildItemVisibility(FilterableTreeItem<T> childItem, boolean visible) {
if (visible) {
sourceList.add(childItemIndexesMap.get(childItem), childItem);
} else {
childItemIndexesMap.put(childItem, sourceList.indexOf(childItem));

sourceList.remove(childItem);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.dlsc.preferencesfx.model.Category;
import com.dlsc.preferencesfx.model.PreferencesFxModel;
import com.dlsc.preferencesfx.util.SearchHandler;
import java.util.stream.Collectors;
import javafx.beans.property.StringProperty;
import javafx.collections.ObservableList;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import org.slf4j.Logger;
Expand Down Expand Up @@ -108,24 +110,26 @@ public void setupBindings() {

}

private void addRecursive(FilterableTreeItem<Category> treeItem, List<Category> categories) {
private void addRecursive(FilterableTreeItem<Category> parentTreeItem, List<Category> categories) {
for (Category category : categories) {
FilterableTreeItem<Category> item = new FilterableTreeItem<>(category);
FilterableTreeItem<Category> treeItem = new FilterableTreeItem<>(category);
if (category.isExpand()) {
item.setExpanded(true);
treeItem.setExpanded(true);
}

// If there are subcategories, add them recursively.
if (category.getChildren() != null) {
addRecursive(item, category.getChildren());
addRecursive(treeItem, category.getChildren());
}
treeItem.getInternalChildren().add(item);
categoryTreeItemMap.put(category, item);

parentTreeItem.getInternalChildren().add(treeItem);
categoryTreeItemMap.put(category, treeItem);

if (category.visibilityProperty() != null) {
category.visibilityProperty().get().addListener((observableValue, aBoolean, newValue) -> {
item.setVisible(newValue);
FilterableTreeItem<Category> childTreeItem = categoryTreeItemMap.get(category);

navigationView.invalidate();
navigationView.rootItem.changeChildItemVisibility(childTreeItem, newValue);
});
}
}
Expand Down

0 comments on commit d61e804

Please sign in to comment.