

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.RootPanel;
import com.mvp4g.client.event.Mvp4gLogger;
import com.smartgwt.client.core.Function;
import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.types.*;
import com.smartgwt.client.util.KeyCallback;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
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.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class DataManagerEntryPoint implements EntryPoint {

	Logger logger = Logger.getLogger("EntryPointLogger");

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		DOM.removeChild(RootPanel.getBodyElement(), DOM.getElementById("loading"));
//		SC.showConsole();

		Canvas canvas = new Canvas();

		final ListGrid countryGrid = new ListGrid();
		countryGrid.setWidth(550);
		countryGrid.setHeight(500);
		countryGrid.setDataSource(CountryXmlDS.getInstance());

		ListGridField nameField = new ListGridField("countryName");
		ListGridField governmentField = new ListGridField("government");
		ListGridField continentField = new ListGridField("continent");
		countryGrid.setFields(nameField, governmentField, continentField);

		countryGrid.setAutoFetchData(true);
		countryGrid.setCanEdit(true);
		countryGrid.setModalEditing(true);
		countryGrid.setEditEvent(ListGridEditEvent.CLICK);
		countryGrid.setListEndEditAction(RowEndEditAction.NEXT);
		countryGrid.setAutoSaveEdits(false);

		countryGrid.setShowAllRecords(true);
		countryGrid.setGroupStartOpen(GroupStartOpen.ALL);
		countryGrid.setGroupByField("continent");

		canvas.addChild(countryGrid);


		IButton editButton = new IButton("Edit New");
		editButton.setTop(550);
		editButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				countryGrid.startEditingNew();
			}
		});
		canvas.addChild(editButton);

		IButton saveButton = new IButton("Save");
		saveButton.setTop(550);
		saveButton.setLeft(140);
		saveButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				final int[] allEditRows = countryGrid.getAllEditRows();
				log("ROWS TO SAVE: "+allEditRows.length);
				countryGrid.saveAllEdits(new Function() {
					@Override
					public void execute() {
						log("--DATA SAVED");
					}
				});
			}
		});
		canvas.addChild(saveButton);

		IButton discardButton = new IButton("Discard");
		discardButton.setTop(550);
		discardButton.setLeft(280);
		discardButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				countryGrid.discardAllEdits();
			}
		});
		canvas.addChild(discardButton);

		canvas.draw();
	}

	private void log(String data) {
		logger.log(Level.SEVERE, data);
		SC.logInfo(data);
	}
}
