package co.focuss.SamplesSGWT.client;

import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.PageKeyHandler;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.PortalLayout;  
import com.smartgwt.client.widgets.layout.Portlet;
import co.focuss.SamplesSGWT.shared.i18n.SamplesSGWTConstants;
import co.focuss.SamplesSGWT.shared.i18n.SamplesSGWTMessages;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.shared.GWT;  
  
public class SamplesSGWT implements EntryPoint {  
  
	// Object used to get access to internationalization constants and messages
	private static SamplesSGWTConstants constants = GWT.create(SamplesSGWTConstants.class);
	private static SamplesSGWTMessages messages = GWT.create(SamplesSGWTMessages.class);
	
	// Presentation constants
	private static final String CONSOLE_SHORTCUT = "D";
  
    public void onModuleLoad() {  
        PortalLayout portalLayout = new PortalLayout();  
        portalLayout.setWidth100();  
        portalLayout.setHeight100();  
          
        Portlet portlet1 = new Portlet();  
        portlet1.setTitle(constants.main_portletA());  
        DataSource ds = DataSource.get("users");
		ListGrid grid = new ListGrid();
		grid.setDataSource(ds);
		grid.fetchData();
		grid.setCanEdit(true);
		portlet1.addItem(grid);
        
        Portlet portlet2 = new Portlet();  
        portlet2.setTitle(constants.main_portletB());  
        Portlet portlet3 = new Portlet();  
        portlet3.setTitle(constants.main_portletC());  
        Portlet portlet4 = new Portlet();  
        portlet4.setTitle(constants.main_portletD());  
          
        
        portalLayout.addPortlet(portlet1, 0, 0);  
        portalLayout.addPortlet(portlet2, 0, 1, 0);  
        portalLayout.addPortlet(portlet3, 0, 1, 1);  
        portalLayout.addPortlet(portlet4, 1, 0);  
        
        SC.say(messages.main_devConsoleKey(CONSOLE_SHORTCUT));
          
        portalLayout.draw();  
        enableAdminConsole();
    }  
    
	private void enableAdminConsole() {
		/** this code enables the Admin Console which provides a graphical interface 
    	 * to configure database connections, create database tables from DataSource
    	 * descriptors, and import test data. 
    	 */
        KeyIdentifier debugKey = new KeyIdentifier();
        debugKey.setCtrlKey(true);
        debugKey.setKeyName(CONSOLE_SHORTCUT);

        Page.registerKey(debugKey, new PageKeyHandler() {
            public void execute(String keyName) {
                SC.showConsole();
            }
        });
	}
}  