package be.repro.client;

import com.google.gwt.core.client.JavaScriptObject;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IconButton;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.Layout;

public class StatefulButtonGroup extends Canvas {

	private Layout container = null;
	
	public StatefulButtonGroup(){
		super();
		this.setHeight(1);
		setStyleName("statefulButtons");
		container = new HLayout();
		container.setHeight(1);
		container.setCanFocus(true);
		this.addChild(container);
	}
	
	public void addButton(int ID, String displayValue){
		addButton(ID, displayValue, null);
	}
	
	public void addButton(int ID, String displayValue, JavaScriptObject objectValue){
		final IconButton btn = new IconButton(displayValue);
		btn.setHeight(25);
		btn.setWidth(110);
		container.addMember(btn);
	}

	public void styleButtons(){
		// Style buttons according position
		for(int i = 0; i < container.getMembers().length; i++){
			Canvas c = container.getMember(i);
			if(c instanceof IconButton){
				if(container.getMembers().length == 1){
					c.setStyleName("singleStatefulButton");
				} else {
					if (i == 0){
						c.setStyleName("leftStatefulButton");
					} else if (i == container.getMembers().length - 1){
						c.setStyleName("rightStatefulButton");
					} else {
						c.setStyleName("middleHorStatefulButton");
					}
				}
			}
		}
	}

	public void clearContent(){
		for (Canvas c:container.getMembers()){
			if(c instanceof IconButton){
				container.removeMember(c);
			}
		}
	}
}