private static @Nullable JComponent getPreferredFocusedComponent(@NotNull JComponent component,
                                                                   @Nullable FocusTraversalPolicy policyToIgnore,
                                                                   @Nullable Field focusTraversalPolicyField) {
    if (!component.isVisible()) {
      return null;
    }

    FocusTraversalPolicy focusTraversalPolicy = null;
    if (component.isFocusTraversalPolicySet()) {
      try {
        focusTraversalPolicyField = Container.class.getDeclaredField("focusTraversalPolicy");
        focusTraversalPolicyField.setAccessible(true);
      }
      catch (ReflectiveOperationException e) {
        focusTraversalPolicyField = null;
      }

      try {
        focusTraversalPolicy =
          focusTraversalPolicyField != null && component.isFocusTraversalPolicySet() ? (FocusTraversalPolicy)focusTraversalPolicyField.get(
            component) : null;
      }
      catch (IllegalAccessException ignored) {
      }
    }

    if (focusTraversalPolicy != null && focusTraversalPolicy != policyToIgnore) {
      if (focusTraversalPolicy.getClass().getName().contains("LegacyGlueFocusTraversalPolicy")) {
        return component;
      }

      Component defaultComponent = focusTraversalPolicy.getDefaultComponent(component);
      if (defaultComponent instanceof JComponent) {
        return (JComponent)defaultComponent;
      }
    }

    if (component instanceof JTabbedPane tabbedPane) {
      Component selectedComponent = tabbedPane.getSelectedComponent();
      if (selectedComponent instanceof JComponent) {
        return getPreferredFocusedComponent((JComponent)selectedComponent);
      }
      return null;
    }

    if (_accept(component)) {
      return component;
    }

    for (Component ca : component.getComponents()) {
      if (!(ca instanceof JComponent)) {
        continue;
      }

      JComponent c = getPreferredFocusedComponent((JComponent)ca, null, focusTraversalPolicyField);
      if (c != null) {
        return c;
      }
    }
    return null;
  }
private static @Nullable JComponent getPreferredFocusedComponent(@NotNull JComponent component,
                                                                   @Nullable FocusTraversalPolicy policyToIgnore,
                                                                   @Nullable Field focusTraversalPolicyField)