package com.smartgwt.sample.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.Record;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.ChangeEvent;
import com.smartgwt.client.widgets.grid.events.ChangeHandler;
import com.smartgwt.client.widgets.layout.VLayout;

public class RecordSelectionExample implements EntryPoint {

    public void onModuleLoad() {
    	
    	VLayout layout = new VLayout(15);
    	layout.setHeight(1200);
        countryGrid = new ListGrid();
        countryGrid.setWidth(500);
        countryGrid.setHeight(224);
        countryGrid.setCanEdit(true);
        countryGrid.setShowAllRecords(true);
        countryGrid.setCanRemoveRecords(true);
        countryGrid.setDeferRemoval(true);
        ListGridField countryCodeField = new ListGridField("countryCode", "Code");
        ListGridField nameField = new ListGridField("countryName", "Country");
        ListGridField  tickBox1 = new ListGridField("tickBox1");
        tickBox1.setType(ListGridFieldType.BOOLEAN);
        tickBox1.setCanToggle(true);
        tickBox1.addChangeHandler(new ChangeHandler() {
            @Override public void onChange(ChangeEvent event) {
            	checkOnlyOneTicked(event,"tickBox1");
            }
        });

        ListGridField  tickBox2 = new ListGridField("tickBox2");
        tickBox2.setType(ListGridFieldType.BOOLEAN);
        tickBox2.setCanToggle(true);
        tickBox2.addChangeHandler(new ChangeHandler() {
            @Override public void onChange(ChangeEvent event) {
            	checkOnlyOneTicked(event,"tickBox2");
            }
        });

        countryGrid.setFields(new ListGridField[] {countryCodeField, nameField,tickBox1,tickBox2});
        countryGrid.setData(createRecords());

        layout.addMember(countryGrid);
        layout.draw();
    }
    
    private void checkOnlyOneTicked(ChangeEvent event,String source)
    {
        int rowNum = event.getRowNum();
        boolean eventValue = new Boolean(event.getValue().toString());
        Record currentRecord = countryGrid.getEditedRecord(rowNum);

        if (source!=null && source.equals("tickBox1"))
        {
        	countryGrid.setEditValue(rowNum, source, eventValue);
            if (eventValue && currentRecord.getAttribute("tickBox2")!=null
                    && currentRecord.getAttribute("tickBox2").equalsIgnoreCase("true")) {
            	countryGrid.setEditValue(rowNum, "tickBox2", !eventValue);
            }
        }else if (source!=null && source.equals("tickBox2"))
        {
        	countryGrid.setEditValue(rowNum, source, eventValue);
            if (eventValue && currentRecord.getAttribute("tickBox1")!=null
                    && currentRecord.getAttribute("tickBox1").equalsIgnoreCase("true")) {
            	countryGrid.setEditValue(rowNum, "tickBox1", !eventValue);
            }
        }
        if (eventValue) {
            for (int i=0;i<countryGrid.getTotalRows();i++) {
                if (i != rowNum)
                {
                	countryGrid.setEditValue(i, source, !eventValue);
                }
            }
        }

    }
    
    private MyRecord[] createRecords()
    {
    	return new MyRecord[]{
    		new MyRecord("USA", "AMERICA"),
    		new MyRecord("UK", "UNITED KINGDOM"),
    		new MyRecord("GM", "GERMANY"),
    		new MyRecord("SP", "SPAIN"),
    		new MyRecord("FR", "FRANCE")
    	};
    }
    ListGrid countryGrid = null;
    
    class MyRecord extends ListGridRecord{
    	
    	public MyRecord(String code, String name)
    	{
    		setCountryCode(code);
    		setCountryName(name);
    	}

		private void setCountryName(String name)
		{
			 setAttribute("countryName", name);
		}

		private void setCountryCode(String code) 
		{
			setAttribute("countryCode", code);   
		}
    	
	    public String getCountryName() 
	    {   
	        return getAttributeAsString("countryName");   
	    }   
	  
	    public String getCountryCode() 
	    {   
	        return getAttributeAsString("countryCode");   
	    }  
    }

}
 
