/*
 * VCSPopup.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.frame;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.resources.client.ImageResource.ImageOptions;
import com.google.gwt.resources.client.ImageResource.RepeatStyle;
import com.google.gwt.user.client.ui.*;
import org.rstudio.core.client.widget.ModalPopupPanel;
import org.rstudio.core.client.widget.NineUpBorder;
import org.rstudio.studio.client.workbench.views.vcs.common.events.SwitchViewEvent;
import org.rstudio.studio.client.workbench.views.vcs.dialog.HistoryPresenter;
import org.rstudio.studio.client.workbench.views.vcs.dialog.ReviewPresenter;

public class VCSPopup
{
   interface Resources extends NineUpBorder.Resources, ClientBundle
   {
      @Override
      @Source("../../../../../../core/client/widget/NineUpBorder.css")
      Styles styles();

      @Override
      @Source("TopLeft.png")
      ImageResource topLeft();

      @Override
      @Source("Top.png")
      @ImageOptions(repeatStyle = RepeatStyle.Horizontal)
      ImageResource top();

      @Override
      @Source("TopRight.png")
      ImageResource topRight();

      @Override
      @Source("Left.png")
      @ImageOptions(repeatStyle = RepeatStyle.Vertical)
      ImageResource left();

      @Override
      @Source("Right.png")
      @ImageOptions(repeatStyle = RepeatStyle.Vertical)
      ImageResource right();

      @Override
      @Source("BottomLeft.png")
      ImageResource bottomLeft();

      @Override
      @Source("Bottom.png")
      @ImageOptions(repeatStyle = RepeatStyle.Horizontal)
      ImageResource bottom();

      @Override
      @Source("BottomRight.png")
      ImageResource bottomRight();

      @Source("Close.png")
      ImageResource close();
   }

   public interface Styles extends NineUpBorder.Styles
   {
   }

   public static void show(ReviewPresenter rpres,
                           HistoryPresenter hpres,
                           boolean showHistory)
   {
      final LayoutPanel swapContainer = new LayoutPanel();
      
      show(swapContainer, rpres, hpres, showHistory);
      
      ModalPopupPanel popup = new ModalPopupPanel(false, false, true);
      Resources res = GWT.<Resources>create(Resources.class);
      NineUpBorder border = new NineUpBorder(
            res,
            32, 20, 17, 20);
      addCloseButton(popup, border);
      border.setSize("100%", "100%");
      border.setFillColor("white");
      border.setWidget(swapContainer);
      popup.setWidget(border);
      popup.setGlassEnabled(true);

      Style popupStyle = popup.getElement().getStyle();
      popupStyle.setZIndex(1001);
      popupStyle.setPosition(Style.Position.ABSOLUTE);
      popupStyle.setTop(0, Unit.PX);
      popupStyle.setBottom(0, Unit.PX);
      popupStyle.setLeft(0, Unit.PX);
      popupStyle.setRight(0, Unit.PX);

      Style contentStyle =
            ((Element) popup.getElement().getFirstChild()).getStyle();
      contentStyle.setWidth(100, Unit.PCT);
      contentStyle.setHeight(100, Unit.PCT);

      popup.center();
      
   }
   
   
   public static void show(final LayoutPanel swapContainer,
                           final ReviewPresenter rpres,
                           final HistoryPresenter hpres,
                           boolean showHistory)
   {
      final Widget review = rpres.asWidget();
      review.setSize("100%", "100%");

      final Widget history = hpres.asWidget();
      history.setSize("100%", "100%");

      swapContainer.setSize("100%", "100%");
      swapContainer.add(review);
      swapContainer.setWidgetLeftRight(review, 0, Unit.PX, 0, Unit.PX);
      swapContainer.setWidgetTopBottom(review, 0, Unit.PX, 0, Unit.PX);
      swapContainer.add(history);
      swapContainer.setWidgetLeftRight(history, 0, Unit.PX, 0, Unit.PX);
      swapContainer.setWidgetTopBottom(history, 0, Unit.PX, 0, Unit.PX);

      if (showHistory)
      {
         swapContainer.setWidgetVisible(review, false);
         hpres.onShow();
      }
      else
      {
         swapContainer.setWidgetVisible(history, false);
         rpres.onShow();
      }

      rpres.addSwitchViewHandler(new SwitchViewEvent.Handler() {
         @Override
         public void onSwitchView(SwitchViewEvent event)
         {
            hpres.onShow();
            swapContainer.setWidgetVisible(history, true);
            swapContainer.setWidgetVisible(review, false);
         }
      });
      hpres.addSwitchViewHandler(new SwitchViewEvent.Handler() {
         @Override
         public void onSwitchView(SwitchViewEvent event)
         {
            rpres.onShow();
            swapContainer.setWidgetVisible(review, true);
            swapContainer.setWidgetVisible(history, false);
         }
      });
   }

   private static void addCloseButton(final ModalPopupPanel popupPanel,
                                      NineUpBorder border)
   {
      Resources res = GWT.create(Resources.class);
      Image closeIcon = new Image(res.close());
      closeIcon.getElement().getStyle().setCursor(Style.Cursor.POINTER);
      closeIcon.addClickHandler(new ClickHandler()
      {
         @Override
         public void onClick(ClickEvent event)
         {
            popupPanel.close();
         }
      });

      LayoutPanel layoutPanel = border.getLayoutPanel();
      layoutPanel.add(closeIcon);
      layoutPanel.setWidgetTopHeight(closeIcon,
                                     15, Unit.PX,
                                     closeIcon.getHeight(), Unit.PX);
      layoutPanel.setWidgetRightWidth(closeIcon,
                                      27, Unit.PX,
                                      closeIcon.getWidth(), Unit.PX);
   }
}

@@ -519,254 +519,29 @@
 gwt.
-core.client.GWT;%0Aimport com.google.gwt.dom.client.Element;%0Aimport com.google.gwt.dom.client.Style;%0Aimport com.google.gwt.dom.client.Style.Unit;%0Aimport com.google.gwt.event.dom.client.ClickEvent;%0Aimport com.google.gwt.event.dom.client.ClickHandler
+dom.client.Style.Unit
 ;%0Aim
@@ -822,63 +822,8 @@
 .*;%0A
-import org.rstudio.core.client.widget.ModalPopupPanel;%0A
 impo
@@ -2290,1261 +2290,8 @@
  %7D%0A%0A
-   public static void show(ReviewPresenter rpres,%0A                           HistoryPresenter hpres,%0A                           boolean showHistory)%0A   %7B%0A      final LayoutPanel swapContainer = new LayoutPanel();%0A      %0A      show(swapContainer, rpres, hpres, showHistory);%0A      %0A      ModalPopupPanel popup = new ModalPopupPanel(false, false, true);%0A      Resources res = GWT.%3CResources%3Ecreate(Resources.class);%0A      NineUpBorder border = new NineUpBorder(%0A            res,%0A            32, 20, 17, 20);%0A      addCloseButton(popup, border);%0A      border.setSize(%22100%25%22, %22100%25%22);%0A      border.setFillColor(%22white%22);%0A      border.setWidget(swapContainer);%0A      popup.setWidget(border);%0A      popup.setGlassEnabled(true);%0A%0A      Style popupStyle = popup.getElement().getStyle();%0A      popupStyle.setZIndex(1001);%0A      popupStyle.setPosition(Style.Position.ABSOLUTE);%0A      popupStyle.setTop(0, Unit.PX);%0A      popupStyle.setBottom(0, Unit.PX);%0A      popupStyle.setLeft(0, Unit.PX);%0A      popupStyle.setRight(0, Unit.PX);%0A%0A      Style contentStyle =%0A            ((Element) popup.getElement().getFirstChild()).getStyle();%0A      contentStyle.setWidth(100, Unit.PCT);%0A      contentStyle.setHeight(100, Unit.PCT);%0A%0A      popup.center();%0A      %0A   %7D%0A   %0A   %0A
    p
@@ -3937,32 +3937,32 @@
 se);%0A         %7D%0A
+
       %7D);%0A   %7D%0A%0A
@@ -3963,940 +3963,7 @@
    %7D
-%0A%0A   private static void addCloseButton(final ModalPopupPanel popupPanel,%0A                                      NineUpBorder border)%0A   %7B%0A      Resources res = GWT.create(Resources.class);%0A      Image closeIcon = new Image(res.close());%0A      closeIcon.getElement().getStyle().setCursor(Style.Cursor.POINTER);%0A      closeIcon.addClickHandler(new ClickHandler()%0A      %7B%0A         @Override%0A         public void onClick(ClickEvent event)%0A         %7B%0A            popupPanel.close();%0A         %7D%0A      %7D);%0A%0A      LayoutPanel layoutPanel = border.getLayoutPanel();%0A      layoutPanel.add(closeIcon);%0A      layoutPanel.setWidgetTopHeight(closeIcon,%0A                                     15, Unit.PX,%0A                                     closeIcon.getHeight(), Unit.PX);%0A      layoutPanel.setWidgetRightWidth(closeIcon,%0A                                      27, Unit.PX,%0A                                      closeIcon.getWidth(), Unit.PX);%0A   %7D
 %0A%7D%0A

remove modal popup panel variation of VCSPopup