package org.test.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import java.util.ArrayList;
import java.util.List;


public class MainEntryPoint implements EntryPoint {

    @Override
    public void onModuleLoad() {

        ListGrid grid = new ListGrid() {

            @Override
            public Canvas createRecordComponent(final ListGridRecord record, Integer colNum) {
                String fieldName = this.getFieldName(colNum);

                if (fieldName.equals("f1")) {
                    HLayout recordCanvas = new HLayout(8);
                    recordCanvas.setHeight(22);
                    recordCanvas.setAlign(Alignment.CENTER);

                    ImgButton i = new ImgButton();
                    i.setSrc("16x16/lock.png");
                    i.setHeight(16);
                    i.setWidth(16);
                    i.setShowDown(false);
                    i.setShowRollOver(false);
                    recordCanvas.addMember(i);

                    ImgButton i1 = new ImgButton();
                    i1.setHeight(16);
                    i1.setWidth(16);
                    i1.setShowDown(false);
                    i1.setShowRollOver(false);
                    i1.setSrc("16x16/delete.png");
                    recordCanvas.addMember(i1);
                    return recordCanvas;
                }
                return null;
            }
        };

        grid.setWidth100();
        grid.setHeight100();
        grid.setShowRecordComponents(true);
        grid.setShowRecordComponentsByCell(true);
        
		////////////////// THIS MAKES THINGS WORST  ////////////////////////////
		grid.setRecordComponentPoolingMode(RecordComponentPoolingMode.RECYCLE);
		////////////////////////////////////////////////////////////////////////

        ListGridField f1 = new ListGridField("f1", "Field 1");
        ListGridField f2 = new ListGridField("f2", "Field 2");
        ListGridField f3 = new ListGridField("f3", "Field 3");

        grid.setFields(f1, f2, f3);

        List<ListGridRecord> list = new ArrayList<>();
        for (int i = 0; i < 500; i++) {
            ListGridRecord rec = new ListGridRecord();
            rec.setAttribute("f1", i);
            rec.setAttribute("f2", "f2 -> " + i);
            rec.setAttribute("f3", "f3 -> " + i);
            list.add(rec);
        }

        grid.setData(list.toArray(new ListGridRecord[]{}));

        RootPanel.get().add(grid);
    }
}
