readonly exception
[fedora-idea.git] / lang-impl / src / com / intellij / codeInspection / ex / QuickFixWrapper.java
blob10a72cee8a1c2428872cfe8a6964595b898c272f
1 package com.intellij.codeInspection.ex;
3 import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
4 import com.intellij.codeInsight.intention.IntentionAction;
5 import com.intellij.codeInspection.LocalQuickFix;
6 import com.intellij.codeInspection.ProblemDescriptor;
7 import com.intellij.codeInspection.QuickFix;
8 import com.intellij.openapi.command.undo.UndoUtil;
9 import com.intellij.openapi.diagnostic.Logger;
10 import com.intellij.openapi.editor.Editor;
11 import com.intellij.openapi.project.Project;
12 import com.intellij.openapi.util.Comparing;
13 import com.intellij.psi.PsiElement;
14 import com.intellij.psi.PsiFile;
15 import com.intellij.util.IncorrectOperationException;
16 import org.jetbrains.annotations.NotNull;
18 /**
19 * @author max
21 public class QuickFixWrapper implements IntentionAction {
23 private static final Logger LOG = Logger.getInstance("com.intellij.codeInspection.ex.QuickFixWrapper");
25 private final ProblemDescriptor myDescriptor;
26 private final int myFixNumber;
29 public static IntentionAction wrap(@NotNull ProblemDescriptor descriptor, int fixNumber) {
30 LOG.assertTrue(fixNumber > -1);
31 LOG.assertTrue(descriptor.getFixes() != null && descriptor.getFixes().length > fixNumber);
33 final QuickFix fix = descriptor.getFixes()[fixNumber];
34 return fix instanceof IntentionAction ? (IntentionAction)fix : new QuickFixWrapper(descriptor, fixNumber);
37 private QuickFixWrapper(@NotNull ProblemDescriptor descriptor, int fixNumber) {
38 myDescriptor = descriptor;
39 myFixNumber = fixNumber;
42 @NotNull
43 public String getText() {
44 return getFamilyName();
47 @NotNull
48 public String getFamilyName() {
49 return myDescriptor.getFixes()[myFixNumber].getName();
52 public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
53 PsiElement psiElement = myDescriptor.getPsiElement();
54 return psiElement != null && psiElement.isValid();
57 public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
58 //if (!CodeInsightUtil.prepareFileForWrite(file)) return;
59 // consider all local quick fixes do it themselves
61 LocalQuickFix fix = (LocalQuickFix)myDescriptor.getFixes()[myFixNumber];
62 fix.applyFix(project, myDescriptor);
63 DaemonCodeAnalyzer.getInstance(project).restart();
64 final PsiElement element = myDescriptor.getPsiElement();
65 if (element != null) {
66 final PsiFile fileForUndo = element.getContainingFile();
67 if (!Comparing.equal(fileForUndo, file)) {
68 UndoUtil.markPsiFileForUndo(fileForUndo);
73 public boolean startInWriteAction() {
74 return true;
77 public LocalQuickFix getFix() {
78 return (LocalQuickFix)myDescriptor.getFixes()[myFixNumber];
81 public String toString() {
82 return getText();