1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor.xmlEditor.actions;
13
14 import java.util.List;
15
16 import org.eclipse.ui.IWorkbenchPart;
17 import org.w3c.dom.Node;
18
19 import sk.baka.ikslibs.DOMUtils;
20
21 /***
22 * Common action for manipulating single node gained from:
23 * <ul>
24 * <li>selection</li>
25 * <li>contructor - if node was specified in constructor, that node is taken
26 * into account, so selection is not handled.</li>
27 * </ul>
28 * How node is gained from selection, is specified by type:
29 * <ul>
30 * <li>{@link NodeManipulateAction#SINGLE} - selection must contain one node
31 * only (otherwise action is disabled), so which is taken for manipulation</li>
32 * <li>{@link NodeManipulateAction#FIRST} - as node for manipualtion is taken
33 * first node in selection</li>
34 * <li>{@link NodeManipulateAction#LAST} - as node for manipualtion is taken
35 * last node in selection</li>
36 * </ul>
37 * and into account is taken way of partially selected nodes processing (this
38 * option is set in constructor):<br>
39 * <ul>
40 * <li>if selection contains some partially selected nodes, then action is
41 * disabled</li>
42 * <li>if selection contains some partially selected nodes, they are proccessed
43 * as completelly selected nodes</li>
44 * </ul>
45 */
46 public abstract class NodeManipulateAction extends XMLAccessModifyAction {
47
48 /***
49 * Ways of how node for manipulation is acquired.
50 *
51 * @author Tomáš Studva 18.8.2006
52 */
53 public enum NodeAcquireType {
54 /***
55 * As node for manipulation is taken first node in selection.
56 */
57 First,
58
59 /***
60 * As node for manipualtion is taken last node in selection.
61 */
62 Last,
63
64 /***
65 * As node for manipulation is taken node specified in
66 * constructor.
67 */
68 Static,
69
70 /***
71 * Selection must contain one node only (otherwise action is
72 * disabled), so which is taken for manipulation.
73 */
74 Single
75 }
76
77 /***
78 * Node to manipulate from selection or from constructor.
79 */
80 private Node node;
81
82 /***
83 * Type of how node for manipulation is gained. One of
84 */
85 private NodeAcquireType type;
86
87 /***
88 * If to work and be enabled only when selection contains nodes, which
89 * are wholly selected
90 * <ul>
91 * <li>true - if selection contains some partially selected nodes, then
92 * action is disabled</li>
93 * <li>false - if selection contains some partially selected nodes,
94 * they are proccessed as completelly selected nodes</li>
95 * </ul>
96 */
97 private boolean wholeNodesOnly = false;
98
99 /***
100 * Constructor. Creates action manipulating with node from selection.
101 * Type must be later specified.
102 *
103 * @param part
104 * associated workbench part
105 */
106 public NodeManipulateAction(IWorkbenchPart part) {
107 super(part);
108 }
109
110 /***
111 * Constructor. Creates action manipulating with node from selection.
112 *
113 * @param part
114 * associated workbench part
115 * @param type
116 * one of {@link NodeAcquireType#First},
117 * {@link NodeAcquireType#Last},
118 * {@link NodeAcquireType#Single}
119 * @param wholeNodesOnly
120 * !!! NOT IMPLEMENTED YET TODO STUDVA If to work and be
121 * enabled only when selection contains nodes, which are
122 * wholly selected
123 * <ul>
124 * <li>true - if selection contains some partially
125 * selected nodes, then action is disabled</li>
126 * <li>false - if selection contains some partially
127 * selected nodes, they are proccessed as completelly
128 * selected nodes</li>
129 * </ul>
130 */
131 public NodeManipulateAction(IWorkbenchPart part, NodeAcquireType type,
132 boolean wholeNodesOnly) {
133 super(part);
134 this.type = type;
135 this.wholeNodesOnly = wholeNodesOnly;
136 }
137
138 /***
139 * Constructor. Creates action manipulating with node node.
140 *
141 * @param node
142 * node to manipulate, must not be null
143 * @param part
144 * associated workbench part
145 */
146 public NodeManipulateAction(Node node, IWorkbenchPart part) {
147 super(part);
148 if (node == null) {
149 throw new IllegalArgumentException();
150 }
151 this.node = node;
152 this.type = NodeAcquireType.Static;
153 }
154
155 /***
156 * Is enabled if and only if node is not null (includes also case of
157 * wholeNodesOnly condition violation).
158 */
159 @Override
160 protected boolean calculateEnabled() {
161 return super.calculateEnabled()
162 && (this.node != null) && !(this.type.equals(NodeAcquireType.Single) && getSelection()
163 .getContents().size() != 1);
164 }
165
166 /***
167 * Returns node to manipulate with, which can be node from constructor
168 * or node processed from selection.
169 *
170 * @return node to manipulate or nullif action is disabled
171 */
172 public Node getNode() {
173 return this.node;
174 }
175
176 /***
177 * Processes selection, <b>if</b> should manipulate with node from
178 * selection. Calculates node to manipulate from selection.
179 */
180 @Override
181 protected void processSelection() {
182 super.processSelection();
183 if (isStatic()) {
184 return;
185 }
186
187 if (getSelection().isEmpty()) {
188 this.node = null;
189 return;
190 }
191
192
193 List<Node> nodes = getSelection().getContents();
194
195 switch (this.type) {
196 case Single: {
197
198
199 if (nodes.size() != 1) {
200 break;
201 }
202 this.node = nodes.get(0);
203 break;
204 }
205 case First: {
206 DOMUtils.sortByDocumentOrder(nodes);
207 this.node = nodes.get(0);
208 break;
209 }
210 case Last: {
211 DOMUtils.sortByDocumentOrder(nodes);
212 this.node = nodes.get(nodes.size() - 1);
213 break;
214 }
215 }
216 }
217
218 /***
219 * True, if node to manipulate was gained from contructor - was
220 * statically gained.
221 */
222 protected boolean isStatic() {
223 return this.type == NodeAcquireType.Static;
224 }
225
226 /***
227 * Getter for type how node for manipulation is gained.
228 */
229 public NodeAcquireType getType() {
230 return this.type;
231 }
232
233 /***
234 * Setter for type how node for manipulation is gained. One of
235 * {@link NodeAcquireType#First}, {@link NodeAcquireType#Last},
236 * {@link NodeAcquireType#Single}. {@link NodeAcquireType#Static} is
237 * not allowed, use instead contructor.
238 */
239 public void setType(NodeAcquireType type) {
240 if (type == NodeAcquireType.Static)
241 throw new IllegalArgumentException();
242 this.type = type;
243 }
244
245 }