1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor.textEditor.editParts;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.eclipse.draw2d.geometry.Point;
18 import org.eclipse.gef.DragTracker;
19 import org.eclipse.gef.EditPolicy;
20 import org.eclipse.gef.GraphicalViewer;
21 import org.eclipse.gef.Request;
22 import org.eclipse.gef.commands.Command;
23
24 import org.eclipse.gef.requests.SelectionRequest;
25
26 import sk.uniba.euromath.document.XMLAccess;
27 import sk.uniba.euromath.editor.textEditor.CaretManager;
28 import sk.uniba.euromath.editor.textEditor.ITextLocator;
29 import sk.uniba.euromath.editor.textEditor.ITextPieceContainer;
30 import sk.uniba.euromath.editor.textEditor.ITextPieceInfo;
31 import sk.uniba.euromath.editor.textEditor.ITextPieceKeeper;
32 import sk.uniba.euromath.editor.textEditor.ITextPieceSelectionStatus;
33 import sk.uniba.euromath.editor.textEditor.Interval;
34 import sk.uniba.euromath.editor.textEditor.TextPieceContainer;
35 import sk.uniba.euromath.editor.textEditor.TextPieceSelectionStatusImpl;
36 import sk.uniba.euromath.editor.textEditor.policies.TextEditPolicy;
37 import sk.uniba.euromath.editor.textEditor.requests.selectionRequest.TextSelectionRequest;
38 import sk.uniba.euromath.editor.textEditor.tools.TextDragTracker;
39 import sk.uniba.euromath.editor.xmlEditor.editParts.XMLEditPart;
40
41 /***
42 * EditPart that represents one line of text. Has also editing capabilities, but
43 * editing does nothing, supposed to be overriden.
44 *
45 * @author Tomáš Studva, Martin Kollar 1.10.2005
46 */
47 public class TextEditPart extends XMLEditPart implements ITextPieceKeeper
48 {
49
50 /***
51 * Container containing this keeper.
52 */
53 private TextPieceContainer textContainer;
54
55 /***
56 * Info about keeped text piece.
57 */
58 private ITextPieceInfo textPieceInfo;
59
60 /***
61 * Manager that cares about Caret
62 */
63 private final CaretManager caretManager;
64
65 /***
66 * Selection status about keeped text piece.
67 */
68 private TextPieceSelectionStatusImpl selStatus;
69
70 /***
71 * Graphical viewer reference.
72 */
73 private final GraphicalViewer graphicalViewer;
74
75 /***
76 * XmlAccess instance.
77 */
78 private final XMLAccess xmlAccess;
79
80 /***
81 * Constructor.
82 *
83 * @param figure
84 * displaying text piece
85 * @param cManager CaretManager
86 * @param graphicalViewer
87 * graphical viewer reference
88 * @param xmlAccess
89 * xml access reference
90 * @param selectable
91 * flag if is selectable
92 */
93 public TextEditPart(ITextLocator figure, CaretManager cManager,
94 GraphicalViewer graphicalViewer, XMLAccess xmlAccess,
95 Boolean selectable) {
96 super(figure, selectable);
97 this.caretManager = cManager;
98 this.graphicalViewer = graphicalViewer;
99 this.xmlAccess = xmlAccess;
100 }
101
102 /***
103 * @see org.eclipse.gef.EditPart#getDragTracker(org.eclipse.gef.Request)
104 * @return TextDragTracker or <code>null</code>
105 */
106 @Override
107 public DragTracker getDragTracker(Request request) {
108 Point location = null;
109 if (request instanceof TextSelectionRequest)
110 location = ((SelectionRequest) request).getLocation();
111
112 if (getID() != null)
113 return new TextDragTracker(this, location);
114
115 return null;
116 }
117
118 /***
119 * Installs adition policies - text policies.
120 */
121 @Override
122 protected void createEditPolicies() {
123 super.createEditPolicies();
124 installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new TextEditPolicy(
125 getXmlAccess(), caretManager));
126
127
128
129 }
130
131
132 /***
133 * Returns empty list, because can't have children.
134 *
135 * @return empty list
136 */
137 @Override
138 public List getModelChildren() {
139 return new ArrayList();
140 }
141
142 /***
143 * Returns first policy that understands request.
144 *
145 * @param request
146 * to understand
147 * @return policy which understands request and null otherwise
148 */
149 protected EditPolicy getUnderstandingPolicy(Request request) {
150 EditPolicy policy = null;
151 EditPolicyIterator iterator = getEditPolicyIterator();
152 while (iterator.hasNext()) {
153 policy = iterator.next();
154 if (policy.understandsRequest(request))
155 return policy;
156 }
157 return null;
158 }
159
160 /***
161 * Finds Understaning policy, gets command from this policy and put it to
162 * command stack for execute.
163 *
164 * @param req
165 * Request
166 */
167 @Override
168 public void performRequest(Request req) {
169 EditPolicy policy = getUnderstandingPolicy(req);
170
171 if (policy != null) {
172 Command cmd = policy.getCommand(req);
173 getViewer().getEditDomain().getCommandStack().execute(cmd);
174 } else {
175 super.performRequest(req);
176 }
177 }
178
179 /***
180 * Returns text hold by this ITextPieceKeeper which is the same as text
181 * displayed by figure of this editpart.
182 *
183 * @return text displayed by figure of this editpart
184 */
185 public String getText() {
186 return getTextLocator().getText();
187 }
188
189 /***
190 *
191 * @see sk.uniba.euromath.editor.textEditor.ITextPieceKeeper#getTextLocator()
192 * @return <code>TextAreaFigure</code> that is ITextLocator
193 */
194 public ITextLocator getTextLocator() {
195 return (ITextLocator) getFigure();
196 }
197
198 /***
199 * @see sk.uniba.euromath.editor.textEditor.ITextPieceKeeper#getTextPieceInfo()
200 */
201 public ITextPieceInfo getTextPieceInfo() {
202 if (this.textPieceInfo == null)
203 throw new IllegalStateException("ITextPieceInfo cannot be null.");
204 return this.textPieceInfo;
205 }
206
207 /***
208 * @see ITextPieceSelectionStatus#getStatus()
209 * @return TextPieceSelectionStatusImpl
210 */
211 public ITextPieceSelectionStatus getSelectionStatus() {
212 return this.selStatus;
213 }
214
215 /***
216 * @see sk.uniba.euromath.editor.textEditor.ITextPieceKeeper#select()
217 */
218 public void select() {
219 select(0, getText().length());
220 }
221
222 /***
223 * @see sk.uniba.euromath.editor.textEditor.ITextPieceKeeper#select(int,
224 * int)
225 */
226 public void select(int start, int end) {
227 start = Math.max(0, start);
228 end = Math.min(getText().length(), end);
229 getSelectionStatus().addSelectionInterval(new Interval(start,end));
230 fireSelectionChanged();
231 }
232
233 /***
234 * @see sk.uniba.euromath.editor.textEditor.ITextPieceKeeper#deselect()
235 */
236 public void deselect() {
237 this.selStatus.clear();
238 fireSelectionChanged();
239 }
240
241 /***
242 * Getter for graphical viewer.
243 *
244 * @return graphical viewer
245 */
246 protected GraphicalViewer getGraphicalViewer() {
247 return this.graphicalViewer;
248 }
249
250 /***
251 * @see sk.uniba.euromath.editor.textEditor.ITextPieceKeeper#getTextContainer()
252 */
253 public ITextPieceContainer getTextContainer() {
254 return this.textContainer;
255 }
256
257 /***
258 * Sets text container and text info from that container.
259 *
260 * @param textContainer
261 */
262 public void setTextContainer(TextPieceContainer textContainer) {
263 this.textContainer = textContainer;
264 setTextPieceInfo(textContainer.getInfo(this));
265 this.selStatus = new TextPieceSelectionStatusImpl(this);
266
267 }
268
269 /***
270 * Sets piece info for this TextEditPart - TextPieceKeeper. Visibility is
271 * reduced, because of consistency with info in TextPieceContainer.
272 *
273 * @param textPieceInfo
274 * to set
275 */
276 private void setTextPieceInfo(ITextPieceInfo textPieceInfo) {
277 this.textPieceInfo = textPieceInfo;
278 }
279
280 /***
281 * @see sk.uniba.euromath.editor.textEditor.ITextPieceKeeper#getPreviousText()
282 */
283 public String getPreviousText() {
284 ITextPieceContainer container = getTextContainer();
285 int index = container.indexOf(this);
286 if (index < 0)
287 throw new IllegalStateException(
288 "Container is in illegal state, because doesn't conatain this piece keeper, but should.");
289 StringBuilder result = new StringBuilder();
290 for (int i = 0; i < index; i++) {
291 result.append(container.getPiece(i).getText());
292 }
293 return result.toString();
294 }
295
296 /***
297 * @return Text in the Container, that should by not very different from
298 * text in TextNode
299 */
300 public String getWholeContainerText() {
301 return getTextContainer().getWholeContainerText();
302 }
303
304 /***
305 * @see sk.uniba.euromath.editor.textEditor.ITextPieceKeeper#getWholeTextToPosition(int)
306 * @param index
307 * local position in text
308 * @return substring of textual node's text, with start index 0 to (offset
309 * of first character of keeper's text + position) in node's text.
310 */
311 public String getWholeTextToPosition(int index) {
312 StringBuilder result = new StringBuilder(getPreviousText());
313 result.append(getText().substring(0, index));
314 return result.toString();
315 }
316
317
318 /***
319 * @return Returns the xmlAccess.
320 */
321 protected XMLAccess getXmlAccess() {
322 return this.xmlAccess;
323 }
324
325 }