$package("js.ui.tree");

js.ui.tree.AbstractTreeNode = function (treeview){
	this.treeview = treeview;
}	

js.ui.tree.AbstractTreeNode.prototype = new js.lang.BaseObject();

js.ui.tree.AbstractTreeNode.prototype.parent = null;

js.ui.tree.AbstractTreeNode.prototype.items = null;

js.ui.tree.AbstractTreeNode.prototype.icon = null;

js.ui.tree.AbstractTreeNode.prototype.isExpanded = function(){
	return this.expanded;
}
js.ui.tree.AbstractTreeNode.prototype.isFocused = function(){
	return this.focused;
}
js.ui.tree.AbstractTreeNode.prototype.setUserObject =  function(value) { 
	this.userObject = value;
}

js.ui.tree.AbstractTreeNode.prototype.getUserObject = function(){ 
	return this.userObject;
}

js.ui.tree.AbstractTreeNode.prototype.setRenderer = function(renderer) { 
	if ((this.renderer) && (this.renderer != renderer)) {
		this.renderer.clear();
	}
	this.renderer = renderer; 
	this.renderer.paint(); 
}
js.ui.tree.AbstractTreeNode.prototype.getRenderer = function(){ 
	if (!this.renderer) this.renderer = new this.treeview.renderClass(this);
	return this.renderer;
}

js.ui.tree.AbstractTreeNode.prototype.focus = function (){
	var	treeview = this.treeview;
	treeview.setSelected(this);
}

js.ui.tree.AbstractTreeNode.prototype.isSelected = function (){
	var	treeview = this.treeview;
	if(treeview)
		return treeview.isSelected(this);
}

js.ui.tree.AbstractTreeNode.prototype.doExpand = 
js.ui.tree.AbstractTreeNode.prototype.setExpanded = function(expanded){
	if(this.isExpanded() ==	expanded) return;
	var	treeview = this.treeview;
	this.expanded =	false;
	if(expanded){
		if(!this.isLeaf()) {
			this.expanded =	true;
			this.getRenderer().expand();
			if(this.treeview.onexpanded) this.treeview.onexpanded(this);
		}
	} else {
		if(!this.isLeaf()) {
			this.getRenderer().collapse();
			if(this.treeview.oncollapsed) this.treeview.oncollapsed(this);
		}
	}
}
js.ui.tree.AbstractTreeNode.prototype.expand = function (){ 
	this.doExpand(true); 
}
js.ui.tree.AbstractTreeNode.prototype.expandAll = function(){ 
	this.expand();
	for (var i = 0; i < this.getChildCount(); i++) {
		var childNode = this.getChildAt(i);
		childNode.expandAll();
	}
}
js.ui.tree.AbstractTreeNode.prototype.collapseAll = function(){
	this.collapse();
	for (var i = 0; i < this.getChildCount(); i++) {
		var childNode = this.getChildAt(i);
		childNode.collapseAll();
	}
}
js.ui.tree.AbstractTreeNode.prototype.collapse = function(){ 
	this.doExpand(false); 
}
js.ui.tree.AbstractTreeNode.prototype.isLastChild = function(){
	if (this.isRoot()) return true;
	var lastChild = this.getParent().getLastChild();
	return (lastChild == this);
} 
js.ui.tree.AbstractTreeNode.prototype.insert = function(newChild, childIndex){
  if (childIndex ==	null) childIndex = 0;
	if (!this.allowsChildren) alert("node does not allow children");
	else if (newChild == null) alert("new child is	null");	
	else if (this.isNodeAncestor(newChild)) alert("new child is	an ancestor");
	var	oldParent =	newChild.getParent();
	if (oldParent != null) oldParent.remove(newChild);
	newChild.setParent(this);
	newChild.treeview = this.treeview;
	if (this.items == null) this.items = new js.lang.Array();
	if (childIndex < this.items.getLength() - 1) 
		this.items.SetItemAt(newChild, childIndex);
	else this.items.AddItem(newChild);
}
js.ui.tree.AbstractTreeNode.prototype.removeAt = function(childIndex){
	var	child =	this.getChildAt(childIndex);
	this.items.removeItemAt(childIndex);
	child.setParent(null);
	//child.parentNode.removeChild(child); 
//	child.ParentContainer.parentNode.removeChild(child.ParentContainer);
	if(this.isLeaf()) this.expanded = false;
	this.getRenderer().paint(this);
	if(this.treeview.ondeletion) this.treeview.ondeletion(this);
}
js.ui.tree.AbstractTreeNode.prototype.remove = function(aChild){
	if (aChild == null)	alert("argument	is null");
	if (!this.isNodeChild(aChild)) alert("argument	is not a child");
	this.removeAt(this.getIndex(aChild));
}
js.ui.tree.AbstractTreeNode.prototype.setParent = function(newParent){
	this.parent	= newParent; 
}
js.ui.tree.AbstractTreeNode.prototype.getParent = function(){ 
	return this.parent;
}
js.ui.tree.AbstractTreeNode.prototype.getChildAt = function(index){
	if (this.items == null) alert("node	has	no children");
	return this.items.getItem(index);
}
js.ui.tree.AbstractTreeNode.prototype.getChildCount = function(){
	if (this.items == null) return 0;
	else return this.items.length;
}
js.ui.tree.AbstractTreeNode.prototype.getIndex = function(aChild){
	if (aChild == null)	alert("argument	is null");
	if (!this.isNodeChild(aChild)) return -1;
	return this.items.indexOf(aChild, 0);
}
js.ui.tree.AbstractTreeNode.prototype.getChildren = function(){ 
	return this.items;
}
js.ui.tree.AbstractTreeNode.prototype.setAllowsChildren = function(allows){
	if (allows != this.allowsChildren) {
		this.allowsChildren	= allows;
		if (!this.allowsChildren) {
			this.removeAllChildren();
		}
	}
}
js.ui.tree.AbstractTreeNode.prototype.getAllowsChildren = function(){ 
	return this.allowsChildren; 
}
js.ui.tree.AbstractTreeNode.prototype.setUserObject = function(userObject){ 
	this.userObject	= userObject; 
}
js.ui.tree.AbstractTreeNode.prototype.getUserObject = function(){ 
	return this.userObject; 
}
js.ui.tree.AbstractTreeNode.prototype.removeFromParent	= function(){
	var	parent = this.getParent();
	if (parent != null)	parent.remove(this);
}
js.ui.tree.AbstractTreeNode.prototype.removeAllChildren = function(){
	for	(var i = this.getChildCount() -	1; i >=	0; i--)	{
		this.removeAt(0);
	}
}
js.ui.tree.AbstractTreeNode.prototype.getItems = function(){
	if(!this.items)
		this.items = new js.lang.Array();
	return this.items;
}

