/*
 * Copyright (c) 2000-2006 JetBrains s.r.o. All Rights Reserved.
 */

package com.intellij.util.xml.tree.actions;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationBundle;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.util.IconLoader;
import com.intellij.ui.treeStructure.SimpleNode;
import com.intellij.util.xml.DomElement;
import com.intellij.util.xml.ElementPresentation;
import com.intellij.util.xml.tree.BaseDomElementNode;
import com.intellij.util.xml.tree.DomFileElementNode;
import com.intellij.util.xml.tree.DomModelTreeView;

/**
 * User: Sergey.Vasiliev
 */
public class DeleteDomElement extends BaseDomTreeAction {

  public DeleteDomElement() {
  }

  public DeleteDomElement(final DomModelTreeView treeView) {
    super(treeView);
  }

  public void actionPerformed(AnActionEvent e, DomModelTreeView treeView) {
    final SimpleNode selectedNode = treeView.getTree().getSelectedNode();

    if (selectedNode instanceof BaseDomElementNode) {

      final DomElement domElement = ((BaseDomElementNode)selectedNode).getDomElement();
      new WriteCommandAction(domElement.getManager().getProject(), domElement.getRoot().getFile()) {
        protected void run(final Result result) throws Throwable {
          domElement.undefine();
        }
      }.execute();
    }
  }

  public void update(AnActionEvent e, DomModelTreeView treeView) {
    final SimpleNode selectedNode = treeView.getTree().getSelectedNode();

    if (selectedNode instanceof DomFileElementNode) {
      e.getPresentation().setVisible(false);
      return;
    }

    boolean enabled = false;
    if (selectedNode instanceof BaseDomElementNode) {
      final DomElement domElement = ((BaseDomElementNode)selectedNode).getDomElement();
      if (domElement.isValid() && domElement.getXmlElement() != null && !domElement.equals(domElement.getRoot().getRootElement())) {
        enabled = true;
      }
    }

    e.getPresentation().setEnabled(enabled);

    final String removeString = ApplicationBundle.message("action.remove");
    if (enabled) {
      final ElementPresentation presentation = ((BaseDomElementNode)selectedNode).getDomElement().getPresentation();

      e.getPresentation().setText(removeString + " " + presentation.getTypeName() +
                                  (presentation.getElementName() == null ? "" : ": " + presentation.getElementName()));
    }
    else {
      e.getPresentation().setText(removeString);
    }

    e.getPresentation().setIcon(IconLoader.getIcon("/general/remove.png"));
  }
}

@@ -375,16 +375,57 @@
 Loader;%0A
+import com.intellij.openapi.ui.Messages;%0A
 import c
@@ -1220,24 +1220,261 @@
 omElement();
+%0A%0A      final int ret = Messages.showOkCancelDialog(getPresentationText(selectedNode) + %22?%22, ApplicationBundle.message(%22action.remove%22),%0A                                                  Messages.getQuestionIcon());%0A      if (ret == 0) %7B
 %0A      new W
@@ -1688,24 +1688,32 @@
 .execute();%0A
+      %7D%0A
     %7D%0A  %7D%0A%0A 
@@ -2366,40 +2366,149 @@
 );%0A%0A
+%0A
     
-final String removeString = 
+if (enabled) %7B%0A      e.getPresentation().setText(getPresentationText(selectedNode));%0A    %7D%0A    else %7B%0A      e.getPresentation().setText(
 Appl
@@ -2549,31 +2549,246 @@
 ve%22)
+)
 ;%0A    
-if (enabled) %7B%0A  
+%7D%0A%0A    e.getPresentation().setIcon(IconLoader.getIcon(%22/general/remove.png%22));%0A  %7D%0A%0A  private static String getPresentationText(final SimpleNode selectedNode) %7B%0A    String removeString = ApplicationBundle.message(%22action.remove%22);%0A
     
@@ -2902,43 +2902,12 @@
 ();%0A
-%0A
     
-  e.getPresentation().setText(
 remo
@@ -2916,16 +2916,17 @@
 String +
+=
  %22 %22 + p
@@ -2953,18 +2953,16 @@
 ame() +%0A
-  
         
@@ -3028,21 +3028,10 @@
 ull 
-? %22%22 : %22: %22 +
+%7C%7C
  pre
@@ -3060,159 +3060,98 @@
 me()
-));%0A    %7D%0A    else %7B%0A      e.getPresentation().setText(removeString);%0A    %7D%0A%0A    e.getPresentation().setIcon(IconLoader.getIcon(%22/general/remove.png%22))
+.trim().length() == 0? %22%22 : %22: %22 + presentation.getElementName());%0A    return removeString
 ;%0A  

delete confirmation for dom tree view elements(IDEADEV-5071)