|
1 /* |
|
2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 * Wrapper for JFace IDocument |
|
17 * |
|
18 */ |
|
19 package com.nokia.tracebuilder.eclipse; |
|
20 |
|
21 import java.io.File; |
|
22 |
|
23 import org.eclipse.jface.text.BadLocationException; |
|
24 import org.eclipse.jface.text.BadPositionCategoryException; |
|
25 import org.eclipse.jface.text.IDocument; |
|
26 import org.eclipse.ui.IEditorInput; |
|
27 import org.eclipse.ui.texteditor.ITextEditor; |
|
28 |
|
29 import com.nokia.tracebuilder.engine.TraceBuilderConfiguration; |
|
30 import com.nokia.tracebuilder.engine.TraceBuilderGlobals; |
|
31 import com.nokia.tracebuilder.engine.TraceLocation; |
|
32 import com.nokia.tracebuilder.source.SourceDocumentInterface; |
|
33 import com.nokia.tracebuilder.source.SourceErrorCodes; |
|
34 import com.nokia.tracebuilder.source.SourceLocationBase; |
|
35 import com.nokia.tracebuilder.source.SourceLocationInterface; |
|
36 import com.nokia.tracebuilder.source.SourceParserException; |
|
37 import com.nokia.tracebuilder.source.SourcePropertyProvider; |
|
38 import com.nokia.tracebuilder.source.SourceSelector; |
|
39 |
|
40 /** |
|
41 * Wrapper for JFace IDocument and Eclipse ITextEditor |
|
42 * |
|
43 */ |
|
44 final class JFaceDocumentWrapper implements SourceDocumentInterface, |
|
45 SourceSelector, SourcePropertyProvider { |
|
46 |
|
47 /** |
|
48 * Document position category |
|
49 */ |
|
50 public static final String TRACEBUILDER_POSITION_CATEGORY = "TraceBuilder.positions"; //$NON-NLS-1$ |
|
51 |
|
52 /** |
|
53 * Document |
|
54 */ |
|
55 private IDocument document; |
|
56 |
|
57 /** |
|
58 * Text editor for the document, may be null |
|
59 */ |
|
60 private ITextEditor textEditor; |
|
61 |
|
62 /** |
|
63 * Selection listener for the editor |
|
64 */ |
|
65 private JFaceSelectionListener selectionListener; |
|
66 |
|
67 /** |
|
68 * Change listener for the editor |
|
69 */ |
|
70 private JFaceSourceChangeListener changeListener; |
|
71 |
|
72 /** |
|
73 * Save listener |
|
74 */ |
|
75 private JFaceDirtyStateListener saveListener; |
|
76 |
|
77 /** |
|
78 * Location updater |
|
79 */ |
|
80 private JFaceLocationUpdater locationUpdater; |
|
81 |
|
82 /** |
|
83 * Owner of this source |
|
84 */ |
|
85 private Object owner; |
|
86 |
|
87 /** |
|
88 * Constructor |
|
89 * |
|
90 * @param document |
|
91 * the document |
|
92 */ |
|
93 JFaceDocumentWrapper(IDocument document) { |
|
94 this.document = document; |
|
95 } |
|
96 |
|
97 /* |
|
98 * (non-Javadoc) |
|
99 * |
|
100 * @see com.nokia.tracebuilder.source.SourceDocumentInterface#get(int, int) |
|
101 */ |
|
102 public String get(int start, int length) throws SourceParserException { |
|
103 try { |
|
104 return document.get(start, length); |
|
105 } catch (BadLocationException e) { |
|
106 throw new SourceParserException(SourceErrorCodes.BAD_LOCATION); |
|
107 } |
|
108 } |
|
109 |
|
110 /* |
|
111 * (non-Javadoc) |
|
112 * |
|
113 * @see com.nokia.tracebuilder.source.SourceDocumentInterface#getChar(int) |
|
114 */ |
|
115 public char getChar(int offset) throws SourceParserException { |
|
116 try { |
|
117 return document.getChar(offset); |
|
118 } catch (BadLocationException e) { |
|
119 throw new SourceParserException(SourceErrorCodes.BAD_LOCATION); |
|
120 } |
|
121 } |
|
122 |
|
123 /* |
|
124 * (non-Javadoc) |
|
125 * |
|
126 * @see com.nokia.tracebuilder.source.SourceDocumentInterface#getLength() |
|
127 */ |
|
128 public int getLength() { |
|
129 return document.getLength(); |
|
130 } |
|
131 |
|
132 /* |
|
133 * (non-Javadoc) |
|
134 * |
|
135 * @see com.nokia.tracebuilder.source.SourceDocumentInterface#getLineOfOffset(int) |
|
136 */ |
|
137 public int getLineOfOffset(int offset) throws SourceParserException { |
|
138 try { |
|
139 return document.getLineOfOffset(offset); |
|
140 } catch (BadLocationException e) { |
|
141 throw new SourceParserException(SourceErrorCodes.BAD_LOCATION); |
|
142 } |
|
143 } |
|
144 |
|
145 /* |
|
146 * (non-Javadoc) |
|
147 * |
|
148 * @see com.nokia.tracebuilder.source.SourceDocumentInterface#replace(int, |
|
149 * int, java.lang.String) |
|
150 */ |
|
151 public void replace(int offset, int length, String newText) |
|
152 throws SourceParserException { |
|
153 try { |
|
154 document.replace(offset, length, newText); |
|
155 } catch (BadLocationException e) { |
|
156 throw new SourceParserException(SourceErrorCodes.BAD_LOCATION); |
|
157 } |
|
158 } |
|
159 |
|
160 /* |
|
161 * (non-Javadoc) |
|
162 * |
|
163 * @see com.nokia.tracebuilder.source.SourceDocumentInterface#getPropertyProvider() |
|
164 */ |
|
165 public SourcePropertyProvider getPropertyProvider() { |
|
166 return this; |
|
167 } |
|
168 |
|
169 /* |
|
170 * (non-Javadoc) |
|
171 * |
|
172 * @see com.nokia.tracebuilder.source.SourceDocumentInterface#getSourceSelector() |
|
173 */ |
|
174 public SourceSelector getSourceSelector() { |
|
175 return this; |
|
176 } |
|
177 |
|
178 /* |
|
179 * (non-Javadoc) |
|
180 * |
|
181 * @see com.nokia.tracebuilder.source.SourceSelector#setSelection(int, int) |
|
182 */ |
|
183 public void setSelection(int offset, int length) { |
|
184 if (textEditor != null) { |
|
185 textEditor.getEditorSite().getPage().bringToTop(textEditor); |
|
186 textEditor.selectAndReveal(offset, length); |
|
187 } |
|
188 } |
|
189 |
|
190 /* |
|
191 * (non-Javadoc) |
|
192 * |
|
193 * @see com.nokia.tracebuilder.source.SourcePropertyProvider#getFileName() |
|
194 */ |
|
195 public String getFileName() { |
|
196 String retval = null; |
|
197 if (textEditor != null) { |
|
198 IEditorInput input = textEditor.getEditorInput(); |
|
199 String path = WorkbenchUtils.getEditorInputPath(input); |
|
200 if (path != null) { |
|
201 retval = new File(path).getName(); |
|
202 } |
|
203 } |
|
204 return retval; |
|
205 } |
|
206 |
|
207 /* |
|
208 * (non-Javadoc) |
|
209 * |
|
210 * @see com.nokia.tracebuilder.source.SourcePropertyProvider#getFilePath() |
|
211 */ |
|
212 public String getFilePath() { |
|
213 String retval = null; |
|
214 if (textEditor != null) { |
|
215 IEditorInput input = textEditor.getEditorInput(); |
|
216 String path = WorkbenchUtils.getEditorInputPath(input); |
|
217 if (path != null) { |
|
218 retval = new File(path).getParent(); |
|
219 } |
|
220 } |
|
221 if (retval != null && !retval.endsWith(File.separator)) { |
|
222 retval += File.separator; |
|
223 } |
|
224 return retval; |
|
225 } |
|
226 |
|
227 /* |
|
228 * (non-Javadoc) |
|
229 * |
|
230 * @see com.nokia.tracebuilder.source.SourceDocumentInterface# |
|
231 * addLocation(com.nokia.tracebuilder.source.SourceLocationInterface) |
|
232 */ |
|
233 public void addLocation(SourceLocationInterface location) { |
|
234 String category = getLocationCategory(((JFaceLocationWrapper) location) |
|
235 .getLocation()); |
|
236 try { |
|
237 if (category == null) { |
|
238 document.addPosition((JFaceLocationWrapper) location); |
|
239 } else { |
|
240 document.addPosition(category, (JFaceLocationWrapper) location); |
|
241 } |
|
242 } catch (Exception e) { |
|
243 if (TraceBuilderConfiguration.ASSERTIONS_ENABLED) { |
|
244 TraceBuilderGlobals.getEvents().postCriticalAssertionFailed( |
|
245 "Failed to add location", e); //$NON-NLS-1$ |
|
246 } |
|
247 } |
|
248 } |
|
249 |
|
250 /* |
|
251 * (non-Javadoc) |
|
252 * |
|
253 * @see com.nokia.tracebuilder.source.SourceDocumentInterface# |
|
254 * removeLocation(com.nokia.tracebuilder.source.SourceLocationInterface) |
|
255 */ |
|
256 public void removeLocation(SourceLocationInterface location) { |
|
257 String category = getLocationCategory(((JFaceLocationWrapper) location) |
|
258 .getLocation()); |
|
259 if (category == null) { |
|
260 document.removePosition((JFaceLocationWrapper) location); |
|
261 } else { |
|
262 try { |
|
263 document.removePosition(category, |
|
264 (JFaceLocationWrapper) location); |
|
265 } catch (Exception e) { |
|
266 if (TraceBuilderConfiguration.ASSERTIONS_ENABLED) { |
|
267 TraceBuilderGlobals.getEvents() |
|
268 .postCriticalAssertionFailed( |
|
269 "Failed to remove location", e); //$NON-NLS-1$ |
|
270 } |
|
271 } |
|
272 } |
|
273 } |
|
274 |
|
275 /* |
|
276 * (non-Javadoc) |
|
277 * |
|
278 * @see com.nokia.tracebuilder.source.SourceDocumentInterface#getOwner() |
|
279 */ |
|
280 public Object getOwner() { |
|
281 return owner; |
|
282 } |
|
283 |
|
284 /* |
|
285 * (non-Javadoc) |
|
286 * |
|
287 * @see com.nokia.tracebuilder.source.SourceDocumentInterface#setOwner(java.lang.Object) |
|
288 */ |
|
289 public void setOwner(Object owner) { |
|
290 this.owner = owner; |
|
291 } |
|
292 |
|
293 /** |
|
294 * Called by the workbench editor monitor to initialize listeners |
|
295 * |
|
296 * @param monitor |
|
297 * the workbench monitor |
|
298 */ |
|
299 void sourceOpened(WorkbenchEditorMonitor monitor) { |
|
300 if (monitor != null) { |
|
301 locationUpdater = new JFaceLocationUpdater( |
|
302 TRACEBUILDER_POSITION_CATEGORY); |
|
303 document.addPositionCategory(TRACEBUILDER_POSITION_CATEGORY); |
|
304 document.addPositionUpdater(locationUpdater); |
|
305 if (changeListener == null) { |
|
306 changeListener = new JFaceSourceChangeListener(monitor, this); |
|
307 document.addDocumentListener(changeListener); |
|
308 } |
|
309 if (textEditor != null && selectionListener == null) { |
|
310 selectionListener = new JFaceSelectionListener(monitor, this); |
|
311 textEditor.getSite().getPage().addPostSelectionListener( |
|
312 selectionListener); |
|
313 saveListener = new JFaceDirtyStateListener(monitor, this); |
|
314 textEditor.addPropertyListener(saveListener); |
|
315 } |
|
316 } |
|
317 } |
|
318 |
|
319 /** |
|
320 * Performs cleanup. Called by WorkbenchEditorMonitor when source is removed |
|
321 */ |
|
322 void sourceClosed() { |
|
323 if (textEditor != null) { |
|
324 if (selectionListener != null) { |
|
325 textEditor.getSite().getPage().removePostSelectionListener( |
|
326 selectionListener); |
|
327 } |
|
328 if (saveListener != null) { |
|
329 textEditor.removePropertyListener(saveListener); |
|
330 } |
|
331 } |
|
332 if (changeListener != null) { |
|
333 document.removeDocumentListener(changeListener); |
|
334 } |
|
335 document.removePositionUpdater(locationUpdater); |
|
336 try { |
|
337 document.removePositionCategory(TRACEBUILDER_POSITION_CATEGORY); |
|
338 } catch (BadPositionCategoryException e) { |
|
339 if (TraceBuilderConfiguration.ASSERTIONS_ENABLED) { |
|
340 TraceBuilderGlobals.getEvents().postAssertionFailed( |
|
341 "Failed to remove position updater", e); //$NON-NLS-1$ |
|
342 } |
|
343 } |
|
344 } |
|
345 |
|
346 /** |
|
347 * Gets the document |
|
348 * |
|
349 * @return the document |
|
350 */ |
|
351 IDocument getDocument() { |
|
352 return document; |
|
353 } |
|
354 |
|
355 /** |
|
356 * Sets the document |
|
357 * |
|
358 * @param document |
|
359 * the new document |
|
360 */ |
|
361 void setDocument(IDocument document) { |
|
362 this.document = document; |
|
363 } |
|
364 |
|
365 /** |
|
366 * Gets the text editor |
|
367 * |
|
368 * @return the text editor |
|
369 */ |
|
370 ITextEditor getTextEditor() { |
|
371 return textEditor; |
|
372 } |
|
373 |
|
374 /** |
|
375 * Sets the text editor |
|
376 * |
|
377 * @param textEditor |
|
378 * the new text editor |
|
379 */ |
|
380 void setTextEditor(ITextEditor textEditor) { |
|
381 this.textEditor = textEditor; |
|
382 } |
|
383 |
|
384 /** |
|
385 * Gets the selection listener |
|
386 * |
|
387 * @return the selection listener |
|
388 */ |
|
389 JFaceSelectionListener getSelectionListener() { |
|
390 return selectionListener; |
|
391 } |
|
392 |
|
393 /** |
|
394 * Gets a category for location |
|
395 * |
|
396 * @param location |
|
397 * the location |
|
398 * @return the category |
|
399 */ |
|
400 private String getLocationCategory(SourceLocationBase location) { |
|
401 String retval; |
|
402 if (location instanceof TraceLocation) { |
|
403 retval = TRACEBUILDER_POSITION_CATEGORY; |
|
404 } else { |
|
405 retval = null; |
|
406 } |
|
407 return retval; |
|
408 } |
|
409 |
|
410 } |