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 * Type action's const.
50 */
51 public static final int FIRST = 101;
52
53 /***
54 * Type action's const.
55 */
56 public static final int LAST = 102;
57
58 /***
59 * Type action's const.
60 */
61 public static final int FIRST_CHILD = 103;
62
63 /***
64 * Type action's const.
65 */
66 public static final int STATIC = 104;
67
68 /***
69 * Type action's const.
70 */
71 public static final int SINGLE = 105;
72
73 /***
74 * Node to manipulate from selection of from constructor.
75 */
76 private Node node;
77
78 /***
79 * Type how node for manipulation is gained. One of
80 * <ul>
81 * <li>{@link NodeManipulateAction#SINGLE} - selection must contain one
82 * node only (otherwise action is disabled), so which is taken for
83 * manipulation</li>
84 * <li>{@link NodeManipulateAction#FIRST} - as node for manipualtion is
85 * taken first node in selection</li>
86 * <li>{@link NodeManipulateAction#LAST} - as node for manipualtion is
87 * taken last node in selection</li>
88 * <li>{@link NodeManipulateAction#STATIC} - as node for manipulation
89 * is taken node specified in constructor.</li>
90 * </ul>
91 */
92 private int type;
93
94 /***
95 * If to work and be enabled only when selection contains nodes, which
96 * are wholly selected
97 * <ul>
98 * <li>true - if selection contains some partially selected nodes, then
99 * action is disabled</li>
100 * <li>false - if selection contains some partially selected nodes,
101 * they are proccessed as completelly selected nodes</li>
102 * </ul>
103 */
104 private boolean wholeNodesOnly = false;
105
106 /***
107 * Constructor. Creates action manipulating with node from selection.
108 * Type must be later specified.
109 *
110 * @param part
111 * associated workbench part
112 */
113 public NodeManipulateAction(IWorkbenchPart part) {
114 super(part);
115 }
116
117 /***
118 * Constructor. Creates action manipulating with node from selection.
119 *
120 * @param part
121 * associated workbench part
122 * @param type
123 * one of {@link NodeManipulateAction#FIRST},
124 * {@link NodeManipulateAction#LAST},
125 * {@link NodeManipulateAction#SINGLE}
126 * @param wholeNodesOnly
127 * !!! NOT IMPLEMENTED YET TODO STUDVA If to work and be
128 * enabled only when selection contains nodes, which are
129 * wholly selected
130 * <ul>
131 * <li>true - if selection contains some partially
132 * selected nodes, then action is disabled</li>
133 * <li>false - if selection contains some partially
134 * selected nodes, they are proccessed as completelly
135 * selected nodes</li>
136 * </ul>
137 */
138 public NodeManipulateAction(IWorkbenchPart part, int type,
139 boolean wholeNodesOnly) {
140 super(part);
141 setType(type);
142 this.wholeNodesOnly = wholeNodesOnly;
143 }
144
145 /***
146 * Constructor. Creates action manipulating with node node.
147 *
148 * @param node
149 * node to manipulate, must not be null
150 * @param part
151 * associated workbench part
152 */
153 public NodeManipulateAction(Node node, IWorkbenchPart part) {
154 super(part);
155 if (node == null)
156 throw new IllegalArgumentException();
157 this.node = node;
158 setType(NodeManipulateAction.STATIC);
159 }
160
161 /***
162 * Is enabled if and only if node is not null (includes also case of
163 * wholeNodesOnly condition violation).
164 */
165 @Override
166 protected boolean calculateEnabled() {
167 return super.calculateEnabled()
168 && (isStatic() || (this.node != null));
169 }
170
171
172
173
174
175
176 @Override
177 protected void clear() {
178 super.clear();
179 if (!isStatic())
180 this.node = null;
181 }
182
183 /***
184 * Returns node to manipulate with, which can be node from constructor
185 * or node processed from selection.
186 *
187 * @return node to manipulate
188 */
189 public Node getNode() {
190 return this.node;
191 }
192
193 /***
194 * Processes selection, <b>if</b> should manipulate with node from
195 * selection. Calculates node to manipulate from selection.
196 */
197 @Override
198 protected void processSelection() {
199 super.processSelection();
200 if (isStatic() || getSelection().isEmpty())
201 return;
202
203
204 List<Node> nodes = getSelection().getContents();
205
206 switch (getType()) {
207 case NodeManipulateAction.SINGLE: {
208
209
210 if (nodes.size() != 1)
211 break;
212 this.node = nodes.get(0);
213 break;
214 }
215 case NodeManipulateAction.FIRST: {
216 DOMUtils.sortByDocumentOrder(nodes);
217 this.node = nodes.get(0);
218 break;
219 }
220 case NodeManipulateAction.LAST: {
221 DOMUtils.sortByDocumentOrder(nodes);
222 this.node = nodes.get(nodes.size() - 1);
223 break;
224 }
225 default:
226 throw new IllegalStateException("Invalid type.");
227 }
228
229 }
230
231 /***
232 * True, if node to manipulate was gained from contructor - was
233 * statically gained.
234 */
235 protected boolean isStatic() {
236 return this.type == NodeManipulateAction.STATIC;
237 }
238
239 /***
240 * Getter for type how node for manipulation is gained. One of
241 * <ul>
242 * <li>{@link NodeManipulateAction#SINGLE} - selection must contain one
243 * node only (otherwise action is disabled), so which is taken for
244 * manipulation</li>
245 * <li>{@link NodeManipulateAction#FIRST} - as node for manipualtion is
246 * taken first node in selection</li>
247 * <li>{@link NodeManipulateAction#LAST} - as node for manipualtion is
248 * taken last node in selection</li>
249 * <li>{@link NodeManipulateAction#STATIC} - as node for manipulation
250 * is taken node specified in constructor.</li>
251 * </ul>
252 */
253 public int getType() {
254 return type;
255 }
256
257 /***
258 * Setter for type how node for manipulation is gained. One of
259 * <ul>
260 * <li>{@link NodeManipulateAction#SINGLE} - selection must contain one
261 * node only (otherwise action is disabled), so which is taken for
262 * manipulation</li>
263 * <li>{@link NodeManipulateAction#FIRST} - as node for manipualtion is
264 * taken first node in selection</li>
265 * <li>{@link NodeManipulateAction#LAST} - as node for manipualtion is
266 * taken last node in selection</li>
267 * </ul>
268 */
269 public void setType(int type) {
270 if ((type != NodeManipulateAction.FIRST)
271 && (type != NodeManipulateAction.LAST)
272 && (type != NodeManipulateAction.FIRST_CHILD))
273 throw new IllegalArgumentException();
274 this.type = type;
275 }
276
277 }