package com.smartgwt.sample.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.util.PageKeyHandler;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;

public class BuiltInDS implements EntryPoint {
	private VLayout mainLayout;
	private IButton recreateBtn;

	private SectionStack ss;
	private SectionStackSection sss1;
	private IButton testBtn;
	private SectionStackSection sss2;

	public void onModuleLoad() {
		KeyIdentifier debugKey = new KeyIdentifier();
		debugKey.setCtrlKey(true);
		debugKey.setKeyName("D");

		Page.registerKey(debugKey, new PageKeyHandler() {
			public void execute(String keyName) {
				SC.showConsole();
			}
		});

		mainLayout = new VLayout(20);
		mainLayout.setWidth100();
		mainLayout.setHeight100();
		
		recreateBtn = new IButton("Recreate");
		recreateBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				recreate();
			}
		});
		mainLayout.addMember(recreateBtn);
		recreate();
		mainLayout.draw();
	}

	private void recreate() {
		if (ss != null) {
			ss.markForDestroy();
			mainLayout.removeMember(ss);
		}

		ss = new SectionStack();
		ss.setHeight100();
		ss.setWidth100();
		sss1 = new SectionStackSection("Section 1");
		sss1.setName("SEC1");
		sss1.setExpanded(true);
		IButton x = new IButton("x");
		sss1.setItems(x);

		testBtn = new IButton("Test");
		sss1.setControls(testBtn);

		sss2 = new SectionStackSection("Section 2");
		sss2.setName("SEC2");
		IButton y = new IButton("y");
		sss2.setItems(y);
		ss.setSections(sss1, sss2);
		mainLayout.addMember(ss, 0);
	}
}