1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor.textEditor.commands;
13
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.gef.commands.Command;
16 import org.w3c.dom.Node;
17 import sk.baka.ikslibs.levelmapper.NodeListID;
18 import sk.baka.ikslibs.ptr.DomPointer;
19 import sk.baka.xml.gene.ExportException;
20 import sk.uniba.euromath.EuroMath;
21 import sk.uniba.euromath.document.XMLAccess;
22 import sk.uniba.euromath.editor.textEditor.ITextPieceKeeper;
23
24 /***
25 * Command to insert text. Only to delete. Use canExecute to test if deletin can
26 * be performed.
27 *
28 * @author Tomáš Studva 1.10.2005
29 */
30 public class InsertTextCommand extends Command {
31
32 /***
33 * Text to insert.
34 */
35 private final String text;
36
37 /***
38 * XMLAccess instance.
39 */
40 private final XMLAccess xmlAccess;
41
42 /***
43 * Pointer to xml, where to insert text.
44 */
45 private final DomPointer insertPointer;
46
47 /***
48 * Constructor. Target of insertion = piece keeper's text.
49 *
50 * @param pieceKeeper
51 * pieceKeeper holding text where insertion will be done
52 * @param position
53 * position of insertion in <b>pieceKeeper's text</b>
54 * @param textToInsert
55 * text to insert
56 * @param xmlAccess
57 * XMLAccess instance
58 */
59 public InsertTextCommand(ITextPieceKeeper pieceKeeper, int position,
60 String textToInsert, XMLAccess xmlAccess) {
61 if ((position < 0) || (position > pieceKeeper.getText().length()))
62 throw new IllegalArgumentException("position");
63 this.text = textToInsert;
64 this.xmlAccess = xmlAccess;
65
66 String id = pieceKeeper.getTextPieceInfo().getNodeID();
67
68 NodeListID textNode = this.xmlAccess.getIdManager().getNodeNull(id);
69 assert textNode != null;
70 assert textNode.isTextual();
71
72
73 String prevText = pieceKeeper.getWholeTextToPosition(position);
74 int nodeTextPosition = textNode.resolveIndex(prevText, prevText
75 .length());
76
77 this.insertPointer = textNode.getPointer(nodeTextPosition, true, true);
78
79 }
80
81 /***
82 * Constructor. Target of insertion = textual node's text.
83 *
84 * @param id
85 * of textual node holding text where insertion will be done
86 * @param position
87 * position of insertion in <b>textual node's text</c>
88 * @param textToInsert
89 * text to insert
90 * @param xmlAccess
91 * XMLAccess instance
92 */
93 public InsertTextCommand(String id, int position, String textToInsert,
94 XMLAccess xmlAccess) {
95 this.text = textToInsert;
96 this.xmlAccess = xmlAccess;
97
98 NodeListID textNode = this.xmlAccess.getIdManager().getNodeNull(id);
99 assert textNode != null;
100 assert textNode.isTextual();
101
102 this.insertPointer = textNode.getPointer(position, true, true);
103
104 }
105
106 @Override
107 public void execute() {
108 if (!canExecute())
109 throw new IllegalArgumentException(
110 "Illegal call of execute method. Command cannot be executed.");
111
112 this.xmlAccess.getModifier().startModify();
113 this.xmlAccess.getModifier().insertText(this.insertPointer.ip,
114 this.insertPointer.parentElement, this.text, Node.TEXT_NODE);
115
116 try {
117 this.xmlAccess.getModifier().endModify();
118 } catch (ExportException ex) {
119 EuroMath.log(IStatus.ERROR, 0, "Error during transformation", ex);
120 }
121 }
122
123 @Override
124 public boolean canExecute() {
125 return !this.insertPointer.inEntity();
126
127 }
128 }