package com.hraccess.ta.projects.ou.client.view.widget.grids;


import java.util.Date;
import java.util.LinkedHashMap;

import com.google.gwt.core.client.GWT;
import com.hraccess.ta.projects.gwt.client.service.OrganizationalUnitService;
import com.hraccess.ta.projects.gwt.client.service.OrganizationalUnitServiceAsync;
import com.hraccess.ta.projects.gwt.client.utils.DateFormattersClient;
import com.hraccess.ta.projects.gwt.client.utils.FieldsName;
import com.hraccess.ta.projects.gwt.client.utils.TAMessages;
import com.hraccess.ta.projects.gwt.client.utils.UserPreferencesConstants;
import com.hraccess.ta.projects.ou.client.datasource.AddressDatasource;
import com.hraccess.ta.projects.ou.client.datasource.CodePaysDS;
import com.hraccess.ta.projects.ou.client.datasource.CodePostalDatasource;
import com.hraccess.ta.projects.ou.client.record.TaskRecord;
import com.hraccess.ta.projects.ou.client.view.widget.menus.ListGridMenu;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Autofit;
import com.smartgwt.client.types.DateDisplayFormat;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.types.SortDirection;
import com.smartgwt.client.util.Offline;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.validator.CustomValidator;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.CellSavedEvent;
import com.smartgwt.client.widgets.grid.events.CellSavedHandler;
import com.smartgwt.client.widgets.grid.events.ChangedEvent;
import com.smartgwt.client.widgets.grid.events.ChangedHandler;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;


public class AddressListGrid extends ListGrid{

       public ListGridMenu contextMenu = new ListGridMenu(FieldsName.ADDRESS, new ListGridMenu.ITEM_MENU[] {ListGridMenu.ITEM_MENU.CREATE});
       
       public static OrganizationalUnitServiceAsync uoService = GWT.create(OrganizationalUnitService.class);
       public static ComboBoxItem codePostalCombo = new ComboBoxItem(FieldsName.CODE_POST);
       public ComboBoxItem communeCombo = new ComboBoxItem(FieldsName.Commune);
       public ComboBoxItem inseeCombo = new ComboBoxItem(FieldsName.Insee);
       public ComboBoxItem codePaysCombo = new ComboBoxItem();
       public ComboBoxItem LieuditCombo= new ComboBoxItem(FieldsName.OU_LIEUDIT);
       public ListGridField code_postal = new ListGridField(FieldsName.OU_CODE_POST, TAMessages.getMessage("ta.ou.address.postal.code"));
       public ListGridField commune = new ListGridField(FieldsName.OU_COMMUNE, TAMessages.getMessage("ta.ou.address.commune"));
       public  ListGridField insee = new ListGridField(FieldsName.OU_INSEE, TAMessages.getMessage("ta.ou.address.insee"));
       public ListGridField Lieudit = new ListGridField(FieldsName.OU_LIEUDIT,"Lieu-dit");
       String communeText, inseeText, lieuDitText;
	   String lieudit, communo, inssee;
	   
	   
       
       public AddressListGrid() {
             createListGrid();
             initHandlers();
       }
       
