/*
 * Copyright 2000-2016 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.internal;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.DumbModeTask;
import com.intellij.openapi.project.DumbServiceImpl;
import com.intellij.openapi.project.Project;
import com.intellij.util.TimeoutUtil;
import org.jetbrains.annotations.NotNull;

/**
 * @author peter
 */
public class ToggleDumbModeAction extends DumbAwareAction {
  private volatile boolean myDumb = false;

  public void actionPerformed(AnActionEvent e) {
    if (myDumb) {
      myDumb = false;
    }
    else {
      myDumb = true;
      final Project project = e.getProject();
      if (project == null) return;

      DumbServiceImpl.getInstance(project).queueTask(new DumbModeTask() {
        @Override
        public void performInDumbMode(@NotNull ProgressIndicator indicator) {
          while (myDumb) {
            indicator.checkCanceled();
            TimeoutUtil.sleep(100);
          }
        }
      });
    }
  }

  @Override
  public void update(AnActionEvent e) {
    Project project = e.getProject();
    if (project == null) {
      e.getPresentation().setEnabled(false);
      return;
    }
    boolean dumb = DumbServiceImpl.getInstance(project).isDumb();
    boolean enabled = !dumb || myDumb;
    if (enabled && myDumb != dumb) myDumb = dumb;
    e.getPresentation().setEnabled(enabled);
    e.getPresentation().setText(dumb ? "Exit Dumb Mode" : "Enter Dumb Mode");
  }
}

@@ -939,16 +939,54 @@
 roject;%0A
+import com.intellij.openapi.util.Key;%0A
 import c
@@ -1157,250 +1157,320 @@
 ate 
-volatile boolean myDumb = false;%0A%0A  public void actionPerformed(AnActionEvent e) %7B%0A    if (myDumb) %7B%0A      myDumb = false;%0A    %7D%0A    else %7B%0A      myDumb = true
+static final Key%3CBoolean%3E DUMB = Key.create(%22ToggleDumbModeAction%22);%0A%0A  public void actionPerformed(AnActionEvent e) %7B%0A    Project project = e.getProject();%0A    if (project == null) return
 ;%0A
+%0A
     
-  final Project project = e.getProject();%0A      if (project == null) return;%0A
+if (isToggledDumb(project)) %7B%0A      setToggledDumb(project, false);%0A    %7D else %7B%0A      setToggledDumb(project, true);
 %0A   
@@ -1653,22 +1653,38 @@
  while (
-myDumb
+isToggledDumb(project)
 ) %7B%0A    
@@ -1797,16 +1797,247 @@
  %7D%0A  %7D%0A%0A
+  private static void setToggledDumb(Project project, boolean value) %7B%0A    project.putUserData(DUMB, value);%0A  %7D%0A%0A  private static boolean isToggledDumb(Project project) %7B%0A    return project.getUserData(DUMB) == Boolean.TRUE;%0A  %7D%0A%0A
   @Overr
@@ -2284,92 +2284,90 @@
     
-boolean enabled = !dumb %7C%7C myDumb;%0A    if (enabled && myDumb != dumb) myDumb = dumb;
+if (!dumb && isToggledDumb(project)) %7B%0A      setToggledDumb(project, false);%0A    %7D
 %0A   
@@ -2402,15 +2402,39 @@
 led(
-enabled
+!dumb %7C%7C isToggledDumb(project)
 );%0A 

make ToggleDumbModeAction work with several projects open