package org.gcube.portlets.user.workspace.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * 
 * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
 *
 */
public class WorkspacePortlet implements EntryPoint {
	/**
	 * This is the entry point method.
	 */
	
	AppController appController;
	
	public void onModuleLoad() {

		AppControllerExplorer appControllerExplorer = new AppControllerExplorer();
		appController = new AppController(appControllerExplorer);
		appController.go(RootPanel.get(ConstantsPortlet.WORKSPACEDIV));
				
		 Window.addResizeHandler(new ResizeHandler() {
             @Override
             public void onResize(ResizeEvent event) {
                     System.out.println("onWindowResized width: "+event.getWidth()+" height: "+event.getHeight());
                     updateSize();
             }
		 });
		 
		 updateSize();

		}
	
	/**
	 * Update window size
	 */
    public void updateSize(){
    	
	    RootPanel workspace = RootPanel.get(ConstantsPortlet.WORKSPACEDIV);
	     
	    int topBorder = workspace.getAbsoluteTop();
	     
	    int leftBorder = workspace.getAbsoluteLeft();
	     
	     
	    int rootHeight = (Window.getClientHeight() - topBorder - 4) ;// - ((footer == null)?0:(footer.getOffsetHeight()-15));
	    
	    if (rootHeight > 550)
	    	rootHeight = 550;
	     
	    int rootWidth = Window.getClientWidth() - 2* leftBorder; //- rightScrollBar;
	    
	    System.out.println("New workspace dimension Height: "+rootHeight+" Width: "+rootWidth);
	    
	    appController.getMainPanel().setHeight(rootHeight);
	    appController.getMainPanel().setWidth(rootWidth);
    }
	
}
