/*
 * Copyright 2000-2017 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.wm.impl;

import com.intellij.ProjectTopics;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModuleRootEvent;
import com.intellij.openapi.roots.ModuleRootListener;
import com.intellij.openapi.startup.StartupActivity;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ex.ToolWindowManagerEx;
import com.intellij.openapi.wm.ext.LibraryDependentToolWindow;
import com.intellij.util.messages.MessageBusConnection;
import org.jetbrains.annotations.NotNull;

public class LibraryDependentToolWindowManager implements StartupActivity {

  @Override
  public void runActivity(@NotNull Project project) {
    final Application application = ApplicationManager.getApplication();
    if (application.isUnitTestMode() ||
        application.isHeadlessEnvironment()) {
      return;
    }

    final ModuleRootListener rootListener = new ModuleRootListener() {
      @Override
      public void rootsChanged(ModuleRootEvent event) {
        checkToolWindowStatuses(project);
      }
    };

    checkToolWindowStatuses(project);

    final MessageBusConnection connection = project.getMessageBus().connect(project);
    connection.subscribe(ProjectTopics.PROJECT_ROOTS, rootListener);
  }

  private static void checkToolWindowStatuses(@NotNull final Project project) {
    ApplicationManager.getApplication().executeOnPooledThread(() -> {
      if (project.isDisposed()) return;

      doCheckToolWindowStatuses(project);
    });
  }

  private static void doCheckToolWindowStatuses(@NotNull final Project project) {
    final ToolWindowManagerEx toolWindowManagerEx = ToolWindowManagerEx.getInstanceEx(project);

    for (LibraryDependentToolWindow libraryToolWindow : Extensions.getExtensions(LibraryDependentToolWindow.EXTENSION_POINT_NAME)) {
      boolean exists = DumbService.getInstance(project)
        .runReadActionInSmartMode(() -> libraryToolWindow.getLibrarySearchHelper().isLibraryExists(project));

      ApplicationManager.getApplication().invokeLater(() -> {
        ToolWindow toolWindow = toolWindowManagerEx.getToolWindow(libraryToolWindow.id);

        if (exists) {
          if (toolWindow == null) {
            toolWindowManagerEx.initToolWindow(libraryToolWindow);
          }
        }
        else {
          if (toolWindow != null) {
            toolWindowManagerEx.unregisterToolWindow(libraryToolWindow.id);
          }
        }
      });
    }
  }
}

@@ -777,24 +777,131 @@
 ionManager;%0A
+import com.intellij.openapi.application.ModalityState;%0Aimport com.intellij.openapi.application.ReadAction;%0A
 import com.i
@@ -1344,32 +1344,93 @@
 dentToolWindow;%0A
+import com.intellij.util.concurrency.SequentialTaskExecutor;%0A
 import com.intel
@@ -1512,16 +1512,62 @@
 tNull;%0A%0A
+import java.util.concurrent.ExecutorService;%0A%0A
 public c
@@ -1635,16 +1635,175 @@
 vity %7B%0A%0A
+  private static final ExecutorService ourExecutor =%0A    SequentialTaskExecutor.createSequentialApplicationPoolExecutor(%22LibraryDependentToolWindowManager%22);%0A%0A
   @Overr
@@ -2529,114 +2529,107 @@
     
-ApplicationManager.getApplication().executeOnPooledThread(() -%3E %7B%0A      if (project.isDisposed()) return;%0A
+final ModalityState currentModalityState = ModalityState.current();%0A%0A    ourExecutor.submit(() -%3E %7B
 %0A   
@@ -2660,24 +2660,46 @@
 uses(project
+, currentModalityState
 );%0A    %7D);%0A 
@@ -2784,83 +2784,198 @@
 ject
-) %7B%0A    final ToolWindowManagerEx toolWindowManagerEx = ToolWindowManagerEx
+,%0A                                                ModalityState currentModalityState) %7B%0A    final DumbService dumbService = ReadAction.compute(() -%3E project.isDisposed() ? null : DumbService
 .get
@@ -2982,18 +2982,16 @@
 Instance
-Ex
 (project
@@ -2987,25 +2987,63 @@
 nce(project)
+)
 ;
+%0A    if (dumbService == null) return;
 %0A%0A    for (L
@@ -3188,17 +3188,17 @@
 xists = 
-D
+d
 umbServi
@@ -3203,38 +3203,8 @@
 vice
-.getInstance(project)%0A        
 .run
@@ -3230,16 +3230,107 @@
 de(() -%3E
+ !project.isDisposed() &&%0A                                                                 
  library
@@ -3451,24 +3451,124 @@
 ter(() -%3E %7B%0A
+        final ToolWindowManagerEx toolWindowManagerEx = ToolWindowManagerEx.getInstanceEx(project);%0A
         Tool
@@ -3944,24 +3944,69 @@
    %7D%0A      %7D
+, currentModalityState, project.getDisposed()
 );%0A    %7D%0A  %7D

convert to StartupActivity #2