/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.portal.client.main.menu.tabs;

import com.google.gwt.core.client.GWT;
import com.portal.client.beans.mgtnet.GWTMgtNetRouter;
import com.portal.client.main.menu.tabs.dalogs.dlgMgtNetRouter;
import com.portal.client.main.menu.tabs.ds.RouterDataSource;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Autofit;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuButton;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.MenuItemSeparator;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;

/**
 *
 * @author paatal
 */
public class RouterTab extends HLayout {

    private static RouterDataSource dataSource = null;
    private VLayout vLayout = null;
    private ListGrid listGrid;

    public RouterTab() {
        setHeight100();
        setWidth100();
        vLayout = new VLayout();
        vLayout.setWidth100();
        vLayout.setHeight100();
        vLayout.setMembersMargin(10);

        addMember(vLayout);
        createMenu();
        createDataGrid();
    }

    private void createDataGrid() throws IllegalStateException {
        dataSource = RouterDataSource.getInstance();
        listGrid = new ListGrid();
        listGrid.setID("mgtNetRouterListGridId");
        listGrid.setWidth100();
        listGrid.setHeight(350);
        listGrid.setAlternateRecordStyles(true);
        listGrid.setShowAllRecords(true);
        listGrid.setCanResizeFields(false);
        listGrid.setCanReorderFields(true);
        listGrid.setCanSort(true);
        listGrid.setDataSource(dataSource);
        listGrid.setAutoFetchData(true);
        listGrid.setShowFilterEditor(true);
        listGrid.setFilterOnKeypress(true);
        listGrid.setAutoFitData(Autofit.HORIZONTAL);
        listGrid.setSelectionType(SelectionStyle.SINGLE);
        listGrid.setDataPageSize(20);

        ListGridField idField = new ListGridField("id", "ID");
        ListGridField nameField = new ListGridField("name", "Name");
        ListGridField ipAddressField = new ListGridField("ipadress", "IP Address");
        ListGridField regDateField = new ListGridField("regdate", "Register Date");
        ListGridField updDateField = new ListGridField("upddate", "Update Date");

        idField.setWidth(100);
        regDateField.setWidth(150);
        updDateField.setWidth(150);

        idField.setAlign(Alignment.LEFT);
        regDateField.setAlign(Alignment.LEFT);
        updDateField.setAlign(Alignment.LEFT);

        idField.setType(ListGridFieldType.INTEGER);
        nameField.setType(ListGridFieldType.TEXT);
        ipAddressField.setType(ListGridFieldType.TEXT);
        regDateField.setType(ListGridFieldType.DATE);
        updDateField.setType(ListGridFieldType.DATE);
        listGrid.setFields(idField, nameField, ipAddressField, regDateField, updDateField);
        listGrid.fetchData();
        listGrid.addRecordDoubleClickHandler(new RecordDoubleClickHandler() {

            public void onRecordDoubleClick(RecordDoubleClickEvent event) {
                ListGridRecord listGridRecords[] = listGrid.getSelection();
                if (listGridRecords == null || listGridRecords.length <= 0) {
                    SC.say("Select Router Record To Edit ");
                    return;
                }
                if (listGridRecords.length > 1) {
                    SC.say("Select Only One Router Record To Edit ");
                    return;
                }
                ListGridRecord listGridRecord = listGridRecords[0];
                GWTMgtNetRouter gWTMgtNetRouter = new GWTMgtNetRouter();
                RouterDataSource.copyValues(listGridRecord, gWTMgtNetRouter);
                dlgMgtNetRouter mgtNetRouter = new dlgMgtNetRouter(dataSource, gWTMgtNetRouter);
                mgtNetRouter.show();
                mgtNetRouter.getForm().focusInItem(1);
            }
        });
        GWT.log(RouterDataSource.getInstance().getTitle(), null);
        vLayout.addMember(listGrid);
    }

    private void createMenu() {
        Menu menu = new Menu();
        menu.setShowShadow(true);
        menu.setShadowDepth(10);

        MenuItem newItem = new MenuItem("New", "/Portal/images/document_plain_new.png", "Ctrl+N");
        MenuItem editItem = new MenuItem("Edit", "/Portal/images/document_edit.png", "Ctrl+O");

        MenuItem exportItem = new MenuItem("Export as...", "/Portal/images/export1.png");
        Menu exportSM = new Menu();
        exportSM.setItems(
                new MenuItem("XML"),
                new MenuItem("CSV"),
                new MenuItem("Plain text"));
        exportItem.setSubmenu(exportSM);

        MenuItem printItem = new MenuItem("Print", "/Portal/images/printer3.png", "Ctrl+P");
        printItem.setEnabled(false);

        MenuItemSeparator separator = new MenuItemSeparator();
        menu.setItems(newItem, editItem, separator, exportItem, separator, printItem);

        vLayout.addMember(new MenuButton("Operations", menu));
        
        newItem.addClickHandler(new ClickHandler() {
            public void onClick(MenuItemClickEvent event) {
                dlgMgtNetRouter mgtNetRouter = new dlgMgtNetRouter(dataSource, null);                
                mgtNetRouter.show();
                mgtNetRouter.getForm().focusInItem(1);
            }
        });
        editItem.addClickHandler(new ClickHandler() {
            public void onClick(MenuItemClickEvent event) {
                ListGridRecord listGridRecords[] = listGrid.getSelection();
                if (listGridRecords == null || listGridRecords.length <= 0) {
                    SC.say("Select Router Record To Edit ");
                    return;
                }
                if (listGridRecords.length > 1) {
                    SC.say("Select Only One Router Record To Edit ");
                    return;
                }
                ListGridRecord listGridRecord = listGridRecords[0];
                GWTMgtNetRouter gWTMgtNetRouter = new GWTMgtNetRouter();
                RouterDataSource.copyValues(listGridRecord, gWTMgtNetRouter);
                dlgMgtNetRouter mgtNetRouter = new dlgMgtNetRouter(dataSource, gWTMgtNetRouter);                
                mgtNetRouter.show();
                mgtNetRouter.getForm().focusInItem(1);
            }
        });
    }
}