	public void createListGrid() {

		setValidateByCell(true);
		setSaveLocally(true);
		setSaveByCell(true);
		setEmptyMessage(TAMessages.getMessage("ta.noItemToShow"));
		setShowRecordComponents(true);
		setShowRecordComponentsByCell(true);
		setWidth100();
		setShowHeader(true);
		setCanEdit(true);
		setCanReorderRecords(false);
		setShowCellContextMenus(false);
		setShowHeaderContextMenu(true);
		setFixedRecordHeights(true);
		setRecordComponentHeight(20);
		setAlternateRecordStyles(false);
		setCanSort(true);
		setHeaderHeight(30);
		setHeaderSpanHeight(15);
		setShowHover(false);
		setCanHover(false);
		setShowRollOver(false);
		setShowAllRecords(true);
		setSelectionType(SelectionStyle.MULTIPLE);
		SelectItem BisTerSelectItem = new SelectItem();
		setEmptyMessage(TAMessages.getMessage("ta.noItemToShow"));
		TextItem textItemMask = new TextItem();
		setCanRemoveRecords(true);
		DateItem dateMask = new DateItem();
		dateMask.setUseMask(true);
		dateMask.setEndDate(new Date(new Date().getYear() + 10, 1, 1));
		setWidth100();
		setHeight("65%");
		setCanEdit(true);
		setAlign(Alignment.CENTER);
		setCellHeight(20);
		setAutoFitData(Autofit.VERTICAL);
		setAlign(Alignment.CENTER);

		codePostalCombo.setTooltip(TAMessages.getMessage("ta.warn.PostalCode"));
		communeCombo.setTooltip(TAMessages.getMessage("ta.warn.Commmune1"));
		inseeCombo.setTooltip(TAMessages.getMessage("ta.warn.INSEE"));
		LieuditCombo.setTooltip(TAMessages.getMessage("ta.warn.LieuDit"));

		ListGridField startDateField = new ListGridField(
				FieldsName.ADDRESS_START_DATE,
				TAMessages.getMessage("ta.date.start"));
		startDateField.setType(ListGridFieldType.DATE);
		startDateField.setWidth("10%");
		startDateField.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATE);
		startDateField.setRequired(true);
		startDateField.setAlign(Alignment.LEFT);
		startDateField.setSortDirection(SortDirection.DESCENDING);
		startDateField.setCanGroupBy(false);
		startDateField.setCanHide(false);
		startDateField.setEditorProperties(dateMask);
		final CustomValidator dateValidator = new CustomValidator() {
			@Override
			protected boolean condition(Object date) {

				String value = "";
				boolean result = false;
				if (date == null)
					result = true;
				else {
					value = DateFormattersClient.dateFormatterforService
							.format((Date) date);
					if (value.toString().length() == 0) {
						result = true;
					} else {
						String[] tab = value.toString().split("/");
						try {
							String year = tab[2];
							if (year.length() < 4 || year.charAt(0) == '0') {
								result = false;
							} else {
								result = true;
							}
						} catch (Exception e) {
							return false;
						}
					}
				}

				return result;
			}
		};

		startDateField.setValidators(dateValidator);

		final ListGridField pays = new ListGridField(FieldsName.Pays,
				TAMessages.getMessage("ta.ou.pays"));
		pays.setWidth("7%");

		pays.setRequired(true);
		pays.setDefaultValue("FRA");

		this.codePaysCombo.setValidateOnChange(true);
		codePaysCombo.setOptionDataSource(CodePaysDS.getInstance());
		codePaysCombo.setEmptyPickListMessage(TAMessages
				.getMessage("ta.noItemToShow"));

		pays.setEditorProperties(codePaysCombo);

		ListGridField streetNum = new ListGridField(FieldsName.OU_STREET_NUM,
				TAMessages.getMessage("ta.ou.address.street.number"));
		streetNum.setWidth("3%");
		streetNum.setType(ListGridFieldType.INTEGER);
		streetNum.setAlign(Alignment.LEFT);
		textItemMask.setLength(4);
		streetNum.setEditorProperties(textItemMask);

		ListGridField bis_ter = new ListGridField(FieldsName.OU_BIS_TER,
				TAMessages.getMessage("ta.ou.address.bis"));
		bis_ter.setWidth("7%");
		LinkedHashMap<String, String> bisTerValueMap = new LinkedHashMap<String, String>();
		bisTerValueMap.put("", "");
		bisTerValueMap.put("T", "Ter");
		bisTerValueMap.put("B", "Bis");
		BisTerSelectItem.setValueMap(bisTerValueMap);
		bis_ter.setEditorProperties(BisTerSelectItem);
		bis_ter.setValueMap(bisTerValueMap);

		ListGridField streetName = new ListGridField(FieldsName.OU_STREET_NAME,
				TAMessages.getMessage("ta.ou.address.street.name"));
		streetName.setWidth("8%");
		textItemMask.setLength(32);
		streetName.setEditorProperties(textItemMask);

		ListGridField completeAddress = new ListGridField(
				FieldsName.OU_COMPLADDRESS,
				TAMessages.getMessage("ta.ou.address.complement"));
		completeAddress.setWidth("15%");
		textItemMask.setLength(38);
		completeAddress.setEditorProperties(textItemMask);

