/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.vcs.log.ui;

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.impl.content.ToolWindowContentUi;
import com.intellij.vcs.log.VcsLogSettings;
import com.intellij.vcs.log.impl.VcsLogManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;

public class VcsLogQuickSettingsActions extends DumbAwareAction {

  @Override
  public void actionPerformed(AnActionEvent e) {
    Project project = e.getProject();
    if (project == null) {
      return;
    }
    VcsLogSettings settings = ServiceManager.getService(project, VcsLogSettings.class);
    VcsLogManager logManager = ServiceManager.getService(project, VcsLogManager.class);
    VcsLogUiImpl logUi = logManager.getLogUi();
    if (logUi == null) {
      return;
    }

    ActionGroup settingsGroup = new MySettingsActionGroup(settings, logUi);
    ActionPopupMenu popupMenu = ActionManager.getInstance().createActionPopupMenu(ToolWindowContentUi.POPUP_PLACE, settingsGroup);
    int x = 0;
    int y = 0;
    InputEvent inputEvent = e.getInputEvent();
    if (inputEvent instanceof MouseEvent) {
      x = ((MouseEvent)inputEvent).getX();
      y = ((MouseEvent)inputEvent).getY();
    }
    popupMenu.getComponent().show(inputEvent.getComponent(), x, y);
  }

  @Override
  public void update(AnActionEvent e) {
    Project project = e.getProject();
    if (project == null) {
      e.getPresentation().setEnabledAndVisible(false);
    }
    else {
      VcsLogManager logManager = ServiceManager.getService(project, VcsLogManager.class);
      e.getPresentation().setEnabledAndVisible(logManager.getLogUi() != null);
    }
  }

  private static class MySettingsActionGroup extends ActionGroup {

    private final VcsLogSettings mySettings;
    private final VcsLogUiImpl myUi;

    public MySettingsActionGroup(VcsLogSettings settings, VcsLogUiImpl ui) {
      mySettings = settings;
      myUi = ui;
    }

    @NotNull
    @Override
    public AnAction[] getChildren(@Nullable AnActionEvent e) {
      return new AnAction[] {
        new ToggleAction("Show Branches Panel") {
          @Override
          public boolean isSelected(AnActionEvent e) {
            return mySettings.isShowBranchesPanel();
          }

          @Override
          public void setSelected(AnActionEvent e, boolean state) {
            mySettings.setShowBranchesPanel(state);
            myUi.setBranchesPanelVisible(state);
          }
        }
      };
    }
  }
}

@@ -723,24 +723,71 @@
 iceManager;%0A
+import com.intellij.openapi.project.DumbAware;%0A
 import com.i
@@ -2931,35 +2931,188 @@
 on%5B%5D
- 
 %7B
+new ShowBranchesPanelAction()%7D;
 %0A    
+%7D%0A%0A
     
-new ToggleAction
+private class ShowBranchesPanelAction extends ToggleAction implements DumbAware %7B%0A      public ShowBranchesPanelAction() %7B%0A        super
 (%22Sh
@@ -3130,23 +3130,27 @@
  Panel%22)
- %7B
+;
 %0A    
+  %7D%0A%0A
       @O
@@ -3149,36 +3149,32 @@
       @Override%0A
-    
       public boo
@@ -3216,20 +3216,16 @@
         
-    
 return m
@@ -3263,27 +3263,19 @@
 ;%0A      
-   
- 
 %7D%0A%0A
-    
       @O
@@ -3282,20 +3282,16 @@
 verride%0A
-    
       pu
@@ -3342,28 +3342,24 @@
 an state) %7B%0A
-    
         mySe
@@ -3402,20 +3402,16 @@
         
-    
 myUi.set
@@ -3449,32 +3449,9 @@
     
-    %7D%0A        %7D%0A      %7D;
+%7D
 %0A   

Make "Show Branches Panel" action dumb aware