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

import com.smartgwt.client.types.MultiComboBoxLayoutStyle;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.MultiComboBoxItem;
import com.smartgwt.client.widgets.layout.VStack;
import com.smartgwt.sample.showcase.client.PanelFactory;
import com.smartgwt.sample.showcase.client.ShowcasePanel;
import com.smartgwt.sample.showcase.client.data.AnimalXmlDS;

import java.util.HashMap;
import java.util.Map;

public class MultiComboBoxSample extends ShowcasePanel {
    private static final String DESCRIPTION = "A <code>MultiComboBoxItem</code> displays a selection of multiple values as " +
            "buttons along with a combo box that is used to select additional values. " +
            "Clicking on a button removes the value from the selection." +
            "<p>" +
            "Pressing the Enter/Return key within the combo box selects the first matching value " +
            "without leaving the field, allowing several values to be selected using only the " +
            "keyboard. Pressing the Tab key within the combo box selects the first matching value " +
            "and then leaves the field normally, taking focus to the next focusable item.";

    public static class Factory implements PanelFactory {
        private String id;

        public ShowcasePanel create() {
            MultiComboBoxSample panel = new MultiComboBoxSample();
            id = panel.getID();
            return panel;
        }

        public String getID() {
            return id;
        }

        public String getDescription() {
            return DESCRIPTION;
        }
    }

    private static final Map<String, MultiComboBoxLayoutStyle> LAYOUT_STYLES;

    static {
        final MultiComboBoxLayoutStyle[] values = MultiComboBoxLayoutStyle.values();
        LAYOUT_STYLES = new HashMap<String, MultiComboBoxLayoutStyle>(values.length);

        for (MultiComboBoxLayoutStyle value : values) {
            LAYOUT_STYLES.put(value.getValue(), value);
        }
    }

    @Override
    public Canvas getViewPanel() {

        final MultiComboBoxLayoutStyle initialLayoutStyle = MultiComboBoxLayoutStyle.FLOW;
        final boolean initialAddUnknownValues = false;

        final MultiComboBoxItem suppliesItem = new MultiComboBoxItem("status", "Status");
        suppliesItem.setLayoutStyle(initialLayoutStyle);

        final DynamicForm suppliesForm = new DynamicForm();
        //Set the dataSource to form
        suppliesForm.setDataSource(AnimalXmlDS.getInstance());

        suppliesForm.setWidth100();
        suppliesForm.setNumCols(1);
        suppliesForm.setTitleOrientation(TitleOrientation.TOP);
        suppliesForm.setItems(suppliesItem);

        final VStack layout = new VStack(10);
        layout.setWidth(500);
        layout.setMembers(suppliesForm);
        return layout;
    }

    public String getIntro() {
        return DESCRIPTION;
    }
}
