FlatInspector: support inspecting whole window including menubar and custom window decoration

This commit is contained in:
Karl Tauber
2020-05-30 15:19:07 +02:00
parent b203ad63ee
commit fc68dfd7bc
2 changed files with 36 additions and 5 deletions

View File

@@ -157,9 +157,8 @@ public class FlatInspector
} }
private void inspect( int x, int y ) { private void inspect( int x, int y ) {
Container contentPane = rootPane.getContentPane(); Point pt = SwingUtilities.convertPoint( rootPane.getGlassPane(), x, y, rootPane );
Point pt = SwingUtilities.convertPoint( rootPane.getGlassPane(), x, y, contentPane ); Component c = getDeepestComponentAt( rootPane, pt.x, pt.y );
Component c = SwingUtilities.getDeepestComponentAt( contentPane, pt.x, pt.y );
for( int i = 0; i < inspectParentLevel && c != null; i++ ) { for( int i = 0; i < inspectParentLevel && c != null; i++ ) {
c = c.getParent(); c = c.getParent();
} }
@@ -173,6 +172,38 @@ public class FlatInspector
showToolTip( c, x, y ); showToolTip( c, x, y );
} }
private Component getDeepestComponentAt( Component parent, int x, int y ) {
if( !parent.contains( x, y ) )
return null;
if( parent instanceof Container ) {
for( Component child : ((Container)parent).getComponents() ) {
if( child == null || !child.isVisible() )
continue;
int cx = x - child.getX();
int cy = y - child.getY();
Component c = (child instanceof Container)
? getDeepestComponentAt( child, cx, cy )
: child.getComponentAt( cx, cy );
if( c == null || !c.isVisible() )
continue;
// ignore highlight figure and tooltip
if( c == highlightFigure || c == tip )
continue;
// ignore glass pane
if( c.getParent() instanceof JRootPane && c == ((JRootPane)c.getParent()).getGlassPane() )
continue;
return c;
}
}
return parent;
}
private void highlight( Component c ) { private void highlight( Component c ) {
if( highlightFigure == null ) { if( highlightFigure == null ) {
highlightFigure = createHighlightFigure(); highlightFigure = createHighlightFigure();
@@ -308,7 +339,7 @@ public class FlatInspector
text += "ContentAreaFilled: " + ((AbstractButton)c).isContentAreaFilled() + '\n'; text += "ContentAreaFilled: " + ((AbstractButton)c).isContentAreaFilled() + '\n';
text += "Focusable: " + c.isFocusable() + '\n'; text += "Focusable: " + c.isFocusable() + '\n';
text += "Left-to-right: " + c.getComponentOrientation().isLeftToRight() + '\n'; text += "Left-to-right: " + c.getComponentOrientation().isLeftToRight() + '\n';
text += "Parent: " + c.getParent().getClass().getName(); text += "Parent: " + (c.getParent() != null ? c.getParent().getClass().getName() : "null");
if( inspectParentLevel > 0 ) if( inspectParentLevel > 0 )
text += "\n\nParent level: " + inspectParentLevel; text += "\n\nParent level: " + inspectParentLevel;

View File

@@ -456,7 +456,7 @@ public class FlatTestFrame
private void inspectChanged() { private void inspectChanged() {
if( inspector == null ) if( inspector == null )
inspector = new FlatInspector( contentPanel ); inspector = new FlatInspector( getRootPane() );
inspector.setEnabled( inspectCheckBox.isSelected() ); inspector.setEnabled( inspectCheckBox.isSelected() );
} }