From b4ef5da317ff2ced206f570cb168c494f4308e36 Mon Sep 17 00:00:00 2001 From: Denis Fokin Date: Thu, 16 Aug 2018 18:18:36 +0300 Subject: [PATCH] Show the state in a popup. Ability to save the state into the clipboard. --- ...DumpFocusableComponentHierarchyAction.java | 79 +++++++++++++------ 1 file changed, 56 insertions(+), 23 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/internal/focus/DumpFocusableComponentHierarchyAction.java b/platform/platform-impl/src/com/intellij/internal/focus/DumpFocusableComponentHierarchyAction.java index f1c0b0887b86..3a1a361661b1 100644 --- a/platform/platform-impl/src/com/intellij/internal/focus/DumpFocusableComponentHierarchyAction.java +++ b/platform/platform-impl/src/com/intellij/internal/focus/DumpFocusableComponentHierarchyAction.java @@ -4,49 +4,82 @@ 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) { - Util.drawOnActiveFrameGraphics(g -> { - Window activeWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(); - Window focusedWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow(); - Component focusedComponent = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); + JFrame visibleFrame = WindowManager.getInstance().findVisibleFrame(); + Window activeWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(); + Window focusedWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow(); + Component focusedComponent = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); - int y = 0; - int FONT_HEIGHT = 30; + CopyOnWriteArrayList dump = new CopyOnWriteArrayList<>(); - g.setColor(Color.BLACK); - g.fillRect(0,0, activeWindow.getBounds().width, activeWindow.getBounds().height); - g.setColor(Color.WHITE); - g.drawString("Active Window: " + activeWindow.getClass().getName(), 0, FONT_HEIGHT*y++); - g.drawString("Focused Window: " + focusedWindow.getClass().getName(), 0, FONT_HEIGHT*y++); - g.drawString("Focused Component: " + focusedComponent.getClass().getName(), 0, 60); + 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 componentTrace = new ArrayList<>(); + ArrayList componentTrace = new ArrayList<>(); - Component c = focusedComponent; + Component c = focusedComponent; - while (c != null) { - componentTrace.add(c); - c = c.getParent(); + 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("^"); } + } - y++; + dump.add("Children count in focused component: " + (focusedComponent == null ? "null" : ((JComponent)focusedComponent).getComponentCount())); - for (int i = componentTrace.size() - 1 ; i >= 0; i--) { - g.drawString(componentTrace.get(i).getClass().getName(), 0, FONT_HEIGHT*y++); - g.drawString("^", 0, FONT_HEIGHT*y + 15); + 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)); + } } + }; - g.drawString("Children count in focused component: " + ((JComponent)focusedComponent).getComponentCount(), 0, FONT_HEIGHT * y); + 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); } }