js.ui.tree.AbstractTreeNode.prototype.add = function(newChild){
	if (!newChild) return;
	newChild.setParent(this);
	newChild.treeview = this.treeview;
	this.getItems().addItem(newChild);
	newChild.getRenderer().setLevel(this.getLevel()+1);
	this.getRenderer().add(newChild.getRenderer());
	this.getRenderer().paintButton();
	newChild.getRenderer().paint();
/*	this.insert(newChild, this.getChildCount());
	this.getRenderer().paintChild(newChild);
	var parentTreeNode = newChild.getParent();
	var previousNode = newChild.getPreviousSibling();
	if (parentTreeNode) parentTreeNode.getRenderer().paint(parentTreeNode);
	if (previousNode) previousNode.getRenderer().paint(previousNode);*/
	if (this.treeview.onaddition) this.treeview.onaddition(newChild);
}
js.ui.tree.AbstractTreeNode.prototype.isNodeAncestor = function(anotherNode){
	if (anotherNode	== null) return false;
	var	ancestor = this;
	if (ancestor ==	anotherNode) return false;
	do {
		if (ancestor ==	anotherNode) return true;
	} while((ancestor =	ancestor.getParent()) != null);
	return false;
}
js.ui.tree.AbstractTreeNode.prototype.isNodeDescendant	= function(anotherNode){
	if (anotherNode	== null) return	false;
	return anotherNode.isNodeAncestor(this);
}
js.ui.tree.AbstractTreeNode.prototype.getSharedAncestor = function(node){
	var aNode = node;
	if(aNode == null) return null;
	while(aNode) {
		otherNode = this;
		while(otherNode) {
			if(aNode==otherNode)return true;
			otherNode=otherNode.getParent();
		}
		aNode = aNode.getParent();
	}
	return false;
}
js.ui.tree.AbstractTreeNode.prototype.isNodeRelated = function(aNode){ 
	return (aNode != null) && (this.getRoot() == aNode.getRoot()); 
} 
js.ui.tree.AbstractTreeNode.prototype.getLevel = function() {
	var	ancestor;
	var	levels = 0;
	ancestor = this;
	while((ancestor	= ancestor.getParent())	!= null){
		levels++;
	}
	return levels;
}
js.ui.tree.AbstractTreeNode.prototype.getPath = function(){}
js.ui.tree.AbstractTreeNode.prototype.getPathToRoot = function(aNode, depth){}
js.ui.tree.AbstractTreeNode.prototype.getUserObjectPath = function(){}
js.ui.tree.AbstractTreeNode.prototype.getRoot = function(){
	var	ancestor = this;
	var	previous;
	do {
		previous = ancestor;
		ancestor = ancestor.getParent();
	} while	(ancestor != null);
	return previous;
}
js.ui.tree.AbstractTreeNode.prototype.isRoot = function(){ 
	return this.getParent() == null; 
} 
js.ui.tree.AbstractTreeNode.prototype.getNextNode = function(){
	if (this.getChildCount() ==	0) {
		var	nextSibling	= this.getNextSibling();
		if (nextSibling	== null) {
			var	aNode =	this.getParent();
			do {
				if (aNode == null) return null;
				nextSibling	= aNode.getNextSibling();
				if (nextSibling	!= null) return nextSibling;
				aNode =	aNode.getParent();
			} while(true);
		} else return nextSibling;
	} else return this.getChildAt(0);
}
js.ui.tree.AbstractTreeNode.prototype.getPreviousNode = function(){
	var	previousSibling;
	var	myParent = this.getParent();
	if (myParent ==	null) return null;
	previousSibling	= this.getPreviousSibling();
	if (previousSibling	!= null) {
		if (previousSibling.getChildCount()	== 0) return previousSibling;
		else return previousSibling.getLastLeaf();
	} else return myParent;
} 
js.ui.tree.AbstractTreeNode.prototype.isNodeChild = function(aNode){
	var	retval;
	if (aNode == null) retval = false;
	else {
		if (this.getChildCount() ==	0) retval = false;
		else retval = (aNode.getParent() == this);
	}
	return retval;
}
js.ui.tree.AbstractTreeNode.prototype.getFirstChild = function(){
	if (this.getChildCount() ==	0) return null;
	return this.getChildAt(0);
}
js.ui.tree.AbstractTreeNode.prototype.getLastChild = function(){
	if (this.getChildCount() ==	0) return null;
	return this.getChildAt(this.getChildCount()	- 1);
}
js.ui.tree.AbstractTreeNode.prototype.getChildAfter = function(aChild){
	if (aChild == null)	alert("argument	is null");
	var	index =	this.getIndex(aChild);
	if (index == -1) alert("node is not a child");
	if (index <	this.getChildCount() - 1) return this.getChildAt(index + 1);
	else return null;
}
js.ui.tree.AbstractTreeNode.prototype.getChildBefore =	function(aChild){
	if (aChild == null)	alert("argument is null");
	var	index =	this.getIndex(aChild);
	if (index == -1) alert("argument is not a child");
	if (index >	0) return this.getChildAt(index - 1);
	else return null;
}
js.ui.tree.AbstractTreeNode.prototype.isNodeSibling = function(anotherNode){
	var	retval;
	if (anotherNode	== null) retval = false;
	else if (anotherNode == this)	retval = true;
	else {
		var	myParent = this.getParent();
		retval = (myParent != null && myParent == anotherNode.getParent());
		if (retval && !(this.getParent()).isNodeChild(anotherNode))	
			alert("sibling has different parent");
	}
	return retval;
} 
js.ui.tree.AbstractTreeNode.prototype.getSiblingCount = function(){
	var	myParent = this.getParent();
	if (myParent ==	null) return 1;
	else return myParent.getChildCount();
}
js.ui.tree.AbstractTreeNode.prototype.getNextSibling =	function(){
	var	retval;
	var	myParent = this.getParent();
	if (myParent ==	null) retval = null;
	else retval = myParent.getChildAfter(this);	
	if (retval != null && !this.isNodeSibling(retval)) 
		alert("child of	parent is not a	sibling");
	return retval;
}
js.ui.tree.AbstractTreeNode.prototype.getPreviousSibling =	function(){
	var	retval;
	var	myParent = this.getParent();
	if (myParent ==	null) retval = null;
	else retval = myParent.getChildBefore(this);
	if (retval != null && !this.isNodeSibling(retval)) 
		alert("child of	parent is not a	sibling");
	return retval;
}
js.ui.tree.AbstractTreeNode.prototype.isLeaf = function(){ 
	return (this.getChildCount() ==	0); 
}
js.ui.tree.AbstractTreeNode.prototype.getFirstLeaf = function(){
	var	node = this;
	while (!node.isLeaf()) {
		node = node.getFirstChild();
	}
	return node;
} 
js.ui.tree.AbstractTreeNode.prototype.getLastLeaf = function(){
	var	node = this;
	while (!node.isLeaf()) {
		node = node.getLastChild();
	}
	return node;
}
js.ui.tree.AbstractTreeNode.prototype.getNextLeaf = function(){
	var	nextSibling;
	var	myParent = this.getParent();
	if (myParent ==	null) return null;
	nextSibling	= this.getNextSibling();	
	if (nextSibling	!= null) return	nextSibling.getFirstLeaf();
	return myParent.getNextLeaf();
}
js.ui.tree.AbstractTreeNode.prototype.getPreviousLeaf = function(){
	var	previousSibling;
	var	myParent = this.getParent();
	if (myParent ==	null) return null;
	previousSibling	= this.getPreviousSibling();	
	if (previousSibling	!= null) return	previousSibling.getLastLeaf();
	return myParent.getPreviousLeaf();
}
js.ui.tree.AbstractTreeNode.prototype.getLeafCount = function(){
	var count = 0;
	var node = this;
	if(node.isLeaf()) return 1;
	for(var i = 0; i < node.getChildCount(); i++) {
		if(node.getChildAt(i) && node.getChildAt(i).isLeaf()) count = count + 1;
		else if(node.getChildAt(i) && !node.getChildAt(i).isLeaf()) 
			count = count + node.getChildAt(i).getLeafCount();
	}
	return count;
}
js.ui.tree.AbstractTreeNode.prototype.clear = function(){
	this.getItems().clear();
	this.getRenderer().getContainer().innerHTML = "";
}

js.ui.tree.TreeNode = function(treeview){
	this.treeview = treeview;
}
js.ui.tree.TreeNode.prototype = new js.ui.tree.AbstractTreeNode();

