// 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;

import com.intellij.openapi.application.AccessToken;
import com.intellij.openapi.diagnostic.Logger;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;

/**
 * Use to assert that no AWT events are pumped during some activity (e.g. action update, write operations, etc)
 *
 * @author peter
 */
public final class ProhibitAWTEvents implements IdeEventQueue.EventDispatcher {
  private static final Logger LOG = Logger.getInstance(ProhibitAWTEvents.class);

  private final String myActivityName;
  private boolean myReported;

  private ProhibitAWTEvents(@NotNull String activityName) {
    myActivityName = activityName;
  }

  @Override
  public boolean dispatch(@NotNull AWTEvent e) {
    if (!myReported) {
      myReported = true;
      LOG.error("AWT events are prohibited inside " + myActivityName + "; got " + e);
    }
    return true;
  }

  @NotNull
  public static AccessToken start(@NotNull String activityName) {
    if (!SwingUtilities.isEventDispatchThread()) {
      // some crazy highlighting queries getData outside EDT: https://youtrack.jetbrains.com/issue/IDEA-162970
      return AccessToken.EMPTY_ACCESS_TOKEN;
    }
    ProhibitAWTEvents dispatcher = new ProhibitAWTEvents(activityName);
    IdeEventQueue.getInstance().addPostprocessor(dispatcher, null);
    return new AccessToken() {
      @Override
      public void finish() {
        IdeEventQueue.getInstance().removePostprocessor(dispatcher);
      }
    };
  }
}

@@ -261,16 +261,57 @@
 Logger;%0A
+import org.jetbrains.annotations.NonNls;%0A
 import o
@@ -385,16 +385,52 @@
 a.awt.*;
+%0Aimport java.util.function.Supplier;
 %0A%0A/**%0A *
@@ -1711,11 +1711,402 @@
   %7D;%0A  %7D
+%0A%0A  public static %3CT%3E T prohibitEventsInside(@NonNls @NotNull String activityName, @NotNull Supplier%3CT%3E supplier) %7B%0A    ProhibitAWTEvents dispatcher = new ProhibitAWTEvents(activityName);%0A    IdeEventQueue.getInstance().addPostprocessor(dispatcher, null);%0A    try %7B%0A      return supplier.get();%0A    %7D%0A    finally %7B%0A      IdeEventQueue.getInstance().removePostprocessor(dispatcher);%0A    %7D%0A  %7D
 %0A%7D%0A

add ProhibitAWTEvents#prohibitEventsInside for convenience