// 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.find.editorHeaderActions;

import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.CustomShortcutSet;
import com.intellij.openapi.actionSystem.Shortcut;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.PopupChooserBuilder;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.wm.IdeFocusManager;
import com.intellij.ui.popup.PopupState;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.JBUI;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.util.List;

public final class Utils {
  private Utils() {
  }

  /**
   * @deprecated use overloaded method instead
   */
  @Deprecated
  @ApiStatus.ScheduledForRemoval(inVersion = "2021.1")
  public static void showCompletionPopup(JComponent toolbarComponent,
                                         final JList list,
                                         @NlsContexts.PopupTitle String title,
                                         final JTextComponent textField,
                                         @NlsContexts.PopupAdvertisement String ad) {
    showCompletionPopup(toolbarComponent, list, title, textField, ad, null);
  }

  public static void showCompletionPopup(JComponent toolbarComponent,
                                         final JList list,
                                         @NlsContexts.PopupTitle String title,
                                         final JTextComponent textField,
                                         @NlsContexts.PopupAdvertisement String ad,
                                         @Nullable PopupState<JBPopup> popupState) {

    final Runnable callback = () -> {
      String selectedValue = (String)list.getSelectedValue();
      if (selectedValue != null) {
        textField.setText(selectedValue);
        IdeFocusManager.getGlobalInstance().requestFocus(textField, false);
      }
    };

    final PopupChooserBuilder builder = JBPopupFactory.getInstance().createListPopupBuilder(list);
    if (title != null) {
      builder.setTitle(title);
    }
    final JBPopup popup = builder.setMovable(false).setResizable(false)
      .setRequestFocus(true).setItemChoosenCallback(callback).createPopup();

    if (ad != null) {
      popup.setAdText(ad, SwingConstants.LEFT);
    }

    if (popupState != null) popupState.prepareToShow(popup);
    if (toolbarComponent != null) {
      popup.showUnderneathOf(toolbarComponent);
    }
    else {
      popup.showUnderneathOf(textField);
    }
  }

  public static void setSmallerFont(final JComponent component) {
    if (SystemInfo.isMac) {
      component.setFont(JBUI.Fonts.smallFont());
    }
  }

  public static void setSmallerFontForChildren(JComponent component) {
    for (Component c : component.getComponents()) {
      if (c instanceof JComponent) {
        setSmallerFont((JComponent)c);
      }
    }
  }

  @NotNull
  public static CustomShortcutSet shortcutSetOf(@NotNull List<Shortcut> shortcuts) {
    return new CustomShortcutSet(shortcuts.toArray(Shortcut.EMPTY_ARRAY));
  }

  @NotNull
  public static List<Shortcut> shortcutsOf(@NotNull String actionId) {
    AnAction action = ActionManager.getInstance().getAction(actionId);
    return action == null ? ContainerUtil.emptyList() : ContainerUtil.immutableList(action.getShortcutSet().getShortcuts());
  }
}

@@ -499,16 +499,70 @@
 actory;%0A
+import com.intellij.openapi.ui.popup.JBPopupListener;%0A
 import c
@@ -1728,20 +1728,679 @@
 ld, ad, 
-null
+(PopupState%3CJBPopup%3E)null);%0A  %7D%0A%0A  /**%0A   * @deprecated use overloaded method instead%0A   */%0A  @Deprecated%0A  @ApiStatus.ScheduledForRemoval(inVersion = %222021.1%22)%0A  public static void showCompletionPopup(JComponent toolbarComponent,%0A                                         final JList list,%0A                                         @NlsContexts.PopupTitle String title,%0A                                         final JTextComponent textField,%0A                                         @NlsContexts.PopupAdvertisement String ad,%0A                                         JBPopupListener listener) %7B%0A    showCompletionPopup(toolbarComponent, list, title, textField, ad
 );%0A  %7D%0A%0A

Fix test of backward compatibility