/*
 * SVNPane.java
 *
 * Copyright (C) 2009-11 by RStudio, Inc.
 *
 * 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.views.vcs.svn;

import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.PopupPanel.PositionCallback;
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
import com.google.inject.Inject;
import org.rstudio.core.client.widget.Toolbar;
import org.rstudio.core.client.widget.ToolbarPopupMenu;
import org.rstudio.studio.client.common.vcs.StatusAndPath;
import org.rstudio.studio.client.workbench.commands.Commands;
import org.rstudio.studio.client.workbench.model.Session;
import org.rstudio.studio.client.workbench.ui.WorkbenchPane;
import org.rstudio.studio.client.workbench.views.vcs.common.ChangelistTable;
import org.rstudio.studio.client.workbench.views.vcs.svn.SVNPresenter.Display;

import java.util.ArrayList;

public class SVNPane extends WorkbenchPane implements Display
{
   @Inject
   public SVNPane(SVNChangelistTablePresenter changelistTablePresenter,
                  Session session,
                  Commands commands)
   {
      super(session.getSessionInfo().getVcsName());

      changelistTablePresenter_ = changelistTablePresenter;
      commands_ = commands;
   }

   @Override
   public void bringToFront()
   {
   }

   @Override
   public void setProgress(boolean showProgress)
   {
   }

   @Override
   protected Toolbar createMainToolbar()
   {
      Toolbar toolbar = new Toolbar();

      toolbar.addLeftWidget(commands_.vcsDiff().createToolbarButton());
      toolbar.addLeftSeparator();
      toolbar.addLeftWidget(commands_.vcsAddFiles().createToolbarButton());
      toolbar.addLeftWidget(commands_.vcsRemoveFiles().createToolbarButton());
      toolbar.addLeftSeparator();
      toolbar.addLeftWidget(commands_.vcsRevert().createToolbarButton());
      toolbar.addLeftSeparator();
      toolbar.addLeftWidget(commands_.vcsPull().createToolbarButton());
//      toolbar.addLeftWidget(commands_.vcsCommit().createToolbarButton());

      toolbar.addLeftSeparator();
      toolbar.addRightWidget(commands_.vcsRefresh().createToolbarButton());

      return toolbar;
   }
   
   @Override
   public void setItems(ArrayList<StatusAndPath> items)
   {
      getChangelistTable().setItems(items);
   }

   @Override
   public ArrayList<StatusAndPath> getSelectedItems()
   {
      return changelistTablePresenter_.getSelectedItems();
   }
   
   @Override
   public int getSelectedItemCount()
   {
      return changelistTablePresenter_.getSelectedItems().size();
   }

   @Override
   public HandlerRegistration addSelectionChangeHandler(Handler handler)
   {
      return changelistTablePresenter_.getView().addSelectionChangeHandler(
                                                                     handler);
   }
   
   @Override
   public ChangelistTable getChangelistTable()
   {
      return changelistTablePresenter_.getView();
   }
   
   @Override
   public void showContextMenu(final int clientX, final int clientY)
   {
      final ToolbarPopupMenu menu = new ToolbarPopupMenu();
      
      menu.addItem(commands_.vcsDiff().createMenuItem(false));
      menu.addSeparator();
      menu.addItem(commands_.vcsAddFiles().createMenuItem(false));
      menu.addItem(commands_.vcsRemoveFiles().createMenuItem(false));
      menu.addSeparator();
      menu.addItem(commands_.vcsRevert().createMenuItem(false));
      menu.addSeparator();
      menu.addItem(commands_.vcsOpen().createMenuItem(false));
    
      menu.setPopupPositionAndShow(new PositionCallback() {
         @Override
         public void setPosition(int offsetWidth, int offsetHeight)
         {
            menu.setPopupPosition(clientX, clientY);     
         }
      });
   }

   @Override
   protected Widget createMainWidget()
   {
      return changelistTablePresenter_.getView();
   }

   @Override
   public void onBeforeSelected()
   {
   }

   @Override
   public void onSelected()
   {
   }

   private final SVNChangelistTablePresenter changelistTablePresenter_;
   private final Commands commands_;
}

@@ -846,25 +846,139 @@
 .Toolbar
-PopupMenu
+Button;%0Aimport org.rstudio.core.client.widget.ToolbarPopupMenu;%0Aimport org.rstudio.studio.client.common.icons.StandardIcons
 ;%0Aimport
@@ -2247,32 +2247,106 @@
 olbarButton());%0A
+      toolbar.addLeftWidget(commands_.vcsRevert().createToolbarButton());%0A
       toolbar.ad
@@ -2355,32 +2355,39 @@
 eftSeparator();%0A
+      %0A
       toolbar.ad
@@ -2403,37 +2403,37 @@
 et(commands_.vcs
-Rever
+Commi
 t().createToolba
@@ -2436,32 +2436,39 @@
 olbarButton());%0A
+      %0A
       toolbar.ad
@@ -2491,37 +2491,203 @@
 ;%0A      
-toolbar.addLeftWidget
+%0A      ToolbarPopupMenu moreMenu = new ToolbarPopupMenu();%0A%0A      moreMenu.addItem(commands_.vcsShowHistory().createMenuItem(false));%0A      moreMenu.addSeparator();%0A      moreMenu.addItem
 (command
@@ -2709,98 +2709,265 @@
 eate
-ToolbarButton(
+MenuItem(false
 ));%0A
-//
       
-toolbar.addLeftWidget(commands_.vcsCommit().createToolbarButton(
+moreMenu.addSeparator();%0A      moreMenu.addItem(commands_.showShellDialog().createMenuItem(false));%0A%0A      toolbar.addLeftWidget(new ToolbarButton(%0A          %22More%22,%0A          StandardIcons.INSTANCE.more_actions(),%0A          moreMenu
 ));%0A
@@ -4264,35 +4264,8 @@
 ));%0A
-      menu.addSeparator();%0A
     

add more menu to svn pane