1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor.xmlEditor.actions;
13
14 import java.util.Collections;
15 import java.util.Set;
16
17 import org.eclipse.ui.IWorkbenchPart;
18 import org.w3c.dom.Attr;
19 import org.w3c.dom.Element;
20 import org.w3c.dom.Node;
21
22 import sk.baka.xml.gene.ExportException;
23 import sk.uniba.euromath.editor.xmlEditor.actions.lang.Messages;
24
25 /***
26 * Deletes node specified in constructor.
27 *
28 * @author Tomáš Studva 9.7.2005
29 */
30 public class DeleteNodeAction extends XMLAccessModifyAction {
31
32 /***
33 * Id of action.
34 */
35 public static final String id = DeleteNodeAction.class.toString();
36
37 /***
38 * Element to delete.
39 */
40 private final Node node;
41
42 /***
43 * Constructor.
44 *
45 * @param node
46 * node to delete.
47 * @param part
48 * associated workbench part
49 */
50 public DeleteNodeAction(Node node, IWorkbenchPart part) {
51 super(part);
52 assert (node != null);
53 this.node = node;
54 setId(id + node.getLocalName());
55 switch (node.getNodeType()) {
56 case Node.ELEMENT_NODE:
57 setText(Messages
58 .getString("DeleteNodeAction.ElementText") + node.getLocalName());
59 break;
60 case Node.ATTRIBUTE_NODE:
61 setText(Messages
62 .getString("DeleteNodeAction.AttributeText") + node.getLocalName());
63 break;
64 default:
65 throw new IllegalArgumentException(
66 "Not implemented - illegal node type.");
67 }
68 }
69
70 @Override
71 protected boolean calculateEnabled() {
72 switch (node.getNodeType()) {
73 case Node.ELEMENT_NODE:
74 final Set<Element> elements = Collections
75 .singleton((Element) this.node);
76 return super.calculateEnabled()
77 && (getModifyHelper()
78 .canDeleteElements(
79 elements,
80 null) == null);
81 case Node.ATTRIBUTE_NODE:
82 return super.calculateEnabled()
83 && (getModifyHelper()
84 .canDeleteAttribute(
85 (Attr) this.node) == null);
86 }
87 throw new IllegalStateException("Not reachable.");
88
89 }
90
91 @Override
92 public void runAsCommand() {
93 try {
94 switch (node.getNodeType()) {
95 case Node.ELEMENT_NODE:
96 final Set<Element> elements = Collections
97 .singleton((Element) this.node);
98 getModifyHelper().deleteElements(getShell(),
99 elements);
100 case Node.ATTRIBUTE_NODE:
101 getModifyHelper().deleteAttribute(getShell(),
102 (Attr) this.node);
103 }
104 } catch (ExportException ex) {
105 handleExportException(ex);
106 }
107
108 }
109 }