/*
 * Copyright 2000-2012 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.openapi.diff.impl.settings;

import com.intellij.icons.AllIcons;
import com.intellij.idea.ActionsBundle;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.wm.impl.content.ToolWindowContentUi;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

/**
 * The "gear" action allowing to configure merge tool visual preferences, such as displaying whitespaces, line numbers and soft wraps.
 *
 * @author Kirill Likhodedov
 * @see DiffMergeSettings
 */
public class DiffMergeSettingsAction extends AnAction {

  @NotNull private final Collection<Editor> myEditors;
  @NotNull private final ActionGroup myActionGroup;
  @NotNull private final DiffMergeSettings mySettings;

  public DiffMergeSettingsAction(@NotNull Collection<Editor> editors, @NotNull DiffMergeSettings settings) {
    super(AllIcons.General.Gear);
    myEditors = editors;
    mySettings = settings;
    myActionGroup = new MergeToolActionGroup();
  }

  @Override
  public void actionPerformed(AnActionEvent e) {
    InputEvent inputEvent = e.getInputEvent();
    ActionPopupMenu popupMenu = ActionManager.getInstance().createActionPopupMenu(ToolWindowContentUi.POPUP_PLACE, myActionGroup);
    int x = 0;
    int y = 0;
    if (inputEvent instanceof MouseEvent) {
      x = ((MouseEvent)inputEvent).getX();
      y = ((MouseEvent)inputEvent).getY();
    }
    popupMenu.getComponent().show(inputEvent.getComponent(), x, y);
  }

  private class MergeToolActionGroup extends ActionGroup {
    @NotNull
    @Override
    public AnAction[] getChildren(@Nullable AnActionEvent e) {
      return new AnAction[] {
        new DiffMergeToggleAction("EditorToggleShowWhitespaces", DiffMergeEditorSetting.WHITESPACES, myEditors, mySettings),
        new DiffMergeToggleAction("EditorToggleShowLineNumbers", DiffMergeEditorSetting.LINE_NUMBERS, myEditors, mySettings),
        new DiffMergeToggleAction("EditorToggleShowIndentLines", DiffMergeEditorSetting.INDENT_LINES, myEditors, mySettings),
        new DiffMergeToggleAction("EditorToggleUseSoftWraps", DiffMergeEditorSetting.SOFT_WRAPS, myEditors, mySettings)
      };
    }
  }

  /**
   * Common class for all actions toggling merge tool editor settings.
   */
  private static class DiffMergeToggleAction extends ToggleAction {

    @NotNull private final DiffMergeEditorSetting mySetting;
    @NotNull private final Collection<Editor> myEditors;
    @NotNull private final DiffMergeSettings mySettings;

    private DiffMergeToggleAction(@NotNull String actionId, @NotNull DiffMergeEditorSetting setting, @NotNull Collection<Editor> editors,
                                  @NotNull DiffMergeSettings settings) {
      super(ActionsBundle.actionText(actionId), ActionsBundle.actionDescription(actionId), null);
      mySetting = setting;
      myEditors = editors;
      mySettings = settings;
    }

    @Override
    public boolean isSelected(AnActionEvent e) {
      return getPreference(mySetting);
    }

    @Override
    public void setSelected(AnActionEvent e, boolean state) {
      setPreference(mySetting, state);
      for (Editor editor : myEditors) {
        mySetting.apply(editor, state);
      }
    }

    private void setPreference(DiffMergeEditorSetting preference, boolean state) {
      mySettings.setPreference(preference, state);
    }

    private boolean getPreference(DiffMergeEditorSetting preference) {
      return mySettings.getPreference(preference);
    }
  }

}

@@ -761,17 +761,27 @@
 nSystem.
-*
+ActionGroup
 ;%0Aimport
@@ -806,21 +806,29 @@
 api.
-editor.Editor
+actionSystem.AnAction
 ;%0Aim
@@ -857,43 +857,132 @@
 api.
-wm.impl.content.ToolWindowContentUi
+actionSystem.AnActionEvent;%0Aimport com.intellij.openapi.actionSystem.ToggleAction;%0Aimport com.intellij.openapi.editor.Editor
 ;%0Aim
@@ -1069,76 +1069,8 @@
 e;%0A%0A
-import java.awt.event.InputEvent;%0Aimport java.awt.event.MouseEvent;%0A
 impo
@@ -1241,37 +1241,8 @@
 %0A *%0A
- * @author Kirill Likhodedov%0A
  * @
@@ -1312,24 +1312,27 @@
 extends 
-An
 Action
+Group
  %7B%0A%0A  @N
@@ -1386,60 +1386,8 @@
 rs;%0A
-  @NotNull private final ActionGroup myActionGroup;%0A
   @N
@@ -1561,676 +1561,137 @@
 per(
-AllIcons.General.Gear);%0A    myEditors = editors;%0A    mySettings = settings;%0A    myActionGroup = new MergeToolActionGroup();%0A  %7D%0A%0A  @Override%0A  public void actionPerformed(AnActionEvent e) %7B%0A    InputEvent inputEvent = e.getInputEvent();%0A    ActionPopupMenu popupMenu = ActionManager.getInstance().createActionPopupMenu(ToolWindowContentUi.POPUP_PLACE, myActionGroup);%0A    int x = 0;%0A    int y = 0;%0A    if (inputEvent instanceof MouseEvent) %7B%0A      x = ((MouseEvent)inputEvent).getX();%0A      y = ((MouseEvent)inputEvent).getY();%0A    %7D%0A    popupMenu.getComponent().show(inputEvent.getComponent(), x, y);%0A  %7D%0A%0A  private class MergeToolActionGroup extends ActionGroup %7B%0A  
+%22Settings%22, null, AllIcons.General.SecondaryGroup);%0A    setPopup(true);%0A    myEditors = editors;%0A    mySettings = settings;%0A  %7D%0A%0A
   @N
@@ -1697,18 +1697,16 @@
 NotNull%0A
-  
   @Overr
@@ -1701,34 +1701,32 @@
 ull%0A  @Override%0A
-  
   public AnActio
@@ -1770,26 +1770,24 @@
 nt e) %7B%0A    
- 
- 
 return new A
@@ -1798,18 +1798,16 @@
 ion%5B%5D %7B%0A
-  
       ne
@@ -1919,34 +1919,32 @@
 ettings),%0A      
-  
 new DiffMergeTog
@@ -2043,34 +2043,32 @@
 ettings),%0A      
- 
- 
 new DiffMergeTog
@@ -2171,26 +2171,24 @@
 ngs),%0A      
-  
 new DiffMerg
@@ -2287,18 +2287,16 @@
 ttings)%0A
-  
     %7D;%0A 
@@ -2300,99 +2300,10 @@
 ;%0A  
-  %7D%0A  %7D%0A%0A  /**%0A   * Common class for all actions toggling merge tool editor settings.%0A   */
+%7D%0A
 %0A  p

Simplify the "gear" button implementation