/*
 * ScrollableToolbarPopupMenu.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.core.client.widget;

import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.*;
import org.rstudio.core.client.dom.DomUtils;
import org.rstudio.core.client.theme.res.ThemeStyles;
import org.rstudio.core.client.widget.ToolbarPopupMenu;

public class ScrollableToolbarPopupMenu extends ToolbarPopupMenu
{
   @Override
   protected ToolbarMenuBar createMenuBar()
   {
      final ScrollableToolbarMenuBar menuBar = new ScrollableToolbarMenuBar(true);
      menuBar.addSelectionHandler(new SelectionHandler<MenuItem>()
      {
         public void onSelection(SelectionEvent<MenuItem> event)
         {
            ensureSelectedIsVisible();
         }
      });
      return menuBar;
   }
   
   public HandlerRegistration addSelectionHandler(SelectionHandler<MenuItem> handler)
   {
      return ((ScrollableToolbarMenuBar)menuBar_).addSelectionHandler(handler);
   }


   public void ensureSelectedIsVisible()
   {
      if (menuBar_.getSelectedItem() != null)
      {
         DomUtils.ensureVisibleVert(scrollPanel_.getElement(),
                                    menuBar_.getSelectedItem().getElement(),
                                    0);
      }
   }
   
   public int getSelectedIndex()
   {
      return menuBar_.getSelectedIndex();
   }

   @Override
   protected Widget wrapMenuBar(ToolbarMenuBar menuBar)
   {
      scrollPanel_ = new ScrollPanel(menuBar);
      scrollPanel_.addStyleName(ThemeStyles.INSTANCE.scrollableMenuBar());
      scrollPanel_.getElement().getStyle().setOverflowY(Overflow.AUTO);
      scrollPanel_.getElement().getStyle().setOverflowX(Overflow.HIDDEN);
      scrollPanel_.getElement().getStyle().setProperty("maxHeight", 
                                                       getMaxHeight() + "px");
      return scrollPanel_;
   }
   
   protected int getMaxHeight()
   {
      return 300;
   }

   protected class ScrollableToolbarMenuBar extends ToolbarMenuBar
      implements HasSelectionHandlers<MenuItem>
   {
      public ScrollableToolbarMenuBar(boolean vertical)
      {
         super(vertical);
      }

      public HandlerRegistration addSelectionHandler(
            SelectionHandler<MenuItem> handler)
      {
         return addHandler(handler, SelectionEvent.getType());
      }

      @Override
      public void selectItem(MenuItem item)
      {
         super.selectItem(item);
         SelectionEvent.fire(this, item);
      }
   }

   private ScrollPanel scrollPanel_;
}


@@ -2485,66 +2485,32 @@
    s
-crollPanel_.getElement().getStyle().setProperty(%22m
+etMaxHeight(getM
 axHeight
 %22, %0A
@@ -2509,66 +2509,65 @@
 ight
-%22, 
+());
 %0A      
-                                  
+return scrollPanel_;%0A
    
+%7D%0A
    
+%0A
    
-     
+protected int
  get
@@ -2581,17 +2581,13 @@
 ht()
- + %22px%22);
+%0A   %7B
 %0A   
@@ -2592,36 +2592,27 @@
      return 
-scrollPanel_
+300
 ;%0A   %7D%0A   %0A 
@@ -2615,37 +2615,38 @@
   %0A   protected 
-int g
+void s
 etMaxHeight()%0A  
@@ -2641,39 +2641,134 @@
 xHeight(
-)%0A   %7B%0A      return 300
+int maxHeight)%0A   %7B%0A      scrollPanel_.getElement().getStyle().setProperty(%22maxHeight%22, %0A            maxHeight + %22px%22)
 ;%0A   %7D%0A%0A

allow dynamic max height for scrollable toolbar popup menu