/*
 * Copyright 2000-2015 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.codeInspection.ex;

import com.intellij.codeInspection.CommonProblemDescriptor;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.presentation.java.SymbolPresentationUtil;
import com.intellij.util.SequentialModalProgressTask;
import com.intellij.util.SequentialTask;
import org.jetbrains.annotations.NotNull;

public abstract class PerformFixesModalTask implements SequentialTask {
  @NotNull
  protected final Project myProject;
  private final CommonProblemDescriptor[] myDescriptors;
  private final SequentialModalProgressTask myTask;
  private final PsiDocumentManager myDocumentManager;
  private int myCount = 0;

  public PerformFixesModalTask(@NotNull Project project,
                               @NotNull CommonProblemDescriptor[] descriptors,
                               @NotNull SequentialModalProgressTask task) {
    myProject = project;
    myDescriptors = descriptors;
    myTask = task;
    myDocumentManager = PsiDocumentManager.getInstance(myProject);
  }

  @Override
  public void prepare() {
  }

  @Override
  public boolean isDone() {
    return myCount > myDescriptors.length - 1;
  }

  @Override
  public boolean iteration() {
    final CommonProblemDescriptor descriptor = myDescriptors[myCount++];
    ProgressIndicator indicator = myTask.getIndicator();
    if (indicator != null) {
      indicator.setFraction((double)myCount / myDescriptors.length);
      String presentableText = "usages";
      if (descriptor instanceof ProblemDescriptor) {
        final PsiElement psiElement = ((ProblemDescriptor)descriptor).getPsiElement();
        if (psiElement != null) {
          presentableText = SymbolPresentationUtil.getSymbolPresentableText(psiElement);
        }
      }
      indicator.setText("Processing " + presentableText);
    }
    ApplicationManager.getApplication().runWriteAction(new Runnable() {
      @Override
      public void run() {
        myDocumentManager.commitAllDocuments();
        applyFix(myProject, descriptor);
      }
    });
    return isDone();
  }

  @Override
  public void stop() {}
  
  protected abstract void applyFix(Project project, CommonProblemDescriptor descriptor);
}

@@ -861,24 +861,129 @@
 sIndicator;%0A
+import com.intellij.openapi.project.DumbModePermission;%0Aimport com.intellij.openapi.project.DumbService;%0A
 import com.i
@@ -2759,24 +2759,167 @@
 ext);%0A    %7D%0A
+    DumbService.allowStartingDumbModeInside(DumbModePermission.MAY_START_MODAL, new Runnable() %7B%0A      @Override%0A      public void run() %7B%0A    
     Applicat
@@ -2976,32 +2976,36 @@
 nable() %7B%0A      
+    
 @Override%0A      
@@ -2996,32 +2996,36 @@
 @Override%0A      
+   
+ 
 public void run(
@@ -3020,32 +3020,36 @@
 ic void run() %7B%0A
+    
         myDocume
@@ -3088,16 +3088,20 @@
         
+ 
+   
 applyFix
@@ -3125,16 +3125,40 @@
 iptor);%0A
+          %7D%0A        %7D);%0A
       %7D%0A

allow modal dumb mode in code cleanup (EA-75351 - Throwable: DumbServiceImpl.scheduleCacheUpdate)