View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius
3    * University, Bratislava. This file is protected by the Mozilla Public License
4    * version 1.1 (the License); you may not use this file except in compliance
5    * with the License. You may obtain a copy of the License at
6    * http://euromath2.sourceforge.net/license.html Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is
8    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9    * KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
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()); //$NON-NLS-1$
59                          break;
60                  case Node.ATTRIBUTE_NODE:
61                          setText(Messages
62                                          .getString("DeleteNodeAction.AttributeText") + node.getLocalName()); //$NON-NLS-1$
63                          break;
64                  default:
65                          throw new IllegalArgumentException(
66                                          "Not implemented - illegal node type."); //$NON-NLS-N$
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 }