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.Node;
16  
17  import sk.baka.xml.gene.ExportException;
18  
19  /***
20   * Action to insert Text/CDATA/Comment/Processing instruction node by wizard.
21   * 
22   * @author Tomáš Studva 11.7.2006
23   */
24  public class InsertTextualNode extends InsertAction {
25          /***
26           * Id of action.
27           */
28          public static final String id = InsertTextualNode.class.toString();
29  
30          private final short nodeType;
31  
32          /***
33           * Constructor to insert Text/CDATA/Entity/Comment/Processing
34           * instruction node by wizard.
35           * 
36           * @param part
37           *                associated workbench part
38           * @param nodeType
39           *                org.w3c.dom.Node types
40           *                <ul>
41           *                <li>{@link Node#TEXT_NODE}</li>
42           *                <li>{@link Node#CDATA_SECTION_NODE}
43           *                <li>{@link Node#COMMENT_NODE}</li>
44           *                <li>{@link Node#PROCESSING_INSTRUCTION_NODE}</li>
45           *                </ul>
46           * @param position
47           *                place where to insert node:
48           */
49          public InsertTextualNode(short nodeType, InsertPosition position,
50                          IWorkbenchPart part) {
51                  super(position, part);
52                  setId(id + Short.toString(nodeType));
53                  // set text
54                  switch (nodeType) {
55                  case Node.CDATA_SECTION_NODE:
56                          setText("CDATA section");
57                          break;
58                  case Node.TEXT_NODE:
59                          setText("Text");
60                          break;
61                  case Node.COMMENT_NODE:
62                          setText("Comment");
63                          break;
64                  case Node.PROCESSING_INSTRUCTION_NODE:
65                          setText("Processing instruction");
66                          break;
67                  default:
68                          throw new IllegalArgumentException();
69  
70                  }
71                  this.nodeType = nodeType;
72          }
73  
74          @Override
75          protected boolean calculateEnabled() {
76                  return super.calculateEnabled()
77                                  && (getModifyHelper().checkInEntity(
78                                                  getPointer()) == null);
79          }
80  
81          /***
82           * Runs wizard and inserts node.
83           */
84          @Override
85          public void runAsCommand() {
86                  try {
87                          switch (this.nodeType) {
88                          case Node.CDATA_SECTION_NODE:
89                                  getModifyHelper().insertTextNode(getShell(),
90                                                  getPointer(), this.nodeType);
91                                  break;
92                          case Node.TEXT_NODE:
93                                  getModifyHelper().insertTextNode(getShell(),
94                                                  getPointer(), this.nodeType);
95                                  break;
96                          case Node.COMMENT_NODE:
97                                  getModifyHelper().insertComment(getShell(),
98                                                  getPointer());
99                                  break;
100                         case Node.PROCESSING_INSTRUCTION_NODE:
101                                 getModifyHelper().insertProcessingInstruction(
102                                                 getShell(), getPointer());
103                                 break;
104                         }
105                 } catch (ExportException e) {
106                         handleExportException(e);
107                 }
108         }
109 }