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.interval.DOMIntervalSet;
20  import sk.baka.ikslibs.ptr.DomPointer;
21  import sk.baka.ikslibs.ptr.DomPointerFactory;
22  import sk.baka.ikslibs.ptr.DomPointerFlag;
23  
24  /***
25   * Common insertion action, needs to be subclassed. Place where to insert node
26   * is any of InsertPosition.
27   * 
28   * @author Tomáš Studva 6.7.2005
29   */
30  public abstract class InsertAction extends NodeManipulateAction {
31  
32          /***
33           * Points to place where insertion will occur.
34           */
35          private DomPointer pointer;
36  
37          /***
38           * Place where to insert node.
39           */
40          private InsertPosition position;
41  
42          /***
43           * Constructor.
44           * 
45           * @param part
46           *                associated workbench part
47           * @param position
48           *                place where to insert node
49           */
50          public InsertAction(InsertPosition position, IWorkbenchPart part) {
51                  super(part);
52                  switch (position) {
53                  case Before:
54                          setType(NodeAcquireType.First);
55                          break;
56                  case After:
57                          setType(NodeAcquireType.Last);
58                          break;
59                  case AsFirstChild:
60                          setType(NodeAcquireType.Single);
61                          break;
62                  case AtCaret:
63                          // doesn't matter, not used
64                          setType(NodeAcquireType.Single);
65                          break;
66                  }
67                  this.position = position;
68          }
69  
70          /***
71           * Constructor.
72           * 
73           * @param nodePosition
74           *                insertion will be near this node
75           * @param position
76           *                place where to insert node, one of:
77           *                <ul>
78           *                <li>{@link InsertPosition#Before} - before
79           *                nodePosition</li>
80           *                <li>{@link InsertPosition#After} - after nodePosition</li>
81           *                <li>{@link InsertPosition#AsFirstChild} - as child of
82           *                nodePosition, which must be element</li>
83           *                </ul>
84           * @param part
85           *                associated workbench part
86           */
87          public InsertAction(Node nodePosition, InsertPosition position,
88                          IWorkbenchPart part) {
89                  super(nodePosition, part);
90                  switch (position) {
91                  case Before:
92                          break;
93                  case After:
94                          break;
95                  case AsFirstChild:
96                          if (nodePosition.getNodeType() != Node.ELEMENT_NODE)
97                                  throw new IllegalArgumentException();
98                          break;
99                  case AtCaret:
100                         throw new IllegalArgumentException();
101                 }
102                 this.position = position;
103         }
104 
105         /***
106          * Enabled if pointer is not null.
107          */
108         @Override
109         protected boolean calculateEnabled() {
110                 if (getSelectionProvider() != null) {
111                         this.selection = getSelectionProvider()
112                                         .getDOMSelection();
113                 } else {
114                         this.selection = new DOMIntervalSet();
115                 }
116                 processSelection();
117 
118                 boolean res = (getXMLAccess() != null)
119                                 && (getPointer() != null)
120                                 && (getPointer().parent.getNodeType() != Node.DOCUMENT_NODE)
121                                 && !(this.position
122                                                 .equals(InsertPosition.AtCaret) && !getSelection()
123                                                 .isEmpty());
124                 return res;
125         }
126 
127         /***
128          * Calculates node for insertion (if is not static) and calculates
129          * position.
130          */
131         @Override
132         protected void processSelection() {
133                 super.processSelection();
134 
135                 switch (this.position) {
136                 case Before:
137                         if ((getNode() == null)
138                                         || getNode().getNodeType() == Node.ATTRIBUTE_NODE
139                                         || getNode().getParentNode() == null) {
140                                 return;
141                         }
142                         this.pointer = DomPointerFactory.create(getNode());
143                         break;
144                 case After:
145                         if ((getNode() == null)
146                                         || getNode().getNodeType() == Node.ATTRIBUTE_NODE
147                                         || getNode().getParentNode() == null) {
148                                 return;
149                         }
150                         this.pointer = DomPointerFactory.create(getNode()
151                                         .getParentNode(), getNode()
152                                         .getNextSibling());
153                         break;
154                 case AsFirstChild:
155                         if ((getNode() == null)
156                                         || getNode().getNodeType() != Node.ELEMENT_NODE) {
157                                 return;
158                         }
159                         this.pointer = DomPointerFactory
160                                         .createFirstIn(
161                                                         getNode(),
162                                                         EnumSet
163                                                                         .of(DomPointerFlag.NotNullParentElement));
164                         break;
165                 case AtCaret:
166                         break;
167                 }
168         }
169 
170         /***
171          * Returns pointer where to insert node.
172          * 
173          * @return Returns the pointer.
174          */
175         protected DomPointer getPointer() {
176                 if (this.position.equals(InsertPosition.AtCaret))
177                         return super.getPointer();
178                 return this.pointer;
179         }
180 
181         @Override
182         public String toString() {
183                 return "InsertAction [" + "text: " + this.getText()
184                                 + " pointer: " + this.pointer + " enabled: "
185                                 + calculateEnabled() + "]";
186         }
187 
188         /***
189          * Setter. Changes insertion position.
190          * 
191          * @param pos to set
192          */
193         public void setInsertPosition(InsertPosition pos) {
194                 this.position = pos;
195         }
196 }