package com.sandbox.client;

/*
 * Smart GWT (GWT for SmartClient)
 * Copyright 2008 and beyond, Isomorphic Software, Inc.
 *
 * Smart GWT is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.  Smart GWT is also
 * available under typical commercial license terms - see
 * http://smartclient.com/license
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.CharacterCasing;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.PageKeyHandler;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.VLayout;

public class StonebranchTest implements EntryPoint {

    public void onModuleLoad() {
        KeyIdentifier debugKey = new KeyIdentifier();
        debugKey.setCtrlKey(true);
        debugKey.setAltKey(true);
        debugKey.setKeyName("D");
        Page.registerKey(debugKey, new PageKeyHandler() {
            @Override
            public void execute(String keyName) {
                SC.showConsole();
            }
        });

        final VLayout layout = new VLayout();
        layout.setWidth100();
        layout.setHeight100();

        final DataSource dataSource = DataSource.getDataSource("item");
        final DynamicForm form = new DynamicForm();
        form.setAlign(Alignment.LEFT);
        form.setDataSource(dataSource);

        final TextItem upperCaseItem = new TextItem("upperCaseItem", "Upper case");
        upperCaseItem.setCharacterCasing(CharacterCasing.UPPER);
        upperCaseItem.setValue("test");

        final TextItem textItem = new TextItem("defaultCaseItem", "Default Case");
        textItem.setValue("test");

        final ButtonItem saveButton = new ButtonItem("saveButton", "Save");
        saveButton.addClickHandler(new com.smartgwt.client.widgets.form.fields.events.ClickHandler() {
            @Override
            public void onClick(com.smartgwt.client.widgets.form.fields.events.ClickEvent event) {
                doSaveOperation(form);

            }
        });

        List<FormItem> itemList = new ArrayList<FormItem>();
        itemList.add(upperCaseItem);
        itemList.add(textItem);
        itemList.add(saveButton);
        form.setItems(itemList.toArray(new FormItem[itemList.size()]));

        form.setIsGroup(true);
        form.setGroupTitle("Details");

        layout.addMember(form);
        layout.draw();
    }

    protected void doSaveOperation(DynamicForm form) {
        DSRequest request = new DSRequest();
        form.setSaveOperationType(DSOperationType.UPDATE);
        form.saveData(new DSCallback() {
            @Override
            public void execute(DSResponse response, Object rawData, DSRequest dsRequest) {
                SC.say(response.getDataAsString());
            }
        }, request);
    }
}
