update copyright
[fedora-idea.git] / xml / dom-impl / src / com / intellij / util / xml / tree / actions / DeleteDomElement.java
blobad0f561f699cec2f6105f914425b1deec9e6bfd3
1 /*
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.
17 package com.intellij.util.xml.tree.actions;
19 import com.intellij.openapi.actionSystem.AnActionEvent;
20 import com.intellij.openapi.application.ApplicationBundle;
21 import com.intellij.openapi.application.Result;
22 import com.intellij.openapi.command.WriteCommandAction;
23 import com.intellij.openapi.ui.Messages;
24 import com.intellij.openapi.util.IconLoader;
25 import com.intellij.ui.treeStructure.SimpleNode;
26 import com.intellij.util.xml.DomElement;
27 import com.intellij.util.xml.DomFileElement;
28 import com.intellij.util.xml.DomUtil;
29 import com.intellij.util.xml.ElementPresentation;
30 import com.intellij.util.xml.tree.BaseDomElementNode;
31 import com.intellij.util.xml.tree.DomFileElementNode;
32 import com.intellij.util.xml.tree.DomModelTreeView;
34 /**
35 * User: Sergey.Vasiliev
37 public class DeleteDomElement extends BaseDomTreeAction {
39 public DeleteDomElement() {
42 public DeleteDomElement(final DomModelTreeView treeView) {
43 super(treeView);
46 public void actionPerformed(AnActionEvent e, DomModelTreeView treeView) {
47 final SimpleNode selectedNode = treeView.getTree().getSelectedNode();
49 if (selectedNode instanceof BaseDomElementNode) {
51 if (selectedNode instanceof DomFileElementNode) {
52 e.getPresentation().setVisible(false);
53 return;
56 final DomElement domElement = ((BaseDomElementNode)selectedNode).getDomElement();
58 final int ret = Messages.showOkCancelDialog(getPresentationText(selectedNode) + "?", ApplicationBundle.message("action.remove"),
59 Messages.getQuestionIcon());
60 if (ret == 0) {
61 new WriteCommandAction(domElement.getManager().getProject(), DomUtil.getFile(domElement)) {
62 protected void run(final Result result) throws Throwable {
63 domElement.undefine();
65 }.execute();
70 public void update(AnActionEvent e, DomModelTreeView treeView) {
71 final SimpleNode selectedNode = treeView.getTree().getSelectedNode();
73 if (selectedNode instanceof DomFileElementNode) {
74 e.getPresentation().setVisible(false);
75 return;
78 boolean enabled = false;
79 if (selectedNode instanceof BaseDomElementNode) {
80 final DomElement domElement = ((BaseDomElementNode)selectedNode).getDomElement();
81 if (domElement.isValid() && DomUtil.hasXml(domElement) && !(domElement.getParent() instanceof DomFileElement)) {
82 enabled = true;
86 e.getPresentation().setEnabled(enabled);
89 if (enabled) {
90 e.getPresentation().setText(getPresentationText(selectedNode));
92 else {
93 e.getPresentation().setText(ApplicationBundle.message("action.remove"));
96 e.getPresentation().setIcon(IconLoader.getIcon("/general/remove.png"));
99 private static String getPresentationText(final SimpleNode selectedNode) {
100 String removeString = ApplicationBundle.message("action.remove");
101 final ElementPresentation presentation = ((BaseDomElementNode)selectedNode).getDomElement().getPresentation();
102 removeString += " " + presentation.getTypeName() +
103 (presentation.getElementName() == null || presentation.getElementName().trim().length() == 0? "" : ": " + presentation.getElementName());
104 return removeString;