org.teamapps.ux.component.tree.SimpleTreeModel Maven / Gradle / Ivy
/*-
* ========================LICENSE_START=================================
* TeamApps
* ---
* Copyright (C) 2014 - 2024 TeamApps.org
* ---
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =========================LICENSE_END==================================
*/
package org.teamapps.ux.component.tree;
import org.apache.commons.lang3.StringUtils;
import org.teamapps.ux.component.template.BaseTemplateTreeNode;
import org.teamapps.ux.model.AbstractTreeModel;
import org.teamapps.ux.model.ComboBoxModel;
import org.teamapps.ux.model.TreeModelChangedEventData;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class SimpleTreeModel extends AbstractTreeModel> implements ComboBoxModel> {
private final List> nodes;
private int maxResultNodes = Integer.MAX_VALUE;
public SimpleTreeModel() {
nodes = new ArrayList<>();
}
public SimpleTreeModel(List> nodes) {
this.nodes = new ArrayList<>(nodes);
}
public void setNodes(List> nodes) {
this.nodes.clear();
this.nodes.addAll(nodes);
onAllNodesChanged.fire();
}
public void addNode(BaseTemplateTreeNode node) {
nodes.add(node);
onChanged.fire(new TreeModelChangedEventData<>(Collections.emptyList(), Collections.singletonList(node)));
}
public void addNodes(List> nodes) {
this.nodes.addAll(nodes);
onChanged.fire(new TreeModelChangedEventData<>(Collections.emptyList(), nodes));
}
public void removeChildren(Collection> parents) {
replaceChildren(parents, Collections.emptyList());
}
public void replaceChildren(Collection> parentsToEmpty, List> nodesToAdd) {
List> nodesToBeRemoved = nodes.stream()
.filter(node -> node.isDescendantOf(parentsToEmpty))
.collect(Collectors.toList());
nodes.removeAll(nodesToBeRemoved);
nodes.addAll(nodesToAdd);
onChanged.fire(new TreeModelChangedEventData<>(nodesToBeRemoved, nodesToAdd));
}
public void updateNode(BaseTemplateTreeNode node) {
onChanged.fire(new TreeModelChangedEventData<>(Collections.emptyList(), Collections.singletonList(node)));
}
public void relocateNode(BaseTemplateTreeNode node) {
onChanged.fire(new TreeModelChangedEventData<>(Collections.singletonList(node), Collections.singletonList(node)));
}
public void removeNode(BaseTemplateTreeNode node) {
nodes.remove(node);
onChanged.fire(new TreeModelChangedEventData<>(Collections.singletonList(node), Collections.emptyList()));
}
public void removeAllNodes() {
nodes.clear();
onAllNodesChanged.fire();
}
public int getMaxResultNodes() {
return maxResultNodes;
}
public void setMaxResultNodes(int maxResultNodes) {
this.maxResultNodes = maxResultNodes;
}
@Override
public List> getRecords(String query) {
if (StringUtils.isEmpty(query)) {
return this.nodes.stream()
.filter(new EagerNodesFilter())
.limit(maxResultNodes)
.collect(Collectors.toList());
} else {
List> filteredTree = this.nodes.stream()
.filter(node -> StringUtils.containsIgnoreCase(node.getCaption(), query))
.flatMap(node -> ((List>) (List) node.getPath()).stream())
.distinct()
.collect(Collectors.toList());
List> treeCopy = BaseTemplateTreeNode.copyTree(filteredTree);
treeCopy.forEach(node -> node.setExpanded(true));
return treeCopy;
}
}
@Override
public List> getRecords() {
return this.getRecords(null);
}
@Override
public List> getChildRecords(BaseTemplateTreeNode parentRecord) {
return nodes.stream()
.filter(record -> record.getPath().contains(parentRecord) && record != parentRecord)
.filter(new EagerNodesFilter(parentRecord))
.collect(Collectors.toList());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy