1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor.xmlEditor.commands;
13
14 import java.util.List;
15
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.gef.commands.Command;
18 import org.w3c.dom.Node;
19
20 import sk.baka.ikslibs.interval.DOMInterval;
21 import sk.baka.ikslibs.interval.DOMIntervalSet;
22 import sk.baka.xml.gene.ExportException;
23 import sk.uniba.euromath.EuroMath;
24 import sk.uniba.euromath.document.XMLAccess;
25 import sk.uniba.euromath.editor.textEditor.CaretManager;
26 import sk.uniba.euromath.editor.textEditor.Direction;
27
28 /***
29 * Command to delete DOMIntervalSet from document.
30 *
31 * @author Tomáš Studva 17.8.2006
32 */
33 public class DeleteDOMIntervalSetCommand extends Command {
34 /***
35 * Set of DOM intervals to delete.
36 */
37 protected final DOMIntervalSet toDelete;
38
39 /***
40 * XMLAccess instance.
41 */
42 protected final XMLAccess xmlAccess;
43
44 /***
45 * Manager of caret.
46 */
47 protected final CaretManager caretManager;
48
49 protected int caretMovement;
50
51 protected final Direction direction;
52
53 /***
54 * Constructor.
55 *
56 * @param toDelete
57 * DOMintervalSet to delete.
58 */
59 public DeleteDOMIntervalSetCommand(DOMIntervalSet toDelete,
60 XMLAccess xmlAccess, CaretManager caretManager,
61 Direction direction, int caretMovement) {
62 this.toDelete = toDelete;
63 this.xmlAccess = xmlAccess;
64 this.caretManager = caretManager;
65 assert (caretMovement > 0);
66 this.caretMovement = caretMovement;
67 this.direction = direction;
68 }
69
70 /***
71 * Constructor.
72 *
73 * @param toDelete
74 * DOMintervalSet to delete.
75 */
76 public DeleteDOMIntervalSetCommand(DOMIntervalSet toDelete,
77 XMLAccess xmlAccess, CaretManager caretManager) {
78 this.toDelete = toDelete;
79 this.xmlAccess = xmlAccess;
80 this.caretManager = caretManager;
81 this.caretMovement = 0;
82 this.direction = Direction.Left;
83 }
84
85 /***
86 * Tests if document schema allows interval to be deleted.
87 */
88 private boolean canDelete(DOMInterval interval) {
89 List<Node> nodes = interval.getContents();
90 for (Node node : nodes) {
91
92 }
93 return true;
94 }
95
96
97
98
99
100
101 @Override
102 public boolean canExecute() {
103 boolean result = true;
104 for (DOMInterval interval : this.toDelete) {
105 result = result & canDelete(interval);
106 }
107 return result;
108 }
109
110
111
112
113
114
115 @Override
116 public void execute() {
117 if (this.caretMovement > 0)
118 this.caretManager.moveCaret(direction,
119 this.caretMovement);
120 this.xmlAccess.getModifier().startModify();
121 this.toDelete.deleteContents(true);
122 try {
123 this.xmlAccess.getModifier().endModify();
124 } catch (ExportException e) {
125 EuroMath.log(IStatus.ERROR, 0,
126 "Error in deletion in document.", e);
127 }
128 }
129
130 @Override
131 public void undo() {
132 this.xmlAccess.getModifier().startModify();
133 this.xmlAccess.getUndoManager().undo();
134 try {
135 this.xmlAccess.getModifier().endModify();
136 } catch (ExportException ex) {
137 EuroMath.log(IStatus.ERROR, 0, "Error when undoing.",
138 ex);
139 }
140 if (this.caretMovement > 0)
141 this.caretManager.moveCaret(Direction
142 .getOpposite(direction),
143 this.caretMovement);
144 }
145
146 @Override
147 public void redo() {
148 if (this.caretMovement > 0)
149 this.caretManager.moveCaret(direction,
150 this.caretMovement);
151 this.xmlAccess.getModifier().startModify();
152 this.xmlAccess.getUndoManager().redo();
153 try {
154 this.xmlAccess.getModifier().endModify();
155 } catch (ExportException ex) {
156 EuroMath.log(IStatus.ERROR, 0, "Error when undoing.",
157 ex);
158 }
159 }
160 }