package be.kuleuven.cs.distrinet.chameleon.ui.widget.tree;

import java.util.Set;

import be.kuleuven.cs.distrinet.rejuse.action.Nothing;
import be.kuleuven.cs.distrinet.rejuse.predicate.False;
import be.kuleuven.cs.distrinet.rejuse.predicate.UniversalPredicate;

public abstract class TristateTreePruner<X,Y> {
	
	public TristateTreePruner(TristateTreePruner<X,Y> next) {
		_next = next;
	}
	
	public UniversalPredicate<? super Y, Nothing> create(TreeNode<?, X> treeNode, Set<TreeNode<?,X>> checked, Set<TreeNode<?,X>> grayed) {
		return create(treeNode, checked, grayed, this);
	}

	private TristateTreePruner<X, Y> _next;

	protected UniversalPredicate<? super Y, Nothing> create(
			TreeNode<?, X> treeNode, 
			Set<TreeNode<?,X>> checked, 
			Set<TreeNode<?,X>> grayed,
			TristateTreePruner<X,Y> first) {
		
		UniversalPredicate<? super Y, Nothing> result = null;
		if(checked.contains(treeNode)) {
			result = checked(treeNode,checked,grayed, first);
		} else if(grayed.contains(treeNode)) {
			result = grayed(treeNode,checked,grayed, first);
		}	else {
			result = new False();
		}
		if(result == null) {
			if(_next != null) {
				result = _next.create(treeNode, checked, grayed, first);
			} else {
				result = new False();
			}
		}
		return result;
	}
	
	public UniversalPredicate<? super Y, Nothing> childrenDisjunction(TreeNode<?,X> node, Set<TreeNode<?,X>> checked,
			Set<TreeNode<?,X>> grayed,TristateTreePruner<X,Y>  first) {
		UniversalPredicate<? super Y, Nothing> result = new False();
		for(TreeNode<?,X> child: node.children()) {
			result = result.or((UniversalPredicate)create(child, checked, grayed,first));
		}
		return result;
	}


	protected abstract UniversalPredicate<? super Y, Nothing> grayed(TreeNode<?,X> node, Set<TreeNode<?,X>> checked, Set<TreeNode<?,X>> grayed,TristateTreePruner<X,Y>  first);

	protected abstract UniversalPredicate<? super Y, Nothing> checked(TreeNode<?,X> node, Set<TreeNode<?,X>> checked, Set<TreeNode<?,X>> grayed,TristateTreePruner<X,Y>  first);
}
@@ -255,16 +255,75 @@
 edicate;
+%0Aimport be.kuleuven.cs.distrinet.rejuse.tree.TreePredicate;
 %0A%0Apublic
@@ -450,33 +450,28 @@
 %7D%0A%09%0A%09public 
-Universal
+Tree
 Predicate%3C? 
@@ -684,25 +684,20 @@
 otected 
-Universal
+Tree
 Predicat
@@ -858,25 +858,20 @@
  %7B%0A%09%09%0A%09%09
-Universal
+Tree
 Predicat
@@ -1103,36 +1103,46 @@
 e %7B%0A%09%09%09result = 
-new 
+TreePredicate.
 False();%0A%09%09%7D%0A%09%09i
@@ -1262,36 +1262,46 @@
  %7B%0A%09%09%09%09result = 
-new 
+TreePredicate.
 False();%0A%09%09%09%7D%0A%09%09
@@ -1332,25 +1332,20 @@
 %09public 
-Universal
+Tree
 Predicat
@@ -1499,25 +1499,20 @@
 st) %7B%0A%09%09
-Universal
+Tree
 Predicat
@@ -1546,12 +1546,22 @@
 t = 
-new 
+TreePredicate.
 Fals
@@ -1634,25 +1634,20 @@
 ult.or((
-Universal
+Tree
 Predicat
@@ -1729,33 +1729,28 @@
 ed abstract 
-Universal
+Tree
 Predicate%3C? 
@@ -1902,25 +1902,20 @@
 bstract 
-Universal
+Tree
 Predicat

Use tree predicates