// Copyright 2000-2018 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.internal.focus;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.ui.JBColor;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

public class DumpFocusableComponentHierarchyAction extends AnAction implements DumbAware {
  @Override
  public void actionPerformed(@NotNull AnActionEvent e) {

    JFrame visibleFrame = WindowManager.getInstance().findVisibleFrame();
    Window activeWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
    Window focusedWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
    Component focusedComponent = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();

    CopyOnWriteArrayList<String> dump = new CopyOnWriteArrayList<>();

    dump.add("Active Window: " +  activeWindow.getClass().getName());
    dump.add("Focused Window: " + (focusedWindow == null ? "null" : focusedWindow.getClass().getName()));
    dump.add("Focused Component: " + (focusedComponent == null ? "null" : focusedComponent.getClass().getName()));

    ArrayList<Component> componentTrace = new ArrayList<>();

    Component c = focusedComponent;

    while (c != null) {
      componentTrace.add(c);
      c = c.getParent();
    }

    for (int i = componentTrace.size() - 1; i >= 0; i--) {
      dump.add(componentTrace.get(i).getClass().getName());
      if (i != 0) {
        dump.add("^");
      }
    }

    dump.add("Children count in focused component: " + (focusedComponent == null ? "null" : ((JComponent)focusedComponent).getComponentCount()));

    JPanel jPanel = new JPanel(new BorderLayout()) {
      @Override
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        int FONT_HEIGHT = 30;

        g.setColor(JBColor.BLACK);
        g.fillRect(0, 0, activeWindow.getBounds().width, activeWindow.getBounds().height);
        g.setColor(JBColor.WHITE);

        for (int i = dump.size() - 1; i >= 0; i --) {
          g.drawString(dump.get(i), 20, 50 + (i * FONT_HEIGHT));
        }
      }
    };

    jPanel.setPreferredSize(visibleFrame.getSize());

    Popup popup = PopupFactory.getSharedInstance().getPopup(visibleFrame, jPanel, visibleFrame.getX(), visibleFrame.getY());
    popup.show();

    jPanel.add(new JButton(new AbstractAction("Close and copy into the clipboard") {
      @Override
      public void actionPerformed(ActionEvent e) {
        StringBuilder dumpAsString = new StringBuilder();
        for (int i = dump.size() - 1; i >= 0; i --) {
          dumpAsString.append(dump.get(i)).append("\n");
        }
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(dumpAsString.toString()), null);
        popup.hide();
      }
    }), BorderLayout.SOUTH);
  }
}

@@ -404,16 +404,64 @@
 BColor;%0A
+import com.intellij.ui.components.JBScrollPane;%0A
 import o
@@ -2104,24 +2104,51 @@
 Count()));%0A%0A
+    int FONT_HEIGHT = 30;%0A%0A
     JPanel j
@@ -2292,39 +2292,8 @@
 );%0A%0A
-        int FONT_HEIGHT = 30;%0A%0A
     
@@ -2348,29 +2348,16 @@
 t(0, 0, 
-activeWindow.
 getBound
@@ -2367,29 +2367,16 @@
 .width, 
-activeWindow.
 getBound
@@ -2597,16 +2597,178 @@
 redSize(
+new Dimension(visibleFrame.getWidth(), dump.size() * FONT_HEIGHT + 200));%0A%0A    JScrollPane scrollPane = new JBScrollPane(jPanel);%0A    scrollPane.setPreferredSize(
 visibleF
@@ -2859,22 +2859,26 @@
 eFrame, 
-j
+scroll
 Pane
-l
 , visibl
@@ -2941,19 +2941,30 @@
     
-jPanel.add(
+JButton closeButton = 
 new 
@@ -3193,33 +3193,32 @@
 ) - 1; i %3E= 0; i
- 
 --) %7B%0A          
@@ -3432,16 +3432,45 @@
 %7D%0A    %7D)
+;%0A%0A    jPanel.add(closeButton
 , Border

Make it scrollable