private void removeLibraryIfNeeded() {
    ApplicationManager.getApplication().assertIsDispatchThread();

    ModifiableModelsProvider modelsProvider = ModifiableModelsProvider.SERVICE.getInstance();
    ModifiableRootModel model = modelsProvider.getModuleModifiableModel(myModule);
    LibraryOrderEntry goLibraryEntry =
        OrderEntryUtil.findLibraryOrderEntry(model, getLibraryName());
    if (goLibraryEntry != null) {
      ApplicationManager.getApplication()
          .runWriteAction(
              () -> {
                Library library = goLibraryEntry.getLibrary();
                if (library != null) {
                  LibraryTable table = library.getTable();
                  if (table != null) {
                    table.removeLibrary(library);
                    model.removeOrderEntry(goLibraryEntry);
                    modelsProvider.commitModuleModifiableModel(model);
                  }
                } else {
                  modelsProvider.disposeModuleModifiableModel(model);
                }
              });
    } else {
      ApplicationManager.getApplication()
          .runWriteAction(() -> modelsProvider.disposeModuleModifiableModel(model));
    }
  }
  @Override
  protected void doOKAction() {
    final LibraryCompositionSettings librarySettings = myComponent.getLibraryCompositionSettings();
    if (librarySettings != null) {
      final ModifiableRootModel modifiableModel =
          myModifiableModelsProvider.getModuleModifiableModel(myModule);
      if (!askAndRemoveDuplicatedLibraryEntry(
          modifiableModel, librarySettings.getLibraryDescription())) {
        if (myConfigurable.isOnlyLibraryAdded()) {
          myModifiableModelsProvider.disposeModuleModifiableModel(modifiableModel);
          return;
        }
        return;
      }

      // Fix
      new WriteAction() {
        @Override
        protected void run(final Result result) {
          myModifiableModelsProvider.commitModuleModifiableModel(modifiableModel);
        }
      }.execute();

      final boolean downloaded = librarySettings.downloadFiles(getRootPane());
      if (!downloaded) {
        int answer =
            Messages.showYesNoDialog(
                getRootPane(),
                ProjectBundle.message("warning.message.some.required.libraries.wasn.t.downloaded"),
                CommonBundle.getWarningTitle(),
                Messages.getWarningIcon());
        if (answer != 0) {
          return;
        }
      }
    }

    new WriteAction() {
      @Override
      protected void run(final Result result) {
        final ModifiableRootModel rootModel =
            myModifiableModelsProvider.getModuleModifiableModel(myModule);
        if (librarySettings != null) {
          librarySettings.addLibraries(
              rootModel, new ArrayList<Library>(), myModel.getLibrariesContainer());
        }
        myConfigurable.addSupport(myModule, rootModel, myModifiableModelsProvider);
        myModifiableModelsProvider.commitModuleModifiableModel(rootModel);
      }
    }.execute();
    super.doOKAction();
  }