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