$package("js.ui.tree");

js.ui.tree.AbstractTreeCellRenderer = function (treeNode){
	this.treeNode = treeNode;
}

js.ui.tree.AbstractTreeCellRenderer.prototype = new js.ui.list.AbstractListItemRenderer();

js.ui.tree.AbstractTreeCellRenderer.prototype.getNativeClass = function (){
	return "js.ui.tree.AbstractTreeCellRenderer";
}

js.ui.tree.AbstractTreeCellRenderer.prototype.bindUI = function (){
	this.__superBindUI = js.ui.list.AbstractListItemRenderer.prototype.bindUI;
	this.__superBindUI();
	this.topBody = this.ui.getElementsByTagName("tbody")[0];
	this.bottomBody = this.ui.getElementsByTagName("tbody")[1];
	this.container = this.bottomBody.getElementsByTagName("td")[0];
	this.btn = new js.ui.Img(); 
	this.btn.setUI(this.topBody.getElementsByTagName("img")[0]);
	this.text = this.ui.getElementsByTagName("a")[0];
	this.iconCell = this.topBody.getElementsByTagName("td")[5];
	this.bindEvents();
}

js.ui.tree.AbstractTreeCellRenderer.prototype.bindEvents = function (){
	var __this = this;
	this.ui.onmouseover = function(event){
		__this.state = 1;
		__this.paintSelect();
		js.ui.Component.eventCancelBubble(event);
	}
	this.btn.ui.onclick = function(event){
		if(__this.container.style.display == "none"){
			__this.expand();
		}
		else
			__this.collapse();
		js.ui.Component.eventCancelBubble(event);
	}
	this.ui.ondblclick = function(event){
		__this.btn.ui.onclick(event);
		js.ui.Component.eventCancelBubble(event);
	}
	this.ui.onclick = function(event){
		if(__this.treeNode)
			__this.treeNode.treeview.setSelected(__this.treeNode);
		js.ui.Component.eventCancelBubble(event);
	}
}
js.ui.tree.AbstractTreeCellRenderer.prototype.setLevel = function (level){
	if(!this.ui) this.getUI();
	this.btn.ui.style.marginLeft = 16*level + "px";
}
js.ui.tree.AbstractTreeCellRenderer.prototype.expand = function (){
	this.container.style.display = "";
	this.paintButton();
}

js.ui.tree.AbstractTreeCellRenderer.prototype.collapse = function (){
	this.container.style.display = "none";
	this.paintButton();
}

js.ui.tree.AbstractTreeCellRenderer.prototype.isSelected = function(){
	if(this.treeNode){
		return this.treeNode.isSelected();
	}
}

js.ui.tree.AbstractTreeCellRenderer.prototype.setIcon = function (url){
	if(!url) return;
	if(!this.icon){
		this.icon = new js.ui.Img();
		this.icon.getUI().className = "icon";
		this.iconCell.appendChild(this.icon.getUI());
	}
	this.icon.setSrc(url);
}

js.ui.tree.AbstractTreeCellRenderer.prototype.paintSelect = function(){
	var css = "Panel";
	if(this.isSelected())
		css = "Panel Panel_selected";
	if(this.state==1)
		css = "Panel Panel_over";
	this.topBody.className = css;
}
js.ui.tree.AbstractTreeCellRenderer.prototype.paintText = function(){
	var text = this.treeNode.userObject;
	if(text=='') text = "&nbsp";
	this.text.innerHTML = text;
}

js.ui.tree.AbstractTreeCellRenderer.prototype.paintIcon = function (){
	var icon = this.treeNode.icon;
	this.setIcon(icon);
}
js.ui.tree.AbstractTreeCellRenderer.prototype.paintButton = function(){
	if(!this.treeNode) return;
	if(this.treeNode.getChildCount()>0){
		this.btn.getUI().style.visibility = "visible";
		if(this.container.style.display=="none")
			this.btn.setSrc(imagePath+"tree/collapse.png");
		else
			this.btn.setSrc(imagePath+"tree/expand.png");
	}
	else
		this.btn.getUI().style.visibility = "hidden";

}

js.ui.tree.AbstractTreeCellRenderer.prototype.paint = function(){
	this.paintButton();
	this.paintIcon();
	this.paintText();
	this.paintSelect();
}

