/*
 * Isomorphic SmartGWT web presentation layer
 * Copyright 2000 and beyond Isomorphic Software, Inc.
 *
 * OWNERSHIP NOTICE
 * Isomorphic Software owns and reserves all rights not expressly granted in this source code,
 * including all intellectual property rights to the structure, sequence, and format of this code
 * and to all designs, interfaces, algorithms, schema, protocols, and inventions expressed herein.
 *
 *  If you have any questions, please email <sourcecode@isomorphic.com>.
 *
 *  This entire comment must accompany any portion of Isomorphic Software source code that is
 *  copied or moved from this file.
 */
package com.smartgwt.sample.showcase.client.dataintegration.java.sql.relations;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.ListGridEditEvent;
import com.smartgwt.client.types.OperatorId;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.sample.showcase.client.PanelFactory;
import com.smartgwt.sample.showcase.client.ShowcasePanel;
import com.smartgwt.sample.showcase.client.SourceEntity;

public class SQLIncludeFrom extends ShowcasePanel {

    private static final String DESCRIPTION = "You can include fields from related data sources. Just declare a field with \"includeFrom\" " +
            "property pointing to field in related data source. Target field can be included from another " +
            "DataSource. For example City DS includes field \"continentName\" from Country DS which is " +
            "included from Continent DS." +
            "<p/>" +
            "You can specify a different field name or you can omit it. In this case field name will default " +
            "to the name of the included field.";

    public static class Factory implements PanelFactory {

        private String id;

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

        public String getID() {
            return id;
        }

        public String getDescription() {
            return DESCRIPTION;
        }
    }

    protected boolean isTopIntro() {
        return true;
    }

    public Canvas getViewPanel() {
        // grid
    	final ListGrid listGrid = new ListGrid();
        listGrid.setDataSource(DataSource.get("cityIncludeFromSQL"));
        listGrid.setWidth(700);
        listGrid.setHeight(224);
        listGrid.setShowFilterEditor(true);
        listGrid.setAlternateRecordStyles(true);
        listGrid.setAutoFetchData(true);
        listGrid.setDataPageSize(50);
        listGrid.setCanEdit(true);
        listGrid.setEditEvent(ListGridEditEvent.CLICK);
        listGrid.setFields(new ListGridField("cityName", "City"),
                new ListGridField("countryId", "Country") {{
                    setDisplayField("countryName");
                    setEditorType(new SelectItem() {{
                        setOptionDataSource(DataSource.get("countryIncludeFromSQL"));
                        setMultiple(true);
                    }});
                }},
                new ListGridField("continent", "Continent"));

        // form
        final DynamicForm form = new DynamicForm();
        form.setDataSource(DataSource.get("cityIncludeFromSQL"));
        form.setFields(new SelectItem("countryId", "Country") {{
        	setOperator(OperatorId.IN_SET);//optional; if not used -> simple criteria; if used -> advanced criteria
        	setDisplayField("countryName");
        	setOptionDataSource(DataSource.get("countryIncludeFromSQL"));
            setMultiple(true);
        }
        });
        
        //button
        Button filterButton= new Button("Fetch");
        filterButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				listGrid.invalidateCache();
				listGrid.fetchData(form.getValuesAsCriteria());
			}
		});
        
        //layout
        VLayout layout = new VLayout(15);        
        layout.addMember(form);
        layout.addMember(filterButton);
        layout.addMember(listGrid);

        return layout;
    }

    public String getIntro() {
        return DESCRIPTION;
    }

    public SourceEntity[] getSourceUrls() {
    // Do not remove commented lines.
    // Forces GenerateSourceFiles to generate DataURLRecords for both data sources.
    // Generated DataURLRecords are used in 'View source' window for showing data source configuration.
//        DataSource.get("cityIncludeFromSQL");
//        DataSource.get("countryIncludeFromSQL");
//        DataSource.get("continentIncludeFromSQL");
        return new SourceEntity[]{
            };
    }
}
