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 org.eclipse.ui.IWorkbenchPart;
15  import org.w3c.dom.Attr;
16  import org.w3c.dom.Comment;
17  import org.w3c.dom.Node;
18  import org.w3c.dom.ProcessingInstruction;
19  
20  import sk.baka.xml.gene.ExportException;
21  import sk.uniba.euromath.editor.xmlEditor.actions.lang.Messages;
22  
23  /***
24   * Action to modify node of any type except ELEMENT_NODE, ENTITY_REFERENCE_NODE.
25   * Node to modify is gained from contructor or from selection(action is enabled
26   * if only one node is selected).
27   * 
28   * @author Tomáš Studva 9.12.2005
29   */
30  public class ModifyNodeAction extends NodeManipulateAction {
31  
32          /***
33           * Default id of action.
34           */
35          public static final String id = ModifyNodeAction.class.toString();
36  
37          /***
38           * Constructor. Creates action to modify node of any type from
39           * selection.
40           * 
41           * @param part
42           *                associated workbench part
43           */
44          public ModifyNodeAction(IWorkbenchPart part) {
45                  super(part, NodeAcquireType.Single, false);
46                  setId(id);
47                  setText(Messages.getString("ModifyNodeAction.Text")); //$NON-NLS-1$
48          }
49  
50          /***
51           * Constructor. Creates action to modify node. Node can't be element.
52           * 
53           * @param node
54           *                node to modify.
55           * @param part
56           */
57          public ModifyNodeAction(Node node, IWorkbenchPart part) {
58                  super(node, part);
59                  if ((node.getNodeType() == Node.ELEMENT_NODE)
60                                  || (node.getNodeType() == Node.ENTITY_REFERENCE_NODE))
61                          throw new IllegalArgumentException();
62                  setId(id + node.toString());
63                  setText(Messages.getString("ModifyNodeAction.Text")); //$NON-NLS-1$
64          }
65  
66          @Override
67          protected boolean calculateEnabled() {
68                  // when modification dependes on schema is used wizard, so let
69                  // it bee all time enabled
70                  return super.calculateEnabled()
71                                  && (getNode().getNodeType() != Node.ELEMENT_NODE)
72                                  && (getNode().getNodeType() != Node.ENTITY_REFERENCE_NODE);
73          }
74  
75          /***
76           * Tries to modify node.
77           */
78          @Override
79          public void runAsCommand() {
80                  try {
81                          switch (getNode().getNodeType()) {
82                          case Node.ATTRIBUTE_NODE:
83                                  getModifyHelper().modifyAttribute(getShell(),
84                                                  (Attr) getNode());
85                                  break;
86                          case Node.TEXT_NODE:
87                                  getModifyHelper().modifyTextNode(getShell(),
88                                                  getNode());
89                                  break;
90                          case Node.CDATA_SECTION_NODE:
91                                  getModifyHelper().modifyTextNode(getShell(),
92                                                  getNode());
93                                  break;
94                          case Node.COMMENT_NODE:
95                                  getModifyHelper().modifyComment(getShell(),
96                                                  (Comment) getNode());
97                                  break;
98                          case Node.ENTITY_NODE:
99                                  // TODO Studva entity node modification, sound
100                                 // of future
101                                 break;
102                         case Node.PROCESSING_INSTRUCTION_NODE:
103                                 getModifyHelper()
104                                                 .modifyProcessingInstruction(
105                                                                 getShell(),
106                                                                 (ProcessingInstruction) getNode());
107                                 break;
108                         }
109                 } catch (ExportException e) {
110                         handleExportException(e);
111                 }
112         }
113 }