// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide.actions;

import com.intellij.openapi.actionSystem.ActionPlaces;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx;
import com.intellij.openapi.fileEditor.impl.EditorWindow;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;

public abstract class SplitterAction extends DumbAwareAction {
  abstract void actionPerformed(@NotNull EditorWindow window);

  @Override
  public void actionPerformed(@NotNull AnActionEvent event) {
    EditorWindow window = event.getData(EditorWindow.DATA_KEY);
    if (window != null) actionPerformed(window);
  }

  @Override
  public void update(@NotNull AnActionEvent event) {
    Presentation presentation = event.getPresentation();
    presentation.setEnabled(isEnabled(event));
    presentation.setVisible(presentation.isEnabled() || !ActionPlaces.isPopupPlace(event.getPlace()));
  }

  boolean isEnabled(@NotNull AnActionEvent event) {
    EditorWindow window = event.getData(EditorWindow.DATA_KEY);
    return window != null && window.inSplitter();
  }


  public static abstract class Goto extends SplitterAction {
    abstract EditorWindow getDestination(@NotNull EditorWindow window);

    @Override
    void actionPerformed(@NotNull EditorWindow window) {
      window.getManager().setCurrentWindow(getDestination(window));
    }


    public static final class Next extends Goto {
      @Override
      EditorWindow getDestination(@NotNull EditorWindow window) {
        return window.getManager().getNextWindow(window);
      }
    }


    public static final class Previous extends Goto {
      @Override
      EditorWindow getDestination(@NotNull EditorWindow window) {
        return window.getManager().getPrevWindow(window);
      }
    }
  }


  public static final class ChangeOrientation extends SplitterAction {
    @Override
    void actionPerformed(@NotNull EditorWindow window) {
      window.getManager().changeSplitterOrientation();
    }
  }


  public static final class Unsplit extends SplitterAction {
    @Override
    void actionPerformed(@NotNull EditorWindow window) {
      window.getManager().unsplitWindow();
    }
  }


  public static final class UnsplitAll extends SplitterAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent event) {
      FileEditorManagerEx manager = getManager(event);
      if (manager != null) manager.unsplitAllWindow();
    }

    @Override
    void actionPerformed(@NotNull EditorWindow window) {
      window.getManager().unsplitAllWindow();
    }

    @Override
    boolean isEnabled(@NotNull AnActionEvent event) {
      if (ActionPlaces.isPopupPlace(event.getPlace())) return super.isEnabled(event);
      FileEditorManagerEx manager = getManager(event);
      return manager != null && manager.isInSplitter();
    }

    private static FileEditorManagerEx getManager(@NotNull AnActionEvent event) {
      Project project = event.getProject();
      return project == null ? null : FileEditorManagerEx.getInstanceEx(project);
    }
  }
}

@@ -2505,32 +2505,33 @@
 All extends 
-Splitter
+DumbAware
 Action %7B%0A   
@@ -2744,181 +2744,113 @@
     
-void actionPerformed(@NotNull EditorWindow window) %7B%0A      window.getManager().unsplitAllWindow();%0A    %7D%0A%0A    @Override%0A    boolean isEnabled(@NotNull AnActionEvent 
+public void update(@NotNull AnActionEvent event) %7B%0A      FileEditorManagerEx manager = getManager(
 event)
- %7B
+;
 %0A   
@@ -2905,107 +2905,172 @@
 ))) 
-return super.isEnabled(event);%0A      FileEditorManagerEx manager = getManager(event);%0A      return 
+%7B%0A        event.getPresentation().setVisible(manager != null && manager.getWindowSplitCount() %3E 2);%0A      %7D%0A      else %7B%0A        event.getPresentation().setEnabled(
 mana
@@ -3106,17 +3106,26 @@
 litter()
-;
+);%0A      %7D
 %0A    %7D%0A%0A

update after review IDEA-CR-62726