private void addSubFacet(FacetType<?, ?> type, FacetTypeId<?> underlyingType) {
    final ProjectFacetsConfigurator facetsConfigurator = myContext.getModulesConfigurator().getFacetsConfigurator();
    List<Facet> suitableParents = new ArrayList<>();
    for (Module module : myContext.getModules()) {
      if (type.isSuitableModuleType(ModuleType.get(module))) {
        suitableParents.addAll(facetsConfigurator.getFacetsByType(module, underlyingType));
      }
    }

    final Iterator<Facet> iterator = suitableParents.iterator();
    while (iterator.hasNext()) {
      Facet<?> parent = iterator.next();
      if (type.isOnlyOneFacetAllowed() && facetsConfigurator.hasFacetOfType(parent.getModule(), parent, type.getId())) {
        iterator.remove();
      }
    }

    final Project project = myContext.getProject();
    if (suitableParents.isEmpty()) {
      final String parentType = FacetTypeRegistry.getInstance().findFacetType(underlyingType).getPresentableName();
      Messages.showErrorDialog(project, JavaUiBundle.message("dialog.message.no.suitable.parent.0.facets.found", parentType),
                               JavaUiBundle.message("dialog.title.cannot.create.0.facet", type.getPresentableName()));
      return;
    }

    ChooseParentFacetDialog dialog = new ChooseParentFacetDialog(project, suitableParents);
    dialog.show();
    final List<Facet> chosen = dialog.getChosenElements();
    if (!dialog.isOK() || chosen.size() != 1) return;
    final Facet<?> parent = chosen.get(0);
    ProjectStructureConfigurable structureConfigurable = myContext.getModulesConfigurator().getProjectStructureConfigurable();
    final Facet<?> facet =
      structureConfigurable.getModulesConfig().getFacetEditorFacade().createAndAddFacet(type, parent.getModule(), parent);
    structureConfigurable.select(facet, true);
  }
private void addSubFacet(FacetType<?, ?> type, FacetTypeId<?> underlyingType)