package com.smartgwt.sample.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.types.AutoScrollDataApproach;
import com.smartgwt.client.types.ChartType;
import com.smartgwt.client.types.Visibility;
import com.smartgwt.client.util.JSOHelper;
import com.smartgwt.client.widgets.chart.ClusterSizeMapper;
import com.smartgwt.client.widgets.chart.FacetChart;
import com.smartgwt.client.widgets.cube.Facet;
import com.smartgwt.client.widgets.drawing.DrawLabel;
import com.smartgwt.client.widgets.drawing.DrawPane;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.layout.VLayout;

public class BuiltInDS implements EntryPoint {

	public static class FittedChart extends FacetChart {  
		  
        DrawPane measurePane;  
        DrawLabel measureLabel;  
  
        public FittedChart() {  
            setMinLabelGap(5);  
            setAutoScrollData(true);  
            setAutoScrollDataApproach(AutoScrollDataApproach.CLUSTERS);  
            setMinClusterSizeMapper(clusterSizeMapper);  
  
            measurePane = new DrawPane();  
            measurePane.setVisibility(Visibility.HIDDEN);  
            measurePane.setWidth(200);  
            measurePane.setHeight(50);  
              
            measureLabel = makeLabelFromTemplate(getDataLabelProperties());  
            measureLabel.setDrawPane(measurePane);  
        }  
  
        public static DrawLabel makeLabelFromTemplate(DrawLabel templateLabel) {  
            DrawLabel realLabel = new DrawLabel();  
            realLabel.setConfig(JSOHelper.cleanProperties(templateLabel.getConfig(), true));  
            return realLabel;  
        }  
          
        ClusterSizeMapper clusterSizeMapper = new ClusterSizeMapper() {  
            public int getClusterSize(int index, Object facetValueId) {  
                measureLabel.setContents((String)facetValueId);  
                int[] box = measureLabel.getBoundingBox();  
                return box[2] - box[0];  
            }  
        };  
          
        public void setBarSizing(boolean enabled) {  
            setMinClusterSizeMapper(enabled ? clusterSizeMapper : null);  
            setAutoScrollDataApproach(enabled ? AutoScrollDataApproach.CLUSTERS :  
                                                AutoScrollDataApproach.LABELS);  
        }  
    }  
  
    public FittedChart chart;  
    public DynamicForm configForm;  
  
    private DynamicForm getConfigForm() {  
        final CheckboxItem scrollItem = new CheckboxItem("scroll", "Scroll Chart to Fit");  
        scrollItem.setHeight(30);  
        scrollItem.setWidth(140);  
        scrollItem.setShowTitle(false);  
        scrollItem.setDefaultValue(true);  
        scrollItem.addChangedHandler(new ChangedHandler() {  
            public void onChanged(ChangedEvent event) {  
                FormItem sizingItem = configForm.getItem("sizing");  
                Boolean value = (Boolean) event.getValue();  
                if (!value) {  
                    chart.setBarSizing(false);  
                    sizingItem.setValue(false);  
                }  
                sizingItem.setDisabled(!value);  
                chart.setAutoScrollData(value);  
            }  
        });  
  
        final CheckboxItem sizingItem = new CheckboxItem("sizing", "Dynamic Bar Thickness");  
        sizingItem.setShowTitle(false);  
        sizingItem.setDefaultValue(true);  
        sizingItem.addChangedHandler(new ChangedHandler() {  
            public void onChanged(ChangedEvent event) {  
                chart.setBarSizing((Boolean) event.getValue());  
            }  
        });          
  
        configForm = new DynamicForm();  
        configForm.setNumCols(4);  
        configForm.setWidth(500);  
        configForm.setItems(scrollItem, sizingItem);  
  
        return configForm;  
    }  
  
    @Override  
    public void onModuleLoad() {  
        chart = new FittedChart();  
        chart.setID("scrollToFitChart");  
        chart.setValueProperty("population");  
        chart.setValueTitle("Population (Thousands)");  
        chart.setTitle("2000 Census Population by State");  
        //chart.setShowDataLabels(true);
        chart.setShowValueOnHover(true);
        chart.setData(AutoScrollDataChartData.getData());  
  
        chart.setAllowedChartTypes(new ChartType[] {  
            ChartType.COLUMN, ChartType.AREA, ChartType.LINE  
        });  
  
        chart.setFacets(new Facet("state", "State"));  
  
        VLayout layout = new VLayout();  
        layout.setWidth100();  
        layout.setHeight100();  
        layout.setMembers(getConfigForm(), chart);  
  
        layout.draw();  
    }  
}