hide non-dumb-aware intentions (EA-65004 - INRE: FileBasedIndexImpl.handleDumbMode)
[idea/community.git] / platform / lang-impl / src / com / intellij / codeInsight / intention / impl / IntentionActionWithTextCaching.java
1 /*
2  * Copyright 2000-2013 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.intellij.codeInsight.intention.impl;
18
19 import com.intellij.codeInsight.daemon.impl.HighlightInfo;
20 import com.intellij.codeInsight.intention.IntentionAction;
21 import com.intellij.openapi.project.DumbService;
22 import com.intellij.openapi.project.PossiblyDumbAware;
23 import com.intellij.openapi.util.Comparing;
24 import com.intellij.openapi.diagnostic.Logger;
25 import org.jetbrains.annotations.NotNull;
26
27 import javax.swing.*;
28 import java.util.List;
29 import java.util.ArrayList;
30
31 /**
32 * @author cdr
33 */
34 class IntentionActionWithTextCaching implements Comparable<IntentionActionWithTextCaching>, PossiblyDumbAware {
35   private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.intention.impl.IntentionActionWithTextCaching");
36   private final List<IntentionAction> myOptionIntentions = new ArrayList<IntentionAction>();
37   private final List<IntentionAction> myOptionErrorFixes = new ArrayList<IntentionAction>();
38   private final List<IntentionAction> myOptionInspectionFixes = new ArrayList<IntentionAction>();
39   private final String myText;
40   private final IntentionAction myAction;
41   private final String myDisplayName;
42   private final Icon myIcon;
43
44   IntentionActionWithTextCaching(IntentionAction action){
45     this(action, action.getText(), null);
46   }
47
48   IntentionActionWithTextCaching(HighlightInfo.IntentionActionDescriptor action){
49     this(action.getAction(), action.getDisplayName(), action.getIcon());
50   }
51
52   private IntentionActionWithTextCaching(IntentionAction action, String displayName, Icon icon) {
53     myIcon = icon;
54     myText = action.getText();
55     // needed for checking errors in user written actions
56     //noinspection ConstantConditions
57     LOG.assertTrue(myText != null, "action "+action.getClass()+" text returned null");
58     myAction = action;
59     myDisplayName = displayName;
60   }
61
62   String getText() {
63     return myText;
64   }
65
66   public void addIntention(final IntentionAction action) {
67     myOptionIntentions.add(action);
68   }
69   public void addErrorFix(final IntentionAction action) {
70     myOptionErrorFixes.add(action);
71   }
72   public void addInspectionFix(final IntentionAction action) {
73     myOptionInspectionFixes.add(action);
74   }
75
76   public IntentionAction getAction() {
77     return myAction;
78   }
79
80   public List<IntentionAction> getOptionIntentions() {
81     return myOptionIntentions;
82   }
83
84   public List<IntentionAction> getOptionErrorFixes() {
85     return myOptionErrorFixes;
86   }
87
88   public List<IntentionAction> getOptionInspectionFixes() {
89     return myOptionInspectionFixes;
90   }
91
92   public String getToolName() {
93     return myDisplayName;
94   }
95
96   public String toString() {
97     return getText();
98   }
99
100   @Override
101   public int compareTo(@NotNull final IntentionActionWithTextCaching other) {
102     if (myAction instanceof Comparable) {
103       return ((Comparable)myAction).compareTo(other.getAction());
104     }
105     else if (other.getAction() instanceof Comparable) {
106       return ((Comparable)other.getAction()).compareTo(myAction);
107     }
108     return Comparing.compare(getText(), other.getText());
109   }
110
111   public Icon getIcon() {
112     return myIcon;
113   }
114
115   @Override
116   public boolean isDumbAware() {
117     return DumbService.isDumbAware(myAction);
118   }
119 }