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.EnumSet;
15  
16  import org.eclipse.ui.IWorkbenchPart;
17  import org.w3c.dom.Node;
18  
19  import sk.baka.ikslibs.ptr.DomPointer;
20  import sk.baka.ikslibs.ptr.DomPointerFactory;
21  import sk.baka.ikslibs.ptr.DomPointerFlag;
22  import sk.baka.xml.gene.ExportException;
23  import sk.uniba.euromath.editor.wizards.document.ElementLoc;
24  import sk.uniba.euromath.editor.wizards.document.InsertElementWizardProvider;
25  
26  /***
27   * Common action to insert node by wizard. Node type is specified in costructor
28   * and as constants are used org.w3c.dom.Node types except attribute:
29   * <ul>
30   * <li>{@link Node#ELEMENT_NODE}</li>
31   * <li>{@link Node#TEXT_NODE}</li>
32   * <li> {@link Node#CDATA_SECTION_NODE},
33   * <li>{@link Node#COMMENT_NODE}</li>
34   * <li>{@link Node#PROCESSING_INSTRUCTION_NODE}</li>
35   * <li>{@link Node#ENTITY_REFERENCE_NODE}</li>
36   * </ul>
37   * Place where to insert node is one of:
38   * <ul>
39   * <li>{@link #BEFORE}</li>
40   * <li>{@link #AFTER}</li>
41   * <li>{@link #AS_FIRST_CHILD}</li>
42   * </ul>
43   * Concrete typed node parameters to make insertion are specified in ui wizard
44   * by user or in constructor by developer.
45   * 
46   * @author Tomáš Studva 6.7.2005
47   */
48  public class InsertNodeAction extends NodeManipulateAction {
49  
50          /***
51           * Position. Inserts node before fisrt selected node.
52           */
53          public static final short BEFORE = 1;
54  
55          /***
56           * Position. Inserts node after last selected node.
57           */
58          public static final short AFTER = 2;
59  
60          /***
61           * Position. Inserts node as first child of selected node(if more nodes
62           * are selected, action is disabled).
63           */
64          public static final short AS_FIRST_CHILD = 3;
65  
66          /***
67           * Points to place whre insertion will occur.
68           */
69          private DomPointer pointer;
70  
71          /***
72           * Type of node to insert.
73           */
74          private final short nodeType;
75  
76          /***
77           * Place where to insert node, one of:
78           * <ul>
79           * <li>{@link #BEFORE}</li>
80           * <li>{@link #AFTER}</li>
81           * <li>{@link #AS_FIRST_CHILD}</li>
82           * </ul>
83           */
84          private final short position;
85  
86          /***
87           * Constructor to insert node by wizard.
88           * 
89           * @param part
90           *                associated workbench part
91           * @param nodeType
92           *                org.w3c.dom.Node types except attribute:
93           *                <ul>
94           *                <li>{@link Node#ELEMENT_NODE}</li>
95           *                <li>{@link Node#TEXT_NODE}</li>
96           *                <li>{@link Node#CDATA_SECTION_NODE}<li>
97           *                <li>{@link Node#COMMENT_NODE}</li>
98           *                <li>{@link Node#PROCESSING_INSTRUCTION_NODE}</li>
99           *                <li>{@link Node#ENTITY_REFERENCE_NODE}</li>
100          *                </ul>
101          * @param position
102          *                place where to insert node, one of:
103          *                <ul>
104          *                <li>{@link #BEFORE}</li>
105          *                <li>{@link #AFTER}</li>
106          *                <li>{@link #AS_FIRST_CHILD}</li>
107          *                </ul>
108          */
109         public InsertNodeAction(short nodeType,
110                         short position, IWorkbenchPart part) {
111                 super(part);
112                 switch (position) {
113                 case InsertNodeAction.BEFORE:
114                         setType(NodeManipulateAction.FIRST);
115                         this.position = position;
116                         break;
117                 case InsertNodeAction.AFTER:
118                         setType(NodeManipulateAction.LAST);
119                         this.position = position;
120                         break;
121                 case InsertNodeAction.AS_FIRST_CHILD:
122                         setType(NodeManipulateAction.SINGLE);
123                         this.position = position;
124                         break;
125                 default:
126                         throw new IllegalArgumentException(
127                                         "Position is invalid.");
128                 }
129 
130                 this.nodeType = nodeType;
131         }
132 
133         /***
134          * Constructor to insert node.
135          * 
136          * @param node
137          *                node to insert
138          * @param position
139          *                place where to insert node, one of:
140          *                <ul>
141          *                <li>{@link #BEFORE}</li>
142          *                <li>{@link #AFTER}</li>
143          *                <li>{@link #AS_FIRST_CHILD}</li>
144          *                </ul>
145          * @param part
146          *                associated workbench part
147          */
148         public InsertNodeAction(Node node, short position, IWorkbenchPart part) {
149                 super(node, part);
150                 this.position = position;
151                 this.nodeType = node.getNodeType();
152         }
153 
154         @Override
155         protected void clear() {
156                 super.clear();
157                 if (!isStatic())
158                         setPointer(null);
159         }
160 
161         /***
162          * Enabled if pointer is not null.
163          */
164         @Override
165         protected boolean calculateEnabled() {
166                 return super.calculateEnabled() && (getPointer() != null);
167         }
168 
169         /***
170          * Calculates node for insertion (if is not static) and calculates
171          * position.
172          */
173         @Override
174         protected void processSelection() {
175                 super.processSelection();
176                 if ((getNode() == null)
177                                 || getNode().getNodeType() == Node.ATTRIBUTE_NODE)
178                         return;
179 
180                 switch (position) {
181                 case InsertNodeAction.BEFORE:
182                         if (getNode().getParentNode() == null)
183                                 return;
184                         setPointer(DomPointerFactory.create(getNode()));
185                         break;
186                 case InsertNodeAction.AFTER:
187                         if (getNode().getParentNode() == null)
188                                 return;
189                         setPointer(DomPointerFactory.create(getNode()
190                                         .getParentNode(), getNode()
191                                         .getNextSibling()));
192                         break;
193                 case InsertNodeAction.AS_FIRST_CHILD:
194                         // TODO STUDVA
195                         setPointer(DomPointerFactory
196                                         .createFirstIn(
197                                                         getNode(),
198                                                         EnumSet
199                                                                         .of(DomPointerFlag.NotNullParentElement)));
200                         break;
201                 default:
202                         throw new IllegalArgumentException(
203                                         "Position is invalid.");
204                 }
205         }
206 
207         /***
208          * Runs wizard and inserts node.
209          */
210         @Override
211         public void run() {
212                 // TODO STUDVA
213                 try {
214                         switch (this.nodeType) {
215                         case Node.ELEMENT_NODE:
216                                 ElementLoc loc = InsertElementWizardProvider
217                                                 .execute(
218                                                                 getShell(),
219                                                                 getXMLAccess(),
220                                                                 getPointer(),
221                                                                 getXMLAccess()
222                                                                                 .getNsManager(),
223                                                                 null);
224                                 if (loc != null) {
225                                         getXMLAccess().getUndoManager().mark();
226                                         loc.insert();
227                                 }
228                                 break;
229                         case Node.CDATA_SECTION_NODE:
230                                 break;
231                         case Node.TEXT_NODE:
232                                 // getModifyHelper().insertTextNode(getShell(),
233                                 // getNode());
234                                 break;
235                         case Node.ENTITY_REFERENCE_NODE:
236                                 getModifyHelper().insertEntity(getShell(),
237                                                 getPointer());
238                                 break;
239                         case Node.COMMENT_NODE:
240                                 // is possible to insert anywhere
241                                 // getXMLAccess().getModifier().insertNode(new
242                                 // Node(), getPointer(), false);
243                                 break;
244                         case Node.PROCESSING_INSTRUCTION_NODE:
245                                 // is possible to insert anywhere
246                                 break;
247                         }
248                 } catch (ExportException e) {
249                         handleExportException(e);
250                 }
251         }
252 
253         /***
254          * @return Returns the pointer.
255          */
256         protected DomPointer getPointer() {
257                 return this.pointer;
258         }
259 
260         /***
261          * @param pointer
262          *                The pointer to set.
263          */
264         protected void setPointer(DomPointer pointer) {
265                 this.pointer = pointer;
266         }
267 }