/*
 * DelayLoadTabShim.java
 *
 * Copyright (C) 2009-12 by RStudio, Inc.
 *
 * Unless you have received this program directly from RStudio pursuant
 * to the terms of a commercial license agreement with RStudio, then
 * this program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */
package org.rstudio.studio.client.workbench.ui;

import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import org.rstudio.core.client.AsyncShim;
import org.rstudio.core.client.events.*;

public abstract class DelayLoadTabShim<T extends IsWidget,
      TParentTab extends DelayLoadWorkbenchTab<T>> extends AsyncShim<T>
{

   protected final TParentTab getParentTab()
   {
      return parentTab_;
   }

   @SuppressWarnings("unchecked")
   public void setParentTab(DelayLoadWorkbenchTab<T> parentTab)
   {
      parentTab_ = (TParentTab) parentTab;
   }

   @Override
   protected void onDelayLoadSuccess(T obj)
   {
      super.onDelayLoadSuccess(obj);
      final Widget child = obj.asWidget();
      
      if (child instanceof HasEnsureVisibleHandlers)
      {
         ((HasEnsureVisibleHandlers)child).addEnsureVisibleHandler(
               new EnsureVisibleHandler()
         {
            public void onEnsureVisible(EnsureVisibleEvent event)
            {
               parentTab_.ensureVisible(event.getActivate());
            }
         });
      }
      
      if (child instanceof HasEnsureHeightHandlers)
      {
         ((HasEnsureHeightHandlers)child).addEnsureHeightHandler(
               new EnsureHeightHandler()
         {
            @Override
            public void onEnsureHeight(EnsureHeightEvent event)
            {
               parentTab_.ensureHeight(event.getHeight());     
            }
         });
      }

      if (child instanceof HasEnsureHiddenHandlers)
      {
         ((HasEnsureHiddenHandlers)child).addEnsureHiddenHandler(
               new EnsureHiddenHandler()
         {
            public void onEnsureHidden(EnsureHiddenEvent event)
            {
               parentTab_.ensureHidden();
            }
         });
      }

      child.setSize("100%", "100%");
      parentTab_.getPanel().add(child);
   }

   public abstract void onBeforeUnselected();
   public abstract void onBeforeSelected();
   public abstract void onSelected();

   private TParentTab parentTab_;
}
