/*
 * Copyright 2000-2009 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.codeInsight.daemon.impl.analysis;

import com.intellij.codeInsight.CodeInsightUtilBase;
import com.intellij.codeInsight.daemon.XmlErrorMessages;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;

/**
 * @author Maxim.Mossienko
 */
public class RemoveAttributeIntentionFix implements IntentionAction {
  private final String myLocalName;
  private final XmlAttribute myAttribute;

  public RemoveAttributeIntentionFix(final String localName, final @NotNull XmlAttribute attribute) {
    myLocalName = localName;
    myAttribute = attribute;
  }

  @NotNull
  public String getText() {
    return XmlErrorMessages.message("remove.attribute.quickfix.text", myLocalName);
  }

  @NotNull
  public String getFamilyName() {
    return XmlErrorMessages.message("remove.attribute.quickfix.family");
  }

  public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    return myAttribute.isValid();
  }

  public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    if (!CodeInsightUtilBase.prepareFileForWrite(file)) return;
    PsiElement next = findNextAttribute(myAttribute);
    myAttribute.delete();

    if (next != null) {
      editor.getCaretModel().moveToOffset(next.getTextRange().getStartOffset());
    }
  }

  private static PsiElement findNextAttribute(final XmlAttribute attribute) {
    PsiElement nextSibling = attribute.getNextSibling();
    while (nextSibling != null) {
      if (nextSibling instanceof XmlAttribute) return nextSibling;
      nextSibling =  nextSibling.getNextSibling();
    }
    return null;
  }

  public boolean startInWriteAction() {
    return true;
  }
}

@@ -791,23 +791,32 @@
 eIns
-ight.intention.
+pection.LocalQuickFixAnd
 Inte
@@ -822,24 +822,36 @@
 entionAction
+OnPsiElement
 ;%0Aimport com
@@ -1050,53 +1050,41 @@
 ort 
-com.intellij.util.IncorrectOperationException
+org.jetbrains.annotations.NotNull
 ;%0Aim
@@ -1115,22 +1115,23 @@
 ations.N
-otN
 ull
+able
 ;%0A%0A/**%0A 
@@ -1205,19 +1205,32 @@
 Fix 
-implement
+extend
 s 
+LocalQuickFixAnd
 Inte
@@ -1236,24 +1236,36 @@
 entionAction
+OnPsiElement
  %7B%0A  private
@@ -1294,50 +1294,8 @@
 ame;
-%0A  private final XmlAttribute myAttribute;
 %0A%0A  
@@ -1390,24 +1390,46 @@
 ttribute) %7B%0A
+    super(attribute);%0A
     myLocalN
@@ -1449,37 +1449,8 @@
 me;%0A
-    myAttribute = attribute;%0A
   %7D%0A
@@ -1707,75 +1707,94 @@
 %0A%0A  
-public boolean isAvailable(@NotNull Project project, Editor editor,
+@Override%0A  public void invoke(@NotNull Project project,%0A                     @NotNull
  Psi
@@ -1806,161 +1806,202 @@
 file
-) %7B
+,
 %0A    
-return myAttribute.isValid();%0A  %7D%0A%0A  public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException
+                 @Nullable(%22is null when called from inspection%22) Editor editor,%0A                     @NotNull PsiElement startElement,%0A                     @NotNull PsiElement endElement)
  %7B%0A 
@@ -2103,18 +2103,20 @@
 tribute(
-my
+(Xml
 Attribut
@@ -2121,25 +2121,39 @@
 ute)
-;%0A    myAttribute
+startElement);%0A    startElement
 .del
@@ -2175,16 +2175,34 @@
 (next !=
+ null && editor !=
  null) %7B
@@ -2294,16 +2294,28 @@
  %7D%0A  %7D%0A%0A
+  @Nullable%0A
   privat

remove attribute fix not to hold a hard ref to psi