		this.code_postal.setWidth("8%");
		this.code_postal.setCanFilter(true);
		this.code_postal.setRequired(true);
		this.code_postal
				.setEditorProperties(createComboBoxItemForCodePostal(codePostalCombo));

		commune.setWidth("12%");
		commune.setCanFilter(true);
		commune.setRequired(false);
		commune.setEditorProperties(createComboBoxItemForCommune(communeCombo));

		insee.setWidth("8%");
		insee.setCanFilter(true);
		insee.setRequired(false);
		insee.setEditorProperties(createComboBoxItemForINSEE(inseeCombo));

		Lieudit.setWidth("13%");
		this.LieuditCombo.setValidateOnChange(true);
		LieuditCombo.setEmptyPickListMessage(TAMessages
				.getMessage("ta.noItemToShow"));
		Lieudit.setEditorProperties(createComboBoxItemForLieuDit(LieuditCombo));

		setFields(startDateField, pays, streetNum, bis_ter, streetName,
				completeAddress, code_postal, commune, Lieudit, insee);

		setContextMenu(contextMenu);

		contextMenu.getItem(0).addClickHandler(new ClickHandler() {
			@Override
			public void onClick(MenuItemClickEvent event) {
				TaskRecord record = new TaskRecord();
				record.setAttribute(FieldsName.Pays, "FRA");
				AddressListGrid.this.getRecordList().addAt(record, 0);
				AddressListGrid.this.startEditing(0);
			}
		});
	}

       private ComboBoxItem createComboBoxItemForCodePostal(ComboBoxItem comb) {
           setTitle(TAMessages.getMessage("ta.project"));
             comb.setOptionDataSource(CodePostalDatasource.getInstance());
             comb.setAllowEmptyValue(false);
             comb.setValidateOnChange(true);
             comb.setMinimumSearchLength(2);
             comb.setSearchStringTooShortMessage(TAMessages.getMessage("ta.warn.PostalCode"));
             comb.setDisplayField(FieldsName.OU_CODE_POST);
             comb.setValueField(FieldsName.OU_CODE_POST);
             comb.setSelectOnFocus(true);
             comb.setPickListWidth(100);
             comb.setEmptyPickListMessage(TAMessages.getMessage("ta.noItemToShow"));
             comb.setFilterFields(FieldsName.OU_CODE_POST);
             comb.setUseClientFiltering(true);
             ListGridField codeField = new ListGridField(FieldsName.OU_CODE_POST);
             comb.setSortField(FieldsName.OU_CODE_POST);
             codeField.setCanFilter(true);
             codeField.setWidth(100);
             ListGrid pickListProperties = new ListGrid();
             pickListProperties.setEmptyMessage(TAMessages.getMessage("ta.noItemToShow"));    
             pickListProperties.setFilterButtonPrompt(TAMessages.getMessage("ta.action.filter"));
             comb.setPickListProperties(pickListProperties);
             setAutoFetchData(true);
             comb.setPickListFields(codeField);
             return comb;
       }
       
       private ComboBoxItem createComboBoxItemForCommune(final ComboBoxItem comb) {
             setTitle(TAMessages.getMessage("ta.project"));
             comb.setOptionDataSource(AddressDatasource.getInstance(this));
             comb.setAllowEmptyValue(false);
             comb.setAddUnknownValues(true);
             comb.setValidateOnExit(true);
             comb.setValueField(FieldsName.PK);
             comb.setDisplayField(FieldsName.OU_COMMUNE);
             comb.setPickListWidth(400);
             comb.setEmptyPickListMessage(TAMessages.getMessage("ta.noItemToShow"));
             comb.setFilterFields(FieldsName.PK);
             ListGridField pkField = new ListGridField(FieldsName.PK);
             pkField.setHidden(true);
             ListGridField codeField = new ListGridField(FieldsName.OU_CODE_POST);
             ListGridField communeField = new ListGridField(FieldsName.OU_COMMUNE);
             ListGridField LieuDitField = new ListGridField(FieldsName.OU_LIEUDIT);
             ListGridField inseeField = new ListGridField(FieldsName.OU_INSEE);
             comb.setSortField(FieldsName.PK);
             codeField.setCanFilter(true);
             codeField.setWidth(100);
             setAutoFetchData(false);
             comb.setPickListFields(pkField,codeField,communeField,LieuDitField,inseeField);
             return comb;
       }
       
       private ComboBoxItem createComboBoxItemForINSEE(ComboBoxItem comb) {
             setTitle(TAMessages.getMessage("ta.project"));
             comb.setOptionDataSource(AddressDatasource.getInstance(this));
             comb.setAllowEmptyValue(false);
             comb.setValidateOnExit(true);
             comb.setAddUnknownValues(true);
             comb.setValueField(FieldsName.PK);
             comb.setDisplayField(FieldsName.OU_INSEE);
             comb.setPickListWidth(400);
             comb.setEmptyPickListMessage(TAMessages.getMessage("ta.noItemToShow"));
             comb.setFilterFields(FieldsName.PK);
             ListGridField pkField = new ListGridField(FieldsName.PK);
             pkField.setHidden(true);
             ListGridField codeField = new ListGridField(FieldsName.OU_CODE_POST);
             ListGridField communeField = new ListGridField(FieldsName.OU_COMMUNE);
             ListGridField LieuDitField = new ListGridField(FieldsName.OU_LIEUDIT);
             ListGridField inseeField = new ListGridField(FieldsName.OU_INSEE);
             comb.setSortField(FieldsName.PK);
             codeField.setCanFilter(true);
             codeField.setWidth(100);
             ListGrid pickListProperties = new ListGrid();
             pickListProperties.setEmptyMessage(TAMessages.getMessage("ta.noItemToShow"));
             pickListProperties.setFields(pkField,codeField,communeField,LieuDitField,inseeField);
             setAutoFetchData(false);
             comb.setPickListFields(pkField,codeField,communeField,LieuDitField,inseeField);
             comb.setPickListProperties(pickListProperties);
             return comb;
       }

       private ComboBoxItem createComboBoxItemForLieuDit(ComboBoxItem comb) {
             setTitle(TAMessages.getMessage("ta.project"));
             comb.setOptionDataSource(AddressDatasource.getInstance(this));
             comb.setAllowEmptyValue(false);
             comb.setAddUnknownValues(true);
             comb.setDisplayField(FieldsName.OU_LIEUDIT);
             comb.setValueField(FieldsName.PK);
             comb.setPickListWidth(400);
             comb.setEmptyPickListMessage(TAMessages.getMessage("ta.noItemToShow"));
             comb.setUseClientFiltering(false);
             comb.setValidateOnChange(false);
             ListGridField codeField = new ListGridField(FieldsName.OU_CODE_POST);
             ListGridField communeField = new ListGridField(FieldsName.OU_COMMUNE);
             ListGridField LieuDit = new ListGridField(FieldsName.OU_LIEUDIT);
             ListGridField inseeField = new ListGridField(FieldsName.OU_INSEE);
             comb.setSortField(FieldsName.OU_LIEUDIT);
             codeField.setCanFilter(true);
             codeField.setWidth(100);
             ListGrid pickListProperties = new ListGrid();
             pickListProperties.setEmptyMessage(TAMessages.getMessage("ta.noItemToShow"));    
             pickListProperties.setFilterButtonPrompt(TAMessages.getMessage("ta.action.filter"));
             comb.setPickListProperties(pickListProperties);
             setAutoFetchData(false);
             comb.setValidateOnExit(true);
             comb.setPickListFields(codeField,communeField,LieuDit,inseeField);
             return comb;
       }
       
       private void initHandlers() {
             code_postal.addChangeHandler(new
                           com.smartgwt.client.widgets.grid.events.ChangeHandler() {
                           @Override
                           public void onChange(
                           com.smartgwt.client.widgets.grid.events.ChangeEvent event) {
                                  
                                   int maxLength = 15;
                                  
                            String codePays =(String) getEditedCell(event.getRowNum(), "pays");
                           
                           if ("FRA".equals(codePays) || "MAR".equals(codePays) || "ITA".equals(codePays) || "ESP".equals(codePays)) {
                                  maxLength = 5;
                           }
                           else if ("BEL".equals(codePays)) {
                                  maxLength = 4;
                           }
                           else if ("NLD".equals(codePays) || "CAN".equals(codePays)) {
                                  maxLength = 6;
                           }
                           
                           else if ("PRT".equals(codePays)) {
                                  maxLength = 7;
                           }
                           
                           else if ("GBR".equals(codePays)) {
                                  maxLength = 8;
                           }
                           
                           else if ("DEU".equals(codePays) || "CHE".equals(codePays) || "USA".equals(codePays)) {
                                  maxLength = 9;
                           }
                                  if(event.getValue().toString()!=null)
                                        if(event.getValue().toString().length() > maxLength)
                                               event.cancel();
                           }
                    });
       
             
       this.code_postal.addChangedHandler(new ChangedHandler(){
                    @Override
                    public void onChanged(final ChangedEvent event) {
                           
                           if( event.getValue()!=null )
                           {
                           event.getForm().getField(FieldsName.OU_INSEE).clearValue();
                                  event.getForm().getField(FieldsName.OU_LIEUDIT).clearValue();
                                  event.getForm().getField(FieldsName.OU_COMMUNE).clearValue();

                                  String codePostal = event.getValue().toString();
                                  Date tmp = new Date();

                        final Criteria postalCriteria = new Criteria(FieldsName.OU_CODE_POST, codePostal);
                        postalCriteria.addCriteria("tdp", tmp);
                           commune.setOptionCriteria(postalCriteria);
                           Lieudit.setOptionCriteria(postalCriteria);
                           insee.setOptionCriteria(postalCriteria);

                           AddressDatasource.getInstance().fetchData(postalCriteria, new DSCallback() {
                                  @Override
                                  public void execute(DSResponse response, Object rawData, DSRequest request) {
                                        Record[] records = response.getData();

                                          if (records != null && records.length >1)
                                          {
                                                      event.getForm().getField(FieldsName.OU_COMMUNE).setOptionCriteria(postalCriteria);
                                          }
                                          if (records != null && records.length ==1)
                                          {
                                                Record record = records[0];
                                               event.getForm().getField(FieldsName.OU_COMMUNE).setValue(record.getAttribute(FieldsName.OU_COMMUNE));
                                               communeText=record.getAttribute(FieldsName.OU_COMMUNE);      
                                               lieuDitText=record.getAttribute(FieldsName.OU_LIEUDIT);
                                               inseeText=record.getAttribute(FieldsName.OU_INSEE);
                                               event.getForm().getField(FieldsName.OU_LIEUDIT).setValue(record.getAttribute(FieldsName.OU_LIEUDIT));
                                               event.getForm().getField(FieldsName.OU_INSEE).setValue(record.getAttribute(FieldsName.OU_INSEE));
									}
								}
							});
				}
			}
		});
             this.code_postal.addCellSavedHandler(new CellSavedHandler() {

                    @Override
                    public void onCellSaved(CellSavedEvent event) {
                    		if(event.getNewValue()!=null){

                    	event.getRecord().setAttribute(FieldsName.OU_COMMUNE,communeText);
                        event.getRecord().setAttribute(FieldsName.OU_LIEUDIT,lieuDitText);
                        event.getRecord().setAttribute(FieldsName.OU_INSEE, inseeText);
                        communeText="";
                        lieuDitText="";
                        inseeText="";
                    }}
             });
             
                                  
             this.Lieudit.addChangedHandler(new  com.smartgwt.client.widgets.grid.events.ChangedHandler() {
                    
                    @Override
                    public void onChanged(
                    final com.smartgwt.client.widgets.grid.events.ChangedEvent event) {
                           if(event.getValue()!=null && event.getForm().getField(FieldsName.OU_CODE_POST).getValue()!=null)
                           {
                                  if(event.getForm().getField(FieldsName.OU_LIEUDIT).getSelectedRecord()!=null){
                                        
                                    communo = event.getForm().getField(FieldsName.OU_LIEUDIT).getSelectedRecord().getAttribute(FieldsName.OU_COMMUNE);
                                    inssee = event.getForm().getField(FieldsName.OU_LIEUDIT).getSelectedRecord().getAttribute(FieldsName.OU_INSEE);
                                    lieudit=event.getForm().getField(FieldsName.OU_LIEUDIT).getDisplayValue();
                                    event.getForm().getField(FieldsName.OU_COMMUNE).setValue(communo);
                                    event.getForm().getField(FieldsName.OU_INSEE).setValue(inssee);
							}
						}

					}
				});

             this.Lieudit.addCellSavedHandler(new CellSavedHandler() {
  				
  				@Override
  				public void onCellSaved(CellSavedEvent event) {
  					if(event.getNewValue()!=null){
  						event.getRecord().setAttribute(FieldsName.OU_COMMUNE,communo);
                        event.getRecord().setAttribute(FieldsName.OU_INSEE, inssee);
                        event.getRecord().setAttribute(FieldsName.OU_LIEUDIT,lieudit);
  					}
  				}
  			});

             this.commune.addChangedHandler(new com.smartgwt.client.widgets.grid.events.ChangedHandler() {
                           
                           @Override
                           public void onChanged(
                           final com.smartgwt.client.widgets.grid.events.ChangedEvent event) {
                                  if(event.getValue()!=null && event.getForm().getField(FieldsName.OU_CODE_POST).getValue()!=null)
                                  {

                                        if(event.getForm().getField(FieldsName.OU_COMMUNE).getSelectedRecord()!=null){
                                               
                                          lieudit = event.getForm().getField(FieldsName.OU_COMMUNE).getSelectedRecord().getAttribute(FieldsName.OU_LIEUDIT);
                                          inssee = event.getForm().getField(FieldsName.OU_COMMUNE).getSelectedRecord().getAttribute(FieldsName.OU_INSEE);
                                          communo=event.getForm().getField(FieldsName.OU_COMMUNE).getDisplayValue();
                                          event.getForm().getField(FieldsName.OU_LIEUDIT).setValue(lieudit);
                                          event.getForm().getField(FieldsName.OU_INSEE).setValue(inssee);
							}
						}

					}
				});
             this.commune.addCellSavedHandler(new CellSavedHandler() {
				
				@Override
				public void onCellSaved(CellSavedEvent event) {
					if(event.getNewValue()!=null){
						event.getRecord().setAttribute(FieldsName.OU_COMMUNE,communo);
                        event.getRecord().setAttribute(FieldsName.OU_INSEE, inssee);
                        event.getRecord().setAttribute(FieldsName.OU_LIEUDIT,lieudit);
					}
				}
			});
             
             
             
      
             this.insee.addChangedHandler(new com.smartgwt.client.widgets.grid.events.ChangedHandler() {
                    
                    @Override
                    public void onChanged(
                    final com.smartgwt.client.widgets.grid.events.ChangedEvent event) {
                           if(event.getValue()!=null && event.getForm().getField(FieldsName.OU_CODE_POST).getValue()!=null)
                           {
                           event.getForm().getField(FieldsName.OU_LIEUDIT).clearValue();
                           event.getForm().getField(FieldsName.OU_COMMUNE).clearValue();
                        if(event.getForm().getField(FieldsName.OU_INSEE).getSelectedRecord()!=null){
                            lieudit = event.getForm().getField(FieldsName.OU_INSEE).getSelectedRecord().getAttribute(FieldsName.OU_LIEUDIT);
                            communo = event.getForm().getField(FieldsName.OU_INSEE).getSelectedRecord().getAttribute(FieldsName.OU_COMMUNE);
                            inssee=event.getForm().getField(FieldsName.OU_INSEE).getDisplayValue();
                            event.getForm().getField(FieldsName.OU_COMMUNE).setValue(communo);
                            event.getForm().getField(FieldsName.OU_LIEUDIT).setValue(lieudit);
							}
						}

					}
				});
             this.insee.addCellSavedHandler(new CellSavedHandler() {
 				
 				@Override
 				public void onCellSaved(CellSavedEvent event) {
 					if(event.getNewValue()!=null){
 						event.getRecord().setAttribute(FieldsName.OU_COMMUNE,communo);
                        event.getRecord().setAttribute(FieldsName.OU_INSEE, inssee);
                        event.getRecord().setAttribute(FieldsName.OU_LIEUDIT,lieudit);
 					}
 				}
 			});
       }

       /**
       * Save user's preferences
       */
       public void savePreferences() {
        String viewState = getViewState();
        Offline.put(UserPreferencesConstants.OU_PROJECTSECTION_ADRESSE_VIEWSTATE, viewState);
        SC.say(TAMessages.getMessage("ta.action.preferences.saved"));
       }
}
