summaryrefslogtreecommitdiff
path: root/platform/core-impl/src/com/intellij/openapi/progress/util/TooManyUsagesStatus.java
blob: f44d497edefaa4422ec10787008ba7188c697b78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright 2000-2014 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.progress.util;

import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.UserDataHolder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

public class TooManyUsagesStatus {
  private static final Key<TooManyUsagesStatus> KEY = Key.create("TooManyUsagesStatus");
  private static final NullStatus NULL_STATUS = new NullStatus();

  @NotNull
  public static TooManyUsagesStatus getFrom(@Nullable ProgressIndicator indicator) {
    TooManyUsagesStatus data = indicator instanceof UserDataHolder ? ((UserDataHolder)indicator).getUserData(KEY) : null;
    return data == null ? NULL_STATUS : data;
  }

  public static TooManyUsagesStatus createFor(@NotNull ProgressIndicator indicator) {
    TooManyUsagesStatus data = null;
    if (indicator instanceof UserDataHolder) {
      data = new TooManyUsagesStatus();
      ((UserDataHolder)indicator).putUserData(KEY, data);
    }
    return data;
  }

  // return true if dialog needs to be shown
  public boolean switchTooManyUsagesStatus() {
    return tooManyUsagesStatus.get() == Status.FEW_USAGES &&
           tooManyUsagesStatus.compareAndSet(Status.FEW_USAGES, Status.WARNING_DIALOG_SHOWN);
  }

  public void userResponded() {
    waitWhileUserClick.countDown();
    tooManyUsagesStatus.set(Status.USER_RESPONDED);
  }

  public enum Status {
    FEW_USAGES, WARNING_DIALOG_SHOWN, USER_RESPONDED
  }

  private final AtomicReference<Status> tooManyUsagesStatus = new AtomicReference<Status>(Status.FEW_USAGES);
  private final CountDownLatch waitWhileUserClick = new CountDownLatch(1);

  public void pauseProcessingIfTooManyUsages() {
    if (tooManyUsagesStatus.get() == Status.WARNING_DIALOG_SHOWN) {
      //assert ApplicationManager.getApplication().isDispatchThread() || !ApplicationManager.getApplication().isReadAccessAllowed();
      try {
        waitWhileUserClick.await(1, TimeUnit.SECONDS);
      }
      catch (InterruptedException ignored) {
      }
    }
  }

  private static class NullStatus extends TooManyUsagesStatus {
    @Override
    public boolean switchTooManyUsagesStatus() {
      return false;
    }

    @Override
    public void userResponded() {
    }

    @Override
    public void pauseProcessingIfTooManyUsages() {
    }
  }
}