2 * Copyright 2000-2009 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com
.intellij
.spellchecker
.actions
;
18 import com
.intellij
.codeInsight
.daemon
.impl
.HighlightInfo
;
19 import com
.intellij
.codeInsight
.daemon
.impl
.quickfix
.QuickFixAction
;
20 import com
.intellij
.codeInsight
.intention
.IntentionAction
;
21 import com
.intellij
.codeInspection
.LocalQuickFix
;
22 import com
.intellij
.codeInspection
.ex
.QuickFixWrapper
;
23 import com
.intellij
.openapi
.actionSystem
.*;
24 import com
.intellij
.openapi
.application
.ApplicationManager
;
25 import com
.intellij
.openapi
.command
.CommandProcessor
;
26 import com
.intellij
.openapi
.diagnostic
.Logger
;
27 import com
.intellij
.openapi
.editor
.Editor
;
28 import com
.intellij
.openapi
.project
.Project
;
29 import com
.intellij
.psi
.PsiFile
;
30 import com
.intellij
.spellchecker
.quickfixes
.SpellCheckerQuickFix
;
31 import com
.intellij
.util
.IncorrectOperationException
;
32 import org
.jetbrains
.annotations
.NotNull
;
33 import org
.jetbrains
.annotations
.Nullable
;
35 import java
.util
.ArrayList
;
36 import java
.util
.HashMap
;
37 import java
.util
.List
;
41 * Spelling action group.
43 public final class SpellingPopupActionGroup
extends ActionGroup
{
44 public SpellingPopupActionGroup() {
47 public SpellingPopupActionGroup(String shortName
, boolean popup
) {
48 super(shortName
, popup
);
51 public AnAction
[] getChildren(@Nullable AnActionEvent e
) {
53 AnAction
[] children
= findActions(e
);
55 if (children
.length
== 0) {
56 e
.getPresentation().setEnabled(false);
60 return AnAction
.EMPTY_ARRAY
;
64 private static AnAction
[] findActions(@NotNull AnActionEvent e
) {
65 PsiFile psiFile
= e
.getData(LangDataKeys
.PSI_FILE
);
66 Project project
= e
.getData(LangDataKeys
.PROJECT
);
67 Editor editor
= e
.getData(LangDataKeys
.EDITOR
);
68 if (psiFile
!= null && project
!= null && editor
!= null) {
69 List
<HighlightInfo
.IntentionActionDescriptor
> quickFixes
= QuickFixAction
.getAvailableActions(editor
, psiFile
, -1);
70 Map
<Anchor
, List
<AnAction
>> children
= new HashMap
<Anchor
, List
<AnAction
>>();
71 ArrayList
<AnAction
> first
= new ArrayList
<AnAction
>();
72 children
.put(Anchor
.FIRST
, first
);
73 ArrayList
<AnAction
> last
= new ArrayList
<AnAction
>();
74 children
.put(Anchor
.LAST
, last
);
75 extractActions(quickFixes
, children
);
76 if (first
.size() > 0 && last
.size() > 0) {
77 first
.add(new Separator());
80 if (first
.size() > 0) {
81 return first
.toArray(new AnAction
[first
.size()]);
85 return AnAction
.EMPTY_ARRAY
;
88 private static void extractActions(List
<HighlightInfo
.IntentionActionDescriptor
> descriptors
, Map
<Anchor
, List
<AnAction
>> actions
) {
89 for (HighlightInfo
.IntentionActionDescriptor actionDescriptor
: descriptors
) {
90 IntentionAction action
= actionDescriptor
.getAction();
91 if (action
instanceof QuickFixWrapper
) {
92 QuickFixWrapper wrapper
= (QuickFixWrapper
)action
;
93 LocalQuickFix localQuickFix
= wrapper
.getFix();
94 if (localQuickFix
instanceof SpellCheckerQuickFix
) {
95 SpellCheckerQuickFix spellCheckerQuickFix
= (SpellCheckerQuickFix
)localQuickFix
;
96 Anchor anchor
= spellCheckerQuickFix
.getPopupActionAnchor();
97 SpellCheckerIntentionAction popupAction
= new SpellCheckerIntentionAction(action
);
98 List
<AnAction
> list
= actions
.get(anchor
);
100 list
.add(popupAction
);
107 public void update(AnActionEvent e
) {
110 if (e
.getPresentation().isVisible() && findActions(e
).length
== 0) {
111 e
.getPresentation().setVisible(false);
116 private static class SpellCheckerIntentionAction
extends AnAction
{
117 private static final Logger LOGGER
= Logger
.getInstance("#SpellCheckerAction");
118 private final IntentionAction intention
;
120 public SpellCheckerIntentionAction(IntentionAction intention
) {
121 super(intention
.getText());
122 this.intention
= intention
;
125 public void actionPerformed(final AnActionEvent e
) {
126 final PsiFile psiFile
= e
.getData(LangDataKeys
.PSI_FILE
);
127 final Project project
= e
.getData(LangDataKeys
.PROJECT
);
128 final Editor editor
= e
.getData(LangDataKeys
.EDITOR
);
129 if (psiFile
!= null && project
!= null && editor
!= null) {
130 ApplicationManager
.getApplication().runWriteAction(new Runnable() {
132 CommandProcessor
.getInstance().executeCommand(project
, new Runnable() {
135 intention
.invoke(project
, editor
, psiFile
);
137 catch (IncorrectOperationException ex
) {
141 }, e
.getPresentation().getText(), e
.getActionManager().getId(SpellCheckerIntentionAction
.this));