package smarttestp.client;

/*
 * Smart GWT (GWT for SmartClient) Copyright 2008 and beyond, Isomorphic Software, Inc. Smart GWT is free software; you
 * can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3 as published
 * by the Free Software Foundation. Smart GWT is also available under typical commercial license terms - see
 * http://smartclient.com/license This software is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.widgets.grid.ListGrid;

public class Smarttest implements EntryPoint {

	@Override
	public void onModuleLoad() {
		final ListGrid countryGrid = new ListGrid();
		countryGrid.setDataSource(CountryDS.getInstance());
//		countryGrid.setDataSource(ServerCountryDS.getInstance());
		countryGrid.setWidth100();
		countryGrid.setHeight100();
		countryGrid.setAutoFetchData(false);

		countryGrid.draw();
		countryGrid.fetchData();
	}

	private static class ServerCountryDS extends RestDataSource {

		private static ServerCountryDS instance = null;

		public static ServerCountryDS getInstance() {
			if (instance == null) {
				instance = new ServerCountryDS("servercountryDS_XML");
			}
			return instance;
		}

		public ServerCountryDS(String id) {
			setID(id);
			setDataFormat(DSDataFormat.XML);
//			setRecordXPath("/country");
			DataSourceField countryCodeField = new DataSourceField("countryCode", FieldType.TEXT, "Code");
			DataSourceField countryNameField = new DataSourceField("countryName", FieldType.TEXT, "Country");
			DataSourceField capitalField = new DataSourceField("capital", FieldType.TEXT, "Capital");
			setFields(countryCodeField, countryNameField, capitalField);

			setDataURL("/smarttest/greet");
		}

	}

	private static class CountryDS extends DataSource {

		// The DataSource would normally be defined external to any classes that use it.  

		private static CountryDS instance = null;

		public static CountryDS getInstance() {
			if (instance == null) {
				instance = new CountryDS("countryDS_XML");
			}
			return instance;
		}

		public CountryDS(String id) {
			setID(id);
			setDataFormat(DSDataFormat.XML);
			setRecordXPath("/List/country");
			DataSourceField countryCodeField = new DataSourceField("countryCode", FieldType.TEXT, "Code");
			DataSourceField countryNameField = new DataSourceField("countryName", FieldType.TEXT, "Country");
			DataSourceField capitalField = new DataSourceField("capital", FieldType.TEXT, "Capital");
			setFields(countryCodeField, countryNameField, capitalField);
			setDataURL("ds/test_data/country.data.xml");
		}

	}

}