Show the state in a popup.

Ability to save the state into the clipboard.
This commit is contained in:
Denis Fokin
2018-08-16 18:23:12 +03:00
parent b66c6aaa9d
commit b4ef5da317
@@ -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<String> 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<Component> componentTrace = new ArrayList<>();
ArrayList<Component> 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);
}
}