package com.smartgwt.sample.showcase.client.forms;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.FormLayoutType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.CanvasItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.SpacerItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.sample.showcase.client.PanelFactory;
import com.smartgwt.sample.showcase.client.ShowcasePanel;

public class FormFocusFirstSample extends ShowcasePanel {
    private static final String DESCRIPTION = "<p>Setting the focus / cursor on the first field of a form is common requirement. Doing this in Smart GWT is as simple as setting <code>form.setAutoFocus(true)</code>.</p>" +
            "<p>This sample also has <code>setSelectOnFocus(true)</code> on the first field.</p>";

    public static class Factory implements PanelFactory {
        private String id;

        public Canvas create() {
            FormFocusFirstSample panel = new FormFocusFirstSample();
            id = panel.getID();
            return panel;
        }

        public String getID() {
            return id;
        }

        public String getDescription() {
            return DESCRIPTION;
        }
    }

    public Canvas getViewPanel() {

        final TestForm form = new TestForm();
        return form;
    }

    public String getIntro() {
        return DESCRIPTION;
    }
}

class TestForm extends DynamicForm {
    public TestForm() {
        setAutoFocus(true);
        setNumCols(3);
        setWidth(300);
        
        setFields();
        finalizeForm();
    }

    private void setFields() {
        TextItem firstName = new TextItem("fName");
        firstName.setTitle("First Name");
        firstName.setSelectOnFocus(true);
        firstName.setWrapTitle(false);
        firstName.setDefaultValue("[First Name]");

        TextItem lastName = new TextItem("lName");
        lastName.setTitle("Last Name");
        lastName.setDefaultValue("[Last Name]");
        lastName.setWrapTitle(false);

        ButtonItem button = new ButtonItem("hello", "Hello");
        button.setStartRow(false);
        button.setWidth(80);
        button.setIcon("icons/16/message.png");
        button.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                SC.say("Hello " + getValueAsString("fName") + " " + getValueAsString("lName"));
            }
        });
        
        TestItem testItem1 = new TestItem();
        TestItem testItem2 = new TestItem();

        setFields(firstName, lastName, button, testItem1, testItem2);
    }
    
    private void finalizeForm() {
        int index = 0;
        int maxSubItems = 2;
        for (FormItem field : getFields()) {
            System.out.println("Setting index "+index+" to field:" + field.getName());
            field.setGlobalTabIndex(index);
            field.setTabIndex(index);
            index = index + maxSubItems;
        }
    }
}

class TestItem extends CanvasItem {
    private TextItem subItem1;
    private TextItem subItem2;
    
    public TestItem() {
        
        subItem1 = new TextItem();
        subItem2 = new TextItem();
        
        subItem1.setShowTitle(false);
        subItem2.setShowTitle(false);
        
        SpacerItem spacer = new SpacerItem();
        spacer.setWidth(10);
        
        DynamicForm innerForm = new DynamicForm();
        innerForm.setAlign(Alignment.LEFT);
        innerForm.setCellPadding(0);
        innerForm.setItemLayout(FormLayoutType.TABLE);
        innerForm.setNumCols(3);
        innerForm.setColWidths("50", "10", "*");
        innerForm.setPadding(0);
        innerForm.setFields(subItem1, spacer, subItem2);

        setCanvas(innerForm);
    }
    
    @Override
    public void setTabIndex(int tabIndex) {
        super.setTabIndex(tabIndex);
        subItem2.setTabIndex(tabIndex);
        subItem1.setTabIndex(tabIndex + 1);
    }

    @Override
    public void setGlobalTabIndex(int tabIndex) {
        super.setGlobalTabIndex(tabIndex);
        subItem2.setGlobalTabIndex(tabIndex);
        subItem1.setGlobalTabIndex(tabIndex + 1);
    }
}