Mercurial > projects > dwt-addons
annotate dwtx/jface/text/TextViewer.d @ 156:a9566845f1cb
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 25 Aug 2008 19:03:46 +0200 |
parents | f70d9508c95c |
children | 7926b636c282 |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2008 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.text.TextViewer; | |
14 | |
131 | 15 import dwtx.jface.text.IDocumentPartitioningListener; // packageimport |
16 import dwtx.jface.text.DefaultTextHover; // packageimport | |
17 import dwtx.jface.text.AbstractInformationControl; // packageimport | |
18 import dwtx.jface.text.TextUtilities; // packageimport | |
19 import dwtx.jface.text.IInformationControlCreatorExtension; // packageimport | |
20 import dwtx.jface.text.AbstractInformationControlManager; // packageimport | |
21 import dwtx.jface.text.ITextViewerExtension2; // packageimport | |
22 import dwtx.jface.text.IDocumentPartitioner; // packageimport | |
23 import dwtx.jface.text.DefaultIndentLineAutoEditStrategy; // packageimport | |
24 import dwtx.jface.text.ITextSelection; // packageimport | |
25 import dwtx.jface.text.Document; // packageimport | |
26 import dwtx.jface.text.FindReplaceDocumentAdapterContentProposalProvider; // packageimport | |
27 import dwtx.jface.text.ITextListener; // packageimport | |
28 import dwtx.jface.text.BadPartitioningException; // packageimport | |
29 import dwtx.jface.text.ITextViewerExtension5; // packageimport | |
30 import dwtx.jface.text.IDocumentPartitionerExtension3; // packageimport | |
31 import dwtx.jface.text.IUndoManager; // packageimport | |
32 import dwtx.jface.text.ITextHoverExtension2; // packageimport | |
33 import dwtx.jface.text.IRepairableDocument; // packageimport | |
34 import dwtx.jface.text.IRewriteTarget; // packageimport | |
35 import dwtx.jface.text.DefaultPositionUpdater; // packageimport | |
36 import dwtx.jface.text.RewriteSessionEditProcessor; // packageimport | |
37 import dwtx.jface.text.TextViewerHoverManager; // packageimport | |
38 import dwtx.jface.text.DocumentRewriteSession; // packageimport | |
39 import dwtx.jface.text.ITextViewerExtension8; // packageimport | |
40 import dwtx.jface.text.RegExMessages; // packageimport | |
41 import dwtx.jface.text.IDelayedInputChangeProvider; // packageimport | |
42 import dwtx.jface.text.ITextOperationTargetExtension; // packageimport | |
43 import dwtx.jface.text.IWidgetTokenOwner; // packageimport | |
44 import dwtx.jface.text.IViewportListener; // packageimport | |
45 import dwtx.jface.text.GapTextStore; // packageimport | |
46 import dwtx.jface.text.MarkSelection; // packageimport | |
47 import dwtx.jface.text.IDocumentPartitioningListenerExtension; // packageimport | |
48 import dwtx.jface.text.IDocumentAdapterExtension; // packageimport | |
49 import dwtx.jface.text.IInformationControlExtension; // packageimport | |
50 import dwtx.jface.text.IDocumentPartitioningListenerExtension2; // packageimport | |
51 import dwtx.jface.text.DefaultDocumentAdapter; // packageimport | |
52 import dwtx.jface.text.ITextViewerExtension3; // packageimport | |
53 import dwtx.jface.text.IInformationControlCreator; // packageimport | |
54 import dwtx.jface.text.TypedRegion; // packageimport | |
55 import dwtx.jface.text.ISynchronizable; // packageimport | |
56 import dwtx.jface.text.IMarkRegionTarget; // packageimport | |
57 import dwtx.jface.text.TextViewerUndoManager; // packageimport | |
58 import dwtx.jface.text.IRegion; // packageimport | |
59 import dwtx.jface.text.IInformationControlExtension2; // packageimport | |
60 import dwtx.jface.text.IDocumentExtension4; // packageimport | |
61 import dwtx.jface.text.IDocumentExtension2; // packageimport | |
62 import dwtx.jface.text.IDocumentPartitionerExtension2; // packageimport | |
63 import dwtx.jface.text.Assert; // packageimport | |
64 import dwtx.jface.text.DefaultInformationControl; // packageimport | |
65 import dwtx.jface.text.IWidgetTokenOwnerExtension; // packageimport | |
66 import dwtx.jface.text.DocumentClone; // packageimport | |
67 import dwtx.jface.text.DefaultUndoManager; // packageimport | |
68 import dwtx.jface.text.IFindReplaceTarget; // packageimport | |
69 import dwtx.jface.text.IAutoEditStrategy; // packageimport | |
70 import dwtx.jface.text.ILineTrackerExtension; // packageimport | |
71 import dwtx.jface.text.IUndoManagerExtension; // packageimport | |
72 import dwtx.jface.text.TextSelection; // packageimport | |
73 import dwtx.jface.text.DefaultAutoIndentStrategy; // packageimport | |
74 import dwtx.jface.text.IAutoIndentStrategy; // packageimport | |
75 import dwtx.jface.text.IPainter; // packageimport | |
76 import dwtx.jface.text.IInformationControl; // packageimport | |
77 import dwtx.jface.text.IInformationControlExtension3; // packageimport | |
78 import dwtx.jface.text.ITextViewerExtension6; // packageimport | |
79 import dwtx.jface.text.IInformationControlExtension4; // packageimport | |
80 import dwtx.jface.text.DefaultLineTracker; // packageimport | |
81 import dwtx.jface.text.IDocumentInformationMappingExtension; // packageimport | |
82 import dwtx.jface.text.IRepairableDocumentExtension; // packageimport | |
83 import dwtx.jface.text.ITextHover; // packageimport | |
84 import dwtx.jface.text.FindReplaceDocumentAdapter; // packageimport | |
85 import dwtx.jface.text.ILineTracker; // packageimport | |
86 import dwtx.jface.text.Line; // packageimport | |
87 import dwtx.jface.text.ITextViewerExtension; // packageimport | |
88 import dwtx.jface.text.IDocumentAdapter; // packageimport | |
89 import dwtx.jface.text.TextEvent; // packageimport | |
90 import dwtx.jface.text.BadLocationException; // packageimport | |
91 import dwtx.jface.text.AbstractDocument; // packageimport | |
92 import dwtx.jface.text.AbstractLineTracker; // packageimport | |
93 import dwtx.jface.text.TreeLineTracker; // packageimport | |
94 import dwtx.jface.text.ITextPresentationListener; // packageimport | |
95 import dwtx.jface.text.Region; // packageimport | |
96 import dwtx.jface.text.ITextViewer; // packageimport | |
97 import dwtx.jface.text.IDocumentInformationMapping; // packageimport | |
98 import dwtx.jface.text.MarginPainter; // packageimport | |
99 import dwtx.jface.text.IPaintPositionManager; // packageimport | |
100 import dwtx.jface.text.TextPresentation; // packageimport | |
101 import dwtx.jface.text.IFindReplaceTargetExtension; // packageimport | |
102 import dwtx.jface.text.ISlaveDocumentManagerExtension; // packageimport | |
103 import dwtx.jface.text.ISelectionValidator; // packageimport | |
104 import dwtx.jface.text.IDocumentExtension; // packageimport | |
105 import dwtx.jface.text.PropagatingFontFieldEditor; // packageimport | |
106 import dwtx.jface.text.ConfigurableLineTracker; // packageimport | |
107 import dwtx.jface.text.SlaveDocumentEvent; // packageimport | |
108 import dwtx.jface.text.IDocumentListener; // packageimport | |
109 import dwtx.jface.text.PaintManager; // packageimport | |
110 import dwtx.jface.text.IFindReplaceTargetExtension3; // packageimport | |
111 import dwtx.jface.text.ITextDoubleClickStrategy; // packageimport | |
112 import dwtx.jface.text.IDocumentExtension3; // packageimport | |
113 import dwtx.jface.text.Position; // packageimport | |
114 import dwtx.jface.text.TextMessages; // packageimport | |
115 import dwtx.jface.text.CopyOnWriteTextStore; // packageimport | |
116 import dwtx.jface.text.WhitespaceCharacterPainter; // packageimport | |
117 import dwtx.jface.text.IPositionUpdater; // packageimport | |
118 import dwtx.jface.text.DefaultTextDoubleClickStrategy; // packageimport | |
119 import dwtx.jface.text.ListLineTracker; // packageimport | |
120 import dwtx.jface.text.ITextInputListener; // packageimport | |
121 import dwtx.jface.text.BadPositionCategoryException; // packageimport | |
122 import dwtx.jface.text.IWidgetTokenKeeperExtension; // packageimport | |
123 import dwtx.jface.text.IInputChangedListener; // packageimport | |
124 import dwtx.jface.text.ITextOperationTarget; // packageimport | |
125 import dwtx.jface.text.IDocumentInformationMappingExtension2; // packageimport | |
126 import dwtx.jface.text.ITextViewerExtension7; // packageimport | |
127 import dwtx.jface.text.IInformationControlExtension5; // packageimport | |
128 import dwtx.jface.text.IDocumentRewriteSessionListener; // packageimport | |
129 import dwtx.jface.text.JFaceTextUtil; // packageimport | |
130 import dwtx.jface.text.AbstractReusableInformationControlCreator; // packageimport | |
131 import dwtx.jface.text.TabsToSpacesConverter; // packageimport | |
132 import dwtx.jface.text.CursorLinePainter; // packageimport | |
133 import dwtx.jface.text.ITextHoverExtension; // packageimport | |
134 import dwtx.jface.text.IEventConsumer; // packageimport | |
135 import dwtx.jface.text.IDocument; // packageimport | |
136 import dwtx.jface.text.IWidgetTokenKeeper; // packageimport | |
137 import dwtx.jface.text.DocumentCommand; // packageimport | |
138 import dwtx.jface.text.TypedPosition; // packageimport | |
139 import dwtx.jface.text.IEditingSupportRegistry; // packageimport | |
140 import dwtx.jface.text.IDocumentPartitionerExtension; // packageimport | |
141 import dwtx.jface.text.AbstractHoverInformationControlManager; // packageimport | |
142 import dwtx.jface.text.IEditingSupport; // packageimport | |
143 import dwtx.jface.text.IMarkSelection; // packageimport | |
144 import dwtx.jface.text.ISlaveDocumentManager; // packageimport | |
145 import dwtx.jface.text.DocumentEvent; // packageimport | |
146 import dwtx.jface.text.DocumentPartitioningChangedEvent; // packageimport | |
147 import dwtx.jface.text.ITextStore; // packageimport | |
148 import dwtx.jface.text.JFaceTextMessages; // packageimport | |
149 import dwtx.jface.text.DocumentRewriteSessionEvent; // packageimport | |
150 import dwtx.jface.text.SequentialRewriteTextStore; // packageimport | |
151 import dwtx.jface.text.DocumentRewriteSessionType; // packageimport | |
152 import dwtx.jface.text.TextAttribute; // packageimport | |
153 import dwtx.jface.text.ITextViewerExtension4; // packageimport | |
154 import dwtx.jface.text.ITypedRegion; // packageimport | |
155 | |
156 | |
129 | 157 import dwt.dwthelper.utils; |
158 | |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
159 import dwtx.dwtxhelper.Collection; |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
160 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
161 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
162 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
163 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
164 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
165 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
166 |
129 | 167 import java.util.regex.PatternSyntaxException; |
168 | |
169 import dwt.DWT; | |
170 import dwt.custom.LineBackgroundEvent; | |
171 import dwt.custom.LineBackgroundListener; | |
172 import dwt.custom.MovementEvent; | |
173 import dwt.custom.MovementListener; | |
174 import dwt.custom.ST; | |
175 import dwt.custom.StyleRange; | |
176 import dwt.custom.StyledText; | |
177 import dwt.custom.StyledTextPrintOptions; | |
178 import dwt.custom.VerifyKeyListener; | |
179 import dwt.events.ControlEvent; | |
180 import dwt.events.ControlListener; | |
181 import dwt.events.DisposeEvent; | |
182 import dwt.events.DisposeListener; | |
183 import dwt.events.KeyEvent; | |
184 import dwt.events.KeyListener; | |
185 import dwt.events.MouseAdapter; | |
186 import dwt.events.MouseEvent; | |
187 import dwt.events.MouseListener; | |
188 import dwt.events.SelectionEvent; | |
189 import dwt.events.SelectionListener; | |
190 import dwt.events.TraverseEvent; | |
191 import dwt.events.TraverseListener; | |
192 import dwt.events.VerifyEvent; | |
193 import dwt.events.VerifyListener; | |
194 import dwt.graphics.Color; | |
195 import dwt.graphics.GC; | |
196 import dwt.graphics.Point; | |
197 import dwt.graphics.Rectangle; | |
198 import dwt.printing.PrintDialog; | |
199 import dwt.printing.Printer; | |
200 import dwt.printing.PrinterData; | |
201 import dwt.widgets.Composite; | |
202 import dwt.widgets.Control; | |
203 import dwt.widgets.Display; | |
204 import dwt.widgets.Event; | |
205 import dwt.widgets.Listener; | |
206 import dwt.widgets.ScrollBar; | |
207 import dwtx.core.runtime.Assert; | |
208 import dwtx.jface.internal.text.NonDeletingPositionUpdater; | |
209 import dwtx.jface.internal.text.StickyHoverManager; | |
210 import dwtx.jface.text.hyperlink.HyperlinkManager; | |
211 import dwtx.jface.text.hyperlink.IHyperlinkDetector; | |
212 import dwtx.jface.text.hyperlink.IHyperlinkDetectorExtension; | |
213 import dwtx.jface.text.hyperlink.IHyperlinkPresenter; | |
214 import dwtx.jface.text.hyperlink.HyperlinkManager.DETECTION_STRATEGY; | |
215 import dwtx.jface.text.projection.ChildDocument; | |
216 import dwtx.jface.text.projection.ChildDocumentManager; | |
217 import dwtx.jface.viewers.IPostSelectionProvider; | |
218 import dwtx.jface.viewers.ISelection; | |
219 import dwtx.jface.viewers.ISelectionChangedListener; | |
220 import dwtx.jface.viewers.ISelectionProvider; | |
221 import dwtx.jface.viewers.SelectionChangedEvent; | |
222 import dwtx.jface.viewers.Viewer; | |
223 | |
224 | |
225 /** | |
226 * DWT based implementation of {@link ITextViewer} and its extension interfaces. | |
227 * Once the viewer and its DWT control have been created the viewer can only | |
228 * indirectly be disposed by disposing its DWT control. | |
229 * <p> | |
230 * Clients are supposed to instantiate a text viewer and subsequently to | |
231 * communicate with it exclusively using the | |
232 * {@link dwtx.jface.text.ITextViewer} interface or any of the | |
233 * implemented extension interfaces. | |
234 * <p> | |
235 * A text viewer serves as text operation target. It only partially supports the | |
236 * external control of the enable state of its text operations. A text viewer is | |
237 * also a widget token owner. Anything that wants to display an overlay window | |
238 * on top of a text viewer should implement the | |
239 * {@link dwtx.jface.text.IWidgetTokenKeeper} interface and participate | |
240 * in the widget token negotiation between the text viewer and all its potential | |
241 * widget token keepers. | |
242 * <p> | |
243 * This class is not intended to be subclassed outside the JFace Text component.</p> | |
244 * @noextend This class is not intended to be subclassed by clients. | |
245 */ | |
246 public class TextViewer : Viewer , | |
247 ITextViewer, ITextViewerExtension, ITextViewerExtension2, ITextViewerExtension4, ITextViewerExtension6, ITextViewerExtension7, ITextViewerExtension8, | |
248 IEditingSupportRegistry, ITextOperationTarget, ITextOperationTargetExtension, | |
249 IWidgetTokenOwner, IWidgetTokenOwnerExtension, IPostSelectionProvider { | |
250 | |
251 /** Internal flag to indicate the debug state. */ | |
147 | 252 public static const bool TRACE_ERRORS= false; |
129 | 253 /** Internal flag to indicate the debug state. */ |
147 | 254 private static const bool TRACE_DOUBLE_CLICK= false; |
129 | 255 |
256 /** | |
257 * Width constraint for text hovers (in characters). | |
258 * @since 3.4 | |
259 */ | |
147 | 260 private static const int TEXT_HOVER_WIDTH_CHARS= 100; //used to be 60 (text font) |
129 | 261 /** |
262 * Height constraint for text hovers (in characters). | |
263 * @since 3.4 | |
264 */ | |
147 | 265 private static const int TEXT_HOVER_HEIGHT_CHARS= 12; //used to be 10 (text font) |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
266 |
129 | 267 /** |
268 * Represents a replace command that brings the text viewer's text widget | |
269 * back in synchronization with text viewer's document after the document | |
270 * has been changed. | |
271 */ | |
272 protected class WidgetCommand { | |
273 | |
274 /** The document event encapsulated by this command. */ | |
275 public DocumentEvent event; | |
276 /** The start of the event. */ | |
277 public int start; | |
278 /** The length of the event. */ | |
279 public int length; | |
280 /** The inserted and replaced text segments of <code>event</code>. */ | |
281 public String text; | |
282 /** The replaced text segments of <code>event</code>. */ | |
283 public String preservedText; | |
284 | |
285 /** | |
286 * Translates a document event into the presentation coordinates of this text viewer. | |
287 * | |
288 * @param e the event to be translated | |
289 */ | |
290 public void setEvent(DocumentEvent e) { | |
291 | |
292 event= e; | |
293 | |
294 start= e.getOffset(); | |
295 length= e.getLength(); | |
296 text= e.getText(); | |
297 | |
298 if (length !is 0) { | |
299 try { | |
300 | |
138 | 301 if ( cast(SlaveDocumentEvent)e ) { |
134 | 302 SlaveDocumentEvent slave= cast(SlaveDocumentEvent) e; |
129 | 303 DocumentEvent master= slave.getMasterEvent(); |
304 if (master !is null) | |
305 preservedText= master.getDocument().get(master.getOffset(), master.getLength()); | |
306 } else { | |
307 preservedText= e.getDocument().get(e.getOffset(), e.getLength()); | |
308 } | |
309 | |
310 } catch (BadLocationException x) { | |
311 preservedText= null; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
312 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
313 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.WidgetCommand.setEvent")); //$NON-NLS-1$ |
129 | 314 } |
315 } else | |
316 preservedText= null; | |
317 } | |
318 } | |
319 | |
320 | |
321 /** | |
322 * Connects a text double click strategy to this viewer's text widget. | |
323 * Calls the double click strategies when the mouse has | |
324 * been clicked inside the text editor. | |
325 */ | |
326 class TextDoubleClickStrategyConnector : MouseAdapter , MovementListener { | |
327 | |
328 /** Internal flag to remember the last double-click selection. */ | |
329 private Point fDoubleClickSelection; | |
330 | |
331 /* | |
332 * @see dwt.events.MouseAdapter#mouseUp(dwt.events.MouseEvent) | |
333 * @since 3.2 | |
334 */ | |
335 public void mouseUp(MouseEvent e) { | |
336 fDoubleClickSelection= null; | |
337 } | |
338 | |
339 /* | |
340 * @see dwt.custom.MovementListener#getNextOffset(dwt.custom.MovementEvent) | |
341 * @since 3.3 | |
342 */ | |
343 public void getNextOffset(MovementEvent event) { | |
344 if (event.movement !is DWT.MOVEMENT_WORD_END) | |
345 return; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
346 |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
347 if (TRACE_DOUBLE_CLICK) { |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
348 System.out_.println("\n+++"); //$NON-NLS-1$ |
129 | 349 print(event); |
350 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
351 |
129 | 352 if (fDoubleClickSelection !is null) { |
353 if (fDoubleClickSelection.x <= event.offset && event.offset <= fDoubleClickSelection.y) | |
354 event.newOffset= fDoubleClickSelection.y; | |
355 } | |
356 } | |
357 | |
358 /* | |
359 * @see dwt.custom.MovementListener#getPreviousOffset(dwt.custom.MovementEvent) | |
360 * @since 3.3 | |
361 */ | |
362 public void getPreviousOffset(MovementEvent event) { | |
363 if (event.movement !is DWT.MOVEMENT_WORD_START) | |
364 return; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
365 |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
366 if (TRACE_DOUBLE_CLICK) { |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
367 System.out_.println("\n---"); //$NON-NLS-1$ |
129 | 368 print(event); |
369 } | |
370 if (fDoubleClickSelection is null) { | |
134 | 371 ITextDoubleClickStrategy s= cast(ITextDoubleClickStrategy) selectContentTypePlugin(getSelectedRange().x, fDoubleClickStrategies); |
129 | 372 if (s !is null) { |
373 StyledText textWidget= getTextWidget(); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
374 s.doubleClicked(this.outer); |
129 | 375 fDoubleClickSelection= textWidget.getSelection(); |
376 event.newOffset= fDoubleClickSelection.x; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
377 if (TRACE_DOUBLE_CLICK) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
378 System.out_.println("- setting selection: x= " + fDoubleClickSelection.x + ", y= " + fDoubleClickSelection.y); //$NON-NLS-1$ //$NON-NLS-2$ |
129 | 379 } |
380 } else { | |
381 if (fDoubleClickSelection.x <= event.offset && event.offset <= fDoubleClickSelection.y) | |
382 event.newOffset= fDoubleClickSelection.x; | |
383 } | |
384 } | |
385 } | |
386 | |
387 /** | |
388 * Print trace info about <code>MovementEvent</code>. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
389 * |
129 | 390 * @param e the event to print |
391 * @since 3.3 | |
392 */ | |
393 private void print(MovementEvent e) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
394 System.out_.println("line offset: " + e.lineOffset); //$NON-NLS-1$ |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
395 System.out_.println("line: " + e.lineText); //$NON-NLS-1$ |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
396 System.out_.println("type: " + e.movement); //$NON-NLS-1$ |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
397 System.out_.println("offset: " + e.offset); //$NON-NLS-1$ |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
398 System.out_.println("newOffset: " + e.newOffset); //$NON-NLS-1$ |
129 | 399 } |
400 | |
401 /** | |
402 * Monitors the area of the viewer's document that is visible in the viewer. | |
403 * If the area might have changed, it informs the text viewer about this | |
404 * potential change and its origin. The origin is internally used for optimization | |
405 * purposes. | |
406 */ | |
407 class ViewportGuard : MouseAdapter | |
408 , ControlListener, KeyListener, SelectionListener { | |
409 | |
410 /* | |
411 * @see ControlListener#controlResized(ControlEvent) | |
412 */ | |
413 public void controlResized(ControlEvent e) { | |
414 updateViewportListeners(RESIZE); | |
415 } | |
416 | |
417 /* | |
418 * @see ControlListener#controlMoved(ControlEvent) | |
419 */ | |
420 public void controlMoved(ControlEvent e) { | |
421 } | |
422 | |
423 /* | |
424 * @see KeyListener#keyReleased | |
425 */ | |
426 public void keyReleased(KeyEvent e) { | |
427 updateViewportListeners(KEY); | |
428 } | |
429 | |
430 /* | |
431 * @see KeyListener#keyPressed | |
432 */ | |
433 public void keyPressed(KeyEvent e) { | |
434 updateViewportListeners(KEY); | |
435 } | |
436 | |
437 /* | |
438 * @see MouseListener#mouseUp | |
439 */ | |
440 public void mouseUp(MouseEvent e) { | |
441 if (fTextWidget !is null) | |
442 fTextWidget.removeSelectionListener(this); | |
443 updateViewportListeners(MOUSE_END); | |
444 } | |
445 | |
446 /* | |
447 * @see MouseListener#mouseDown | |
448 */ | |
449 public void mouseDown(MouseEvent e) { | |
450 if (fTextWidget !is null) | |
451 fTextWidget.addSelectionListener(this); | |
452 } | |
453 | |
454 /* | |
455 * @see SelectionListener#widgetSelected | |
456 */ | |
457 public void widgetSelected(SelectionEvent e) { | |
458 if (e.widget is fScroller) | |
459 updateViewportListeners(SCROLLER); | |
460 else | |
461 updateViewportListeners(MOUSE); | |
462 } | |
463 | |
464 /* | |
465 * @see SelectionListener#widgetDefaultSelected | |
466 */ | |
467 public void widgetDefaultSelected(SelectionEvent e) {} | |
468 } | |
469 | |
470 /** | |
471 * This position updater is used to keep the selection during text shift operations. | |
472 */ | |
473 static class ShiftPositionUpdater : DefaultPositionUpdater { | |
474 | |
475 /** | |
476 * Creates the position updater for the given category. | |
477 * | |
478 * @param category the category this updater takes care of | |
479 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
480 protected this(String category) { |
129 | 481 super(category); |
482 } | |
483 | |
484 /** | |
485 * If an insertion happens at the selection's start offset, | |
486 * the position is extended rather than shifted. | |
487 */ | |
488 protected void adaptToInsert() { | |
489 | |
490 int myStart= fPosition.offset; | |
491 int myEnd= fPosition.offset + fPosition.length -1; | |
492 myEnd= Math.max(myStart, myEnd); | |
493 | |
494 int yoursStart= fOffset; | |
495 int yoursEnd= fOffset + fReplaceLength -1; | |
496 yoursEnd= Math.max(yoursStart, yoursEnd); | |
497 | |
498 if (myEnd < yoursStart) | |
499 return; | |
500 | |
501 if (myStart <= yoursStart) { | |
502 fPosition.length += fReplaceLength; | |
503 return; | |
504 } | |
505 | |
506 if (myStart > yoursStart) | |
507 fPosition.offset += fReplaceLength; | |
508 } | |
509 } | |
510 | |
511 /** | |
512 * Internal document listener on the visible document. | |
513 */ | |
514 class VisibleDocumentListener : IDocumentListener { | |
515 | |
516 /* | |
517 * @see IDocumentListener#documentAboutToBeChanged | |
518 */ | |
519 public void documentAboutToBeChanged(DocumentEvent e) { | |
520 if (e.getDocument() is getVisibleDocument()) | |
521 fWidgetCommand.setEvent(e); | |
522 handleVisibleDocumentAboutToBeChanged(e); | |
523 } | |
524 | |
525 /* | |
526 * @see IDocumentListener#documentChanged | |
527 */ | |
528 public void documentChanged(DocumentEvent e) { | |
529 if (fWidgetCommand.event is e) | |
530 updateTextListeners(fWidgetCommand); | |
531 fLastSentSelectionChange= null; | |
532 handleVisibleDocumentChanged(e); | |
533 } | |
534 } | |
535 | |
536 /** | |
537 * Internal verify listener. | |
538 */ | |
539 class TextVerifyListener : VerifyListener { | |
540 | |
541 /** | |
542 * Indicates whether verify events are forwarded or ignored. | |
543 * @since 2.0 | |
544 */ | |
545 private bool fForward= true; | |
546 | |
547 /** | |
548 * Tells the listener to forward received events. | |
549 * | |
550 * @param forward <code>true</code> if forwarding should be enabled. | |
551 * @since 2.0 | |
552 */ | |
553 public void forward(bool forward) { | |
554 fForward= forward; | |
555 } | |
556 | |
557 /* | |
558 * @see VerifyListener#verifyText(VerifyEvent) | |
559 */ | |
560 public void verifyText(VerifyEvent e) { | |
561 if (fForward) | |
562 handleVerifyEvent(e); | |
563 } | |
564 } | |
565 | |
566 /** | |
567 * The viewer's manager responsible for registered verify key listeners. | |
568 * Uses batches rather than robust iterators because of performance issues. | |
569 * <p> | |
570 * The implementation is reentrant, i.e. installed listeners may trigger | |
571 * further <code>VerifyKeyEvent</code>s that may cause other listeners to be | |
572 * installed, but not thread safe. | |
573 * </p> | |
574 * @since 2.0 | |
575 */ | |
576 class VerifyKeyListenersManager : VerifyKeyListener { | |
577 | |
578 /** | |
579 * Represents a batched addListener/removeListener command. | |
580 */ | |
581 class Batch { | |
582 /** The index at which to insert the listener. */ | |
583 int index; | |
584 /** The listener to be inserted. */ | |
585 VerifyKeyListener listener; | |
586 | |
587 /** | |
588 * Creates a new batch containing the given listener for the given index. | |
589 * | |
590 * @param l the listener to be added | |
591 * @param i the index at which to insert the listener | |
592 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
593 public this(VerifyKeyListener l, int i) { |
129 | 594 listener= l; |
595 index= i; | |
596 } | |
597 } | |
598 | |
599 /** List of registered verify key listeners. */ | |
600 private List fListeners= new ArrayList(); | |
601 /** List of pending batches. */ | |
602 private List fBatched= new ArrayList(); | |
603 /** The reentrance count. */ | |
604 private int fReentranceCount= 0; | |
605 | |
606 /* | |
607 * @see VerifyKeyListener#verifyKey(VerifyEvent) | |
608 */ | |
609 public void verifyKey(VerifyEvent event) { | |
610 if (fListeners.isEmpty()) | |
611 return; | |
612 | |
613 try { | |
614 fReentranceCount++; | |
615 Iterator iterator= fListeners.iterator(); | |
616 while (iterator.hasNext() && event.doit) { | |
134 | 617 VerifyKeyListener listener= cast(VerifyKeyListener) iterator.next(); |
129 | 618 listener.verifyKey(event); // we might trigger reentrant calls on GTK |
619 } | |
620 } finally { | |
621 fReentranceCount--; | |
622 } | |
623 if (fReentranceCount is 0) | |
624 processBatchedRequests(); | |
625 } | |
626 | |
627 /** | |
628 * Processes the pending batched requests. | |
629 */ | |
630 private void processBatchedRequests() { | |
631 if (!fBatched.isEmpty()) { | |
632 Iterator e= fBatched.iterator(); | |
633 while (e.hasNext()) { | |
134 | 634 Batch batch= cast(Batch) e.next(); |
129 | 635 insertListener(batch.listener, batch.index); |
636 } | |
637 fBatched.clear(); | |
638 } | |
639 } | |
640 | |
641 /** | |
642 * Returns the number of registered verify key listeners. | |
643 * | |
644 * @return the number of registered verify key listeners | |
645 */ | |
646 public int numberOfListeners() { | |
647 return fListeners.size(); | |
648 } | |
649 | |
650 /** | |
651 * Inserts the given listener at the given index or moves it | |
652 * to that index. | |
653 * | |
654 * @param listener the listener to be inserted | |
655 * @param index the index of the listener or -1 for remove | |
656 */ | |
657 public void insertListener(VerifyKeyListener listener, int index) { | |
658 | |
659 if (index is -1) { | |
660 removeListener(listener); | |
661 } else if (listener !is null) { | |
662 | |
663 if (fReentranceCount > 0) { | |
664 | |
665 fBatched.add(new Batch(listener, index)); | |
666 | |
667 } else { | |
668 | |
669 int idx= -1; | |
670 | |
671 // find index based on identity | |
672 int size= fListeners.size(); | |
673 for (int i= 0; i < size; i++) { | |
674 if (listener is fListeners.get(i)) { | |
675 idx= i; | |
676 break; | |
677 } | |
678 } | |
679 | |
680 // move or add it | |
681 if (idx !is index) { | |
682 | |
683 if (idx !is -1) | |
684 fListeners.remove(idx); | |
685 | |
686 if (index > fListeners.size()) | |
687 fListeners.add(listener); | |
688 else | |
689 fListeners.add(index, listener); | |
690 } | |
691 | |
692 if (size is 0) // checking old size, i.e. current size is size + 1 | |
693 install(); | |
694 } | |
695 } | |
696 } | |
697 | |
698 /** | |
699 * Removes the given listener. | |
700 * | |
701 * @param listener the listener to be removed | |
702 */ | |
703 public void removeListener(VerifyKeyListener listener) { | |
704 if (listener is null) | |
705 return; | |
706 | |
707 if (fReentranceCount > 0) { | |
708 | |
709 fBatched.add(new Batch(listener, -1)); | |
710 | |
711 } else { | |
712 | |
713 int size= fListeners.size(); | |
714 for (int i= 0; i < size; i++) { | |
715 if (listener is fListeners.get(i)) { | |
716 fListeners.remove(i); | |
717 if (size is 1) // checking old size, i.e. current size is size - 1 | |
718 uninstall(); | |
719 return; | |
720 } | |
721 } | |
722 } | |
723 } | |
724 | |
725 /** | |
726 * Installs this manager. | |
727 */ | |
728 private void install() { | |
729 StyledText textWidget= getTextWidget(); | |
730 if (textWidget !is null && !textWidget.isDisposed()) | |
731 textWidget.addVerifyKeyListener(this); | |
732 } | |
733 | |
734 /** | |
735 * Uninstalls this manager. | |
736 */ | |
737 private void uninstall() { | |
738 StyledText textWidget= getTextWidget(); | |
739 if (textWidget !is null && !textWidget.isDisposed()) | |
740 textWidget.removeVerifyKeyListener(this); | |
741 } | |
742 } | |
743 | |
744 | |
745 /** | |
746 * Reification of a range in which a find replace operation is performed. This range is visually | |
747 * highlighted in the viewer as long as the replace operation is in progress. | |
748 * | |
749 * @since 2.0 | |
750 */ | |
751 class FindReplaceRange : LineBackgroundListener, ITextListener, IPositionUpdater { | |
752 | |
753 /** Internal name for the position category used to update the range. */ | |
146 | 754 private const static String RANGE_CATEGORY= "dwtx.jface.text.TextViewer.find.range"; //$NON-NLS-1$ |
129 | 755 |
756 /** The highlight color of this range. */ | |
757 private Color fHighlightColor; | |
758 /** The position used to lively update this range's extent. */ | |
759 private Position fPosition; | |
760 | |
761 /** Creates a new find/replace range with the given extent. | |
762 * | |
763 * @param range the extent of this range | |
764 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
765 public this(IRegion range) { |
129 | 766 setRange(range); |
767 } | |
768 | |
769 /** | |
770 * Sets the extent of this range. | |
771 * | |
772 * @param range the extent of this range | |
773 */ | |
774 public void setRange(IRegion range) { | |
775 fPosition= new Position(range.getOffset(), range.getLength()); | |
776 } | |
777 | |
778 /** | |
779 * Returns the extent of this range. | |
780 * | |
781 * @return the extent of this range | |
782 */ | |
783 public IRegion getRange() { | |
784 return new Region(fPosition.getOffset(), fPosition.getLength()); | |
785 } | |
786 | |
787 /** | |
788 * Sets the highlight color of this range. Causes the range to be redrawn. | |
789 * | |
790 * @param color the highlight color | |
791 */ | |
792 public void setHighlightColor(Color color) { | |
793 fHighlightColor= color; | |
794 paint(); | |
795 } | |
796 | |
797 /* | |
798 * @see LineBackgroundListener#lineGetBackground(LineBackgroundEvent) | |
799 * @since 2.0 | |
800 */ | |
801 public void lineGetBackground(LineBackgroundEvent event) { | |
802 /* Don't use cached line information because of patched redrawing events. */ | |
803 | |
804 if (fTextWidget !is null) { | |
805 int offset= widgetOffset2ModelOffset(event.lineOffset); | |
806 if (fPosition.includes(offset)) | |
807 event.lineBackground= fHighlightColor; | |
808 } | |
809 } | |
810 | |
811 /** | |
812 * Installs this range. The range registers itself as background | |
813 * line painter and text listener. Also, it creates a category with the | |
814 * viewer's document to maintain its own extent. | |
815 */ | |
816 public void install() { | |
137 | 817 this.outer.addTextListener(this); |
129 | 818 fTextWidget.addLineBackgroundListener(this); |
819 | |
137 | 820 IDocument document= this.outer.getDocument(); |
129 | 821 try { |
822 document.addPositionCategory(RANGE_CATEGORY); | |
823 document.addPosition(RANGE_CATEGORY, fPosition); | |
824 document.addPositionUpdater(this); | |
825 } catch (BadPositionCategoryException e) { | |
826 // should not happen | |
827 } catch (BadLocationException e) { | |
828 // should not happen | |
829 } | |
830 | |
831 paint(); | |
832 } | |
833 | |
834 /** | |
835 * Uninstalls this range. | |
836 * @see #install() | |
837 */ | |
838 public void uninstall() { | |
839 | |
840 // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19612 | |
841 | |
137 | 842 IDocument document= this.outer.getDocument(); |
129 | 843 if (document !is null) { |
844 document.removePositionUpdater(this); | |
845 document.removePosition(fPosition); | |
846 } | |
847 | |
848 if (fTextWidget !is null && !fTextWidget.isDisposed()) | |
849 fTextWidget.removeLineBackgroundListener(this); | |
850 | |
137 | 851 this.outer.removeTextListener(this); |
129 | 852 |
853 clear(); | |
854 } | |
855 | |
856 /** | |
857 * Clears the highlighting of this range. | |
858 */ | |
859 private void clear() { | |
860 if (fTextWidget !is null && !fTextWidget.isDisposed()) | |
861 fTextWidget.redraw(); | |
862 } | |
863 | |
864 /** | |
865 * Paints the highlighting of this range. | |
866 */ | |
867 private void paint() { | |
868 | |
869 IRegion widgetRegion= modelRange2WidgetRange(fPosition); | |
870 int offset= widgetRegion.getOffset(); | |
871 int length= widgetRegion.getLength(); | |
872 | |
873 int count= fTextWidget.getCharCount(); | |
874 if (offset + length >= count) { | |
875 length= count - offset; // clip | |
876 | |
877 Point upperLeft= fTextWidget.getLocationAtOffset(offset); | |
878 Point lowerRight= fTextWidget.getLocationAtOffset(offset + length); | |
879 int width= fTextWidget.getClientArea().width; | |
880 int height= fTextWidget.getLineHeight(offset + length) + lowerRight.y - upperLeft.y; | |
881 fTextWidget.redraw(upperLeft.x, upperLeft.y, width, height, false); | |
882 } | |
883 | |
884 fTextWidget.redrawRange(offset, length, true); | |
885 } | |
886 | |
887 /* | |
888 * @see ITextListener#textChanged(TextEvent) | |
889 * @since 2.0 | |
890 */ | |
891 public void textChanged(TextEvent event) { | |
892 if (event.getViewerRedrawState()) | |
893 paint(); | |
894 } | |
895 | |
896 /* | |
897 * @see IPositionUpdater#update(DocumentEvent) | |
898 * @since 2.0 | |
899 */ | |
900 public void update(DocumentEvent event) { | |
901 int offset= event.getOffset(); | |
902 int length= event.getLength(); | |
903 int delta= event.getText().length() - length; | |
904 | |
905 if (offset < fPosition.getOffset()) | |
906 fPosition.setOffset(fPosition.getOffset() + delta); | |
907 else if (offset < fPosition.getOffset() + fPosition.getLength()) | |
908 fPosition.setLength(fPosition.getLength() + delta); | |
909 } | |
910 } | |
911 | |
912 /** | |
913 * This viewer's find/replace target. | |
914 */ | |
915 class FindReplaceTarget : IFindReplaceTarget, IFindReplaceTargetExtension, IFindReplaceTargetExtension3 { | |
916 | |
917 /** The range for this target. */ | |
918 private FindReplaceRange fRange; | |
919 /** The highlight color of the range of this target. */ | |
920 private Color fScopeHighlightColor; | |
921 /** The document partitioner remembered in case of a "Replace All". */ | |
922 private Map fRememberedPartitioners; | |
923 /** | |
924 * The active rewrite session. | |
925 * @since 3.1 | |
926 */ | |
927 private DocumentRewriteSession fRewriteSession; | |
928 | |
929 /* | |
930 * @see IFindReplaceTarget#getSelectionText() | |
931 */ | |
932 public String getSelectionText() { | |
137 | 933 Point s= this.outer.getSelectedRange(); |
129 | 934 if (s.x > -1 && s.y > -1) { |
935 try { | |
137 | 936 IDocument document= this.outer.getDocument(); |
129 | 937 return document.get(s.x, s.y); |
938 } catch (BadLocationException x) { | |
939 } | |
940 } | |
941 return ""; //$NON-NLS-1$ | |
942 } | |
943 | |
944 /* | |
945 * @see IFindReplaceTarget#replaceSelection(String) | |
946 */ | |
947 public void replaceSelection(String text) { | |
948 replaceSelection(text, false); | |
949 } | |
950 | |
951 /* | |
952 * @see IFindReplaceTarget#replaceSelection(String) | |
953 */ | |
954 public void replaceSelection(String text, bool regExReplace) { | |
137 | 955 Point s= this.outer.getSelectedRange(); |
129 | 956 if (s.x > -1 && s.y > -1) { |
957 try { | |
137 | 958 IRegion matchRegion= this.outer.getFindReplaceDocumentAdapter().replace(text, regExReplace); |
129 | 959 int length= -1; |
960 if (matchRegion !is null) | |
961 length= matchRegion.getLength(); | |
962 | |
963 if (text !is null && length > 0) | |
137 | 964 this.outer.setSelectedRange(s.x, length); |
129 | 965 } catch (BadLocationException x) { |
966 } | |
967 } | |
968 } | |
969 | |
970 /* | |
971 * @see IFindReplaceTarget#isEditable() | |
972 */ | |
973 public bool isEditable() { | |
137 | 974 return this.outer.isEditable(); |
129 | 975 } |
976 | |
977 /* | |
978 * @see IFindReplaceTarget#getSelection() | |
979 */ | |
980 public Point getSelection() { | |
137 | 981 Point modelSelection= this.outer.getSelectedRange(); |
129 | 982 Point widgetSelection= modelSelection2WidgetSelection(modelSelection); |
983 return widgetSelection !is null ? widgetSelection : new Point(-1, -1); | |
984 } | |
985 | |
986 /* | |
987 * @see IFindReplaceTarget#findAndSelect(int, String, bool, bool, bool) | |
988 */ | |
989 public int findAndSelect(int widgetOffset, String findString, bool searchForward, bool caseSensitive, bool wholeWord) { | |
990 try { | |
991 return findAndSelect(widgetOffset, findString, searchForward, caseSensitive, wholeWord, false); | |
992 } catch (PatternSyntaxException x) { | |
993 return -1; | |
994 } | |
995 } | |
996 | |
997 /* | |
998 * @see IFindReplaceTarget#findAndSelect(int, String, bool, bool, bool) | |
999 */ | |
1000 public int findAndSelect(int widgetOffset, String findString, bool searchForward, bool caseSensitive, bool wholeWord, bool regExSearch) { | |
1001 | |
1002 int modelOffset= widgetOffset is -1 ? -1 : widgetOffset2ModelOffset(widgetOffset); | |
1003 | |
1004 if (fRange !is null) { | |
1005 IRegion range= fRange.getRange(); | |
137 | 1006 modelOffset= this.outer.findAndSelectInRange(modelOffset, findString, searchForward, caseSensitive, wholeWord, range.getOffset(), range.getLength(), regExSearch); |
129 | 1007 } else { |
137 | 1008 modelOffset= this.outer.findAndSelect(modelOffset, findString, searchForward, caseSensitive, wholeWord, regExSearch); |
129 | 1009 } |
1010 | |
1011 widgetOffset= modelOffset is -1 ? -1 : modelOffset2WidgetOffset(modelOffset); | |
1012 return widgetOffset; | |
1013 } | |
1014 | |
1015 /* | |
1016 * @see IFindReplaceTarget#canPerformFind() | |
1017 */ | |
1018 public bool canPerformFind() { | |
137 | 1019 return this.outer.canPerformFind(); |
129 | 1020 } |
1021 | |
1022 /* | |
1023 * @see IFindReplaceTargetExtension#beginSession() | |
1024 * @since 2.0 | |
1025 */ | |
1026 public void beginSession() { | |
1027 fRange= null; | |
1028 } | |
1029 | |
1030 /* | |
1031 * @see IFindReplaceTargetExtension#endSession() | |
1032 * @since 2.0 | |
1033 */ | |
1034 public void endSession() { | |
1035 if (fRange !is null) { | |
1036 fRange.uninstall(); | |
1037 fRange= null; | |
1038 } | |
1039 } | |
1040 | |
1041 /* | |
1042 * @see IFindReplaceTargetExtension#getScope() | |
1043 * @since 2.0 | |
1044 */ | |
1045 public IRegion getScope() { | |
1046 return fRange is null ? null : fRange.getRange(); | |
1047 } | |
1048 | |
1049 /* | |
1050 * @see IFindReplaceTargetExtension#getLineSelection() | |
1051 * @since 2.0 | |
1052 */ | |
1053 public Point getLineSelection() { | |
137 | 1054 Point point= this.outer.getSelectedRange(); |
129 | 1055 |
1056 try { | |
137 | 1057 IDocument document= this.outer.getDocument(); |
129 | 1058 |
1059 // beginning of line | |
1060 int line= document.getLineOfOffset(point.x); | |
1061 int offset= document.getLineOffset(line); | |
1062 | |
1063 // end of line | |
1064 IRegion lastLineInfo= document.getLineInformationOfOffset(point.x + point.y); | |
1065 int lastLine= document.getLineOfOffset(point.x + point.y); | |
1066 int length; | |
1067 if (lastLineInfo.getOffset() is point.x + point.y && lastLine > 0) | |
1068 length= document.getLineOffset(lastLine - 1) + document.getLineLength(lastLine - 1) - offset; | |
1069 else | |
1070 length= lastLineInfo.getOffset() + lastLineInfo.getLength() - offset; | |
1071 | |
1072 return new Point(offset, length); | |
1073 | |
1074 } catch (BadLocationException e) { | |
1075 // should not happen | |
1076 return new Point(point.x, 0); | |
1077 } | |
1078 } | |
1079 | |
1080 /* | |
1081 * @see IFindReplaceTargetExtension#setSelection(int, int) | |
1082 * @since 2.0 | |
1083 */ | |
1084 public void setSelection(int modelOffset, int modelLength) { | |
137 | 1085 this.outer.setSelectedRange(modelOffset, modelLength); |
129 | 1086 } |
1087 | |
1088 /* | |
1089 * @see IFindReplaceTargetExtension#setScope(IRegion) | |
1090 * @since 2.0 | |
1091 */ | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1092 public void setScope(IRegion scope_) { |
129 | 1093 if (fRange !is null) |
1094 fRange.uninstall(); | |
1095 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1096 if (scope_ is null) { |
129 | 1097 fRange= null; |
1098 return; | |
1099 } | |
1100 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1101 fRange= new FindReplaceRange(scope_); |
129 | 1102 fRange.setHighlightColor(fScopeHighlightColor); |
1103 fRange.install(); | |
1104 } | |
1105 | |
1106 /* | |
1107 * @see IFindReplaceTargetExtension#setScopeHighlightColor(Color) | |
1108 * @since 2.0 | |
1109 */ | |
1110 public void setScopeHighlightColor(Color color) { | |
1111 if (fRange !is null) | |
1112 fRange.setHighlightColor(color); | |
1113 fScopeHighlightColor= color; | |
1114 } | |
1115 | |
1116 /* | |
1117 * @see IFindReplaceTargetExtension#setReplaceAllMode(bool) | |
1118 * @since 2.0 | |
1119 */ | |
1120 public void setReplaceAllMode(bool replaceAll) { | |
1121 | |
1122 // http://bugs.eclipse.org/bugs/show_bug.cgi?id=18232 | |
1123 | |
137 | 1124 IDocument document= this.outer.getDocument(); |
129 | 1125 |
1126 if (replaceAll) { | |
1127 | |
138 | 1128 if ( cast(IDocumentExtension4)document ) { |
134 | 1129 IDocumentExtension4 extension= cast(IDocumentExtension4) document; |
129 | 1130 fRewriteSession= extension.startRewriteSession(DocumentRewriteSessionType.SEQUENTIAL); |
1131 } else { | |
137 | 1132 this.outer.setRedraw(false); |
1133 this.outer.startSequentialRewriteMode(false); | |
129 | 1134 |
1135 if (fUndoManager !is null) | |
1136 fUndoManager.beginCompoundChange(); | |
1137 | |
1138 fRememberedPartitioners= TextUtilities.removeDocumentPartitioners(document); | |
1139 } | |
1140 | |
1141 } else { | |
1142 | |
138 | 1143 if ( cast(IDocumentExtension4)document ) { |
134 | 1144 IDocumentExtension4 extension= cast(IDocumentExtension4) document; |
129 | 1145 extension.stopRewriteSession(fRewriteSession); |
1146 } else { | |
137 | 1147 this.outer.setRedraw(true); |
1148 this.outer.stopSequentialRewriteMode(); | |
129 | 1149 |
1150 if (fUndoManager !is null) | |
1151 fUndoManager.endCompoundChange(); | |
1152 | |
1153 if (fRememberedPartitioners !is null) | |
1154 TextUtilities.addDocumentPartitioners(document, fRememberedPartitioners); | |
1155 } | |
1156 } | |
1157 } | |
1158 } | |
1159 | |
1160 | |
1161 /** | |
1162 * The viewer's rewrite target. | |
1163 * @since 2.0 | |
1164 */ | |
1165 class RewriteTarget : IRewriteTarget { | |
1166 | |
1167 /* | |
1168 * @see dwtx.jface.text.IRewriteTarget#beginCompoundChange() | |
1169 */ | |
1170 public void beginCompoundChange() { | |
1171 if (fUndoManager !is null) | |
1172 fUndoManager.beginCompoundChange(); | |
1173 } | |
1174 | |
1175 /* | |
1176 * @see dwtx.jface.text.IRewriteTarget#endCompoundChange() | |
1177 */ | |
1178 public void endCompoundChange() { | |
1179 if (fUndoManager !is null) | |
1180 fUndoManager.endCompoundChange(); | |
1181 } | |
1182 | |
1183 /* | |
1184 * @see dwtx.jface.text.IRewriteTarget#getDocument() | |
1185 */ | |
1186 public IDocument getDocument() { | |
137 | 1187 return this.outer.getDocument(); |
129 | 1188 } |
1189 | |
1190 /* | |
1191 * @see dwtx.jface.text.IRewriteTarget#setRedraw(bool) | |
1192 */ | |
1193 public void setRedraw(bool redraw) { | |
137 | 1194 this.outer.setRedraw(redraw); |
129 | 1195 } |
1196 } | |
1197 | |
1198 /** | |
1199 * Value object used as key in the text hover configuration table. It is | |
1200 * modifiable only inside this compilation unit to allow the reuse of created | |
1201 * objects for efficiency reasons | |
1202 * | |
1203 * @since 2.1 | |
1204 */ | |
1205 protected class TextHoverKey { | |
1206 | |
1207 /** The content type this key belongs to */ | |
1208 private String fContentType; | |
1209 /** The state mask */ | |
1210 private int fStateMask; | |
1211 | |
1212 /** | |
1213 * Creates a new text hover key for the given content type and state mask. | |
1214 * | |
1215 * @param contentType the content type | |
1216 * @param stateMask the state mask | |
1217 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
1218 protected this(String contentType, int stateMask) { |
129 | 1219 Assert.isNotNull(contentType); |
1220 fContentType= contentType; | |
1221 fStateMask= stateMask; | |
1222 } | |
1223 | |
1224 /* | |
1225 * @see java.lang.Object#equals(java.lang.Object) | |
1226 */ | |
1227 public bool equals(Object obj) { | |
1228 if (obj is null || obj.getClass() !is getClass()) | |
1229 return false; | |
134 | 1230 TextHoverKey textHoverKey= cast(TextHoverKey)obj; |
129 | 1231 return textHoverKey.fContentType.equals(fContentType) && textHoverKey.fStateMask is fStateMask; |
1232 } | |
1233 | |
1234 /* | |
1235 * @see java.lang.Object#hashCode() | |
1236 */ | |
1237 public int hashCode() { | |
1238 return fStateMask << 16 | fContentType.hashCode(); | |
1239 } | |
1240 | |
1241 /** | |
1242 * Sets the state mask of this text hover key. | |
1243 * | |
1244 * @param stateMask the state mask | |
1245 */ | |
1246 private void setStateMask(int stateMask) { | |
1247 fStateMask= stateMask; | |
1248 } | |
1249 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1250 |
129 | 1251 /** |
1252 * Captures and remembers the viewer state (selection and visual position). {@link TextViewer.ViewerState} | |
1253 * instances are normally used once and then discarded, similar to the following snippet: | |
1254 * <pre> | |
1255 * ViewerState state= new ViewerState(); // remember the state | |
1256 * doStuff(); // operation that may call setRedraw() and perform complex document modifications | |
1257 * state.restore(true); // restore the remembered state | |
1258 * </pre> | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1259 * |
129 | 1260 * @since 3.3 |
1261 */ | |
1262 private final class ViewerState { | |
1263 /** The position tracking the selection. */ | |
1264 private Position fSelection; | |
1265 /** <code>true</code> if {@link #fSelection} was originally backwards. */ | |
1266 private bool fReverseSelection; | |
1267 /** <code>true</code> if the selection has been updated while in redraw(off) mode. */ | |
1268 private bool fSelectionSet; | |
1269 /** The position tracking the visually stable line. */ | |
1270 private Position fStableLine; | |
1271 /** The pixel offset of the stable line measured from the client area. */ | |
1272 private int fStablePixel; | |
1273 | |
1274 /** The position updater for {@link #fSelection} and {@link #fStableLine}. */ | |
1275 private IPositionUpdater fUpdater; | |
1276 /** The document that the position updater and the positions are registered with. */ | |
1277 private IDocument fUpdaterDocument; | |
1278 /** The position category used by {@link #fUpdater}. */ | |
1279 private String fUpdaterCategory; | |
1280 | |
1281 /** | |
1282 * Creates a new viewer state instance and connects it to the current document. | |
1283 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
1284 public this() { |
129 | 1285 IDocument document= getDocument(); |
1286 if (document !is null) | |
1287 connect(document); | |
1288 } | |
1289 | |
1290 /** | |
1291 * Returns the normalized selection, i.e. the the selection length is always non-negative. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1292 * |
129 | 1293 * @return the normalized selection |
1294 */ | |
1295 public Point getSelection() { | |
1296 if (fSelection is null) | |
1297 return new Point(-1, -1); | |
1298 return new Point(fSelection.getOffset(), fSelection.getLength()); | |
1299 } | |
1300 | |
1301 /** | |
1302 * Updates the selection. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1303 * |
129 | 1304 * @param offset the new selection offset |
1305 * @param length the new selection length | |
1306 */ | |
1307 public void updateSelection(int offset, int length) { | |
1308 fSelectionSet= true; | |
1309 if (fSelection is null) | |
1310 fSelection= new Position(offset, length); | |
1311 else | |
1312 updatePosition(fSelection, offset, length); | |
1313 } | |
1314 | |
1315 /** | |
1316 * Restores the state and disconnects it from the document. The selection is no longer | |
1317 * tracked after this call. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1318 * |
129 | 1319 * @param restoreViewport <code>true</code> to restore both selection and viewport, |
1320 * <code>false</code> to only restore the selection | |
1321 */ | |
1322 public void restore(bool restoreViewport) { | |
1323 if (isConnected()) | |
1324 disconnect(); | |
1325 if (fSelection !is null) { | |
1326 int offset= fSelection.getOffset(); | |
1327 int length= fSelection.getLength(); | |
1328 if (fReverseSelection) { | |
1329 offset-= length; | |
1330 length= -length; | |
1331 } | |
1332 setSelectedRange(offset, length); | |
1333 if (restoreViewport) | |
1334 updateViewport(); | |
1335 } | |
1336 } | |
1337 | |
1338 /** | |
1339 * Updates the viewport, trying to keep the | |
1340 * {@linkplain StyledText#getLinePixel(int) line pixel} of the caret line stable. If the | |
1341 * selection has been updated while in redraw(false) mode, the new selection is revealed. | |
1342 */ | |
1343 private void updateViewport() { | |
1344 if (fSelectionSet) { | |
1345 revealRange(fSelection.getOffset(), fSelection.getLength()); | |
1346 } else if (fStableLine !is null) { | |
1347 int stableLine; | |
1348 try { | |
1349 stableLine= fUpdaterDocument.getLineOfOffset(fStableLine.getOffset()); | |
1350 } catch (BadLocationException x) { | |
1351 // ignore and return silently | |
1352 return; | |
1353 } | |
1354 int stableWidgetLine= getClosestWidgetLineForModelLine(stableLine); | |
1355 if (stableWidgetLine is -1) | |
1356 return; | |
1357 int linePixel= getTextWidget().getLinePixel(stableWidgetLine); | |
1358 int delta= fStablePixel - linePixel; | |
1359 int topPixel= getTextWidget().getTopPixel(); | |
1360 getTextWidget().setTopPixel(topPixel - delta); | |
1361 } | |
1362 } | |
1363 | |
1364 /** | |
1365 * Remembers the viewer state. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1366 * |
129 | 1367 * @param document the document to remember the state of |
1368 */ | |
1369 private void connect(IDocument document) { | |
1370 Assert.isLegal(document !is null); | |
1371 Assert.isLegal(!isConnected()); | |
1372 fUpdaterDocument= document; | |
1373 try { | |
1374 fUpdaterCategory= SELECTION_POSITION_CATEGORY + hashCode(); | |
1375 fUpdater= new NonDeletingPositionUpdater(fUpdaterCategory); | |
1376 fUpdaterDocument.addPositionCategory(fUpdaterCategory); | |
1377 fUpdaterDocument.addPositionUpdater(fUpdater); | |
1378 | |
1379 Point selectionRange= getSelectedRange(); | |
1380 fReverseSelection= selectionRange.y < 0; | |
1381 int offset, length; | |
1382 if (fReverseSelection) { | |
1383 offset= selectionRange.x + selectionRange.y; | |
1384 length= -selectionRange.y; | |
1385 } else { | |
1386 offset= selectionRange.x; | |
1387 length= selectionRange.y; | |
1388 } | |
1389 | |
1390 fSelection= new Position(offset, length); | |
1391 fSelectionSet= false; | |
1392 fUpdaterDocument.addPosition(fUpdaterCategory, fSelection); | |
1393 | |
1394 int stableLine= getStableLine(); | |
1395 int stableWidgetLine= modelLine2WidgetLine(stableLine); | |
1396 fStablePixel= getTextWidget().getLinePixel(stableWidgetLine); | |
1397 IRegion stableLineInfo= fUpdaterDocument.getLineInformation(stableLine); | |
1398 fStableLine= new Position(stableLineInfo.getOffset(), stableLineInfo.getLength()); | |
1399 fUpdaterDocument.addPosition(fUpdaterCategory, fStableLine); | |
1400 } catch (BadPositionCategoryException e) { | |
1401 // cannot happen | |
1402 Assert.isTrue(false); | |
1403 } catch (BadLocationException e) { | |
1404 // should not happen except on concurrent modification | |
1405 // ignore and disconnect | |
1406 disconnect(); | |
1407 } | |
1408 } | |
1409 | |
1410 /** | |
1411 * Updates a position with the given information and clears its deletion state. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1412 * |
129 | 1413 * @param position the position to update |
1414 * @param offset the new selection offset | |
1415 * @param length the new selection length | |
1416 */ | |
1417 private void updatePosition(Position position, int offset, int length) { | |
1418 position.setOffset(offset); | |
1419 position.setLength(length); | |
1420 // http://bugs.eclipse.org/bugs/show_bug.cgi?id=32795 | |
1421 position.isDeleted= false; | |
1422 } | |
1423 | |
1424 /** | |
1425 * Returns the document line to keep visually stable. If the caret line is (partially) | |
1426 * visible, it is returned, otherwise the topmost (partially) visible line is returned. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1427 * |
129 | 1428 * @return the visually stable line of this viewer state |
1429 */ | |
1430 private int getStableLine() { | |
1431 int stableLine; // the model line that we try to keep stable | |
1432 int caretLine= getTextWidget().getLineAtOffset(getTextWidget().getCaretOffset()); | |
1433 if (caretLine < JFaceTextUtil.getPartialTopIndex(getTextWidget()) || caretLine > JFaceTextUtil.getPartialBottomIndex(getTextWidget())) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1434 stableLine= JFaceTextUtil.getPartialTopIndex(this.outer); |
129 | 1435 } else { |
1436 stableLine= widgetLine2ModelLine(caretLine); | |
1437 } | |
1438 return stableLine; | |
1439 } | |
1440 | |
1441 /** | |
1442 * Returns <code>true</code> if the viewer state is being tracked, <code>false</code> | |
1443 * otherwise. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1444 * |
129 | 1445 * @return the tracking state |
1446 */ | |
1447 private bool isConnected() { | |
1448 return fUpdater !is null; | |
1449 } | |
1450 | |
1451 /** | |
1452 * Disconnects from the document. | |
1453 */ | |
1454 private void disconnect() { | |
1455 Assert.isTrue(isConnected()); | |
1456 try { | |
1457 fUpdaterDocument.removePosition(fUpdaterCategory, fSelection); | |
1458 fUpdaterDocument.removePosition(fUpdaterCategory, fStableLine); | |
1459 fUpdaterDocument.removePositionUpdater(fUpdater); | |
1460 fUpdater= null; | |
1461 fUpdaterDocument.removePositionCategory(fUpdaterCategory); | |
1462 fUpdaterCategory= null; | |
1463 } catch (BadPositionCategoryException x) { | |
1464 // cannot happen | |
1465 Assert.isTrue(false); | |
1466 } | |
1467 } | |
1468 } | |
1469 | |
1470 /** | |
1471 * Internal cursor listener i.e. aggregation of mouse and key listener. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1472 * |
129 | 1473 * @since 3.0 |
1474 */ | |
1475 private class CursorListener : KeyListener, MouseListener { | |
1476 | |
1477 /** | |
1478 * Installs this cursor listener. | |
1479 */ | |
1480 private void install() { | |
1481 if (fTextWidget !is null && !fTextWidget.isDisposed()) { | |
1482 fTextWidget.addKeyListener(this); | |
1483 fTextWidget.addMouseListener(this); | |
1484 } | |
1485 } | |
1486 | |
1487 /** | |
1488 * Uninstalls this cursor listener. | |
1489 */ | |
1490 private void uninstall() { | |
1491 if (fTextWidget !is null && !fTextWidget.isDisposed()) { | |
1492 fTextWidget.removeKeyListener(this); | |
1493 fTextWidget.removeMouseListener(this); | |
1494 } | |
1495 } | |
1496 | |
1497 /* | |
1498 * @see KeyListener#keyPressed(dwt.events.KeyEvent) | |
1499 */ | |
1500 public void keyPressed(KeyEvent event) { | |
1501 } | |
1502 | |
1503 /* | |
1504 * @see KeyListener#keyPressed(dwt.events.KeyEvent) | |
1505 */ | |
1506 public void keyReleased(KeyEvent e) { | |
1507 if (fTextWidget.getSelectionCount() is 0) { | |
1508 fLastSentSelectionChange= null; | |
1509 queuePostSelectionChanged(e.character is DWT.DEL); | |
1510 } | |
1511 } | |
1512 | |
1513 /* | |
1514 * @see MouseListener#mouseDoubleClick(dwt.events.MouseEvent) | |
1515 */ | |
1516 public void mouseDoubleClick(MouseEvent e) { | |
1517 } | |
1518 | |
1519 /* | |
1520 * @see MouseListener#mouseDown(dwt.events.MouseEvent) | |
1521 */ | |
1522 public void mouseDown(MouseEvent e) { | |
1523 } | |
1524 | |
1525 /* | |
1526 * @see MouseListener#mouseUp(dwt.events.MouseEvent) | |
1527 */ | |
1528 public void mouseUp(MouseEvent event) { | |
1529 if (fTextWidget.getSelectionCount() is 0) | |
1530 queuePostSelectionChanged(false); | |
1531 } | |
1532 } | |
1533 | |
1534 /** | |
1535 * Internal listener to document rewrite session state changes. | |
1536 * @since 3.1 | |
1537 */ | |
1538 private class DocumentRewriteSessionListener : IDocumentRewriteSessionListener { | |
1539 | |
1540 /* | |
1541 * @see dwtx.jface.text.IDocumentRewriteSessionListener#documentRewriteSessionChanged(dwtx.jface.text.DocumentRewriteSessionEvent) | |
1542 */ | |
1543 public void documentRewriteSessionChanged(DocumentRewriteSessionEvent event) { | |
137 | 1544 IRewriteTarget target= this.outer.getRewriteTarget(); |
129 | 1545 // FIXME always use setRedraw to avoid flickering due to scrolling |
1546 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=158746 | |
1547 bool toggleRedraw= true || event.getSession().getSessionType() !is DocumentRewriteSessionType.UNRESTRICTED_SMALL; | |
1548 final bool viewportStabilize= !toggleRedraw; | |
1549 if (DocumentRewriteSessionEvent.SESSION_START is event.getChangeType()) { | |
1550 if (toggleRedraw) | |
1551 target.setRedraw(false); | |
1552 target.beginCompoundChange(); | |
1553 if (viewportStabilize && fViewerState is null) | |
1554 fViewerState= new ViewerState(); | |
1555 } else if (DocumentRewriteSessionEvent.SESSION_STOP is event.getChangeType()) { | |
1556 if (viewportStabilize && fViewerState !is null) { | |
1557 fViewerState.restore(true); | |
1558 fViewerState= null; | |
1559 } | |
1560 target.endCompoundChange(); | |
1561 if (toggleRedraw) | |
1562 target.setRedraw(true); | |
1563 } | |
1564 } | |
1565 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1566 |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1567 |
129 | 1568 /** |
1569 * Identifies the scrollbars as originators of a view port change. | |
1570 */ | |
1571 protected static final int SCROLLER= 1; | |
1572 /** | |
1573 * Identifies mouse moves as originators of a view port change. | |
1574 */ | |
1575 protected static final int MOUSE= 2; | |
1576 /** | |
1577 * Identifies mouse button up as originator of a view port change. | |
1578 */ | |
1579 protected static final int MOUSE_END= 3; | |
1580 /** | |
1581 * Identifies key strokes as originators of a view port change. | |
1582 */ | |
1583 protected static final int KEY= 4; | |
1584 /** | |
1585 * Identifies window resizing as originator of a view port change. | |
1586 */ | |
1587 protected static final int RESIZE= 5; | |
1588 /** | |
1589 * Identifies internal reasons as originators of a view port change. | |
1590 */ | |
1591 protected static final int INTERNAL= 6; | |
1592 | |
1593 /** Internal name of the position category used selection preservation during shift. */ | |
1594 protected static final String SHIFTING= "__TextViewer_shifting"; //$NON-NLS-1$ | |
1595 | |
1596 /** | |
1597 * Base position category name used by the selection updater | |
1598 * @since 3.1 | |
1599 */ | |
147 | 1600 private static const String SELECTION_POSITION_CATEGORY= "_textviewer_selection_category"; //$NON-NLS-1$ |
129 | 1601 /** The viewer's text widget */ |
1602 private StyledText fTextWidget; | |
1603 /** The viewer's input document */ | |
1604 private IDocument fDocument; | |
1605 /** The viewer's visible document */ | |
1606 private IDocument fVisibleDocument; | |
1607 /** The viewer's document adapter */ | |
1608 private IDocumentAdapter fDocumentAdapter; | |
1609 /** The slave document manager */ | |
1610 private ISlaveDocumentManager fSlaveDocumentManager; | |
1611 /** The text viewer's double click strategies connector */ | |
1612 private TextDoubleClickStrategyConnector fDoubleClickStrategyConnector; | |
1613 /** The text viewer's view port guard */ | |
1614 private ViewportGuard fViewportGuard; | |
1615 /** Caches the graphical coordinate of the first visible line */ | |
1616 private int fTopInset= 0; | |
1617 /** The most recent document modification as widget command */ | |
1618 private WidgetCommand fWidgetCommand= new WidgetCommand(); | |
1619 /** The DWT control's scrollbars */ | |
1620 private ScrollBar fScroller; | |
1621 /** Listener on the visible document */ | |
1622 private VisibleDocumentListener fVisibleDocumentListener= new VisibleDocumentListener(); | |
1623 /** Verify listener */ | |
1624 private TextVerifyListener fVerifyListener= new TextVerifyListener(); | |
1625 /** The most recent widget modification as document command */ | |
1626 private DocumentCommand fDocumentCommand= new DocumentCommand(); | |
1627 /** The viewer's find/replace target */ | |
1628 private IFindReplaceTarget fFindReplaceTarget; | |
1629 /** | |
1630 * The text viewer's hovering controller | |
1631 * @since 2.0 | |
1632 */ | |
1633 private TextViewerHoverManager fTextHoverManager; | |
1634 /** | |
1635 * The viewer widget token keeper | |
1636 * @since 2.0 | |
1637 */ | |
1638 private IWidgetTokenKeeper fWidgetTokenKeeper; | |
1639 /** | |
1640 * The viewer's manager of verify key listeners | |
1641 * @since 2.0 | |
1642 */ | |
1643 private VerifyKeyListenersManager fVerifyKeyListenersManager= new VerifyKeyListenersManager(); | |
1644 /** | |
1645 * The mark position. | |
1646 * @since 2.0 | |
1647 */ | |
1648 protected Position fMarkPosition; | |
1649 /** | |
1650 * The mark position category. | |
1651 * @since 2.0 | |
1652 */ | |
146 | 1653 private const String MARK_POSITION_CATEGORY="__mark_category_" + hashCode(); //$NON-NLS-1$ |
129 | 1654 /** |
1655 * The mark position updater | |
1656 * @since 2.0 | |
1657 */ | |
146 | 1658 private const IPositionUpdater fMarkPositionUpdater= new DefaultPositionUpdater(MARK_POSITION_CATEGORY); |
129 | 1659 /** |
1660 * The flag indicating the redraw behavior | |
1661 * @since 2.0 | |
1662 */ | |
1663 private int fRedrawCounter= 0; | |
1664 /** | |
1665 * The viewer's rewrite target | |
1666 * @since 2.0 | |
1667 */ | |
1668 private IRewriteTarget fRewriteTarget; | |
1669 /** | |
1670 * The viewer's cursor listener. | |
1671 * @since 3.0 | |
1672 */ | |
1673 private CursorListener fCursorListener; | |
1674 /** | |
1675 * Last selection range sent to selection change listeners. | |
1676 * @since 3.0 | |
1677 */ | |
1678 private IRegion fLastSentSelectionChange; | |
1679 /** | |
1680 * The registered post selection changed listeners. | |
1681 * @since 3.0 | |
1682 */ | |
1683 private List fPostSelectionChangedListeners; | |
1684 /** | |
1685 * Queued post selection changed events count. | |
1686 * @since 3.0 | |
1687 */ | |
146 | 1688 private const int[] fNumberOfPostSelectionChangedEvents= new int[1]; |
129 | 1689 /** |
1690 * Last selection range sent to post selection change listeners. | |
1691 * @since 3.0 | |
1692 */ | |
1693 private IRegion fLastSentPostSelectionChange; | |
1694 /** | |
1695 * The set of registered editor helpers. | |
1696 * @since 3.1 | |
1697 */ | |
1698 private Set fEditorHelpers= new HashSet(); | |
1699 /** | |
1700 * The internal rewrite session listener. | |
1701 * @since 3.1 | |
1702 */ | |
1703 private DocumentRewriteSessionListener fDocumentRewriteSessionListener= new DocumentRewriteSessionListener(); | |
1704 | |
1705 /** Should the auto indent strategies ignore the next edit operation */ | |
1706 protected bool fIgnoreAutoIndent= false; | |
1707 /** The strings a line is prefixed with on SHIFT_RIGHT and removed from each line on SHIFT_LEFT */ | |
1708 protected Map fIndentChars; | |
1709 /** The string a line is prefixed with on PREFIX and removed from each line on STRIP_PREFIX */ | |
1710 protected Map fDefaultPrefixChars; | |
1711 /** The text viewer's text double click strategies */ | |
1712 protected Map fDoubleClickStrategies; | |
1713 /** The text viewer's undo manager */ | |
1714 protected IUndoManager fUndoManager; | |
1715 /** The text viewer's auto indent strategies */ | |
1716 protected Map fAutoIndentStrategies; | |
1717 /** The text viewer's text hovers */ | |
1718 protected Map fTextHovers; | |
1719 /** All registered view port listeners> */ | |
1720 protected List fViewportListeners; | |
1721 /** The last visible vertical position of the top line */ | |
1722 protected int fLastTopPixel; | |
1723 /** All registered text listeners */ | |
1724 protected List fTextListeners; | |
1725 /** All registered text input listeners */ | |
1726 protected List fTextInputListeners; | |
1727 /** The text viewer's event consumer */ | |
1728 protected IEventConsumer fEventConsumer; | |
1729 /** Indicates whether the viewer's text presentation should be replaced are modified. */ | |
1730 protected bool fReplaceTextPresentation= false; | |
1731 /** | |
1732 * The creator of the text hover control | |
1733 * @since 2.0 | |
1734 */ | |
1735 protected IInformationControlCreator fHoverControlCreator; | |
1736 /** | |
1737 * The mapping between model and visible document. | |
1738 * @since 2.1 | |
1739 */ | |
1740 protected IDocumentInformationMapping fInformationMapping; | |
1741 /** | |
1742 * The viewer's paint manager. | |
1743 * @since 2.1 | |
1744 */ | |
1745 protected PaintManager fPaintManager; | |
1746 /** | |
1747 * The viewers partitioning. I.e. the partitioning name the viewer uses to access partitioning information of its input document. | |
1748 * @since 3.0 | |
1749 */ | |
1750 protected String fPartitioning; | |
1751 /** | |
1752 * All registered text presentation listeners. | |
1753 * since 3.0 | |
1754 */ | |
1755 protected List fTextPresentationListeners; | |
1756 /** | |
1757 * The find/replace document adapter. | |
1758 * @since 3.0 | |
1759 */ | |
1760 protected FindReplaceDocumentAdapter fFindReplaceDocumentAdapter; | |
1761 | |
1762 /** | |
1763 * The text viewer's hyperlink detectors. | |
1764 * @since 3.1 | |
1765 */ | |
1766 protected IHyperlinkDetector[] fHyperlinkDetectors; | |
1767 /** | |
1768 * The text viewer's hyperlink presenter. | |
1769 * @since 3.1 | |
1770 */ | |
1771 protected IHyperlinkPresenter fHyperlinkPresenter; | |
1772 /** | |
1773 * The text viewer's hyperlink manager. | |
1774 * @since 3.1 | |
1775 */ | |
1776 protected HyperlinkManager fHyperlinkManager; | |
1777 /** | |
1778 * The DWT key modifier mask which in combination | |
1779 * with the left mouse button triggers the hyperlink mode. | |
1780 * @since 3.1 | |
1781 */ | |
1782 protected int fHyperlinkStateMask; | |
1783 /** | |
1784 * The viewer state when in non-redraw state, <code>null</code> otherwise. | |
1785 * @since 3.3 | |
1786 */ | |
1787 private ViewerState fViewerState; | |
1788 /** | |
1789 * The editor's tab converter. | |
1790 * @since 3.3 | |
1791 */ | |
1792 private IAutoEditStrategy fTabsToSpacesConverter; | |
1793 | |
1794 | |
1795 //---- Construction and disposal ------------------ | |
1796 | |
1797 | |
1798 /** | |
1799 * Internal use only | |
1800 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
1801 protected this() { |
129 | 1802 } |
1803 | |
1804 /** | |
1805 * Create a new text viewer with the given DWT style bits. | |
1806 * The viewer is ready to use but does not have any plug-in installed. | |
1807 * | |
1808 * @param parent the parent of the viewer's control | |
1809 * @param styles the DWT style bits for the viewer's control, | |
1810 * <em>if <code>DWT.WRAP</code> is set then a custom document adapter needs to be provided, see {@link #createDocumentAdapter()} | |
1811 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
1812 public this(Composite parent, int styles) { |
129 | 1813 createControl(parent, styles); |
1814 } | |
1815 | |
1816 /** | |
1817 * Factory method to create the text widget to be used as the viewer's text widget. | |
1818 * | |
1819 * @param parent the parent of the styled text | |
1820 * @param styles the styles for the styled text | |
1821 * @return the text widget to be used | |
1822 */ | |
1823 protected StyledText createTextWidget(Composite parent, int styles) { | |
1824 return new StyledText(parent, styles); | |
1825 } | |
1826 | |
1827 /** | |
1828 * Factory method to create the document adapter to be used by this viewer. | |
1829 * | |
1830 * @return the document adapter to be used | |
1831 */ | |
1832 protected IDocumentAdapter createDocumentAdapter() { | |
1833 return new DefaultDocumentAdapter(); | |
1834 } | |
1835 | |
1836 /** | |
1837 * Creates the viewer's DWT control. The viewer's text widget either is | |
1838 * the control or is a child of the control. | |
1839 * | |
1840 * @param parent the parent of the viewer's control | |
1841 * @param styles the DWT style bits for the viewer's control | |
1842 */ | |
1843 protected void createControl(Composite parent, int styles) { | |
1844 | |
1845 fTextWidget= createTextWidget(parent, styles); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1846 |
129 | 1847 // Support scroll page upon MOD1+MouseWheel |
135 | 1848 fTextWidget.addListener(DWT.MouseWheel, new class() Listener { |
129 | 1849 |
1850 public void handleEvent(Event event) { | |
1851 if (((event.stateMask & DWT.MOD1) is 0)) | |
1852 return; | |
1853 | |
1854 int topIndex= fTextWidget.getTopIndex(); | |
1855 int bottomIndex= JFaceTextUtil.getBottomIndex(fTextWidget); | |
1856 | |
1857 if (event.count > 0) | |
1858 fTextWidget.setTopIndex(2 * topIndex - bottomIndex); | |
1859 else | |
1860 fTextWidget.setTopIndex(bottomIndex); | |
1861 | |
1862 updateViewportListeners(INTERNAL); | |
1863 } | |
1864 }); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1865 |
129 | 1866 fTextWidget.addDisposeListener( |
135 | 1867 new class() DisposeListener { |
129 | 1868 public void widgetDisposed(DisposeEvent e) { |
1869 handleDispose(); | |
1870 } | |
1871 } | |
1872 ); | |
1873 | |
1874 fTextWidget.setFont(parent.getFont()); | |
1875 fTextWidget.setDoubleClickEnabled(true); | |
1876 | |
1877 /* | |
1878 * Disable DWT Shift+TAB traversal in this viewer | |
1879 * 1GIYQ9K: ITPUI:WINNT - StyledText swallows Shift+TAB | |
1880 */ | |
135 | 1881 fTextWidget.addTraverseListener(new class() TraverseListener { |
129 | 1882 public void keyTraversed(TraverseEvent e) { |
1883 if ((DWT.SHIFT is e.stateMask) && ('\t' is e.character)) | |
1884 e.doit= false; | |
1885 } | |
1886 }); | |
1887 | |
1888 // where does the first line start | |
1889 fTopInset= -fTextWidget.computeTrim(0, 0, 0, 0).y; | |
1890 | |
1891 fVerifyListener.forward(true); | |
1892 fTextWidget.addVerifyListener(fVerifyListener); | |
1893 | |
135 | 1894 fTextWidget.addSelectionListener(new class() SelectionListener { |
129 | 1895 public void widgetDefaultSelected(SelectionEvent event) { |
1896 selectionChanged(event.x, event.y - event.x); | |
1897 } | |
1898 public void widgetSelected(SelectionEvent event) { | |
1899 selectionChanged(event.x, event.y - event.x); | |
1900 } | |
1901 }); | |
1902 | |
1903 fCursorListener= new CursorListener(); | |
1904 fCursorListener.install(); | |
1905 | |
1906 initializeViewportUpdate(); | |
1907 } | |
1908 | |
1909 /* | |
1910 * @see Viewer#getControl() | |
1911 */ | |
1912 public Control getControl() { | |
1913 return fTextWidget; | |
1914 } | |
1915 | |
1916 /* | |
1917 * @see ITextViewer#activatePlugins() | |
1918 */ | |
1919 public void activatePlugins() { | |
1920 | |
1921 if (fDoubleClickStrategies !is null && !fDoubleClickStrategies.isEmpty() && fDoubleClickStrategyConnector is null) { | |
1922 fDoubleClickStrategyConnector= new TextDoubleClickStrategyConnector(); | |
1923 fTextWidget.addWordMovementListener(fDoubleClickStrategyConnector); | |
1924 fTextWidget.addMouseListener(fDoubleClickStrategyConnector); | |
1925 } | |
1926 | |
1927 ensureHoverControlManagerInstalled(); | |
1928 ensureHyperlinkManagerInstalled(); | |
1929 | |
1930 if (fUndoManager !is null) { | |
1931 fUndoManager.connect(this); | |
1932 fUndoManager.reset(); | |
1933 } | |
1934 } | |
1935 | |
1936 /** | |
1937 * After this method has been executed the caller knows that any installed text hover has been installed. | |
1938 */ | |
1939 private void ensureHoverControlManagerInstalled() { | |
1940 if (fTextHovers !is null && !fTextHovers.isEmpty() && fHoverControlCreator !is null && fTextHoverManager is null) { | |
1941 fTextHoverManager= new TextViewerHoverManager(this, fHoverControlCreator); | |
1942 fTextHoverManager.install(this.getTextWidget()); | |
1943 fTextHoverManager.setSizeConstraints(TEXT_HOVER_WIDTH_CHARS, TEXT_HOVER_HEIGHT_CHARS, false, true); | |
1944 fTextHoverManager.setInformationControlReplacer(new StickyHoverManager(this)); | |
1945 } | |
1946 } | |
1947 | |
1948 /* | |
1949 * @see ITextViewer#resetPlugins() | |
1950 */ | |
1951 public void resetPlugins() { | |
1952 if (fUndoManager !is null) | |
1953 fUndoManager.reset(); | |
1954 } | |
1955 | |
1956 /** | |
1957 * Frees all resources allocated by this viewer. Internally called when the viewer's | |
1958 * control has been disposed. | |
1959 */ | |
1960 protected void handleDispose() { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1961 |
129 | 1962 setDocument(null); |
1963 | |
1964 if (fPaintManager !is null) { | |
1965 fPaintManager.dispose(); | |
1966 fPaintManager= null; | |
1967 } | |
1968 | |
1969 removeViewPortUpdate(); | |
1970 fViewportGuard= null; | |
1971 | |
1972 if (fViewportListeners !is null) { | |
1973 fViewportListeners.clear(); | |
1974 fViewportListeners= null; | |
1975 } | |
1976 | |
1977 if (fTextListeners !is null) { | |
1978 fTextListeners.clear(); | |
1979 fTextListeners= null; | |
1980 } | |
1981 | |
1982 if (fTextInputListeners !is null) { | |
1983 fTextInputListeners.clear(); | |
1984 fTextInputListeners= null; | |
1985 } | |
1986 | |
1987 if (fPostSelectionChangedListeners !is null) { | |
1988 fPostSelectionChangedListeners.clear(); | |
1989 fPostSelectionChangedListeners= null; | |
1990 } | |
1991 | |
1992 if (fAutoIndentStrategies !is null) { | |
1993 fAutoIndentStrategies.clear(); | |
1994 fAutoIndentStrategies= null; | |
1995 } | |
1996 | |
1997 if (fUndoManager !is null) { | |
1998 fUndoManager.disconnect(); | |
1999 fUndoManager= null; | |
2000 } | |
2001 | |
2002 if (fDoubleClickStrategies !is null) { | |
2003 fDoubleClickStrategies.clear(); | |
2004 fDoubleClickStrategies= null; | |
2005 } | |
2006 | |
2007 if (fTextHovers !is null) { | |
2008 fTextHovers.clear(); | |
2009 fTextHovers= null; | |
2010 } | |
2011 | |
2012 fDoubleClickStrategyConnector= null; | |
2013 | |
2014 if (fTextHoverManager !is null) { | |
2015 fTextHoverManager.dispose(); | |
2016 fTextHoverManager= null; | |
2017 } | |
2018 | |
2019 if (fVisibleDocumentListener !is null) { | |
2020 if (fVisibleDocument !is null) | |
2021 fVisibleDocument.removeDocumentListener(fVisibleDocumentListener); | |
2022 fVisibleDocumentListener= null; | |
2023 } | |
2024 | |
2025 if (fDocumentAdapter !is null) { | |
2026 fDocumentAdapter.setDocument(null); | |
2027 fDocumentAdapter= null; | |
2028 } | |
2029 | |
2030 if (fSlaveDocumentManager !is null) { | |
2031 if (fVisibleDocument !is null) | |
2032 fSlaveDocumentManager.freeSlaveDocument(fVisibleDocument); | |
2033 fSlaveDocumentManager= null; | |
2034 } | |
2035 | |
2036 if (fCursorListener !is null) { | |
2037 fCursorListener.uninstall(); | |
2038 fCursorListener= null; | |
2039 } | |
2040 | |
2041 if (fHyperlinkManager !is null) { | |
2042 fHyperlinkManager.uninstall(); | |
2043 fHyperlinkManager= null; | |
2044 } | |
2045 | |
2046 fHyperlinkDetectors= null; | |
2047 fVisibleDocument= null; | |
2048 fDocument= null; | |
2049 fScroller= null; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
2050 |
129 | 2051 fTextWidget= null; |
2052 } | |
2053 | |
2054 | |
2055 //---- simple getters and setters | |
2056 | |
2057 /* | |
2058 * @see dwtx.jface.text.ITextViewer#getTextWidget() | |
2059 */ | |
2060 public StyledText getTextWidget() { | |
2061 return fTextWidget; | |
2062 } | |
2063 | |
2064 /** | |
2065 * The delay in milliseconds before an empty selection | |
2066 * changed event is sent by the cursor listener. | |
2067 * <p> | |
2068 * Note: The return value is used to initialize the cursor | |
2069 * listener. To return a non-constant value has no effect.</p> | |
2070 * <p> | |
2071 * The same value (<code>500</code>) is used in <code>OpenStrategy.TIME</code>.</p> | |
2072 * | |
2073 * @return delay in milliseconds | |
2074 * @see dwtx.jface.util.OpenStrategy | |
2075 * @since 3.0 | |
2076 */ | |
2077 protected int getEmptySelectionChangedEventDelay() { | |
2078 return 500; | |
2079 } | |
2080 | |
2081 /** | |
2082 * {@inheritDoc} | |
2083 * @deprecated since 3.1, use | |
2084 * {@link ITextViewerExtension2#prependAutoEditStrategy(IAutoEditStrategy, String)} and | |
2085 * {@link ITextViewerExtension2#removeAutoEditStrategy(IAutoEditStrategy, String)} instead | |
2086 */ | |
2087 public void setAutoIndentStrategy(IAutoIndentStrategy strategy, String contentType) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
2088 setAutoEditStrategies([ strategy ], contentType); |
129 | 2089 } |
2090 | |
2091 /** | |
2092 * Sets the given edit strategy as the only strategy for the given content type. | |
2093 * | |
2094 * @param strategies the auto edit strategies | |
2095 * @param contentType the content type | |
2096 * @since 3.1 | |
2097 */ | |
2098 protected final void setAutoEditStrategies(IAutoEditStrategy[] strategies, String contentType) { | |
2099 if (fAutoIndentStrategies is null) | |
2100 fAutoIndentStrategies= new HashMap(); | |
2101 | |
134 | 2102 List autoEditStrategies= cast(List) fAutoIndentStrategies.get(contentType); |
129 | 2103 |
2104 if (strategies is null) { | |
2105 if (autoEditStrategies is null) | |
2106 return; | |
2107 | |
2108 fAutoIndentStrategies.put(contentType, null); | |
2109 | |
2110 } else { | |
2111 if (autoEditStrategies is null) { | |
2112 autoEditStrategies= new ArrayList(); | |
2113 fAutoIndentStrategies.put(contentType, autoEditStrategies); | |
2114 } | |
2115 | |
2116 autoEditStrategies.clear(); | |
2117 autoEditStrategies.addAll(Arrays.asList(strategies)); | |
2118 } | |
2119 } | |
2120 | |
2121 /* | |
2122 * @see dwtx.jface.text.ITextViewerExtension2#prependAutoEditStrategy(dwtx.jface.text.IAutoEditStrategy, java.lang.String) | |
2123 * @since 2.1 | |
2124 */ | |
2125 public void prependAutoEditStrategy(IAutoEditStrategy strategy, String contentType) { | |
2126 | |
2127 if (strategy is null || contentType is null) | |
2128 throw new IllegalArgumentException(); | |
2129 | |
2130 if (fAutoIndentStrategies is null) | |
2131 fAutoIndentStrategies= new HashMap(); | |
2132 | |
134 | 2133 List autoEditStrategies= cast(List) fAutoIndentStrategies.get(contentType); |
129 | 2134 if (autoEditStrategies is null) { |
2135 autoEditStrategies= new ArrayList(); | |
2136 fAutoIndentStrategies.put(contentType, autoEditStrategies); | |
2137 } | |
2138 | |
2139 autoEditStrategies.add(0, strategy); | |
2140 } | |
2141 | |
2142 /* | |
2143 * @see dwtx.jface.text.ITextViewerExtension2#removeAutoEditStrategy(dwtx.jface.text.IAutoEditStrategy, java.lang.String) | |
2144 * @since 2.1 | |
2145 */ | |
2146 public void removeAutoEditStrategy(IAutoEditStrategy strategy, String contentType) { | |
2147 if (fAutoIndentStrategies is null) | |
2148 return; | |
2149 | |
134 | 2150 List autoEditStrategies= cast(List) fAutoIndentStrategies.get(contentType); |
129 | 2151 if (autoEditStrategies is null) |
2152 return; | |
2153 | |
2154 for (final Iterator iterator= autoEditStrategies.iterator(); iterator.hasNext(); ) { | |
2155 if (iterator.next().equals(strategy)) { | |
2156 iterator.remove(); | |
2157 break; | |
2158 } | |
2159 } | |
2160 | |
2161 if (autoEditStrategies.isEmpty()) | |
2162 fAutoIndentStrategies.put(contentType, null); | |
2163 } | |
2164 | |
2165 /* | |
2166 * @see ITextViewer#setEventConsumer(IEventConsumer) | |
2167 */ | |
2168 public void setEventConsumer(IEventConsumer consumer) { | |
2169 fEventConsumer= consumer; | |
2170 } | |
2171 | |
2172 /* | |
2173 * @see ITextViewer#setIndentPrefixes(String[], String) | |
2174 */ | |
2175 public void setIndentPrefixes(String[] indentPrefixes, String contentType) { | |
2176 | |
2177 int i= -1; | |
2178 bool ok= (indentPrefixes !is null); | |
2179 while (ok && ++i < indentPrefixes.length) | |
2180 ok= (indentPrefixes[i] !is null); | |
2181 | |
2182 if (ok) { | |
2183 | |
2184 if (fIndentChars is null) | |
2185 fIndentChars= new HashMap(); | |
2186 | |
2187 fIndentChars.put(contentType, indentPrefixes); | |
2188 | |
2189 } else if (fIndentChars !is null) | |
2190 fIndentChars.remove(contentType); | |
2191 } | |
2192 | |
2193 /* | |
2194 * @see ITextViewer#getTopInset() | |
2195 */ | |
2196 public int getTopInset() { | |
2197 return fTopInset; | |
2198 } | |
2199 | |
2200 /* | |
2201 * @see ITextViewer#isEditable() | |
2202 */ | |
2203 public bool isEditable() { | |
2204 if (fTextWidget is null) | |
2205 return false; | |
2206 return fTextWidget.getEditable(); | |
2207 } | |
2208 | |
2209 /* | |
2210 * @see ITextViewer#setEditable(bool) | |
2211 */ | |
2212 public void setEditable(bool editable) { | |
2213 if (fTextWidget !is null) | |
2214 fTextWidget.setEditable(editable); | |
2215 } | |
2216 | |
2217 /* | |
2218 * @see ITextViewer#setDefaultPrefixes | |
2219 * @since 2.0 | |
2220 */ | |
2221 public void setDefaultPrefixes(String[] defaultPrefixes, String contentType) { | |
2222 | |
2223 if (defaultPrefixes !is null && defaultPrefixes.length > 0) { | |
2224 if (fDefaultPrefixChars is null) | |
2225 fDefaultPrefixChars= new HashMap(); | |
2226 fDefaultPrefixChars.put(contentType, defaultPrefixes); | |
2227 } else if (fDefaultPrefixChars !is null) | |
2228 fDefaultPrefixChars.remove(contentType); | |
2229 } | |
2230 | |
2231 /* | |
2232 * @see ITextViewer#setUndoManager(IUndoManager) | |
2233 */ | |
2234 public void setUndoManager(IUndoManager undoManager) { | |
2235 fUndoManager= undoManager; | |
2236 } | |
2237 | |
2238 /* | |
2239 * @see ITextViewerExtension6#getUndoManager() | |
2240 * @since 3.1 | |
2241 */ | |
2242 public IUndoManager getUndoManager() { | |
2243 return fUndoManager; | |
2244 } | |
2245 | |
2246 /* | |
2247 * @see ITextViewer#setTextHover(ITextHover, String) | |
2248 */ | |
2249 public void setTextHover(ITextHover hover, String contentType) { | |
2250 setTextHover(hover, contentType, ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK); | |
2251 } | |
2252 | |
2253 /* | |
2254 * @see ITextViewerExtension2#setTextHover(ITextHover, String, int) | |
2255 * @since 2.1 | |
2256 */ | |
2257 public void setTextHover(ITextHover hover, String contentType, int stateMask) { | |
2258 TextHoverKey key= new TextHoverKey(contentType, stateMask); | |
2259 if (hover !is null) { | |
2260 if (fTextHovers is null) { | |
2261 fTextHovers= new HashMap(); | |
2262 } | |
2263 fTextHovers.put(key, hover); | |
2264 } else if (fTextHovers !is null) | |
2265 fTextHovers.remove(key); | |
2266 | |
2267 ensureHoverControlManagerInstalled(); | |
2268 } | |
2269 | |
2270 /* | |
2271 * @see ITextViewerExtension2#removeTextHovers(String) | |
2272 * @since 2.1 | |
2273 */ | |
2274 public void removeTextHovers(String contentType) { | |
2275 if (fTextHovers is null) | |
2276 return; | |
2277 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
2278 Iterator iter= (new HashSet(fTextHovers.keySet())).iterator(); |
129 | 2279 while (iter.hasNext()) { |
134 | 2280 TextHoverKey key= cast(TextHoverKey)iter.next(); |
129 | 2281 if (key.fContentType.equals(contentType)) |
2282 fTextHovers.remove(key); | |
2283 } | |
2284 } | |
2285 | |
2286 /** | |
2287 * Returns the text hover for a given offset. | |
2288 * | |
2289 * @param offset the offset for which to return the text hover | |
2290 * @return the text hover for the given offset | |
2291 */ | |
2292 protected ITextHover getTextHover(int offset) { | |
2293 return getTextHover(offset, ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK); | |
2294 } | |
2295 | |
2296 /** | |
2297 * Returns the text hover for a given offset and a given state mask. | |
2298 * | |
2299 * @param offset the offset for which to return the text hover | |
2300 * @param stateMask the DWT event state mask | |
2301 * @return the text hover for the given offset and state mask | |
2302 * @since 2.1 | |
2303 */ | |
2304 protected ITextHover getTextHover(int offset, int stateMask) { | |
2305 if (fTextHovers is null) | |
2306 return null; | |
2307 | |
2308 IDocument document= getDocument(); | |
2309 if (document is null) | |
2310 return null; | |
2311 | |
2312 try { | |
2313 TextHoverKey key= new TextHoverKey(TextUtilities.getContentType(document, getDocumentPartitioning(), offset, true), stateMask); | |
2314 Object textHover= fTextHovers.get(key); | |
2315 if (textHover is null) { | |
2316 // Use default text hover | |
2317 key.setStateMask(ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK); | |
2318 textHover= fTextHovers.get(key); | |
2319 } | |
134 | 2320 return cast(ITextHover) textHover; |
129 | 2321 } catch (BadLocationException x) { |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
2322 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
2323 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.selectContentTypePlugin")); //$NON-NLS-1$ |
129 | 2324 } |
2325 return null; | |
2326 } | |
2327 | |
2328 /** | |
2329 * Returns the text hovering controller of this viewer. | |
2330 * | |
2331 * @return the text hovering controller of this viewer | |
2332 * @since 2.0 | |
2333 */ | |
2334 protected AbstractInformationControlManager getTextHoveringController() { | |
2335 return fTextHoverManager; | |
2336 } | |
2337 | |
2338 /** | |
2339 * Sets the creator for the hover controls. | |
2340 * | |
2341 * @param creator the hover control creator | |
2342 * @since 2.0 | |
2343 */ | |
2344 public void setHoverControlCreator(IInformationControlCreator creator) { | |
2345 fHoverControlCreator= creator; | |
2346 } | |
2347 | |
2348 /** | |
2349 * {@inheritDoc} | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
2350 * |
129 | 2351 * @since 3.4 |
2352 */ | |
2353 public void setHoverEnrichMode(ITextViewerExtension8.EnrichMode mode) { | |
2354 if (fTextHoverManager is null) | |
2355 return; | |
2356 fTextHoverManager.setHoverEnrichMode(mode); | |
2357 } | |
2358 | |
2359 /* | |
2360 * @see IWidgetTokenOwner#requestWidgetToken(IWidgetTokenKeeper) | |
2361 * @since 2.0 | |
2362 */ | |
2363 public bool requestWidgetToken(IWidgetTokenKeeper requester) { | |
2364 if (fTextWidget !is null) { | |
2365 if (fWidgetTokenKeeper !is null) { | |
2366 if (fWidgetTokenKeeper is requester) | |
2367 return true; | |
2368 if (fWidgetTokenKeeper.requestWidgetToken(this)) { | |
2369 fWidgetTokenKeeper= requester; | |
2370 return true; | |
2371 } | |
2372 } else { | |
2373 fWidgetTokenKeeper= requester; | |
2374 return true; | |
2375 } | |
2376 } | |
2377 return false; | |
2378 } | |
2379 | |
2380 /* | |
2381 * @see dwtx.jface.text.IWidgetTokenOwnerExtension#requestWidgetToken(dwtx.jface.text.IWidgetTokenKeeper, int) | |
2382 * @since 3.0 | |
2383 */ | |
2384 public bool requestWidgetToken(IWidgetTokenKeeper requester, int priority) { | |
2385 if (fTextWidget !is null) { | |
2386 if (fWidgetTokenKeeper !is null) { | |
2387 | |
2388 if (fWidgetTokenKeeper is requester) | |
2389 return true; | |
2390 | |
2391 bool accepted= false; | |
138 | 2392 if ( cast(IWidgetTokenKeeperExtension)fWidgetTokenKeeper ) { |
134 | 2393 IWidgetTokenKeeperExtension extension= cast(IWidgetTokenKeeperExtension) fWidgetTokenKeeper; |
129 | 2394 accepted= extension.requestWidgetToken(this, priority); |
2395 } else { | |
2396 accepted= fWidgetTokenKeeper.requestWidgetToken(this); | |
2397 } | |
2398 | |
2399 if (accepted) { | |
2400 fWidgetTokenKeeper= requester; | |
2401 return true; | |
2402 } | |
2403 | |
2404 } else { | |
2405 fWidgetTokenKeeper= requester; | |
2406 return true; | |
2407 } | |
2408 } | |
2409 return false; | |
2410 } | |
2411 | |
2412 /* | |
2413 * @see IWidgetTokenOwner#releaseWidgetToken(IWidgetTokenKeeper) | |
2414 * @since 2.0 | |
2415 */ | |
2416 public void releaseWidgetToken(IWidgetTokenKeeper tokenKeeper) { | |
2417 if (fWidgetTokenKeeper is tokenKeeper) | |
2418 fWidgetTokenKeeper= null; | |
2419 } | |
2420 | |
2421 | |
2422 //---- Selection | |
2423 | |
2424 /* | |
2425 * @see ITextViewer#getSelectedRange() | |
2426 */ | |
2427 public Point getSelectedRange() { | |
2428 | |
2429 if (!redraws() && fViewerState !is null) | |
2430 return fViewerState.getSelection(); | |
2431 | |
2432 if (fTextWidget !is null) { | |
2433 Point p= fTextWidget.getSelectionRange(); | |
2434 p= widgetSelection2ModelSelection(p); | |
2435 if (p !is null) | |
2436 return p; | |
2437 } | |
2438 | |
2439 return new Point(-1, -1); | |
2440 } | |
2441 | |
2442 /* | |
2443 * @see ITextViewer#setSelectedRange(int, int) | |
2444 */ | |
2445 public void setSelectedRange(int selectionOffset, int selectionLength) { | |
2446 | |
2447 if (!redraws()) { | |
2448 if (fViewerState !is null) | |
2449 fViewerState.updateSelection(selectionOffset, selectionLength); | |
2450 return; | |
2451 } | |
2452 | |
2453 if (fTextWidget is null) | |
2454 return; | |
2455 | |
2456 IRegion widgetSelection= modelRange2ClosestWidgetRange(new Region(selectionOffset, selectionLength)); | |
2457 if (widgetSelection !is null) { | |
2458 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
2459 int[] selectionRange= [ widgetSelection.getOffset(), widgetSelection.getLength() ]; |
129 | 2460 validateSelectionRange(selectionRange); |
2461 if (selectionRange[0] >= 0) { | |
2462 fTextWidget.setSelectionRange(selectionRange[0], selectionRange[1]); | |
2463 selectionChanged(selectionRange[0], selectionRange[1]); | |
2464 } | |
2465 } | |
2466 } | |
2467 | |
2468 /** | |
2469 * Validates and adapts the given selection range if it is not a valid | |
2470 * widget selection. The widget selection is invalid if it starts or ends | |
2471 * inside a multi-character line delimiter. If so, the selection is adapted to | |
2472 * start <b>after</b> the divided line delimiter and to end <b>before</b> | |
2473 * the divided line delimiter. The parameter passed in is changed in-place | |
2474 * when being adapted. An adaptation to <code>[-1, -1]</code> indicates | |
2475 * that the selection range could not be validated. | |
2476 * Subclasses may reimplement this method. | |
2477 * | |
2478 * @param selectionRange selectionRange[0] is the offset, selectionRange[1] | |
2479 * the length of the selection to validate. | |
2480 * @since 2.0 | |
2481 */ | |
2482 protected void validateSelectionRange(int[] selectionRange) { | |
2483 | |
2484 IDocument document= getVisibleDocument(); | |
2485 if (document is null) { | |
2486 selectionRange[0]= -1; | |
2487 selectionRange[1]= -1; | |
2488 return; | |
2489 } | |
2490 | |
2491 int documentLength= document.getLength(); | |
2492 int offset= selectionRange[0]; | |
2493 int length= selectionRange[1]; | |
2494 | |
2495 if (length < 0) { | |
2496 length= - length; | |
2497 offset -= length; | |
2498 } | |
2499 | |
2500 if (offset <0) | |
2501 offset= 0; | |
2502 | |
2503 if (offset > documentLength) | |
2504 offset= documentLength; | |
2505 | |
2506 int delta= (offset + length) - documentLength; | |
2507 if (delta > 0) | |
2508 length -= delta; | |
2509 | |
2510 try { | |
2511 | |
2512 int lineNumber= document.getLineOfOffset(offset); | |
2513 IRegion lineInformation= document.getLineInformation(lineNumber); | |
2514 | |
2515 int lineEnd= lineInformation.getOffset() + lineInformation.getLength(); | |
2516 delta= offset - lineEnd; | |
2517 if (delta > 0) { | |
2518 // in the middle of a multi-character line delimiter | |
2519 offset= lineEnd; | |
2520 String delimiter= document.getLineDelimiter(lineNumber); | |
2521 if (delimiter !is null) | |
2522 offset += delimiter.length(); | |
2523 } | |
2524 | |
2525 int end= offset + length; | |
2526 lineInformation= document.getLineInformationOfOffset(end); | |
2527 lineEnd= lineInformation.getOffset() + lineInformation.getLength(); | |
2528 delta= end - lineEnd; | |
2529 if (delta > 0) { | |
2530 // in the middle of a multi-character line delimiter | |
2531 length -= delta; | |
2532 } | |
2533 | |
2534 } catch (BadLocationException x) { | |
2535 selectionRange[0]= -1; | |
2536 selectionRange[1]= -1; | |
2537 return; | |
2538 } | |
2539 | |
2540 if (selectionRange[1] < 0) { | |
2541 selectionRange[0]= offset + length; | |
2542 selectionRange[1]= -length; | |
2543 } else { | |
2544 selectionRange[0]= offset; | |
2545 selectionRange[1]= length; | |
2546 } | |
2547 } | |
2548 | |
2549 /* | |
2550 * @see Viewer#setSelection(ISelection) | |
2551 */ | |
2552 public void setSelection(ISelection selection, bool reveal) { | |
138 | 2553 if ( cast(ITextSelection)selection ) { |
134 | 2554 ITextSelection s= cast(ITextSelection) selection; |
129 | 2555 setSelectedRange(s.getOffset(), s.getLength()); |
2556 if (reveal) | |
2557 revealRange(s.getOffset(), s.getLength()); | |
2558 } | |
2559 } | |
2560 | |
2561 /* | |
2562 * @see Viewer#getSelection() | |
2563 */ | |
2564 public ISelection getSelection() { | |
2565 Point p= getSelectedRange(); | |
2566 if (p.x is -1 || p.y is -1) | |
2567 return TextSelection.emptySelection(); | |
2568 | |
2569 return new TextSelection(getDocument(), p.x, p.y); | |
2570 } | |
2571 | |
2572 /* | |
2573 * @see ITextViewer#getSelectionProvider() | |
2574 */ | |
2575 public ISelectionProvider getSelectionProvider() { | |
2576 return this; | |
2577 } | |
2578 | |
2579 /* | |
2580 * @see dwtx.jface.text.IPostSelectionProvider#addPostSelectionChangedListener(dwtx.jface.viewers.ISelectionChangedListener) | |
2581 * @since 3.0 | |
2582 */ | |
2583 public void addPostSelectionChangedListener(ISelectionChangedListener listener) { | |
2584 | |
2585 Assert.isNotNull(listener); | |
2586 | |
2587 if (fPostSelectionChangedListeners is null) | |
2588 fPostSelectionChangedListeners= new ArrayList(); | |
2589 | |
2590 if (!fPostSelectionChangedListeners.contains(listener)) | |
2591 fPostSelectionChangedListeners.add(listener); | |
2592 } | |
2593 | |
2594 /* | |
2595 * @see dwtx.jface.text.IPostSelectionProvider#removePostSelectionChangedListener(dwtx.jface.viewers.ISelectionChangedListener) | |
2596 * @since 3.0 | |
2597 */ | |
2598 public void removePostSelectionChangedListener(ISelectionChangedListener listener) { | |
2599 | |
2600 Assert.isNotNull(listener); | |
2601 | |
2602 if (fPostSelectionChangedListeners !is null) { | |
2603 fPostSelectionChangedListeners.remove(listener); | |
2604 if (fPostSelectionChangedListeners.size() is 0) | |
2605 fPostSelectionChangedListeners= null; | |
2606 } | |
2607 } | |
2608 | |
2609 /** | |
2610 * Get the text widget's display. | |
2611 * | |
2612 * @return the display or <code>null</code> if the display cannot be retrieved or if the display is disposed | |
2613 * @since 3.0 | |
2614 */ | |
2615 private Display getDisplay() { | |
2616 if (fTextWidget is null || fTextWidget.isDisposed()) | |
2617 return null; | |
2618 | |
2619 Display display= fTextWidget.getDisplay(); | |
2620 if (display !is null && display.isDisposed()) | |
2621 return null; | |
2622 | |
2623 return display; | |
2624 } | |
2625 | |
2626 /** | |
2627 * Starts a timer to send out a post selection changed event. | |
2628 * | |
2629 * @param fireEqualSelection <code>true</code> iff the event must be fired if the selection does not change | |
2630 * @since 3.0 | |
2631 */ | |
156 | 2632 private void queuePostSelectionChanged(bool fireEqualSelection) { |
129 | 2633 Display display= getDisplay(); |
2634 if (display is null) | |
2635 return; | |
2636 | |
2637 fNumberOfPostSelectionChangedEvents[0]++; | |
135 | 2638 display.timerExec(getEmptySelectionChangedEventDelay(), new class() Runnable { |
129 | 2639 final int id= fNumberOfPostSelectionChangedEvents[0]; |
2640 public void run() { | |
2641 if (id is fNumberOfPostSelectionChangedEvents[0]) { | |
2642 // Check again because this is executed after the delay | |
2643 if (getDisplay() !is null) { | |
2644 Point selection= fTextWidget.getSelectionRange(); | |
2645 if (selection !is null) { | |
2646 IRegion r= widgetRange2ModelRange(new Region(selection.x, selection.y)); | |
2647 if (fireEqualSelection || (r !is null && !r.equals(fLastSentPostSelectionChange)) || r is null) { | |
2648 fLastSentPostSelectionChange= r; | |
2649 firePostSelectionChanged(selection.x, selection.y); | |
2650 } | |
2651 } | |
2652 } | |
2653 } | |
2654 } | |
2655 }); | |
2656 } | |
2657 | |
2658 /** | |
2659 * Sends out a text selection changed event to all registered post selection changed listeners. | |
2660 * | |
2661 * @param offset the offset of the newly selected range in the visible document | |
2662 * @param length the length of the newly selected range in the visible document | |
2663 * @since 3.0 | |
2664 */ | |
2665 protected void firePostSelectionChanged(int offset, int length) { | |
2666 if (redraws()) { | |
2667 IRegion r= widgetRange2ModelRange(new Region(offset, length)); | |
2668 ISelection selection= r !is null ? new TextSelection(getDocument(), r.getOffset(), r.getLength()) : TextSelection.emptySelection(); | |
2669 SelectionChangedEvent event= new SelectionChangedEvent(this, selection); | |
2670 firePostSelectionChanged(event); | |
2671 } | |
2672 } | |
2673 | |
2674 /** | |
2675 * Sends out a text selection changed event to all registered listeners and | |
2676 * registers the selection changed event to be sent out to all post selection | |
2677 * listeners. | |
2678 * | |
2679 * @param offset the offset of the newly selected range in the visible document | |
2680 * @param length the length of the newly selected range in the visible document | |
2681 */ | |
2682 protected void selectionChanged(int offset, int length) { | |
2683 queuePostSelectionChanged(true); | |
2684 fireSelectionChanged(offset, length); | |
2685 } | |
2686 | |
2687 /** | |
2688 * Sends out a text selection changed event to all registered listeners. | |
2689 * | |
2690 * @param offset the offset of the newly selected range in the visible document | |
2691 * @param length the length of the newly selected range in the visible document | |
2692 * @since 3.0 | |
2693 */ | |
2694 protected void fireSelectionChanged(int offset, int length) { | |
2695 if (redraws()) { | |
2696 IRegion r= widgetRange2ModelRange(new Region(offset, length)); | |
2697 if ((r !is null && !r.equals(fLastSentSelectionChange)) || r is null) { | |
2698 fLastSentSelectionChange= r; | |
2699 ISelection selection= r !is null ? new TextSelection(getDocument(), r.getOffset(), r.getLength()) : TextSelection.emptySelection(); | |
2700 SelectionChangedEvent event= new SelectionChangedEvent(this, selection); | |
2701 fireSelectionChanged(event); | |
2702 } | |
2703 } | |
2704 } | |
2705 | |
2706 /** | |
2707 * Sends the given event to all registered post selection changed listeners. | |
2708 * | |
2709 * @param event the selection event | |
2710 * @since 3.0 | |
2711 */ | |
2712 private void firePostSelectionChanged(SelectionChangedEvent event) { | |
2713 List listeners= fPostSelectionChangedListeners; | |
2714 if (listeners !is null) { | |
2715 listeners= new ArrayList(listeners); | |
2716 for (int i= 0; i < listeners.size(); i++) { | |
134 | 2717 ISelectionChangedListener l= cast(ISelectionChangedListener) listeners.get(i); |
129 | 2718 l.selectionChanged(event); |
2719 } | |
2720 } | |
2721 } | |
2722 | |
2723 /** | |
2724 * Sends out a mark selection changed event to all registered listeners. | |
2725 * | |
2726 * @param offset the offset of the mark selection in the visible document, the offset is <code>-1</code> if the mark was cleared | |
2727 * @param length the length of the mark selection, may be negative if the caret is before the mark. | |
2728 * @since 2.0 | |
2729 */ | |
2730 protected void markChanged(int offset, int length) { | |
2731 if (redraws()) { | |
2732 | |
2733 if (offset !is -1) { | |
2734 IRegion r= widgetRange2ModelRange(new Region(offset, length)); | |
2735 offset= r.getOffset(); | |
2736 length= r.getLength(); | |
2737 } | |
2738 | |
2739 ISelection selection= new MarkSelection(getDocument(), offset, length); | |
2740 SelectionChangedEvent event= new SelectionChangedEvent(this, selection); | |
2741 fireSelectionChanged(event); | |
2742 } | |
2743 } | |
2744 | |
2745 | |
2746 //---- Text listeners | |
2747 | |
2748 /* | |
2749 * @see ITextViewer#addTextListener(ITextListener) | |
2750 */ | |
2751 public void addTextListener(ITextListener listener) { | |
2752 | |
2753 Assert.isNotNull(listener); | |
2754 | |
2755 if (fTextListeners is null) | |
2756 fTextListeners= new ArrayList(); | |
2757 | |
2758 if (!fTextListeners.contains(listener)) | |
2759 fTextListeners.add(listener); | |
2760 } | |
2761 | |
2762 /* | |
2763 * @see ITextViewer#removeTextListener(ITextListener) | |
2764 */ | |
2765 public void removeTextListener(ITextListener listener) { | |
2766 | |
2767 Assert.isNotNull(listener); | |
2768 | |
2769 if (fTextListeners !is null) { | |
2770 fTextListeners.remove(listener); | |
2771 if (fTextListeners.size() is 0) | |
2772 fTextListeners= null; | |
2773 } | |
2774 } | |
2775 | |
2776 /** | |
2777 * Informs all registered text listeners about the change specified by the | |
2778 * widget command. This method does not use a robust iterator. | |
2779 * | |
2780 * @param cmd the widget command translated into a text event sent to all text listeners | |
2781 */ | |
2782 protected void updateTextListeners(WidgetCommand cmd) { | |
2783 List textListeners= fTextListeners; | |
2784 if (textListeners !is null) { | |
2785 textListeners= new ArrayList(textListeners); | |
2786 DocumentEvent event= cmd.event; | |
138 | 2787 if ( cast(SlaveDocumentEvent)event ) |
134 | 2788 event= (cast(SlaveDocumentEvent) event).getMasterEvent(); |
129 | 2789 |
2790 TextEvent e= new TextEvent(cmd.start, cmd.length, cmd.text, cmd.preservedText, event, redraws()); | |
2791 for (int i= 0; i < textListeners.size(); i++) { | |
134 | 2792 ITextListener l= cast(ITextListener) textListeners.get(i); |
129 | 2793 l.textChanged(e); |
2794 } | |
2795 } | |
2796 } | |
2797 | |
2798 //---- Text input listeners | |
2799 | |
2800 /* | |
2801 * @see ITextViewer#addTextInputListener(ITextInputListener) | |
2802 */ | |
2803 public void addTextInputListener(ITextInputListener listener) { | |
2804 | |
2805 Assert.isNotNull(listener); | |
2806 | |
2807 if (fTextInputListeners is null) | |
2808 fTextInputListeners= new ArrayList(); | |
2809 | |
2810 if (!fTextInputListeners.contains(listener)) | |
2811 fTextInputListeners.add(listener); | |
2812 } | |
2813 | |
2814 /* | |
2815 * @see ITextViewer#removeTextInputListener(ITextInputListener) | |
2816 */ | |
2817 public void removeTextInputListener(ITextInputListener listener) { | |
2818 | |
2819 Assert.isNotNull(listener); | |
2820 | |
2821 if (fTextInputListeners !is null) { | |
2822 fTextInputListeners.remove(listener); | |
2823 if (fTextInputListeners.size() is 0) | |
2824 fTextInputListeners= null; | |
2825 } | |
2826 } | |
2827 | |
2828 /** | |
2829 * Informs all registered text input listeners about the forthcoming input change, | |
2830 * This method does not use a robust iterator. | |
2831 * | |
2832 * @param oldInput the old input document | |
2833 * @param newInput the new input document | |
2834 */ | |
2835 protected void fireInputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) { | |
2836 List listener= fTextInputListeners; | |
2837 if (listener !is null) { | |
2838 for (int i= 0; i < listener.size(); i++) { | |
134 | 2839 ITextInputListener l= cast(ITextInputListener) listener.get(i); |
129 | 2840 l.inputDocumentAboutToBeChanged(oldInput, newInput); |
2841 } | |
2842 } | |
2843 } | |
2844 | |
2845 /** | |
2846 * Informs all registered text input listeners about the successful input change, | |
2847 * This method does not use a robust iterator. | |
2848 * | |
2849 * @param oldInput the old input document | |
2850 * @param newInput the new input document | |
2851 */ | |
2852 protected void fireInputDocumentChanged(IDocument oldInput, IDocument newInput) { | |
2853 List listener= fTextInputListeners; | |
2854 if (listener !is null) { | |
2855 for (int i= 0; i < listener.size(); i++) { | |
134 | 2856 ITextInputListener l= cast(ITextInputListener) listener.get(i); |
129 | 2857 l.inputDocumentChanged(oldInput, newInput); |
2858 } | |
2859 } | |
2860 } | |
2861 | |
2862 //---- Document | |
2863 | |
2864 /* | |
2865 * @see Viewer#getInput() | |
2866 */ | |
2867 public Object getInput() { | |
2868 return getDocument(); | |
2869 } | |
2870 | |
2871 /* | |
2872 * @see ITextViewer#getDocument() | |
2873 */ | |
2874 public IDocument getDocument() { | |
2875 return fDocument; | |
2876 } | |
2877 | |
2878 /* | |
2879 * @see Viewer#setInput(Object) | |
2880 */ | |
2881 public void setInput(Object input) { | |
2882 | |
2883 IDocument document= null; | |
138 | 2884 if ( cast(IDocument)input ) |
134 | 2885 document= cast(IDocument) input; |
129 | 2886 |
2887 setDocument(document); | |
2888 } | |
2889 | |
2890 /* | |
2891 * @see ITextViewer#setDocument(IDocument) | |
2892 */ | |
2893 public void setDocument(IDocument document) { | |
2894 | |
2895 fReplaceTextPresentation= true; | |
2896 fireInputDocumentAboutToBeChanged(fDocument, document); | |
2897 | |
2898 IDocument oldDocument= fDocument; | |
2899 fDocument= document; | |
2900 | |
2901 setVisibleDocument(fDocument); | |
2902 | |
2903 resetPlugins(); | |
2904 inputChanged(fDocument, oldDocument); | |
2905 | |
2906 fireInputDocumentChanged(oldDocument, fDocument); | |
2907 fLastSentSelectionChange= null; | |
2908 fReplaceTextPresentation= false; | |
2909 } | |
2910 | |
2911 /* | |
2912 * @see ITextViewer#setDocument(IDocument, int, int) | |
2913 */ | |
2914 public void setDocument(IDocument document, int modelRangeOffset, int modelRangeLength) { | |
2915 | |
2916 fReplaceTextPresentation= true; | |
2917 fireInputDocumentAboutToBeChanged(fDocument, document); | |
2918 | |
2919 IDocument oldDocument= fDocument; | |
2920 fDocument= document; | |
2921 | |
2922 try { | |
2923 | |
2924 IDocument slaveDocument= createSlaveDocument(document); | |
2925 updateSlaveDocument(slaveDocument, modelRangeOffset, modelRangeLength); | |
2926 setVisibleDocument(slaveDocument); | |
2927 | |
2928 } catch (BadLocationException x) { | |
2929 throw new IllegalArgumentException(JFaceTextMessages.getString("TextViewer.error.invalid_visible_region_1")); //$NON-NLS-1$ | |
2930 } | |
2931 | |
2932 resetPlugins(); | |
2933 inputChanged(fDocument, oldDocument); | |
2934 | |
2935 fireInputDocumentChanged(oldDocument, fDocument); | |
2936 fLastSentSelectionChange= null; | |
2937 fReplaceTextPresentation= false; | |
2938 } | |
2939 | |
2940 /** | |
2941 * Creates a slave document for the given document if there is a slave document manager | |
2942 * associated with this viewer. | |
2943 * | |
2944 * @param document the master document | |
2945 * @return the newly created slave document | |
2946 * @since 2.1 | |
2947 */ | |
2948 protected IDocument createSlaveDocument(IDocument document) { | |
2949 ISlaveDocumentManager manager= getSlaveDocumentManager(); | |
2950 if (manager !is null) { | |
2951 if (manager.isSlaveDocument(document)) | |
2952 return document; | |
2953 return manager.createSlaveDocument(document); | |
2954 } | |
2955 return document; | |
2956 } | |
2957 | |
2958 /** | |
2959 * Sets the given slave document to the specified range of its master document. | |
2960 * | |
2961 * @param visibleDocument the slave document | |
2962 * @param visibleRegionOffset the offset of the master document range | |
2963 * @param visibleRegionLength the length of the master document range | |
2964 * @return <code>true</code> if the slave has been adapted successfully | |
2965 * @throws BadLocationException in case the specified range is not valid in the master document | |
2966 * @since 2.1 | |
2967 * @deprecated use <code>updateSlaveDocument</code> instead | |
2968 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
135
diff
changeset
|
2969 protected bool updateVisibleDocument(IDocument visibleDocument, int visibleRegionOffset, int visibleRegionLength) { |
138 | 2970 if ( cast(ChildDocument)visibleDocument ) { |
134 | 2971 ChildDocument childDocument= cast(ChildDocument) visibleDocument; |
129 | 2972 |
2973 IDocument document= childDocument.getParentDocument(); | |
2974 int line= document.getLineOfOffset(visibleRegionOffset); | |
2975 int offset= document.getLineOffset(line); | |
2976 int length= (visibleRegionOffset - offset) + visibleRegionLength; | |
2977 | |
2978 Position parentRange= childDocument.getParentDocumentRange(); | |
2979 if (offset !is parentRange.getOffset() || length !is parentRange.getLength()) { | |
2980 childDocument.setParentDocumentRange(offset, length); | |
2981 return true; | |
2982 } | |
2983 } | |
2984 return false; | |
2985 } | |
2986 | |
2987 /** | |
2988 * Updates the given slave document to show the specified range of its master document. | |
2989 * | |
2990 * @param slaveDocument the slave document | |
2991 * @param modelRangeOffset the offset of the master document range | |
2992 * @param modelRangeLength the length of the master document range | |
2993 * @return <code>true</code> if the slave has been adapted successfully | |
2994 * @throws BadLocationException in case the specified range is not valid in the master document | |
2995 * @since 3.0 | |
2996 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
135
diff
changeset
|
2997 protected bool updateSlaveDocument(IDocument slaveDocument, int modelRangeOffset, int modelRangeLength) { |
129 | 2998 return updateVisibleDocument(slaveDocument, modelRangeOffset, modelRangeLength); |
2999 } | |
3000 | |
3001 | |
3002 | |
3003 //---- View ports | |
3004 | |
3005 /** | |
3006 * Initializes all listeners and structures required to set up view port listeners. | |
3007 */ | |
3008 private void initializeViewportUpdate() { | |
3009 | |
3010 if (fViewportGuard !is null) | |
3011 return; | |
3012 | |
3013 if (fTextWidget !is null) { | |
3014 | |
3015 fViewportGuard= new ViewportGuard(); | |
3016 fLastTopPixel= -1; | |
3017 | |
3018 fTextWidget.addKeyListener(fViewportGuard); | |
3019 fTextWidget.addMouseListener(fViewportGuard); | |
3020 | |
3021 fScroller= fTextWidget.getVerticalBar(); | |
3022 if (fScroller !is null) | |
3023 fScroller.addSelectionListener(fViewportGuard); | |
3024 } | |
3025 } | |
3026 | |
3027 /** | |
3028 * Removes all listeners and structures required to set up view port listeners. | |
3029 */ | |
3030 private void removeViewPortUpdate() { | |
3031 | |
3032 if (fTextWidget !is null) { | |
3033 | |
3034 fTextWidget.removeKeyListener(fViewportGuard); | |
3035 fTextWidget.removeMouseListener(fViewportGuard); | |
3036 | |
3037 if (fScroller !is null && !fScroller.isDisposed()) { | |
3038 fScroller.removeSelectionListener(fViewportGuard); | |
3039 fScroller= null; | |
3040 } | |
3041 | |
3042 fViewportGuard= null; | |
3043 } | |
3044 } | |
3045 | |
3046 /* | |
3047 * @see ITextViewer#addViewportListener(IViewportListener) | |
3048 */ | |
3049 public void addViewportListener(IViewportListener listener) { | |
3050 | |
3051 if (fViewportListeners is null) { | |
3052 fViewportListeners= new ArrayList(); | |
3053 initializeViewportUpdate(); | |
3054 } | |
3055 | |
3056 if (!fViewportListeners.contains(listener)) | |
3057 fViewportListeners.add(listener); | |
3058 } | |
3059 | |
3060 /* | |
3061 * @see ITextViewer#removeViewportListener(IVewportListener) | |
3062 */ | |
3063 public void removeViewportListener(IViewportListener listener) { | |
3064 if (fViewportListeners !is null) | |
3065 fViewportListeners.remove(listener); | |
3066 } | |
3067 | |
3068 /** | |
3069 * Checks whether the view port changed and if so informs all registered | |
3070 * listeners about the change. | |
3071 * | |
3072 * @param origin describes under which circumstances this method has been called. | |
3073 * | |
3074 * @see IViewportListener | |
3075 */ | |
3076 protected void updateViewportListeners(int origin) { | |
3077 | |
3078 if (redraws()) { | |
3079 int topPixel= fTextWidget.getTopPixel(); | |
3080 if (topPixel >= 0 && topPixel !is fLastTopPixel) { | |
3081 if (fViewportListeners !is null) { | |
3082 for (int i= 0; i < fViewportListeners.size(); i++) { | |
134 | 3083 IViewportListener l= cast(IViewportListener) fViewportListeners.get(i); |
129 | 3084 l.viewportChanged(topPixel); |
3085 } | |
3086 } | |
3087 fLastTopPixel= topPixel; | |
3088 } | |
3089 } | |
3090 } | |
3091 | |
3092 //---- scrolling and revealing | |
3093 | |
3094 /* | |
3095 * @see ITextViewer#getTopIndex() | |
3096 */ | |
3097 public int getTopIndex() { | |
3098 | |
3099 if (fTextWidget !is null) { | |
3100 int top= fTextWidget.getTopIndex(); | |
3101 return widgetLine2ModelLine(top); | |
3102 } | |
3103 | |
3104 return -1; | |
3105 } | |
3106 | |
3107 /* | |
3108 * @see ITextViewer#setTopIndex(int) | |
3109 */ | |
3110 public void setTopIndex(int index) { | |
3111 | |
3112 if (fTextWidget !is null) { | |
3113 | |
3114 int widgetLine= modelLine2WidgetLine(index); | |
3115 if (widgetLine is -1) | |
3116 widgetLine= getClosestWidgetLineForModelLine(index); | |
3117 | |
3118 if (widgetLine > -1) { | |
3119 fTextWidget.setTopIndex(widgetLine); | |
3120 updateViewportListeners(INTERNAL); | |
3121 } | |
3122 } | |
3123 } | |
3124 | |
3125 /** | |
3126 * Returns the number of lines that can fully fit into the viewport. This is computed by | |
3127 * dividing the widget's client area height by the widget's line height. The result is only | |
3128 * accurate if the widget does not use variable line heights - for that reason, clients should | |
3129 * not use this method any longer and use the client area height of the text widget to find out | |
3130 * how much content fits into it. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3131 * |
129 | 3132 * @return the view port height in lines |
3133 * @deprecated as of 3.2 | |
3134 */ | |
3135 protected int getVisibleLinesInViewport() { | |
3136 if (fTextWidget !is null) { | |
3137 Rectangle clArea= fTextWidget.getClientArea(); | |
3138 if (!clArea.isEmpty()) | |
3139 return clArea.height / fTextWidget.getLineHeight(); | |
3140 } | |
3141 return -1; | |
3142 } | |
3143 | |
3144 /* | |
3145 * @see ITextViewer#getBottomIndex() | |
3146 */ | |
3147 public int getBottomIndex() { | |
3148 | |
3149 if (fTextWidget is null) | |
3150 return -1; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3151 |
129 | 3152 int widgetBottom= JFaceTextUtil.getBottomIndex(fTextWidget); |
3153 return widgetLine2ModelLine(widgetBottom); | |
3154 } | |
3155 | |
3156 /* | |
3157 * @see ITextViewer#getTopIndexStartOffset() | |
3158 */ | |
3159 public int getTopIndexStartOffset() { | |
3160 | |
3161 if (fTextWidget !is null) { | |
3162 int top= fTextWidget.getTopIndex(); | |
3163 try { | |
3164 top= getVisibleDocument().getLineOffset(top); | |
3165 return widgetOffset2ModelOffset(top); | |
3166 } catch (BadLocationException ex) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3167 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3168 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.getTopIndexStartOffset")); //$NON-NLS-1$ |
129 | 3169 } |
3170 } | |
3171 | |
3172 return -1; | |
3173 } | |
3174 | |
3175 /* | |
3176 * @see ITextViewer#getBottomIndexEndOffset() | |
3177 */ | |
3178 public int getBottomIndexEndOffset() { | |
3179 try { | |
3180 | |
3181 IRegion line= getDocument().getLineInformation(getBottomIndex()); | |
3182 int bottomEndOffset= line.getOffset() + line.getLength() - 1; | |
3183 | |
3184 IRegion coverage= getModelCoverage(); | |
3185 if (coverage is null) | |
3186 return -1; | |
3187 | |
3188 int coverageEndOffset= coverage.getOffset() + coverage.getLength() - 1; | |
3189 return Math.min(coverageEndOffset, bottomEndOffset); | |
3190 | |
3191 } catch (BadLocationException ex) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3192 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3193 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.getBottomIndexEndOffset")); //$NON-NLS-1$ |
129 | 3194 return getDocument().getLength() - 1; |
3195 } | |
3196 } | |
3197 | |
3198 /* | |
3199 * @see ITextViewer#revealRange(int, int) | |
3200 */ | |
3201 public void revealRange(int start, int length) { | |
3202 | |
3203 if (fTextWidget is null || !redraws()) | |
3204 return; | |
3205 | |
3206 IRegion modelRange= new Region(start, length); | |
3207 IRegion widgetRange= modelRange2ClosestWidgetRange(modelRange); | |
3208 if (widgetRange !is null) { | |
3209 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3210 int[] range= [ widgetRange.getOffset(), widgetRange.getLength() ]; |
129 | 3211 validateSelectionRange(range); |
3212 if (range[0] >= 0) | |
3213 internalRevealRange(range[0], range[0] + range[1]); | |
3214 | |
3215 } else { | |
3216 | |
3217 IRegion coverage= getModelCoverage(); | |
3218 int cursor= (coverage is null || start < coverage.getOffset()) ? 0 : getVisibleDocument().getLength(); | |
3219 internalRevealRange(cursor, cursor); | |
3220 } | |
3221 } | |
3222 | |
3223 /** | |
3224 * Reveals the given range of the visible document. | |
3225 * | |
3226 * @param start the start offset of the range | |
3227 * @param end the end offset of the range | |
3228 */ | |
3229 protected void internalRevealRange(int start, int end) { | |
3230 | |
3231 try { | |
3232 | |
3233 IDocument doc= getVisibleDocument(); | |
3234 | |
3235 int startLine= doc.getLineOfOffset(start); | |
3236 int endLine= doc.getLineOfOffset(end); | |
3237 | |
3238 int top= fTextWidget.getTopIndex(); | |
3239 if (top > -1) { | |
3240 | |
3241 // scroll vertically | |
3242 int bottom= JFaceTextUtil.getBottomIndex(fTextWidget); | |
3243 int lines= bottom - top; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3244 |
129 | 3245 // if the widget is not scrollable as it is displaying the entire content |
3246 // setTopIndex won't have any effect. | |
3247 | |
3248 if (startLine >= top && startLine <= bottom && endLine >= top && endLine <= bottom ) { | |
3249 | |
3250 // do not scroll at all as it is already visible | |
3251 | |
3252 } else { | |
3253 | |
3254 int delta= Math.max(0, lines - (endLine - startLine)); | |
3255 fTextWidget.setTopIndex(startLine - delta/3); | |
3256 updateViewportListeners(INTERNAL); | |
3257 } | |
3258 | |
3259 // scroll horizontally | |
3260 | |
3261 if (endLine < startLine) { | |
3262 endLine += startLine; | |
3263 startLine= endLine - startLine; | |
3264 endLine -= startLine; | |
3265 } | |
3266 | |
3267 int startPixel= -1; | |
3268 int endPixel= -1; | |
3269 | |
3270 if (endLine > startLine) { | |
3271 // reveal the beginning of the range in the start line | |
3272 IRegion extent= getExtent(start, start); | |
3273 startPixel= extent.getOffset() + fTextWidget.getHorizontalPixel(); | |
3274 endPixel= startPixel; | |
3275 | |
3276 } else { | |
3277 IRegion extent= getExtent(start, end); | |
3278 startPixel= extent.getOffset() + fTextWidget.getHorizontalPixel(); | |
3279 endPixel= startPixel + extent.getLength(); | |
3280 } | |
3281 | |
3282 int visibleStart= fTextWidget.getHorizontalPixel(); | |
3283 int visibleEnd= visibleStart + fTextWidget.getClientArea().width; | |
3284 | |
3285 // scroll only if not yet visible | |
3286 if (startPixel < visibleStart || visibleEnd < endPixel) { | |
3287 | |
3288 // set buffer zone to 10 pixels | |
3289 int bufferZone= 10; | |
3290 | |
3291 int newOffset= visibleStart; | |
3292 | |
3293 int visibleWidth= visibleEnd - visibleStart; | |
3294 int selectionPixelWidth= endPixel - startPixel; | |
3295 | |
3296 if (startPixel < visibleStart) | |
3297 newOffset= startPixel; | |
3298 else if (selectionPixelWidth + bufferZone < visibleWidth) | |
3299 newOffset= endPixel + bufferZone - visibleWidth; | |
3300 else | |
3301 newOffset= startPixel; | |
3302 | |
134 | 3303 float index= (cast(float)newOffset) / (cast(float)getAverageCharWidth()); |
129 | 3304 |
3305 fTextWidget.setHorizontalIndex(Math.round(index)); | |
3306 } | |
3307 | |
3308 } | |
3309 } catch (BadLocationException e) { | |
3310 throw new IllegalArgumentException(JFaceTextMessages.getString("TextViewer.error.invalid_range")); //$NON-NLS-1$ | |
3311 } | |
3312 } | |
3313 | |
3314 /** | |
3315 * Returns the width of the text when being drawn into this viewer's widget. | |
3316 * | |
3317 * @param text the string to measure | |
3318 * @return the width of the presentation of the given string | |
3319 * @deprecated use <code>getWidthInPixels(int, int)</code> instead | |
3320 */ | |
3321 final protected int getWidthInPixels(String text) { | |
3322 GC gc= new GC(fTextWidget); | |
3323 gc.setFont(fTextWidget.getFont()); | |
3324 Point extent= gc.textExtent(text); | |
3325 gc.dispose(); | |
3326 return extent.x; | |
3327 } | |
3328 | |
3329 /** | |
3330 * Returns the region covered by the given start and end offset. | |
3331 * The result is relative to the upper left corner of the widget | |
3332 * client area. | |
3333 * | |
3334 * @param start offset relative to the start of this viewer's view port | |
3335 * 0 <= offset <= getCharCount() | |
3336 * @param end offset relative to the start of this viewer's view port | |
3337 * 0 <= offset <= getCharCount() | |
3338 * @return the region covered by start and end offset | |
3339 */ | |
3340 final protected IRegion getExtent(int start, int end) { | |
3341 if (end > 0 && start < end) { | |
3342 Rectangle bounds= fTextWidget.getTextBounds(start, end - 1); | |
3343 return new Region(bounds.x, bounds.width); | |
3344 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3345 |
129 | 3346 return new Region(fTextWidget.getLocationAtOffset(start).x, 0); |
3347 } | |
3348 | |
3349 /** | |
3350 * Returns the width of the representation of a text range in the | |
3351 * visible region of the viewer's document as drawn in this viewer's | |
3352 * widget. | |
3353 * | |
3354 * @param offset the offset of the text range in the visible region | |
3355 * @param length the length of the text range in the visible region | |
3356 * @return the width of the presentation of the specified text range | |
3357 * @since 2.0 | |
3358 */ | |
3359 final protected int getWidthInPixels(int offset, int length) { | |
3360 return getExtent(offset, offset + length).getLength(); | |
3361 } | |
3362 | |
3363 /** | |
3364 * Returns the average character width of this viewer's widget. | |
3365 * | |
3366 * @return the average character width of this viewer's widget | |
3367 */ | |
3368 final protected int getAverageCharWidth() { | |
3369 return JFaceTextUtil.getAverageCharWidth(getTextWidget()); | |
3370 } | |
3371 | |
3372 /* | |
3373 * @see Viewer#refresh() | |
3374 */ | |
3375 public void refresh() { | |
3376 setDocument(getDocument()); | |
3377 } | |
3378 | |
3379 //---- visible range support | |
3380 | |
3381 /** | |
3382 * Returns the slave document manager | |
3383 * | |
3384 * @return the slave document manager | |
3385 * @since 2.1 | |
3386 */ | |
3387 protected ISlaveDocumentManager getSlaveDocumentManager() { | |
3388 if (fSlaveDocumentManager is null) | |
3389 fSlaveDocumentManager= createSlaveDocumentManager(); | |
3390 return fSlaveDocumentManager; | |
3391 } | |
3392 | |
3393 /** | |
3394 * Creates a new slave document manager. This implementation always | |
3395 * returns a <code>ChildDocumentManager</code>. | |
3396 * | |
3397 * @return ISlaveDocumentManager | |
3398 * @since 2.1 | |
3399 */ | |
3400 protected ISlaveDocumentManager createSlaveDocumentManager() { | |
3401 return new ChildDocumentManager(); | |
3402 } | |
3403 | |
3404 /* | |
3405 * @see dwtx.jface.text.ITextViewer#invalidateTextPresentation() | |
3406 */ | |
3407 public final void invalidateTextPresentation() { | |
3408 if (fVisibleDocument !is null) { | |
3409 fWidgetCommand.event= null; | |
3410 fWidgetCommand.start= 0; | |
3411 fWidgetCommand.length= fVisibleDocument.getLength(); | |
3412 fWidgetCommand.text= fVisibleDocument.get(); | |
3413 updateTextListeners(fWidgetCommand); | |
3414 } | |
3415 } | |
3416 | |
3417 /** | |
3418 * Invalidates the given range of the text presentation. | |
3419 * | |
3420 * @param offset the offset of the range to be invalidated | |
3421 * @param length the length of the range to be invalidated | |
3422 * @since 2.1 | |
3423 */ | |
3424 public final void invalidateTextPresentation(int offset, int length) { | |
3425 if (fVisibleDocument !is null) { | |
3426 | |
3427 IRegion widgetRange= modelRange2WidgetRange(new Region(offset, length)); | |
3428 if (widgetRange !is null) { | |
3429 | |
3430 fWidgetCommand.event= null; | |
3431 fWidgetCommand.start= widgetRange.getOffset(); | |
3432 fWidgetCommand.length= widgetRange.getLength(); | |
3433 | |
3434 try { | |
3435 fWidgetCommand.text= fVisibleDocument.get(widgetRange.getOffset(), widgetRange.getLength()); | |
3436 updateTextListeners(fWidgetCommand); | |
3437 } catch (BadLocationException x) { | |
3438 // can not happen because of previous checking | |
3439 } | |
3440 } | |
3441 } | |
3442 } | |
3443 | |
3444 /** | |
3445 * Initializes the text widget with the visual document and | |
3446 * invalidates the overall presentation. | |
3447 */ | |
3448 private void initializeWidgetContents() { | |
3449 | |
3450 if (fTextWidget !is null && fVisibleDocument !is null) { | |
3451 | |
3452 // set widget content | |
3453 if (fDocumentAdapter is null) | |
3454 fDocumentAdapter= createDocumentAdapter(); | |
3455 | |
3456 fDocumentAdapter.setDocument(fVisibleDocument); | |
3457 fTextWidget.setContent(fDocumentAdapter); | |
3458 | |
3459 // invalidate presentation | |
3460 invalidateTextPresentation(); | |
3461 } | |
3462 } | |
3463 | |
3464 /** | |
3465 * Frees the given document if it is a slave document. | |
3466 * | |
3467 * @param slave the potential slave document | |
3468 * @since 3.0 | |
3469 */ | |
3470 protected void freeSlaveDocument(IDocument slave) { | |
3471 ISlaveDocumentManager manager= getSlaveDocumentManager(); | |
3472 if (manager !is null && manager.isSlaveDocument(slave)) | |
3473 manager.freeSlaveDocument(slave); | |
3474 } | |
3475 | |
3476 /** | |
3477 * Sets this viewer's visible document. The visible document represents the | |
3478 * visible region of the viewer's input document. | |
3479 * | |
3480 * @param document the visible document | |
3481 */ | |
3482 protected void setVisibleDocument(IDocument document) { | |
3483 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3484 if (fVisibleDocument is document && cast(ChildDocument)fVisibleDocument ) { |
129 | 3485 // optimization for new child documents |
3486 return; | |
3487 } | |
3488 | |
3489 if (fVisibleDocument !is null) { | |
3490 if (fVisibleDocumentListener !is null) | |
3491 fVisibleDocument.removeDocumentListener(fVisibleDocumentListener); | |
3492 if (fVisibleDocument !is document) | |
3493 freeSlaveDocument(fVisibleDocument); | |
3494 } | |
3495 | |
3496 fVisibleDocument= document; | |
3497 initializeDocumentInformationMapping(fVisibleDocument); | |
3498 | |
3499 initializeWidgetContents(); | |
3500 | |
3501 fFindReplaceDocumentAdapter= null; | |
3502 if (fVisibleDocument !is null && fVisibleDocumentListener !is null) | |
3503 fVisibleDocument.addDocumentListener(fVisibleDocumentListener); | |
3504 } | |
3505 | |
3506 /** | |
3507 * Hook method called when the visible document is about to be changed. | |
3508 * <p> | |
3509 * Subclasses may override. | |
3510 * | |
3511 * @param event the document event | |
3512 * @since 3.0 | |
3513 */ | |
3514 protected void handleVisibleDocumentAboutToBeChanged(DocumentEvent event) { | |
3515 } | |
3516 | |
3517 /** | |
3518 * Hook method called when the visible document has been changed. | |
3519 * <p> | |
3520 * Subclasses may override. | |
3521 * | |
3522 * @param event the document event | |
3523 * @since 3.0 | |
3524 */ | |
3525 protected void handleVisibleDocumentChanged(DocumentEvent event) { | |
3526 } | |
3527 | |
3528 /** | |
3529 * Initializes the document information mapping between the given slave document and | |
3530 * its master document. | |
3531 * | |
3532 * @param visibleDocument the slave document | |
3533 * @since 2.1 | |
3534 */ | |
3535 protected void initializeDocumentInformationMapping(IDocument visibleDocument) { | |
3536 ISlaveDocumentManager manager= getSlaveDocumentManager(); | |
3537 fInformationMapping= manager is null ? null : manager.createMasterSlaveMapping(visibleDocument); | |
3538 } | |
3539 | |
3540 /** | |
3541 * Returns the viewer's visible document. | |
3542 * | |
3543 * @return the viewer's visible document | |
3544 */ | |
3545 protected IDocument getVisibleDocument() { | |
3546 return fVisibleDocument; | |
3547 } | |
3548 | |
3549 /** | |
3550 * Returns the offset of the visible region. | |
3551 * | |
3552 * @return the offset of the visible region | |
3553 */ | |
3554 protected int _getVisibleRegionOffset() { | |
3555 | |
3556 IDocument document= getVisibleDocument(); | |
138 | 3557 if ( cast(ChildDocument)document ) { |
134 | 3558 ChildDocument cdoc= cast(ChildDocument) document; |
129 | 3559 return cdoc.getParentDocumentRange().getOffset(); |
3560 } | |
3561 | |
3562 return 0; | |
3563 } | |
3564 | |
3565 /* | |
3566 * @see ITextViewer#getVisibleRegion() | |
3567 */ | |
3568 public IRegion getVisibleRegion() { | |
3569 | |
3570 IDocument document= getVisibleDocument(); | |
138 | 3571 if ( cast(ChildDocument)document ) { |
134 | 3572 Position p= (cast(ChildDocument) document).getParentDocumentRange(); |
129 | 3573 return new Region(p.getOffset(), p.getLength()); |
3574 } | |
3575 | |
3576 return new Region(0, document is null ? 0 : document.getLength()); | |
3577 } | |
3578 | |
3579 /* | |
3580 * @see ITextViewer#overlapsWithVisibleRegion(int, int) | |
3581 */ | |
3582 public bool overlapsWithVisibleRegion(int start, int length) { | |
3583 IDocument document= getVisibleDocument(); | |
138 | 3584 if ( cast(ChildDocument)document ) { |
134 | 3585 ChildDocument cdoc= cast(ChildDocument) document; |
129 | 3586 return cdoc.getParentDocumentRange().overlapsWith(start, length); |
3587 } else if (document !is null) { | |
3588 int size= document.getLength(); | |
3589 return (start >= 0 && length >= 0 && start + length <= size); | |
3590 } | |
3591 return false; | |
3592 } | |
3593 | |
3594 /* | |
3595 * @see ITextViewer#setVisibleRegion(int, int) | |
3596 */ | |
3597 public void setVisibleRegion(int start, int length) { | |
3598 | |
3599 IRegion region= getVisibleRegion(); | |
3600 if (start is region.getOffset() && length is region.getLength()) { | |
3601 // nothing to change | |
3602 return; | |
3603 } | |
3604 | |
3605 setRedraw(false); | |
3606 try { | |
3607 | |
3608 IDocument slaveDocument= createSlaveDocument(getVisibleDocument()); | |
3609 if (updateSlaveDocument(slaveDocument, start, length)) | |
3610 setVisibleDocument(slaveDocument); | |
3611 | |
3612 } catch (BadLocationException x) { | |
3613 throw new IllegalArgumentException(JFaceTextMessages.getString("TextViewer.error.invalid_visible_region_2")); //$NON-NLS-1$ | |
3614 } finally { | |
3615 setRedraw(true); | |
3616 } | |
3617 } | |
3618 | |
3619 /* | |
3620 * @see ITextViewer#resetVisibleRegion() | |
3621 */ | |
3622 public void resetVisibleRegion() { | |
3623 ISlaveDocumentManager manager= getSlaveDocumentManager(); | |
3624 if (manager !is null) { | |
3625 IDocument slave= getVisibleDocument(); | |
3626 IDocument master= manager.getMasterDocument(slave); | |
3627 if (master !is null) { | |
3628 setVisibleDocument(master); | |
3629 manager.freeSlaveDocument(slave); | |
3630 } | |
3631 } | |
3632 } | |
3633 | |
3634 | |
3635 //-------------------------------------- | |
3636 | |
3637 /* | |
3638 * @see ITextViewer#setTextDoubleClickStrategy(ITextDoubleClickStrategy, String) | |
3639 */ | |
3640 public void setTextDoubleClickStrategy(ITextDoubleClickStrategy strategy, String contentType) { | |
3641 | |
3642 if (strategy !is null) { | |
3643 if (fDoubleClickStrategies is null) | |
3644 fDoubleClickStrategies= new HashMap(); | |
3645 fDoubleClickStrategies.put(contentType, strategy); | |
3646 } else if (fDoubleClickStrategies !is null) | |
3647 fDoubleClickStrategies.remove(contentType); | |
3648 } | |
3649 | |
3650 /** | |
3651 * Selects from the given map the one which is registered under | |
3652 * the content type of the partition in which the given offset is located. | |
3653 * | |
3654 * @param plugins the map from which to choose | |
3655 * @param offset the offset for which to find the plug-in | |
3656 * @return the plug-in registered under the offset's content type | |
3657 */ | |
3658 protected Object selectContentTypePlugin(int offset, Map plugins) { | |
3659 try { | |
3660 return selectContentTypePlugin(TextUtilities.getContentType(getDocument(), getDocumentPartitioning(), offset, true), plugins); | |
3661 } catch (BadLocationException x) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3662 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3663 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.selectContentTypePlugin")); //$NON-NLS-1$ |
129 | 3664 } |
3665 return null; | |
3666 } | |
3667 | |
3668 /** | |
3669 * Selects from the given <code>plug-ins</code> this one which is | |
3670 * registered for the given content <code>type</code>. | |
3671 * | |
3672 * @param type the type to be used as lookup key | |
3673 * @param plugins the table to be searched | |
3674 * @return the plug-in in the map for the given content type | |
3675 */ | |
3676 private Object selectContentTypePlugin(String type, Map plugins) { | |
3677 | |
3678 if (plugins is null) | |
3679 return null; | |
3680 | |
3681 return plugins.get(type); | |
3682 } | |
3683 | |
3684 /** | |
3685 * Hook called on receipt of a <code>VerifyEvent</code>. The event has | |
3686 * been translated into a <code>DocumentCommand</code> which can now be | |
3687 * manipulated by interested parties. By default, the hook forwards the command | |
3688 * to the installed instances of <code>IAutoEditStrategy</code>. | |
3689 * | |
3690 * @param command the document command representing the verify event | |
3691 */ | |
3692 protected void customizeDocumentCommand(DocumentCommand command) { | |
3693 if (isIgnoringAutoEditStrategies()) | |
3694 return; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3695 |
129 | 3696 IDocument document= getDocument(); |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3697 |
129 | 3698 if (fTabsToSpacesConverter !is null) |
3699 fTabsToSpacesConverter.customizeDocumentCommand(document, command); | |
3700 | |
134 | 3701 List strategies= cast(List) selectContentTypePlugin(command.offset, fAutoIndentStrategies); |
129 | 3702 if (strategies is null) |
3703 return; | |
3704 | |
3705 switch (strategies.size()) { | |
3706 // optimization | |
3707 case 0: | |
3708 break; | |
3709 | |
3710 case 1: | |
134 | 3711 (cast(IAutoEditStrategy) strategies.iterator().next()).customizeDocumentCommand(document, command); |
129 | 3712 break; |
3713 | |
3714 // make iterator robust against adding/removing strategies from within strategies | |
3715 default: | |
3716 strategies= new ArrayList(strategies); | |
3717 for (final Iterator iterator= strategies.iterator(); iterator.hasNext(); ) | |
134 | 3718 (cast(IAutoEditStrategy) iterator.next()).customizeDocumentCommand(document, command); |
129 | 3719 |
3720 break; | |
3721 } | |
3722 } | |
3723 | |
3724 /** | |
3725 * Handles the verify event issued by the viewer's text widget. | |
3726 * | |
3727 * @see VerifyListener#verifyText(VerifyEvent) | |
3728 * @param e the verify event | |
3729 */ | |
3730 protected void handleVerifyEvent(VerifyEvent e) { | |
3731 | |
3732 if (fEventConsumer !is null) { | |
3733 fEventConsumer.processEvent(e); | |
3734 if (!e.doit) | |
3735 return; | |
3736 } | |
3737 | |
3738 IRegion modelRange= event2ModelRange(e); | |
3739 fDocumentCommand.setEvent(e, modelRange); | |
3740 customizeDocumentCommand(fDocumentCommand); | |
3741 if (!fDocumentCommand.fillEvent(e, modelRange)) { | |
3742 | |
3743 bool compoundChange= fDocumentCommand.getCommandCount() > 1; | |
3744 try { | |
3745 | |
3746 fVerifyListener.forward(false); | |
3747 | |
3748 if (compoundChange && fUndoManager !is null) | |
3749 fUndoManager.beginCompoundChange(); | |
3750 | |
3751 fDocumentCommand.execute(getDocument()); | |
3752 | |
3753 if (fTextWidget !is null) { | |
3754 int documentCaret= fDocumentCommand.caretOffset; | |
3755 if (documentCaret is -1) { | |
3756 // old behavior of document command | |
3757 documentCaret= fDocumentCommand.offset + (fDocumentCommand.text is null ? 0 : fDocumentCommand.text.length()); | |
3758 } | |
3759 | |
3760 int widgetCaret= modelOffset2WidgetOffset(documentCaret); | |
3761 if (widgetCaret is -1) { | |
3762 // try to move it to the closest spot | |
3763 IRegion region= getModelCoverage(); | |
3764 if (region !is null) { | |
3765 if (documentCaret <= region.getOffset()) | |
3766 widgetCaret= 0; | |
3767 else if (documentCaret >= region.getOffset() + region.getLength()) | |
3768 widgetCaret= getVisibleRegion().getLength(); | |
3769 } | |
3770 } | |
3771 | |
3772 if (widgetCaret !is -1) { | |
3773 // there is a valid widget caret | |
3774 fTextWidget.setCaretOffset(widgetCaret); | |
3775 } | |
3776 | |
3777 fTextWidget.showSelection(); | |
3778 } | |
3779 } catch (BadLocationException x) { | |
3780 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3781 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3782 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.verifyText")); //$NON-NLS-1$ |
129 | 3783 |
3784 } finally { | |
3785 | |
3786 if (compoundChange && fUndoManager !is null) | |
3787 fUndoManager.endCompoundChange(); | |
3788 | |
3789 fVerifyListener.forward(true); | |
3790 | |
3791 } | |
3792 } | |
3793 } | |
3794 | |
3795 //---- text manipulation | |
3796 | |
3797 /** | |
3798 * Returns whether the marked region of this viewer is empty. | |
3799 * | |
3800 * @return <code>true</code> if the marked region of this viewer is empty, otherwise <code>false</code> | |
3801 * @since 2.0 | |
3802 */ | |
3803 private bool isMarkedRegionEmpty() { | |
3804 return | |
3805 fTextWidget is null || | |
3806 fMarkPosition is null || | |
3807 fMarkPosition.isDeleted() || | |
3808 modelRange2WidgetRange(fMarkPosition) is null; | |
3809 } | |
3810 | |
3811 /* | |
3812 * @see ITextViewer#canDoOperation(int) | |
3813 */ | |
3814 public bool canDoOperation(int operation) { | |
3815 | |
3816 if (fTextWidget is null || !redraws()) | |
3817 return false; | |
3818 | |
3819 switch (operation) { | |
3820 case CUT: | |
3821 return isEditable() &&(fTextWidget.getSelectionCount() > 0 || !isMarkedRegionEmpty()); | |
3822 case COPY: | |
3823 return fTextWidget.getSelectionCount() > 0 || !isMarkedRegionEmpty(); | |
3824 case DELETE: | |
3825 case PASTE: | |
3826 return isEditable(); | |
3827 case SELECT_ALL: | |
3828 return true; | |
3829 case SHIFT_LEFT: | |
3830 case SHIFT_RIGHT: | |
3831 return isEditable() && fIndentChars !is null && areMultipleLinesSelected(); | |
3832 case PREFIX: | |
3833 case STRIP_PREFIX: | |
3834 return isEditable() && fDefaultPrefixChars !is null; | |
3835 case UNDO: | |
3836 return fUndoManager !is null && fUndoManager.undoable(); | |
3837 case REDO: | |
3838 return fUndoManager !is null && fUndoManager.redoable(); | |
3839 case PRINT: | |
3840 return isPrintable(); | |
3841 } | |
3842 | |
3843 return false; | |
3844 } | |
3845 | |
3846 /* | |
3847 * @see ITextViewer#doOperation(int) | |
3848 */ | |
3849 public void doOperation(int operation) { | |
3850 | |
3851 if (fTextWidget is null || !redraws()) | |
3852 return; | |
3853 | |
3854 Point selection= null; | |
3855 | |
3856 switch (operation) { | |
3857 | |
3858 case UNDO: | |
3859 if (fUndoManager !is null) { | |
3860 ignoreAutoEditStrategies(true); | |
3861 fUndoManager.undo(); | |
3862 ignoreAutoEditStrategies(false); | |
3863 } | |
3864 break; | |
3865 case REDO: | |
3866 if (fUndoManager !is null) { | |
3867 ignoreAutoEditStrategies(true); | |
3868 fUndoManager.redo(); | |
3869 ignoreAutoEditStrategies(false); | |
3870 } | |
3871 break; | |
3872 case CUT: | |
3873 if (fTextWidget.getSelectionCount() is 0) | |
3874 copyMarkedRegion(true); | |
3875 else | |
3876 fTextWidget.cut(); | |
3877 | |
3878 selection= fTextWidget.getSelectionRange(); | |
3879 fireSelectionChanged(selection.x, selection.y); | |
3880 | |
3881 break; | |
3882 case COPY: | |
3883 if (fTextWidget.getSelectionCount() is 0) | |
3884 copyMarkedRegion(false); | |
3885 else | |
3886 fTextWidget.copy(); | |
3887 break; | |
3888 case PASTE: | |
3889 // ignoreAutoEditStrategies(true); | |
3890 fTextWidget.paste(); | |
3891 selection= fTextWidget.getSelectionRange(); | |
3892 fireSelectionChanged(selection.x, selection.y); | |
3893 // ignoreAutoEditStrategies(false); | |
3894 break; | |
3895 case DELETE: | |
3896 fTextWidget.invokeAction(ST.DELETE_NEXT); | |
3897 selection= fTextWidget.getSelectionRange(); | |
3898 fireSelectionChanged(selection.x, selection.y); | |
3899 break; | |
3900 case SELECT_ALL: { | |
3901 if (getDocument() !is null) | |
3902 setSelectedRange(0, getDocument().getLength()); | |
3903 break; | |
3904 } | |
3905 case SHIFT_RIGHT: | |
3906 shift(false, true, false); | |
3907 break; | |
3908 case SHIFT_LEFT: | |
3909 shift(false, false, false); | |
3910 break; | |
3911 case PREFIX: | |
3912 shift(true, true, true); | |
3913 break; | |
3914 case STRIP_PREFIX: | |
3915 shift(true, false, true); | |
3916 break; | |
3917 case PRINT: | |
3918 print(); | |
3919 break; | |
3920 } | |
3921 } | |
3922 | |
3923 /** | |
3924 * Tells this viewer whether the registered auto edit strategies should be ignored. | |
3925 * | |
3926 * @param ignore <code>true</code> if the strategies should be ignored. | |
3927 * @since 2.1 | |
3928 */ | |
3929 protected void ignoreAutoEditStrategies(bool ignore) { | |
3930 if (fIgnoreAutoIndent is ignore) | |
3931 return; | |
3932 | |
3933 fIgnoreAutoIndent= ignore; | |
3934 | |
3935 IDocument document= getDocument(); | |
138 | 3936 if ( cast(IDocumentExtension2)document ) { |
134 | 3937 IDocumentExtension2 extension= cast(IDocumentExtension2) document; |
129 | 3938 if (ignore) |
3939 extension.ignorePostNotificationReplaces(); | |
3940 else | |
3941 extension.acceptPostNotificationReplaces(); | |
3942 } | |
3943 } | |
3944 | |
3945 /** | |
3946 * Returns whether this viewer ignores the registered auto edit strategies. | |
3947 * | |
3948 * @return <code>true</code> if the strategies are ignored | |
3949 * @since 2.1 | |
3950 */ | |
3951 protected bool isIgnoringAutoEditStrategies() { | |
3952 return fIgnoreAutoIndent; | |
3953 } | |
3954 | |
3955 /* | |
3956 * @see ITextOperationTargetExtension#enableOperation(int, bool) | |
3957 * @since 2.0 | |
3958 */ | |
3959 public void enableOperation(int operation, bool enable) { | |
3960 /* | |
3961 * NO-OP by default. | |
3962 * Will be changed to regularly disable the known operations. | |
3963 */ | |
3964 } | |
3965 | |
3966 /** | |
3967 * Copies/cuts the marked region. | |
3968 * | |
3969 * @param delete <code>true</code> if the region should be deleted rather than copied. | |
3970 * @since 2.0 | |
3971 */ | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3972 protected void copyMarkedRegion(bool delete_) { |
129 | 3973 |
3974 if (fTextWidget is null) | |
3975 return; | |
3976 | |
3977 if (fMarkPosition is null || fMarkPosition.isDeleted() || modelRange2WidgetRange(fMarkPosition) is null) | |
3978 return; | |
3979 | |
3980 int widgetMarkOffset= modelOffset2WidgetOffset(fMarkPosition.offset); | |
3981 Point selection= fTextWidget.getSelection(); | |
3982 if (selection.x <= widgetMarkOffset) | |
3983 fTextWidget.setSelection(selection.x, widgetMarkOffset); | |
3984 else | |
3985 fTextWidget.setSelection(widgetMarkOffset, selection.x); | |
3986 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3987 if (delete_) { |
129 | 3988 fTextWidget.cut(); |
3989 } else { | |
3990 fTextWidget.copy(); | |
3991 fTextWidget.setSelection(selection.x); // restore old cursor position | |
3992 } | |
3993 } | |
3994 | |
3995 /** | |
3996 * Deletes the current selection. If the selection has the length 0 | |
3997 * the selection is automatically extended to the right - either by 1 | |
3998 * or by the length of line delimiter if at the end of a line. | |
3999 * | |
4000 * @deprecated use <code>StyledText.invokeAction</code> instead | |
4001 */ | |
4002 protected void deleteText() { | |
4003 fTextWidget.invokeAction(ST.DELETE_NEXT); | |
4004 } | |
4005 | |
4006 /** | |
4007 * A block is selected if the character preceding the start of the | |
4008 * selection is a new line character. | |
4009 * | |
4010 * @return <code>true</code> if a block is selected | |
4011 */ | |
4012 protected bool isBlockSelected() { | |
4013 | |
4014 Point s= getSelectedRange(); | |
4015 if (s.y is 0) | |
4016 return false; | |
4017 | |
4018 try { | |
4019 | |
4020 IDocument document= getDocument(); | |
4021 int line= document.getLineOfOffset(s.x); | |
4022 int start= document.getLineOffset(line); | |
4023 return (s.x is start); | |
4024 | |
4025 } catch (BadLocationException x) { | |
4026 } | |
4027 | |
4028 return false; | |
4029 } | |
4030 | |
4031 /** | |
4032 * Returns <code>true</code> if one line is completely selected or if multiple lines are selected. | |
4033 * Being completely selected means that all characters except the new line characters are | |
4034 * selected. | |
4035 * | |
4036 * @return <code>true</code> if one or multiple lines are selected | |
4037 * @since 2.0 | |
4038 */ | |
4039 protected bool areMultipleLinesSelected() { | |
4040 Point s= getSelectedRange(); | |
4041 if (s.y is 0) | |
4042 return false; | |
4043 | |
4044 try { | |
4045 | |
4046 IDocument document= getDocument(); | |
4047 int startLine= document.getLineOfOffset(s.x); | |
4048 int endLine= document.getLineOfOffset(s.x + s.y); | |
4049 IRegion line= document.getLineInformation(startLine); | |
4050 return startLine !is endLine || (s.x is line.getOffset() && s.y is line.getLength()); | |
4051 | |
4052 } catch (BadLocationException x) { | |
4053 } | |
4054 | |
4055 return false; | |
4056 } | |
4057 | |
4058 /** | |
4059 * Returns the index of the first line whose start offset is in the given text range. | |
4060 * | |
4061 * @param region the text range in characters where to find the line | |
4062 * @return the first line whose start index is in the given range, -1 if there is no such line | |
4063 */ | |
4064 private int getFirstCompleteLineOfRegion(IRegion region) { | |
4065 | |
4066 try { | |
4067 | |
4068 IDocument d= getDocument(); | |
4069 | |
4070 int startLine= d.getLineOfOffset(region.getOffset()); | |
4071 | |
4072 int offset= d.getLineOffset(startLine); | |
4073 if (offset >= region.getOffset()) | |
4074 return startLine; | |
4075 | |
4076 offset= d.getLineOffset(startLine + 1); | |
4077 return (offset > region.getOffset() + region.getLength() ? -1 : startLine + 1); | |
4078 | |
4079 } catch (BadLocationException x) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4080 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4081 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.getFirstCompleteLineOfRegion")); //$NON-NLS-1$ |
129 | 4082 } |
4083 | |
4084 return -1; | |
4085 } | |
4086 | |
4087 | |
4088 /** | |
4089 * Creates a region describing the text block (something that starts at | |
4090 * the beginning of a line) completely containing the current selection. | |
4091 * | |
4092 * @param selection the selection to use | |
4093 * @return the region describing the text block comprising the given selection | |
4094 * @since 2.0 | |
4095 */ | |
4096 private IRegion getTextBlockFromSelection(Point selection) { | |
4097 | |
4098 try { | |
4099 IDocument document= getDocument(); | |
4100 IRegion line= document.getLineInformationOfOffset(selection.x); | |
4101 int length= selection.y is 0 ? line.getLength() : selection.y + (selection.x - line.getOffset()); | |
4102 return new Region(line.getOffset(), length); | |
4103 | |
4104 } catch (BadLocationException x) { | |
4105 } | |
4106 | |
4107 return null; | |
4108 } | |
4109 | |
4110 /** | |
4111 * Shifts a text block to the right or left using the specified set of prefix characters. | |
4112 * The prefixes must start at the beginning of the line. | |
4113 * | |
4114 * @param useDefaultPrefixes says whether the configured default or indent prefixes should be used | |
4115 * @param right says whether to shift to the right or the left | |
4116 * | |
4117 * @deprecated use shift(bool, bool, bool) instead | |
4118 */ | |
4119 protected void shift(bool useDefaultPrefixes, bool right) { | |
4120 shift(useDefaultPrefixes, right, false); | |
4121 } | |
4122 | |
4123 /** | |
4124 * Shifts a text block to the right or left using the specified set of prefix characters. | |
4125 * If white space should be ignored the prefix characters must not be at the beginning of | |
4126 * the line when shifting to the left. There may be whitespace in front of the prefixes. | |
4127 * | |
4128 * @param useDefaultPrefixes says whether the configured default or indent prefixes should be used | |
4129 * @param right says whether to shift to the right or the left | |
4130 * @param ignoreWhitespace says whether whitespace in front of prefixes is allowed | |
4131 * @since 2.0 | |
4132 */ | |
4133 protected void shift(bool useDefaultPrefixes, bool right, bool ignoreWhitespace) { | |
4134 if (fUndoManager !is null) | |
4135 fUndoManager.beginCompoundChange(); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4136 |
129 | 4137 IDocument d= getDocument(); |
4138 Map partitioners= null; | |
4139 DocumentRewriteSession rewriteSession= null; | |
4140 try { | |
4141 Point selection= getSelectedRange(); | |
4142 IRegion block= getTextBlockFromSelection(selection); | |
4143 ITypedRegion[] regions= TextUtilities.computePartitioning(d, getDocumentPartitioning(), block.getOffset(), block.getLength(), false); | |
4144 | |
4145 int lineCount= 0; | |
4146 int[] lines= new int[regions.length * 2]; // [start line, end line, start line, end line, ...] | |
4147 for (int i= 0, j= 0; i < regions.length; i++, j+= 2) { | |
4148 // start line of region | |
4149 lines[j]= getFirstCompleteLineOfRegion(regions[i]); | |
4150 // end line of region | |
4151 int length= regions[i].getLength(); | |
4152 int offset= regions[i].getOffset() + length; | |
4153 if (length > 0) | |
4154 offset--; | |
4155 lines[j + 1]= (lines[j] is -1 ? -1 : d.getLineOfOffset(offset)); | |
4156 lineCount += lines[j + 1] - lines[j] + 1; | |
4157 } | |
4158 | |
138 | 4159 if ( cast(IDocumentExtension4)d ) { |
134 | 4160 IDocumentExtension4 extension= cast(IDocumentExtension4) d; |
129 | 4161 rewriteSession= extension.startRewriteSession(DocumentRewriteSessionType.SEQUENTIAL); |
4162 } else { | |
4163 setRedraw(false); | |
4164 startSequentialRewriteMode(true); | |
4165 } | |
4166 if (lineCount >= 20) | |
4167 partitioners= TextUtilities.removeDocumentPartitioners(d); | |
4168 | |
4169 // Perform the shift operation. | |
4170 Map map= (useDefaultPrefixes ? fDefaultPrefixChars : fIndentChars); | |
4171 for (int i= 0, j= 0; i < regions.length; i++, j += 2) { | |
156 | 4172 String[] prefixes= stringArrayFromObject(selectContentTypePlugin(regions[i].getType(), map)); |
129 | 4173 if (prefixes !is null && prefixes.length > 0 && lines[j] >= 0 && lines[j + 1] >= 0) { |
4174 if (right) | |
4175 shiftRight(lines[j], lines[j + 1], prefixes[0]); | |
4176 else | |
4177 shiftLeft(lines[j], lines[j + 1], prefixes, ignoreWhitespace); | |
4178 } | |
4179 } | |
4180 | |
4181 } catch (BadLocationException x) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4182 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4183 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.shift_1")); //$NON-NLS-1$ |
129 | 4184 |
4185 } finally { | |
4186 | |
4187 if (partitioners !is null) | |
4188 TextUtilities.addDocumentPartitioners(d, partitioners); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4189 |
138 | 4190 if ( cast(IDocumentExtension4)d ) { |
134 | 4191 IDocumentExtension4 extension= cast(IDocumentExtension4) d; |
129 | 4192 extension.stopRewriteSession(rewriteSession); |
4193 } else { | |
4194 stopSequentialRewriteMode(); | |
4195 setRedraw(true); | |
4196 } | |
4197 | |
4198 if (fUndoManager !is null) | |
4199 fUndoManager.endCompoundChange(); | |
4200 } | |
4201 } | |
4202 | |
4203 /** | |
4204 * Shifts the specified lines to the right inserting the given prefix | |
4205 * at the beginning of each line | |
4206 * | |
4207 * @param prefix the prefix to be inserted | |
4208 * @param startLine the first line to shift | |
4209 * @param endLine the last line to shift | |
4210 * @since 2.0 | |
4211 */ | |
4212 private void shiftRight(int startLine, int endLine, String prefix) { | |
4213 | |
4214 try { | |
4215 | |
4216 IDocument d= getDocument(); | |
4217 while (startLine <= endLine) { | |
4218 d.replace(d.getLineOffset(startLine++), 0, prefix); | |
4219 } | |
4220 | |
4221 } catch (BadLocationException x) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4222 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4223 System.out_.println("TextViewer.shiftRight: BadLocationException"); //$NON-NLS-1$ |
129 | 4224 } |
4225 } | |
4226 | |
4227 /** | |
4228 * Shifts the specified lines to the right or to the left. On shifting to the right | |
4229 * it insert <code>prefixes[0]</code> at the beginning of each line. On shifting to the | |
4230 * left it tests whether each of the specified lines starts with one of the specified | |
4231 * prefixes and if so, removes the prefix. | |
4232 * | |
4233 * @param startLine the first line to shift | |
4234 * @param endLine the last line to shift | |
4235 * @param prefixes the prefixes to be used for shifting | |
4236 * @param ignoreWhitespace <code>true</code> if whitespace should be ignored, <code>false</code> otherwise | |
4237 * @since 2.0 | |
4238 */ | |
4239 private void shiftLeft(int startLine, int endLine, String[] prefixes, bool ignoreWhitespace) { | |
4240 | |
4241 IDocument d= getDocument(); | |
4242 | |
4243 try { | |
4244 | |
4245 IRegion[] occurrences= new IRegion[endLine - startLine + 1]; | |
4246 | |
4247 // find all the first occurrences of prefix in the given lines | |
4248 for (int i= 0; i < occurrences.length; i++) { | |
4249 | |
4250 IRegion line= d.getLineInformation(startLine + i); | |
4251 String text= d.get(line.getOffset(), line.getLength()); | |
4252 | |
4253 int index= -1; | |
4254 int[] found= TextUtilities.indexOf(prefixes, text, 0); | |
4255 if (found[0] !is -1) { | |
4256 if (ignoreWhitespace) { | |
4257 String s= d.get(line.getOffset(), found[0]); | |
4258 s= s.trim(); | |
4259 if (s.length() is 0) | |
4260 index= line.getOffset() + found[0]; | |
4261 } else if (found[0] is 0) | |
4262 index= line.getOffset(); | |
4263 } | |
4264 | |
4265 if (index > -1) { | |
4266 // remember where prefix is in line, so that it can be removed | |
4267 int length= prefixes[found[1]].length(); | |
4268 if (length is 0 && !ignoreWhitespace && line.getLength() > 0) { | |
4269 // found a non-empty line which cannot be shifted | |
4270 return; | |
4271 } | |
4272 occurrences[i]= new Region(index, length); | |
4273 } else { | |
4274 // found a line which cannot be shifted | |
4275 return; | |
4276 } | |
4277 } | |
4278 | |
4279 // OK - change the document | |
4280 int decrement= 0; | |
4281 for (int i= 0; i < occurrences.length; i++) { | |
4282 IRegion r= occurrences[i]; | |
4283 d.replace(r.getOffset() - decrement, r.getLength(), ""); //$NON-NLS-1$ | |
4284 decrement += r.getLength(); | |
4285 } | |
4286 | |
4287 } catch (BadLocationException x) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4288 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4289 System.out_.println("TextViewer.shiftLeft: BadLocationException"); //$NON-NLS-1$ |
129 | 4290 } |
4291 } | |
4292 | |
4293 /** | |
4294 * Returns whether the shown text can be printed. | |
4295 * | |
4296 * @return the viewer's printable mode | |
4297 */ | |
4298 protected bool isPrintable() { | |
4299 /* | |
4300 * 1GK7Q10: ITPUI:WIN98 - internal error after invoking print at editor view | |
4301 * Changed from returning true to testing the length of the printer queue | |
4302 */ | |
4303 PrinterData[] printerList= Printer.getPrinterList(); | |
4304 return (printerList !is null && printerList.length > 0); | |
4305 } | |
4306 | |
4307 /** | |
4308 * {@inheritDoc} | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4309 * |
129 | 4310 * @since 3.4 |
4311 */ | |
4312 public void print(StyledTextPrintOptions options) { | |
4313 final PrintDialog dialog= new PrintDialog(fTextWidget.getShell(), DWT.PRIMARY_MODAL); | |
4314 final PrinterData data= dialog.open(); | |
4315 | |
4316 if (data !is null) { | |
4317 final Printer printer= new Printer(data); | |
4318 final Runnable styledTextPrinter= fTextWidget.print(printer, options); | |
4319 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4320 void threadrun() { |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4321 styledTextPrinter.run(); |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4322 printer.dispose(); |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4323 } |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4324 Thread printingThread= new Thread( &threadrun ); |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4325 printingThread.name = "Printing"; //$NON-NLS-1$ |
129 | 4326 printingThread.start(); |
4327 } | |
4328 } | |
4329 | |
4330 /** | |
4331 * Brings up a print dialog and calls <code>printContents(Printer)</code> | |
4332 * which performs the actual print. | |
4333 */ | |
4334 protected void print() { | |
4335 StyledTextPrintOptions options= new StyledTextPrintOptions(); | |
4336 options.printTextFontStyle= true; | |
4337 options.printTextForeground= true; | |
4338 print(options); | |
4339 } | |
4340 | |
4341 //------ find support | |
4342 | |
4343 /** | |
4344 * Adheres to the contract of {@link IFindReplaceTarget#canPerformFind()}. | |
4345 * | |
4346 * @return <code>true</code> if find can be performed, <code>false</code> otherwise | |
4347 */ | |
4348 protected bool canPerformFind() { | |
4349 IDocument d= getVisibleDocument(); | |
4350 return (fTextWidget !is null && d !is null && d.getLength() > 0); | |
4351 } | |
4352 | |
4353 /** | |
4354 * Adheres to the contract of {@link IFindReplaceTarget#findAndSelect(int, String, bool, bool, bool)}. | |
4355 * | |
4356 * @param startPosition the start position | |
4357 * @param findString the find string specification | |
4358 * @param forwardSearch the search direction | |
4359 * @param caseSensitive <code>true</code> if case sensitive, <code>false</code> otherwise | |
4360 * @param wholeWord <code>true</code> if match must be whole words, <code>false</code> otherwise | |
4361 * @return the model offset of the first match | |
4362 * @deprecated as of 3.0 use {@link #findAndSelect(int, String, bool, bool, bool, bool)} | |
4363 */ | |
4364 protected int findAndSelect(int startPosition, String findString, bool forwardSearch, bool caseSensitive, bool wholeWord) { | |
4365 try { | |
4366 return findAndSelect(startPosition, findString, forwardSearch, caseSensitive, wholeWord, false); | |
4367 } catch (IllegalStateException ex) { | |
4368 return -1; | |
4369 } catch (PatternSyntaxException ex) { | |
4370 return -1; | |
4371 } | |
4372 } | |
4373 | |
4374 /** | |
4375 * Adheres to the contract of | |
4376 * {@link dwtx.jface.text.IFindReplaceTargetExtension3#findAndSelect(int, String, bool, bool, bool, bool)}. | |
4377 * | |
4378 * @param startPosition the start position | |
4379 * @param findString the find string specification | |
4380 * @param forwardSearch the search direction | |
4381 * @param caseSensitive <code>true</code> if case sensitive, <code>false</code> otherwise | |
4382 * @param wholeWord <code>true</code> if matches must be whole words, <code>false</code> otherwise | |
4383 * @param regExSearch <code>true</code> if <code>findString</code> is a regular expression, <code>false</code> otherwise | |
4384 * @return the model offset of the first match | |
4385 * | |
4386 */ | |
4387 protected int findAndSelect(int startPosition, String findString, bool forwardSearch, bool caseSensitive, bool wholeWord, bool regExSearch) { | |
4388 if (fTextWidget is null) | |
4389 return -1; | |
4390 | |
4391 try { | |
4392 | |
4393 int widgetOffset= (startPosition is -1 ? startPosition : modelOffset2WidgetOffset(startPosition)); | |
4394 FindReplaceDocumentAdapter adapter= getFindReplaceDocumentAdapter(); | |
4395 IRegion matchRegion= adapter.find(widgetOffset, findString, forwardSearch, caseSensitive, wholeWord, regExSearch); | |
4396 if (matchRegion !is null) { | |
4397 int widgetPos= matchRegion.getOffset(); | |
4398 int length= matchRegion.getLength(); | |
4399 | |
4400 // Prevents setting of widget selection with line delimiters at beginning or end | |
4401 char startChar= adapter.charAt(widgetPos); | |
4402 char endChar= adapter.charAt(widgetPos+length-1); | |
4403 bool borderHasLineDelimiter= startChar is '\n' || startChar is '\r' || endChar is '\n' || endChar is '\r'; | |
4404 bool redraws= redraws(); | |
4405 if (borderHasLineDelimiter && redraws) | |
4406 setRedraw(false); | |
4407 | |
4408 if (redraws()) { | |
4409 fTextWidget.setSelectionRange(widgetPos, length); | |
4410 internalRevealRange(widgetPos, widgetPos + length); | |
4411 selectionChanged(widgetPos, length); | |
4412 } else { | |
4413 setSelectedRange(widgetOffset2ModelOffset(widgetPos), length); | |
4414 if (redraws) | |
4415 setRedraw(true); | |
4416 } | |
4417 | |
4418 return widgetOffset2ModelOffset(widgetPos); | |
4419 } | |
4420 | |
4421 } catch (BadLocationException x) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4422 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4423 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.findAndSelect")); //$NON-NLS-1$ |
129 | 4424 } |
4425 | |
4426 return -1; | |
4427 } | |
4428 | |
4429 /** | |
4430 * Adheres to the contract of {@link dwtx.jface.text.IFindReplaceTargetExtension3#findAndSelect(int, String, bool, bool, bool, bool)}. | |
4431 * | |
4432 * @param startPosition the start position | |
4433 * @param findString the find string specification | |
4434 * @param forwardSearch the search direction | |
4435 * @param caseSensitive <code>true</code> if case sensitive, <code>false</code> otherwise | |
4436 * @param wholeWord <code>true</code> if matches must be whole words, <code>false</code> otherwise | |
4437 * @param rangeOffset the search scope offset | |
4438 * @param rangeLength the search scope length | |
4439 * @param regExSearch <code>true</code> if <code>findString</code> is a regular expression, <code>false</code> otherwise | |
4440 * @return the model offset of the first match | |
4441 * @since 3.0 | |
4442 */ | |
4443 protected int findAndSelectInRange(int startPosition, String findString, bool forwardSearch, bool caseSensitive, bool wholeWord, int rangeOffset, int rangeLength, bool regExSearch) { | |
4444 if (fTextWidget is null) | |
4445 return -1; | |
4446 | |
4447 try { | |
4448 | |
4449 int modelOffset; | |
4450 if (forwardSearch && (startPosition is -1 || startPosition < rangeOffset)) { | |
4451 modelOffset= rangeOffset; | |
4452 } else if (!forwardSearch && (startPosition is -1 || startPosition > rangeOffset + rangeLength)) { | |
4453 modelOffset= rangeOffset + rangeLength; | |
4454 } else { | |
4455 modelOffset= startPosition; | |
4456 } | |
4457 | |
4458 int widgetOffset= modelOffset2WidgetOffset(modelOffset); | |
4459 if (widgetOffset is -1) | |
4460 return -1; | |
4461 | |
4462 FindReplaceDocumentAdapter adapter= getFindReplaceDocumentAdapter(); | |
4463 IRegion matchRegion= adapter.find(widgetOffset, findString, forwardSearch, caseSensitive, wholeWord, regExSearch); | |
4464 int widgetPos= -1; | |
4465 int length= 0; | |
4466 if (matchRegion !is null) { | |
4467 widgetPos= matchRegion.getOffset(); | |
4468 length= matchRegion.getLength(); | |
4469 } | |
4470 int modelPos= widgetPos is -1 ? -1 : widgetOffset2ModelOffset(widgetPos); | |
4471 | |
4472 if (widgetPos !is -1 && (modelPos < rangeOffset || modelPos + length > rangeOffset + rangeLength)) | |
4473 widgetPos= -1; | |
4474 | |
4475 if (widgetPos > -1) { | |
4476 | |
4477 // Prevents setting of widget selection with line delimiters at beginning or end | |
4478 char startChar= adapter.charAt(widgetPos); | |
4479 char endChar= adapter.charAt(widgetPos+length-1); | |
4480 bool borderHasLineDelimiter= startChar is '\n' || startChar is '\r' || endChar is '\n' || endChar is '\r'; | |
4481 bool redraws= redraws(); | |
4482 if (borderHasLineDelimiter && redraws) | |
4483 setRedraw(false); | |
4484 | |
4485 if (redraws()) { | |
4486 fTextWidget.setSelectionRange(widgetPos, length); | |
4487 internalRevealRange(widgetPos, widgetPos + length); | |
4488 selectionChanged(widgetPos, length); | |
4489 } else { | |
4490 setSelectedRange(modelPos, length); | |
4491 if (redraws) | |
4492 setRedraw(true); | |
4493 } | |
4494 | |
4495 return modelPos; | |
4496 } | |
4497 | |
4498 | |
4499 } catch (BadLocationException x) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4500 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4501 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.findAndSelect")); //$NON-NLS-1$ |
129 | 4502 } |
4503 | |
4504 return -1; | |
4505 } | |
4506 | |
4507 //---------- text presentation support | |
4508 | |
4509 /* | |
4510 * @see ITextViewer#setTextColor(Color) | |
4511 */ | |
4512 public void setTextColor(Color color) { | |
4513 if (color !is null) | |
4514 setTextColor(color, 0, getDocument().getLength(), true); | |
4515 } | |
4516 | |
4517 /* | |
4518 * @see ITextViewer#setTextColor(Color, start, length, bool) | |
4519 */ | |
4520 public void setTextColor(Color color, int start, int length, bool controlRedraw) { | |
4521 if (fTextWidget !is null) { | |
4522 | |
4523 StyleRange s= new StyleRange(); | |
4524 s.foreground= color; | |
4525 s.start= start; | |
4526 s.length= length; | |
4527 | |
4528 s= modelStyleRange2WidgetStyleRange(s); | |
4529 if (s !is null) { | |
4530 if (controlRedraw) | |
4531 fTextWidget.setRedraw(false); | |
4532 try { | |
4533 fTextWidget.setStyleRange(s); | |
4534 } finally { | |
4535 if (controlRedraw) | |
4536 fTextWidget.setRedraw(true); | |
4537 } | |
4538 } | |
4539 } | |
4540 } | |
4541 | |
4542 /** | |
4543 * Adds the given presentation to the viewer's style information. | |
4544 * | |
4545 * @param presentation the presentation to be added | |
4546 */ | |
4547 private void addPresentation(TextPresentation presentation) { | |
4548 | |
4549 StyleRange range= presentation.getDefaultStyleRange(); | |
4550 if (range !is null) { | |
4551 | |
4552 range= modelStyleRange2WidgetStyleRange(range); | |
4553 if (range !is null) | |
4554 fTextWidget.setStyleRange(range); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4555 |
129 | 4556 ArrayList ranges= new ArrayList(presentation.getDenumerableRanges()); |
4557 Iterator e= presentation.getNonDefaultStyleRangeIterator(); | |
4558 while (e.hasNext()) { | |
134 | 4559 range= cast(StyleRange) e.next(); |
129 | 4560 range= modelStyleRange2WidgetStyleRange(range); |
4561 if (range !is null) | |
4562 ranges.add(range); | |
4563 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4564 |
129 | 4565 if (!ranges.isEmpty()) |
156 | 4566 fTextWidget.replaceStyleRanges(0, 0, arraycast!(StyleRange)(ranges.toArray())); |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4567 |
129 | 4568 } else { |
4569 IRegion region= modelRange2WidgetRange(presentation.getCoverage()); | |
4570 if (region is null) | |
4571 return; | |
4572 | |
4573 List list= new ArrayList(presentation.getDenumerableRanges()); | |
4574 Iterator e= presentation.getAllStyleRangeIterator(); | |
4575 while (e.hasNext()) { | |
134 | 4576 range= cast(StyleRange) e.next(); |
129 | 4577 range= modelStyleRange2WidgetStyleRange(range); |
4578 if (range !is null) | |
4579 list.add(range); | |
4580 } | |
4581 | |
4582 if (!list.isEmpty()) { | |
4583 StyleRange[] ranges= new StyleRange[list.size()]; | |
4584 list.toArray(ranges); | |
4585 fTextWidget.replaceStyleRanges(region.getOffset(), region.getLength(), ranges); | |
4586 } | |
4587 } | |
4588 } | |
4589 | |
4590 /** | |
4591 * Applies the given presentation to the given text widget. Helper method. | |
4592 * | |
4593 * @param presentation the style information | |
4594 * @since 2.1 | |
4595 */ | |
4596 private void applyTextPresentation(TextPresentation presentation) { | |
4597 | |
4598 List list= new ArrayList(presentation.getDenumerableRanges()); | |
4599 Iterator e= presentation.getAllStyleRangeIterator(); | |
4600 while (e.hasNext()) { | |
134 | 4601 StyleRange range= cast(StyleRange) e.next(); |
129 | 4602 range= modelStyleRange2WidgetStyleRange(range); |
4603 if (range !is null) | |
4604 list.add(range); | |
4605 } | |
4606 | |
4607 if (!list.isEmpty()) { | |
4608 StyleRange[] ranges= new StyleRange[list.size()]; | |
4609 list.toArray(ranges); | |
4610 fTextWidget.setStyleRanges(ranges); | |
4611 } | |
4612 } | |
4613 | |
4614 /** | |
4615 * Returns the visible region if it is not equal to the whole document. | |
4616 * Otherwise returns <code>null</code>. | |
4617 * | |
4618 * @return the viewer's visible region if smaller than input document, otherwise <code>null</code> | |
4619 */ | |
4620 protected IRegion _internalGetVisibleRegion() { | |
4621 | |
4622 IDocument document= getVisibleDocument(); | |
138 | 4623 if ( cast(ChildDocument)document ) { |
134 | 4624 Position p= (cast(ChildDocument) document).getParentDocumentRange(); |
129 | 4625 return new Region(p.getOffset(), p.getLength()); |
4626 } | |
4627 | |
4628 return null; | |
4629 } | |
4630 | |
4631 /* | |
4632 * @see ITextViewer#changeTextPresentation(TextPresentation, bool) | |
4633 */ | |
4634 public void changeTextPresentation(TextPresentation presentation, bool controlRedraw) { | |
4635 | |
4636 if (presentation is null || !redraws()) | |
4637 return; | |
4638 | |
4639 if (fTextWidget is null) | |
4640 return; | |
4641 | |
4642 | |
4643 /* | |
4644 * Call registered text presentation listeners | |
4645 * and let them apply their presentation. | |
4646 */ | |
4647 if (fTextPresentationListeners !is null) { | |
4648 ArrayList listeners= new ArrayList(fTextPresentationListeners); | |
4649 for (int i= 0, size= listeners.size(); i < size; i++) { | |
134 | 4650 ITextPresentationListener listener= cast(ITextPresentationListener)listeners.get(i); |
129 | 4651 listener.applyTextPresentation(presentation); |
4652 } | |
4653 } | |
4654 | |
4655 if (presentation.isEmpty()) | |
4656 return; | |
4657 | |
4658 if (controlRedraw) | |
4659 fTextWidget.setRedraw(false); | |
4660 | |
4661 if (fReplaceTextPresentation) | |
4662 applyTextPresentation(presentation); | |
4663 else | |
4664 addPresentation(presentation); | |
4665 | |
4666 if (controlRedraw) | |
4667 fTextWidget.setRedraw(true); | |
4668 } | |
4669 | |
4670 /* | |
4671 * @see ITextViewer#getFindReplaceTarget() | |
4672 */ | |
4673 public IFindReplaceTarget getFindReplaceTarget() { | |
4674 if (fFindReplaceTarget is null) | |
4675 fFindReplaceTarget= new FindReplaceTarget(); | |
4676 return fFindReplaceTarget; | |
4677 } | |
4678 | |
4679 /** | |
4680 * Returns the find/replace document adapter. | |
4681 * | |
4682 * @return the find/replace document adapter. | |
4683 * @since 3.0 | |
4684 */ | |
4685 protected FindReplaceDocumentAdapter getFindReplaceDocumentAdapter() { | |
4686 if (fFindReplaceDocumentAdapter is null) | |
4687 fFindReplaceDocumentAdapter= new FindReplaceDocumentAdapter(getVisibleDocument()); | |
4688 return fFindReplaceDocumentAdapter; | |
4689 } | |
4690 | |
4691 /* | |
4692 * @see ITextViewer#getTextOperationTarget() | |
4693 */ | |
4694 public ITextOperationTarget getTextOperationTarget() { | |
4695 return this; | |
4696 } | |
4697 | |
4698 /* | |
4699 * @see ITextViewerExtension#appendVerifyKeyListener(VerifyKeyListener) | |
4700 * @since 2.0 | |
4701 */ | |
4702 public void appendVerifyKeyListener(VerifyKeyListener listener) { | |
4703 int index= fVerifyKeyListenersManager.numberOfListeners(); | |
4704 fVerifyKeyListenersManager.insertListener(listener, index); | |
4705 } | |
4706 | |
4707 /* | |
4708 * @see ITextViewerExtension#prependVerifyKeyListener(VerifyKeyListener) | |
4709 * @since 2.0 | |
4710 */ | |
4711 public void prependVerifyKeyListener(VerifyKeyListener listener) { | |
4712 fVerifyKeyListenersManager.insertListener(listener, 0); | |
4713 | |
4714 } | |
4715 | |
4716 /* | |
4717 * @see ITextViewerExtension#removeVerifyKeyListener(VerifyKeyListener) | |
4718 * @since 2.0 | |
4719 */ | |
4720 public void removeVerifyKeyListener(VerifyKeyListener listener) { | |
4721 fVerifyKeyListenersManager.removeListener(listener); | |
4722 } | |
4723 | |
4724 /* | |
4725 * @see ITextViewerExtension#getMark() | |
4726 * @since 2.0 | |
4727 */ | |
4728 public int getMark() { | |
4729 return fMarkPosition is null || fMarkPosition.isDeleted() ? -1 : fMarkPosition.getOffset(); | |
4730 } | |
4731 | |
4732 /* | |
4733 * @see ITextViewerExtension#setMark(int) | |
4734 * @since 2.0 | |
4735 */ | |
4736 public void setMark(int offset) { | |
4737 | |
4738 // clear | |
4739 if (offset is -1) { | |
4740 if (fMarkPosition !is null && !fMarkPosition.isDeleted()) { | |
4741 | |
4742 IDocument document= getDocument(); | |
4743 if (document !is null) | |
4744 document.removePosition(fMarkPosition); | |
4745 } | |
4746 | |
4747 fMarkPosition= null; | |
4748 | |
4749 markChanged(-1, 0); | |
4750 | |
4751 // set | |
4752 } else { | |
4753 | |
4754 IDocument document= getDocument(); | |
4755 if (document is null) { | |
4756 fMarkPosition= null; | |
4757 return; | |
4758 } | |
4759 | |
4760 if (fMarkPosition !is null) | |
4761 document.removePosition(fMarkPosition); | |
4762 | |
4763 fMarkPosition= null; | |
4764 | |
4765 try { | |
4766 | |
4767 Position position= new Position(offset); | |
4768 document.addPosition(MARK_POSITION_CATEGORY, position); | |
4769 fMarkPosition= position; | |
4770 | |
4771 } catch (BadLocationException e) { | |
4772 return; | |
4773 } catch (BadPositionCategoryException e) { | |
4774 return; | |
4775 } | |
4776 | |
4777 markChanged(modelOffset2WidgetOffset(fMarkPosition.offset), 0); | |
4778 } | |
4779 } | |
4780 | |
4781 /* | |
4782 * @see Viewer#inputChanged(Object, Object) | |
4783 * @since 2.0 | |
4784 */ | |
4785 protected void inputChanged(Object newInput, Object oldInput) { | |
4786 | |
134 | 4787 IDocument oldDocument= cast(IDocument) oldInput; |
129 | 4788 if (oldDocument !is null) { |
4789 | |
4790 if (fMarkPosition !is null && !fMarkPosition.isDeleted()) | |
4791 oldDocument.removePosition(fMarkPosition); | |
4792 | |
4793 try { | |
4794 oldDocument.removePositionUpdater(fMarkPositionUpdater); | |
4795 oldDocument.removePositionCategory(MARK_POSITION_CATEGORY); | |
4796 | |
4797 } catch (BadPositionCategoryException e) { | |
4798 } | |
4799 } | |
4800 | |
4801 fMarkPosition= null; | |
4802 | |
138 | 4803 if ( cast(IDocumentExtension4)oldDocument ) { |
134 | 4804 IDocumentExtension4 document= cast(IDocumentExtension4) oldDocument; |
129 | 4805 document.removeDocumentRewriteSessionListener(fDocumentRewriteSessionListener); |
4806 } | |
4807 | |
4808 super.inputChanged(newInput, oldInput); | |
4809 | |
138 | 4810 if ( cast(IDocumentExtension4)newInput ) { |
134 | 4811 IDocumentExtension4 document= cast(IDocumentExtension4) newInput; |
129 | 4812 document.addDocumentRewriteSessionListener(fDocumentRewriteSessionListener); |
4813 } | |
4814 | |
134 | 4815 IDocument newDocument= cast(IDocument) newInput; |
129 | 4816 if (newDocument !is null) { |
4817 newDocument.addPositionCategory(MARK_POSITION_CATEGORY); | |
4818 newDocument.addPositionUpdater(fMarkPositionUpdater); | |
4819 } | |
4820 } | |
4821 | |
4822 /** | |
4823 * Informs all text listeners about the change of the viewer's redraw state. | |
4824 * @since 2.0 | |
4825 */ | |
4826 private void fireRedrawChanged() { | |
4827 fWidgetCommand.start= 0; | |
4828 fWidgetCommand.length= 0; | |
4829 fWidgetCommand.text= null; | |
4830 fWidgetCommand.event= null; | |
4831 updateTextListeners(fWidgetCommand); | |
4832 } | |
4833 | |
4834 /** | |
4835 * Enables the redrawing of this text viewer. | |
4836 * @since 2.0 | |
4837 */ | |
4838 protected void enabledRedrawing() { | |
4839 enabledRedrawing(-1); | |
4840 } | |
4841 /** | |
4842 * Enables the redrawing of this text viewer. | |
4843 * | |
4844 * @param topIndex the top index to be set or <code>-1</code> | |
4845 * @since 3.0 | |
4846 */ | |
4847 protected void enabledRedrawing(int topIndex) { | |
138 | 4848 if ( cast(IDocumentAdapterExtension)fDocumentAdapter ) { |
134 | 4849 IDocumentAdapterExtension extension= cast(IDocumentAdapterExtension) fDocumentAdapter; |
129 | 4850 StyledText textWidget= getTextWidget(); |
4851 if (textWidget !is null && !textWidget.isDisposed()) { | |
4852 extension.resumeForwardingDocumentChanges(); | |
4853 if (topIndex > -1) { | |
4854 try { | |
4855 setTopIndex(topIndex); | |
4856 } catch (IllegalArgumentException x) { | |
4857 // changes don't allow for the previous top pixel | |
4858 } | |
4859 } | |
4860 } | |
4861 } | |
4862 | |
4863 if (fViewerState !is null) { | |
4864 fViewerState.restore(topIndex is -1); | |
4865 fViewerState= null; | |
4866 } | |
4867 | |
4868 if (fTextWidget !is null && !fTextWidget.isDisposed()) | |
4869 fTextWidget.setRedraw(true); | |
4870 | |
4871 fireRedrawChanged(); | |
4872 } | |
4873 | |
4874 /** | |
4875 * Disables the redrawing of this text viewer. Subclasses may extend. | |
4876 * @since 2.0 | |
4877 */ | |
4878 protected void disableRedrawing() { | |
4879 if (fViewerState is null) | |
4880 fViewerState= new ViewerState(); | |
4881 | |
138 | 4882 if ( cast(IDocumentAdapterExtension)fDocumentAdapter ) { |
134 | 4883 IDocumentAdapterExtension extension= cast(IDocumentAdapterExtension) fDocumentAdapter; |
129 | 4884 extension.stopForwardingDocumentChanges(); |
4885 } | |
4886 | |
4887 if (fTextWidget !is null && !fTextWidget.isDisposed()) | |
4888 fTextWidget.setRedraw(false); | |
4889 | |
4890 fireRedrawChanged(); | |
4891 } | |
4892 | |
4893 /* | |
4894 * @see ITextViewerExtension#setRedraw(bool) | |
4895 * @since 2.0 | |
4896 */ | |
4897 public final void setRedraw(bool redraw) { | |
4898 setRedraw(redraw, -1); | |
4899 } | |
4900 | |
4901 /** | |
4902 * Basically same functionality as | |
4903 * <code>ITextViewerExtension.setRedraw(bool)</code>. Adds a way for | |
4904 * subclasses to pass in a desired top index that should be used when | |
4905 * <code>redraw</code> is <code>true</code>. If <code>topIndex</code> | |
4906 * is -1, this method is identical to | |
4907 * <code>ITextViewerExtension.setRedraw(bool)</code>. | |
4908 * | |
4909 * @see ITextViewerExtension#setRedraw(bool) | |
4910 * | |
4911 * @param redraw | |
4912 * @param topIndex | |
4913 * @since 3.0 | |
4914 */ | |
4915 protected final void setRedraw(bool redraw, int topIndex) { | |
4916 if (!redraw) { | |
4917 | |
4918 ++ fRedrawCounter; | |
4919 if (fRedrawCounter is 1) | |
4920 disableRedrawing(); | |
4921 | |
4922 } else { | |
4923 -- fRedrawCounter; | |
4924 if (fRedrawCounter is 0) { | |
4925 if (topIndex is -1) | |
4926 enabledRedrawing(); | |
4927 else | |
4928 enabledRedrawing(topIndex); | |
4929 } | |
4930 } | |
4931 } | |
4932 | |
4933 /** | |
4934 * Returns whether this viewer redraws itself. | |
4935 * | |
4936 * @return <code>true</code> if this viewer redraws itself | |
4937 * @since 2.0 | |
4938 */ | |
4939 protected final bool redraws() { | |
4940 return fRedrawCounter <= 0; | |
4941 } | |
4942 | |
4943 /** | |
4944 * Starts the sequential rewrite mode of the viewer's document. | |
4945 * | |
4946 * @param normalized <code>true</code> if the rewrite is performed from the start to the end of the document | |
4947 * @since 2.0 | |
4948 * @deprecated since 3.1 use {@link IDocumentExtension4#startRewriteSession(DocumentRewriteSessionType)} instead | |
4949 */ | |
4950 protected final void startSequentialRewriteMode(bool normalized) { | |
4951 IDocument document= getDocument(); | |
138 | 4952 if ( cast(IDocumentExtension)document ) { |
134 | 4953 IDocumentExtension extension= cast(IDocumentExtension) document; |
129 | 4954 extension.startSequentialRewrite(normalized); |
4955 } | |
4956 } | |
4957 | |
4958 /** | |
4959 * Sets the sequential rewrite mode of the viewer's document. | |
4960 * | |
4961 * @since 2.0 | |
4962 * @deprecated since 3.1 use {@link IDocumentExtension4#stopRewriteSession(DocumentRewriteSession)} instead | |
4963 */ | |
4964 protected final void stopSequentialRewriteMode() { | |
4965 IDocument document= getDocument(); | |
138 | 4966 if ( cast(IDocumentExtension)document ) { |
134 | 4967 IDocumentExtension extension= cast(IDocumentExtension) document; |
129 | 4968 extension.stopSequentialRewrite(); |
4969 } | |
4970 } | |
4971 | |
4972 /* | |
4973 * @see dwtx.jface.text.ITextViewerExtension#getRewriteTarget() | |
4974 * @since 2.0 | |
4975 */ | |
4976 public IRewriteTarget getRewriteTarget() { | |
4977 if (fRewriteTarget is null) | |
4978 fRewriteTarget= new RewriteTarget(); | |
4979 return fRewriteTarget; | |
4980 } | |
4981 | |
4982 /* | |
4983 * @see dwtx.jface.text.ITextViewerExtension2#getCurrentTextHover() | |
4984 */ | |
4985 public ITextHover getCurrentTextHover() { | |
4986 if (fTextHoverManager is null) | |
4987 return null; | |
4988 return fTextHoverManager.getCurrentTextHover(); | |
4989 } | |
4990 | |
4991 /* | |
4992 * @see dwtx.jface.text.ITextViewerExtension2#getHoverEventLocation() | |
4993 */ | |
4994 public Point getHoverEventLocation() { | |
4995 if (fTextHoverManager is null) | |
4996 return null; | |
4997 return fTextHoverManager.getHoverEventLocation(); | |
4998 } | |
4999 | |
5000 /** | |
5001 * Returns the paint manager of this viewer. | |
5002 * | |
5003 * @return the paint manager of this viewer | |
5004 * @since 2.1 | |
5005 */ | |
5006 protected PaintManager getPaintManager() { | |
5007 if (fPaintManager is null) | |
5008 fPaintManager= new PaintManager(this); | |
5009 return fPaintManager; | |
5010 } | |
5011 | |
5012 /** | |
5013 * Adds the given painter to this viewer. If the painter is already registered | |
5014 * this method is without effect. | |
5015 * | |
5016 * @param painter the painter to be added | |
5017 * @since 2.1 | |
5018 */ | |
5019 public void addPainter(IPainter painter) { | |
5020 getPaintManager().addPainter(painter); | |
5021 } | |
5022 | |
5023 /** | |
5024 * Removes the given painter from this viewer. If the painter has previously not been | |
5025 * added to this viewer this method is without effect. | |
5026 * | |
5027 * @param painter the painter to be removed | |
5028 * @since 2.1 | |
5029 */ | |
5030 public void removePainter(IPainter painter) { | |
5031 getPaintManager().removePainter(painter); | |
5032 } | |
5033 | |
5034 // ----------------------------------- conversions ------------------------------------------------------- | |
5035 | |
5036 /** | |
5037 * Implements the contract of {@link ITextViewerExtension5#modelLine2WidgetLine(int)}. | |
5038 * | |
5039 * @param modelLine the model line | |
5040 * @return the corresponding widget line or <code>-1</code> | |
5041 * @since 2.1 | |
5042 */ | |
5043 public int modelLine2WidgetLine(int modelLine) { | |
5044 if (fInformationMapping is null) | |
5045 return modelLine; | |
5046 | |
5047 try { | |
5048 return fInformationMapping.toImageLine(modelLine); | |
5049 } catch (BadLocationException x) { | |
5050 } | |
5051 | |
5052 return -1; | |
5053 } | |
5054 | |
5055 /** | |
5056 * Implements the contract of {@link ITextViewerExtension5#modelOffset2WidgetOffset(int)}. | |
5057 * | |
5058 * @param modelOffset the model offset | |
5059 * @return the corresponding widget offset or <code>-1</code> | |
5060 * @since 2.1 | |
5061 */ | |
5062 public int modelOffset2WidgetOffset(int modelOffset) { | |
5063 if (fInformationMapping is null) | |
5064 return modelOffset; | |
5065 | |
5066 try { | |
5067 return fInformationMapping.toImageOffset(modelOffset); | |
5068 } catch (BadLocationException x) { | |
5069 } | |
5070 | |
5071 return -1; | |
5072 } | |
5073 | |
5074 /** | |
5075 * Implements the contract of {@link ITextViewerExtension5#modelRange2WidgetRange(IRegion)}. | |
5076 * | |
5077 * @param modelRange the model range | |
5078 * @return the corresponding widget range or <code>null</code> | |
5079 * @since 2.1 | |
5080 */ | |
5081 public IRegion modelRange2WidgetRange(IRegion modelRange) { | |
5082 if (fInformationMapping is null) | |
5083 return modelRange; | |
5084 | |
5085 try { | |
5086 | |
5087 if (modelRange.getLength() < 0) { | |
5088 Region reversed= new Region(modelRange.getOffset() + modelRange.getLength(), -modelRange.getLength()); | |
5089 IRegion result= fInformationMapping.toImageRegion(reversed); | |
5090 if (result !is null) | |
5091 return new Region(result.getOffset() + result.getLength(), -result.getLength()); | |
5092 } | |
5093 return fInformationMapping.toImageRegion(modelRange); | |
5094 | |
5095 } catch (BadLocationException x) { | |
5096 } | |
5097 | |
5098 return null; | |
5099 } | |
5100 | |
5101 /** | |
5102 * Similar to {@link #modelRange2WidgetRange(IRegion)}, but more forgiving: | |
5103 * if <code>modelRange</code> describes a region entirely hidden in the | |
5104 * image, then this method returns the zero-length region at the offset of | |
5105 * the folded region. | |
5106 * | |
5107 * @param modelRange the model range | |
5108 * @return the corresponding widget range, or <code>null</code> | |
5109 * @since 3.1 | |
5110 */ | |
5111 protected IRegion modelRange2ClosestWidgetRange(IRegion modelRange) { | |
138 | 5112 if (!( cast(IDocumentInformationMappingExtension2)fInformationMapping )) |
129 | 5113 return modelRange2WidgetRange(modelRange); |
5114 | |
5115 try { | |
5116 if (modelRange.getLength() < 0) { | |
5117 Region reversed= new Region(modelRange.getOffset() + modelRange.getLength(), -modelRange.getLength()); | |
134 | 5118 IRegion result= (cast(IDocumentInformationMappingExtension2) fInformationMapping).toClosestImageRegion(reversed); |
129 | 5119 if (result !is null) |
5120 return new Region(result.getOffset() + result.getLength(), -result.getLength()); | |
5121 } | |
134 | 5122 return (cast(IDocumentInformationMappingExtension2) fInformationMapping).toClosestImageRegion(modelRange); |
129 | 5123 |
5124 } catch (BadLocationException x) { | |
5125 } | |
5126 | |
5127 return null; | |
5128 } | |
5129 | |
5130 /** | |
5131 * Implements the contract of {@link ITextViewerExtension5#widgetLine2ModelLine(int)}. | |
5132 * | |
5133 * @param widgetLine the widget line | |
5134 * @return the corresponding model line | |
5135 * @since 2.1 | |
5136 */ | |
5137 public int widgetlLine2ModelLine(int widgetLine) { | |
5138 return widgetLine2ModelLine(widgetLine); | |
5139 } | |
5140 | |
5141 /** | |
5142 * Implements the contract of {@link ITextViewerExtension5#widgetLine2ModelLine(int)}. | |
5143 * | |
5144 * @param widgetLine the widget line | |
5145 * @return the corresponding model line or <code>-1</code> | |
5146 * @since 3.0 | |
5147 */ | |
5148 public int widgetLine2ModelLine(int widgetLine) { | |
5149 if (fInformationMapping is null) | |
5150 return widgetLine; | |
5151 | |
5152 try { | |
5153 return fInformationMapping.toOriginLine(widgetLine); | |
5154 } catch (BadLocationException x) { | |
5155 } | |
5156 | |
5157 return -1; | |
5158 } | |
5159 | |
5160 /** | |
5161 * Implements the contract of {@link ITextViewerExtension5#widgetOffset2ModelOffset(int)}. | |
5162 * | |
5163 * @param widgetOffset the widget offset | |
5164 * @return the corresponding model offset or <code>-1</code> | |
5165 * @since 2.1 | |
5166 */ | |
5167 public int widgetOffset2ModelOffset(int widgetOffset) { | |
5168 if (fInformationMapping is null) | |
5169 return widgetOffset; | |
5170 | |
5171 try { | |
5172 return fInformationMapping.toOriginOffset(widgetOffset); | |
5173 } catch (BadLocationException x) { | |
5174 if (widgetOffset is getVisibleDocument().getLength()) { | |
5175 IRegion coverage= fInformationMapping.getCoverage(); | |
5176 return coverage.getOffset() + coverage.getLength(); | |
5177 } | |
5178 } | |
5179 | |
5180 return -1; | |
5181 } | |
5182 | |
5183 /** | |
5184 * Implements the contract of {@link ITextViewerExtension5#widgetRange2ModelRange(IRegion)}. | |
5185 * | |
5186 * @param widgetRange the widget range | |
5187 * @return the corresponding model range or <code>null</code> | |
5188 * @since 2.1 | |
5189 */ | |
5190 public IRegion widgetRange2ModelRange(IRegion widgetRange) { | |
5191 if (fInformationMapping is null) | |
5192 return widgetRange; | |
5193 | |
5194 try { | |
5195 | |
5196 if (widgetRange.getLength() < 0) { | |
5197 Region reveresed= new Region(widgetRange.getOffset() + widgetRange.getLength(), -widgetRange.getLength()); | |
5198 IRegion result= fInformationMapping.toOriginRegion(reveresed); | |
5199 return new Region(result.getOffset() + result.getLength(), -result.getLength()); | |
5200 } | |
5201 | |
5202 return fInformationMapping.toOriginRegion(widgetRange); | |
5203 | |
5204 } catch (BadLocationException x) { | |
5205 int modelOffset= widgetOffset2ModelOffset(widgetRange.getOffset()); | |
5206 if (modelOffset > -1) { | |
5207 int modelEndOffset= widgetOffset2ModelOffset(widgetRange.getOffset() + widgetRange.getLength()); | |
5208 if (modelEndOffset > -1) | |
5209 return new Region(modelOffset, modelEndOffset - modelOffset); | |
5210 } | |
5211 } | |
5212 | |
5213 return null; | |
5214 } | |
5215 | |
5216 /** | |
5217 * Implements the contract of {@link ITextViewerExtension5#getModelCoverage()}. | |
5218 * | |
5219 * @return the model coverage | |
5220 * @since 2.1 | |
5221 */ | |
5222 public IRegion getModelCoverage() { | |
5223 if (fInformationMapping is null) { | |
5224 IDocument document= getDocument(); | |
5225 if (document is null) | |
5226 return null; | |
5227 return new Region(0, document.getLength()); | |
5228 } | |
5229 | |
5230 return fInformationMapping.getCoverage(); | |
5231 } | |
5232 | |
5233 /** | |
5234 * Returns the line of the widget whose corresponding line in the viewer's document | |
5235 * is closest to the given line in the viewer's document or <code>-1</code>. | |
5236 * | |
5237 * @param modelLine the line in the viewer's document | |
5238 * @return the line in the widget that corresponds best to the given line in the viewer's document or <code>-1</code> | |
5239 * @since 2.1 | |
5240 */ | |
5241 protected int getClosestWidgetLineForModelLine(int modelLine) { | |
5242 if (fInformationMapping is null) | |
5243 return modelLine; | |
5244 | |
5245 try { | |
5246 return fInformationMapping.toClosestImageLine(modelLine); | |
5247 } catch (BadLocationException x) { | |
5248 } | |
5249 | |
5250 return -1; | |
5251 } | |
5252 | |
5253 /** | |
5254 * Translates a style range given relative to the viewer's document into style | |
5255 * ranges relative to the viewer's widget or <code>null</code>. | |
5256 * | |
5257 * @param range the style range in the coordinates of the viewer's document | |
5258 * @return the style range in the coordinates of the viewer's widget or <code>null</code> | |
5259 * @since 2.1 | |
5260 */ | |
5261 protected StyleRange modelStyleRange2WidgetStyleRange(StyleRange range) { | |
5262 IRegion region= modelRange2WidgetRange(new Region(range.start, range.length)); | |
5263 if (region !is null) { | |
134 | 5264 StyleRange result= cast(StyleRange) range.clone(); |
129 | 5265 result.start= region.getOffset(); |
5266 result.length= region.getLength(); | |
5267 return result; | |
5268 } | |
5269 return null; | |
5270 } | |
5271 | |
5272 /** | |
5273 * Same as {@link #modelRange2WidgetRange(IRegion)} just for a {@link dwtx.jface.text.Position}. | |
5274 * | |
5275 * @param modelPosition the position describing a range in the viewer's document | |
5276 * @return a region describing a range in the viewer's widget | |
5277 * @since 2.1 | |
5278 */ | |
5279 protected IRegion modelRange2WidgetRange(Position modelPosition) { | |
5280 return modelRange2WidgetRange(new Region(modelPosition.getOffset(), modelPosition.getLength())); | |
5281 } | |
5282 | |
5283 /** | |
5284 * Translates the widget region of the given verify event into | |
5285 * the corresponding region of the viewer's document. | |
5286 * | |
5287 * @param event the verify event | |
5288 * @return the region of the viewer's document corresponding to the verify event | |
5289 * @since 2.1 | |
5290 */ | |
5291 protected IRegion event2ModelRange(VerifyEvent event) { | |
5292 | |
5293 Region region= null; | |
5294 if (event.start <= event.end) | |
5295 region= new Region(event.start, event.end - event.start); | |
5296 else | |
5297 region= new Region(event.end, event.start - event.end); | |
5298 | |
5299 return widgetRange2ModelRange(region); | |
5300 } | |
5301 | |
5302 /** | |
5303 * Translates the given widget selection into the corresponding region | |
5304 * of the viewer's document or returns <code>null</code> if this fails. | |
5305 * | |
5306 * @param widgetSelection the widget selection | |
5307 * @return the region of the viewer's document corresponding to the widget selection or <code>null</code> | |
5308 * @since 2.1 | |
5309 */ | |
5310 protected Point widgetSelection2ModelSelection(Point widgetSelection) { | |
5311 IRegion region= new Region(widgetSelection.x, widgetSelection.y); | |
5312 region= widgetRange2ModelRange(region); | |
5313 return region is null ? null : new Point(region.getOffset(), region.getLength()); | |
5314 } | |
5315 | |
5316 /** | |
5317 * Translates the given selection range of the viewer's document into | |
5318 * the corresponding widget range or returns <code>null</code> of this fails. | |
5319 * | |
5320 * @param modelSelection the selection range of the viewer's document | |
5321 * @return the widget range corresponding to the selection range or <code>null</code> | |
5322 * @since 2.1 | |
5323 */ | |
5324 protected Point modelSelection2WidgetSelection(Point modelSelection) { | |
5325 if (fInformationMapping is null) | |
5326 return modelSelection; | |
5327 | |
5328 try { | |
5329 IRegion region= new Region(modelSelection.x, modelSelection.y); | |
5330 region= fInformationMapping.toImageRegion(region); | |
5331 if (region !is null) | |
5332 return new Point(region.getOffset(), region.getLength()); | |
5333 } catch (BadLocationException x) { | |
5334 } | |
5335 | |
5336 return null; | |
5337 } | |
5338 | |
5339 /** | |
5340 * Implements the contract of {@link ITextViewerExtension5#widgetLineOfWidgetOffset(int)}. | |
5341 * | |
5342 * @param widgetOffset the widget offset | |
5343 * @return the corresponding widget line or <code>-1</code> | |
5344 * @since 2.1 | |
5345 */ | |
5346 public int widgetLineOfWidgetOffset(int widgetOffset) { | |
5347 IDocument document= getVisibleDocument(); | |
5348 if (document !is null) { | |
5349 try { | |
5350 return document.getLineOfOffset(widgetOffset); | |
5351 } catch (BadLocationException e) { | |
5352 } | |
5353 } | |
5354 return -1; | |
5355 } | |
5356 | |
5357 /* | |
5358 * @see dwtx.jface.text.ITextViewerExtension4#moveFocusToWidgetToken() | |
5359 * @since 3.0 | |
5360 */ | |
5361 public bool moveFocusToWidgetToken() { | |
138 | 5362 if ( cast(IWidgetTokenKeeperExtension)fWidgetTokenKeeper ) { |
134 | 5363 IWidgetTokenKeeperExtension extension= cast(IWidgetTokenKeeperExtension) fWidgetTokenKeeper; |
129 | 5364 return extension.setFocus(this); |
5365 } | |
5366 return false; | |
5367 } | |
5368 | |
5369 /** | |
5370 * Sets the document partitioning of this viewer. The partitioning is used by this viewer to | |
5371 * access partitioning information of the viewers input document. | |
5372 * | |
5373 * @param partitioning the partitioning name | |
5374 * @since 3.0 | |
5375 */ | |
5376 public void setDocumentPartitioning(String partitioning) { | |
5377 fPartitioning= partitioning; | |
5378 } | |
5379 | |
5380 /** | |
5381 * Returns the document partitioning for this viewer. | |
5382 * | |
5383 * @return the document partitioning for this viewer | |
5384 * @since 3.0 | |
5385 */ | |
5386 protected String getDocumentPartitioning() { | |
5387 return fPartitioning; | |
5388 } | |
5389 | |
5390 //---- Text presentation listeners ---- | |
5391 | |
5392 /* | |
5393 * @see ITextViewerExtension4#addTextPresentationListener(ITextPresentationListener) | |
5394 * @since 3.0 | |
5395 */ | |
5396 public void addTextPresentationListener(ITextPresentationListener listener) { | |
5397 | |
5398 Assert.isNotNull(listener); | |
5399 | |
5400 if (fTextPresentationListeners is null) | |
5401 fTextPresentationListeners= new ArrayList(); | |
5402 | |
5403 if (!fTextPresentationListeners.contains(listener)) | |
5404 fTextPresentationListeners.add(listener); | |
5405 } | |
5406 | |
5407 /* | |
5408 * @see ITextViewerExtension4#removeTextPresentationListener(ITextPresentationListener) | |
5409 * @since 3.0 | |
5410 */ | |
5411 public void removeTextPresentationListener(ITextPresentationListener listener) { | |
5412 | |
5413 Assert.isNotNull(listener); | |
5414 | |
5415 if (fTextPresentationListeners !is null) { | |
5416 fTextPresentationListeners.remove(listener); | |
5417 if (fTextPresentationListeners.size() is 0) | |
5418 fTextPresentationListeners= null; | |
5419 } | |
5420 } | |
5421 | |
5422 /* | |
5423 * @see dwtx.jface.text.IEditingSupportRegistry#registerHelper(dwtx.jface.text.IEditingSupport) | |
5424 * @since 3.1 | |
5425 */ | |
5426 public void register(IEditingSupport helper) { | |
5427 Assert.isLegal(helper !is null); | |
5428 fEditorHelpers.add(helper); | |
5429 } | |
5430 | |
5431 /* | |
5432 * @see dwtx.jface.text.IEditingSupportRegistry#deregisterHelper(dwtx.jface.text.IEditingSupport) | |
5433 * @since 3.1 | |
5434 */ | |
5435 public void unregister(IEditingSupport helper) { | |
5436 fEditorHelpers.remove(helper); | |
5437 } | |
5438 | |
5439 /* | |
5440 * @see dwtx.jface.text.IEditingSupportRegistry#getCurrentHelpers() | |
5441 * @since 3.1 | |
5442 */ | |
5443 public IEditingSupport[] getRegisteredSupports() { | |
156 | 5444 return arraycast!(IEditingSupport)( fEditorHelpers.toArray()); |
129 | 5445 } |
5446 | |
5447 /* | |
5448 * @see dwtx.jface.text.ITextViewerExtension6#setHyperlinkDetectors(dwtx.jface.text.hyperlink.IHyperlinkDetector[], int) | |
5449 * @since 3.1 | |
5450 */ | |
5451 public void setHyperlinkDetectors(IHyperlinkDetector[] hyperlinkDetectors, int eventStateMask) { | |
5452 if (fHyperlinkDetectors !is null) { | |
5453 for (int i= 0; i < fHyperlinkDetectors.length; i++) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
5454 if (cast(IHyperlinkDetectorExtension)fHyperlinkDetectors[i] ) |
134 | 5455 (cast(IHyperlinkDetectorExtension)fHyperlinkDetectors[i]).dispose(); |
129 | 5456 } |
5457 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
5458 |
129 | 5459 bool enable= hyperlinkDetectors !is null && hyperlinkDetectors.length > 0; |
5460 fHyperlinkStateMask= eventStateMask; | |
5461 fHyperlinkDetectors= hyperlinkDetectors; | |
5462 if (enable) { | |
5463 if (fHyperlinkManager !is null) { | |
5464 fHyperlinkManager.setHyperlinkDetectors(fHyperlinkDetectors); | |
5465 fHyperlinkManager.setHyperlinkStateMask(fHyperlinkStateMask); | |
5466 } | |
5467 ensureHyperlinkManagerInstalled(); | |
5468 } else { | |
5469 if (fHyperlinkManager !is null) | |
5470 fHyperlinkManager.uninstall(); | |
5471 fHyperlinkManager= null; | |
5472 } | |
5473 } | |
5474 | |
5475 /** | |
5476 * Sets the hyperlink presenter. | |
5477 * <p> | |
5478 * This is only valid as long as the hyperlink manager hasn't | |
5479 * been created yet. | |
5480 * </p> | |
5481 * | |
5482 * @param hyperlinkPresenter the hyperlink presenter | |
5483 * @throws IllegalStateException if the hyperlink manager has already been created | |
5484 * @since 3.1 | |
5485 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
135
diff
changeset
|
5486 public void setHyperlinkPresenter(IHyperlinkPresenter hyperlinkPresenter) { |
129 | 5487 if (fHyperlinkManager !is null) |
5488 throw new IllegalStateException(); | |
5489 | |
5490 fHyperlinkPresenter= hyperlinkPresenter; | |
5491 ensureHyperlinkManagerInstalled(); | |
5492 } | |
5493 | |
5494 /** | |
5495 * Ensures that the hyperlink manager has been | |
5496 * installed if a hyperlink detector is available. | |
5497 * | |
5498 * @since 3.1 | |
5499 */ | |
5500 private void ensureHyperlinkManagerInstalled() { | |
5501 if (fHyperlinkDetectors !is null && fHyperlinkDetectors.length > 0 && fHyperlinkPresenter !is null && fHyperlinkManager is null) { | |
5502 DETECTION_STRATEGY strategy= fHyperlinkPresenter.canShowMultipleHyperlinks() ? HyperlinkManager.ALL : HyperlinkManager.FIRST; | |
5503 fHyperlinkManager= new HyperlinkManager(strategy); | |
5504 fHyperlinkManager.install(this, fHyperlinkPresenter, fHyperlinkDetectors, fHyperlinkStateMask); | |
5505 } | |
5506 } | |
5507 | |
5508 /* | |
5509 * @see dwtx.jface.text.ITextViewerExtension7#setTabsToSpacesConverter(dwtx.jface.text.IAutoEditStrategy) | |
5510 * @since 3.3 | |
5511 */ | |
5512 public void setTabsToSpacesConverter(IAutoEditStrategy converter) { | |
5513 fTabsToSpacesConverter= converter; | |
5514 } | |
5515 | |
5516 } |