Mercurial > projects > dwt-addons
annotate dwtx/jface/text/TextViewer.d @ 172:d994a8b2cdf7
again compile fixes
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 10 Sep 2008 23:14:02 +0200 |
parents | 862b05e0334a |
children | c6d7b1ea700b |
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 */ | |
162 | 1687 private /+const+/ int[] 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 fNumberOfPostSelectionChangedEvents= new int[1]; |
1806 fEditorHelpers= new HashSet(); | |
159 | 1807 } |
129 | 1808 /** |
1809 * Internal use only | |
1810 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
1811 protected this() { |
159 | 1812 instanceInit(); |
129 | 1813 } |
1814 | |
1815 /** | |
1816 * Create a new text viewer with the given DWT style bits. | |
1817 * The viewer is ready to use but does not have any plug-in installed. | |
1818 * | |
1819 * @param parent the parent of the viewer's control | |
1820 * @param styles the DWT style bits for the viewer's control, | |
1821 * <em>if <code>DWT.WRAP</code> is set then a custom document adapter needs to be provided, see {@link #createDocumentAdapter()} | |
1822 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
1823 public this(Composite parent, int styles) { |
159 | 1824 instanceInit(); |
129 | 1825 createControl(parent, styles); |
1826 } | |
1827 | |
1828 /** | |
1829 * Factory method to create the text widget to be used as the viewer's text widget. | |
1830 * | |
1831 * @param parent the parent of the styled text | |
1832 * @param styles the styles for the styled text | |
1833 * @return the text widget to be used | |
1834 */ | |
1835 protected StyledText createTextWidget(Composite parent, int styles) { | |
1836 return new StyledText(parent, styles); | |
1837 } | |
1838 | |
1839 /** | |
1840 * Factory method to create the document adapter to be used by this viewer. | |
1841 * | |
1842 * @return the document adapter to be used | |
1843 */ | |
1844 protected IDocumentAdapter createDocumentAdapter() { | |
1845 return new DefaultDocumentAdapter(); | |
1846 } | |
1847 | |
1848 /** | |
1849 * Creates the viewer's DWT control. The viewer's text widget either is | |
1850 * the control or is a child of the control. | |
1851 * | |
1852 * @param parent the parent of the viewer's control | |
1853 * @param styles the DWT style bits for the viewer's control | |
1854 */ | |
1855 protected void createControl(Composite parent, int styles) { | |
1856 | |
1857 fTextWidget= createTextWidget(parent, styles); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1858 |
129 | 1859 // Support scroll page upon MOD1+MouseWheel |
135 | 1860 fTextWidget.addListener(DWT.MouseWheel, new class() Listener { |
129 | 1861 |
1862 public void handleEvent(Event event) { | |
1863 if (((event.stateMask & DWT.MOD1) is 0)) | |
1864 return; | |
1865 | |
1866 int topIndex= fTextWidget.getTopIndex(); | |
1867 int bottomIndex= JFaceTextUtil.getBottomIndex(fTextWidget); | |
1868 | |
1869 if (event.count > 0) | |
1870 fTextWidget.setTopIndex(2 * topIndex - bottomIndex); | |
1871 else | |
1872 fTextWidget.setTopIndex(bottomIndex); | |
1873 | |
1874 updateViewportListeners(INTERNAL); | |
1875 } | |
1876 }); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1877 |
129 | 1878 fTextWidget.addDisposeListener( |
135 | 1879 new class() DisposeListener { |
129 | 1880 public void widgetDisposed(DisposeEvent e) { |
1881 handleDispose(); | |
1882 } | |
1883 } | |
1884 ); | |
1885 | |
1886 fTextWidget.setFont(parent.getFont()); | |
1887 fTextWidget.setDoubleClickEnabled(true); | |
1888 | |
1889 /* | |
1890 * Disable DWT Shift+TAB traversal in this viewer | |
1891 * 1GIYQ9K: ITPUI:WINNT - StyledText swallows Shift+TAB | |
1892 */ | |
135 | 1893 fTextWidget.addTraverseListener(new class() TraverseListener { |
129 | 1894 public void keyTraversed(TraverseEvent e) { |
1895 if ((DWT.SHIFT is e.stateMask) && ('\t' is e.character)) | |
1896 e.doit= false; | |
1897 } | |
1898 }); | |
1899 | |
1900 // where does the first line start | |
1901 fTopInset= -fTextWidget.computeTrim(0, 0, 0, 0).y; | |
1902 | |
1903 fVerifyListener.forward(true); | |
1904 fTextWidget.addVerifyListener(fVerifyListener); | |
1905 | |
135 | 1906 fTextWidget.addSelectionListener(new class() SelectionListener { |
129 | 1907 public void widgetDefaultSelected(SelectionEvent event) { |
1908 selectionChanged(event.x, event.y - event.x); | |
1909 } | |
1910 public void widgetSelected(SelectionEvent event) { | |
1911 selectionChanged(event.x, event.y - event.x); | |
1912 } | |
1913 }); | |
1914 | |
1915 fCursorListener= new CursorListener(); | |
1916 fCursorListener.install(); | |
1917 | |
1918 initializeViewportUpdate(); | |
1919 } | |
1920 | |
1921 /* | |
1922 * @see Viewer#getControl() | |
1923 */ | |
1924 public Control getControl() { | |
1925 return fTextWidget; | |
1926 } | |
1927 | |
1928 /* | |
1929 * @see ITextViewer#activatePlugins() | |
1930 */ | |
1931 public void activatePlugins() { | |
1932 | |
1933 if (fDoubleClickStrategies !is null && !fDoubleClickStrategies.isEmpty() && fDoubleClickStrategyConnector is null) { | |
1934 fDoubleClickStrategyConnector= new TextDoubleClickStrategyConnector(); | |
1935 fTextWidget.addWordMovementListener(fDoubleClickStrategyConnector); | |
1936 fTextWidget.addMouseListener(fDoubleClickStrategyConnector); | |
1937 } | |
1938 | |
1939 ensureHoverControlManagerInstalled(); | |
1940 ensureHyperlinkManagerInstalled(); | |
1941 | |
1942 if (fUndoManager !is null) { | |
1943 fUndoManager.connect(this); | |
1944 fUndoManager.reset(); | |
1945 } | |
1946 } | |
1947 | |
1948 /** | |
1949 * After this method has been executed the caller knows that any installed text hover has been installed. | |
1950 */ | |
1951 private void ensureHoverControlManagerInstalled() { | |
1952 if (fTextHovers !is null && !fTextHovers.isEmpty() && fHoverControlCreator !is null && fTextHoverManager is null) { | |
1953 fTextHoverManager= new TextViewerHoverManager(this, fHoverControlCreator); | |
1954 fTextHoverManager.install(this.getTextWidget()); | |
1955 fTextHoverManager.setSizeConstraints(TEXT_HOVER_WIDTH_CHARS, TEXT_HOVER_HEIGHT_CHARS, false, true); | |
1956 fTextHoverManager.setInformationControlReplacer(new StickyHoverManager(this)); | |
1957 } | |
1958 } | |
1959 | |
1960 /* | |
1961 * @see ITextViewer#resetPlugins() | |
1962 */ | |
1963 public void resetPlugins() { | |
1964 if (fUndoManager !is null) | |
1965 fUndoManager.reset(); | |
1966 } | |
1967 | |
1968 /** | |
1969 * Frees all resources allocated by this viewer. Internally called when the viewer's | |
1970 * control has been disposed. | |
1971 */ | |
1972 protected void handleDispose() { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1973 |
129 | 1974 setDocument(null); |
1975 | |
1976 if (fPaintManager !is null) { | |
1977 fPaintManager.dispose(); | |
1978 fPaintManager= null; | |
1979 } | |
1980 | |
1981 removeViewPortUpdate(); | |
1982 fViewportGuard= null; | |
1983 | |
1984 if (fViewportListeners !is null) { | |
1985 fViewportListeners.clear(); | |
1986 fViewportListeners= null; | |
1987 } | |
1988 | |
1989 if (fTextListeners !is null) { | |
1990 fTextListeners.clear(); | |
1991 fTextListeners= null; | |
1992 } | |
1993 | |
1994 if (fTextInputListeners !is null) { | |
1995 fTextInputListeners.clear(); | |
1996 fTextInputListeners= null; | |
1997 } | |
1998 | |
1999 if (fPostSelectionChangedListeners !is null) { | |
2000 fPostSelectionChangedListeners.clear(); | |
2001 fPostSelectionChangedListeners= null; | |
2002 } | |
2003 | |
2004 if (fAutoIndentStrategies !is null) { | |
2005 fAutoIndentStrategies.clear(); | |
2006 fAutoIndentStrategies= null; | |
2007 } | |
2008 | |
2009 if (fUndoManager !is null) { | |
2010 fUndoManager.disconnect(); | |
2011 fUndoManager= null; | |
2012 } | |
2013 | |
2014 if (fDoubleClickStrategies !is null) { | |
2015 fDoubleClickStrategies.clear(); | |
2016 fDoubleClickStrategies= null; | |
2017 } | |
2018 | |
2019 if (fTextHovers !is null) { | |
2020 fTextHovers.clear(); | |
2021 fTextHovers= null; | |
2022 } | |
2023 | |
2024 fDoubleClickStrategyConnector= null; | |
2025 | |
2026 if (fTextHoverManager !is null) { | |
2027 fTextHoverManager.dispose(); | |
2028 fTextHoverManager= null; | |
2029 } | |
2030 | |
2031 if (fVisibleDocumentListener !is null) { | |
2032 if (fVisibleDocument !is null) | |
2033 fVisibleDocument.removeDocumentListener(fVisibleDocumentListener); | |
2034 fVisibleDocumentListener= null; | |
2035 } | |
2036 | |
2037 if (fDocumentAdapter !is null) { | |
2038 fDocumentAdapter.setDocument(null); | |
2039 fDocumentAdapter= null; | |
2040 } | |
2041 | |
2042 if (fSlaveDocumentManager !is null) { | |
2043 if (fVisibleDocument !is null) | |
2044 fSlaveDocumentManager.freeSlaveDocument(fVisibleDocument); | |
2045 fSlaveDocumentManager= null; | |
2046 } | |
2047 | |
2048 if (fCursorListener !is null) { | |
2049 fCursorListener.uninstall(); | |
2050 fCursorListener= null; | |
2051 } | |
2052 | |
2053 if (fHyperlinkManager !is null) { | |
2054 fHyperlinkManager.uninstall(); | |
2055 fHyperlinkManager= null; | |
2056 } | |
2057 | |
2058 fHyperlinkDetectors= null; | |
2059 fVisibleDocument= null; | |
2060 fDocument= null; | |
2061 fScroller= null; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
2062 |
129 | 2063 fTextWidget= null; |
2064 } | |
2065 | |
2066 | |
2067 //---- simple getters and setters | |
2068 | |
2069 /* | |
2070 * @see dwtx.jface.text.ITextViewer#getTextWidget() | |
2071 */ | |
2072 public StyledText getTextWidget() { | |
2073 return fTextWidget; | |
2074 } | |
2075 | |
2076 /** | |
2077 * The delay in milliseconds before an empty selection | |
2078 * changed event is sent by the cursor listener. | |
2079 * <p> | |
2080 * Note: The return value is used to initialize the cursor | |
2081 * listener. To return a non-constant value has no effect.</p> | |
2082 * <p> | |
2083 * The same value (<code>500</code>) is used in <code>OpenStrategy.TIME</code>.</p> | |
2084 * | |
2085 * @return delay in milliseconds | |
2086 * @see dwtx.jface.util.OpenStrategy | |
2087 * @since 3.0 | |
2088 */ | |
2089 protected int getEmptySelectionChangedEventDelay() { | |
2090 return 500; | |
2091 } | |
2092 | |
2093 /** | |
2094 * {@inheritDoc} | |
2095 * @deprecated since 3.1, use | |
2096 * {@link ITextViewerExtension2#prependAutoEditStrategy(IAutoEditStrategy, String)} and | |
2097 * {@link ITextViewerExtension2#removeAutoEditStrategy(IAutoEditStrategy, String)} instead | |
2098 */ | |
2099 public void setAutoIndentStrategy(IAutoIndentStrategy strategy, String contentType) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
2100 setAutoEditStrategies([ strategy ], contentType); |
129 | 2101 } |
2102 | |
2103 /** | |
2104 * Sets the given edit strategy as the only strategy for the given content type. | |
2105 * | |
2106 * @param strategies the auto edit strategies | |
2107 * @param contentType the content type | |
2108 * @since 3.1 | |
2109 */ | |
2110 protected final void setAutoEditStrategies(IAutoEditStrategy[] strategies, String contentType) { | |
2111 if (fAutoIndentStrategies is null) | |
2112 fAutoIndentStrategies= new HashMap(); | |
2113 | |
134 | 2114 List autoEditStrategies= cast(List) fAutoIndentStrategies.get(contentType); |
129 | 2115 |
2116 if (strategies is null) { | |
2117 if (autoEditStrategies is null) | |
2118 return; | |
2119 | |
162 | 2120 fAutoIndentStrategies.put(contentType, cast(Object)null); |
129 | 2121 |
2122 } else { | |
2123 if (autoEditStrategies is null) { | |
2124 autoEditStrategies= new ArrayList(); | |
162 | 2125 fAutoIndentStrategies.put(contentType, cast(Object)autoEditStrategies); |
129 | 2126 } |
2127 | |
2128 autoEditStrategies.clear(); | |
162 | 2129 autoEditStrategies.addAll(Arrays.asList(arraycast!(Object)(strategies))); |
129 | 2130 } |
2131 } | |
2132 | |
2133 /* | |
2134 * @see dwtx.jface.text.ITextViewerExtension2#prependAutoEditStrategy(dwtx.jface.text.IAutoEditStrategy, java.lang.String) | |
2135 * @since 2.1 | |
2136 */ | |
2137 public void prependAutoEditStrategy(IAutoEditStrategy strategy, String contentType) { | |
2138 | |
2139 if (strategy is null || contentType is null) | |
162 | 2140 throw new IllegalArgumentException(""); |
129 | 2141 |
2142 if (fAutoIndentStrategies is null) | |
2143 fAutoIndentStrategies= new HashMap(); | |
2144 | |
134 | 2145 List autoEditStrategies= cast(List) fAutoIndentStrategies.get(contentType); |
129 | 2146 if (autoEditStrategies is null) { |
2147 autoEditStrategies= new ArrayList(); | |
162 | 2148 fAutoIndentStrategies.put(contentType,cast(Object) autoEditStrategies); |
2149 } | |
2150 | |
2151 autoEditStrategies.add(0, cast(Object)strategy); | |
129 | 2152 } |
2153 | |
2154 /* | |
2155 * @see dwtx.jface.text.ITextViewerExtension2#removeAutoEditStrategy(dwtx.jface.text.IAutoEditStrategy, java.lang.String) | |
2156 * @since 2.1 | |
2157 */ | |
2158 public void removeAutoEditStrategy(IAutoEditStrategy strategy, String contentType) { | |
2159 if (fAutoIndentStrategies is null) | |
2160 return; | |
2161 | |
134 | 2162 List autoEditStrategies= cast(List) fAutoIndentStrategies.get(contentType); |
129 | 2163 if (autoEditStrategies is null) |
2164 return; | |
2165 | |
2166 for (final Iterator iterator= autoEditStrategies.iterator(); iterator.hasNext(); ) { | |
162 | 2167 if (iterator.next().opEquals(cast(Object)strategy)) { |
129 | 2168 iterator.remove(); |
2169 break; | |
2170 } | |
2171 } | |
2172 | |
2173 if (autoEditStrategies.isEmpty()) | |
162 | 2174 fAutoIndentStrategies.put(contentType, cast(Object)null); |
129 | 2175 } |
2176 | |
2177 /* | |
2178 * @see ITextViewer#setEventConsumer(IEventConsumer) | |
2179 */ | |
2180 public void setEventConsumer(IEventConsumer consumer) { | |
2181 fEventConsumer= consumer; | |
2182 } | |
2183 | |
2184 /* | |
2185 * @see ITextViewer#setIndentPrefixes(String[], String) | |
2186 */ | |
2187 public void setIndentPrefixes(String[] indentPrefixes, String contentType) { | |
2188 | |
2189 int i= -1; | |
2190 bool ok= (indentPrefixes !is null); | |
2191 while (ok && ++i < indentPrefixes.length) | |
2192 ok= (indentPrefixes[i] !is null); | |
2193 | |
2194 if (ok) { | |
2195 | |
2196 if (fIndentChars is null) | |
2197 fIndentChars= new HashMap(); | |
2198 | |
162 | 2199 fIndentChars.put(contentType, new ArrayWrapperObject( stringcast(indentPrefixes))); |
129 | 2200 |
2201 } else if (fIndentChars !is null) | |
2202 fIndentChars.remove(contentType); | |
2203 } | |
2204 | |
2205 /* | |
2206 * @see ITextViewer#getTopInset() | |
2207 */ | |
2208 public int getTopInset() { | |
2209 return fTopInset; | |
2210 } | |
2211 | |
2212 /* | |
2213 * @see ITextViewer#isEditable() | |
2214 */ | |
2215 public bool isEditable() { | |
2216 if (fTextWidget is null) | |
2217 return false; | |
2218 return fTextWidget.getEditable(); | |
2219 } | |
2220 | |
2221 /* | |
2222 * @see ITextViewer#setEditable(bool) | |
2223 */ | |
2224 public void setEditable(bool editable) { | |
2225 if (fTextWidget !is null) | |
2226 fTextWidget.setEditable(editable); | |
2227 } | |
2228 | |
2229 /* | |
2230 * @see ITextViewer#setDefaultPrefixes | |
2231 * @since 2.0 | |
2232 */ | |
2233 public void setDefaultPrefixes(String[] defaultPrefixes, String contentType) { | |
2234 | |
2235 if (defaultPrefixes !is null && defaultPrefixes.length > 0) { | |
2236 if (fDefaultPrefixChars is null) | |
2237 fDefaultPrefixChars= new HashMap(); | |
162 | 2238 fDefaultPrefixChars.put(contentType, new ArrayWrapperObject( stringcast(defaultPrefixes))); |
129 | 2239 } else if (fDefaultPrefixChars !is null) |
2240 fDefaultPrefixChars.remove(contentType); | |
2241 } | |
2242 | |
2243 /* | |
2244 * @see ITextViewer#setUndoManager(IUndoManager) | |
2245 */ | |
2246 public void setUndoManager(IUndoManager undoManager) { | |
2247 fUndoManager= undoManager; | |
2248 } | |
2249 | |
2250 /* | |
2251 * @see ITextViewerExtension6#getUndoManager() | |
2252 * @since 3.1 | |
2253 */ | |
2254 public IUndoManager getUndoManager() { | |
2255 return fUndoManager; | |
2256 } | |
2257 | |
2258 /* | |
2259 * @see ITextViewer#setTextHover(ITextHover, String) | |
2260 */ | |
2261 public void setTextHover(ITextHover hover, String contentType) { | |
2262 setTextHover(hover, contentType, ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK); | |
2263 } | |
2264 | |
2265 /* | |
2266 * @see ITextViewerExtension2#setTextHover(ITextHover, String, int) | |
2267 * @since 2.1 | |
2268 */ | |
2269 public void setTextHover(ITextHover hover, String contentType, int stateMask) { | |
2270 TextHoverKey key= new TextHoverKey(contentType, stateMask); | |
2271 if (hover !is null) { | |
2272 if (fTextHovers is null) { | |
2273 fTextHovers= new HashMap(); | |
2274 } | |
162 | 2275 fTextHovers.put(key, cast(Object)hover); |
129 | 2276 } else if (fTextHovers !is null) |
2277 fTextHovers.remove(key); | |
2278 | |
2279 ensureHoverControlManagerInstalled(); | |
2280 } | |
2281 | |
2282 /* | |
2283 * @see ITextViewerExtension2#removeTextHovers(String) | |
2284 * @since 2.1 | |
2285 */ | |
2286 public void removeTextHovers(String contentType) { | |
2287 if (fTextHovers is null) | |
2288 return; | |
2289 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
2290 Iterator iter= (new HashSet(fTextHovers.keySet())).iterator(); |
129 | 2291 while (iter.hasNext()) { |
134 | 2292 TextHoverKey key= cast(TextHoverKey)iter.next(); |
129 | 2293 if (key.fContentType.equals(contentType)) |
2294 fTextHovers.remove(key); | |
2295 } | |
2296 } | |
2297 | |
2298 /** | |
2299 * Returns the text hover for a given offset. | |
2300 * | |
2301 * @param offset the offset for which to return the text hover | |
2302 * @return the text hover for the given offset | |
2303 */ | |
2304 protected ITextHover getTextHover(int offset) { | |
2305 return getTextHover(offset, ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK); | |
2306 } | |
161 | 2307 package ITextHover getTextHover_package(int offset) { |
2308 return getTextHover(offset); | |
2309 } | |
129 | 2310 |
2311 /** | |
2312 * Returns the text hover for a given offset and a given state mask. | |
2313 * | |
2314 * @param offset the offset for which to return the text hover | |
2315 * @param stateMask the DWT event state mask | |
2316 * @return the text hover for the given offset and state mask | |
2317 * @since 2.1 | |
2318 */ | |
161 | 2319 package ITextHover getTextHover_package(int offset, int stateMask) { |
2320 return getTextHover(offset,stateMask); | |
2321 } | |
129 | 2322 protected ITextHover getTextHover(int offset, int stateMask) { |
2323 if (fTextHovers is null) | |
2324 return null; | |
2325 | |
2326 IDocument document= getDocument(); | |
2327 if (document is null) | |
2328 return null; | |
2329 | |
2330 try { | |
2331 TextHoverKey key= new TextHoverKey(TextUtilities.getContentType(document, getDocumentPartitioning(), offset, true), stateMask); | |
2332 Object textHover= fTextHovers.get(key); | |
2333 if (textHover is null) { | |
2334 // Use default text hover | |
2335 key.setStateMask(ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK); | |
2336 textHover= fTextHovers.get(key); | |
2337 } | |
134 | 2338 return cast(ITextHover) textHover; |
129 | 2339 } catch (BadLocationException x) { |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
2340 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
2341 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.selectContentTypePlugin")); //$NON-NLS-1$ |
129 | 2342 } |
2343 return null; | |
2344 } | |
2345 | |
2346 /** | |
2347 * Returns the text hovering controller of this viewer. | |
2348 * | |
2349 * @return the text hovering controller of this viewer | |
2350 * @since 2.0 | |
2351 */ | |
2352 protected AbstractInformationControlManager getTextHoveringController() { | |
2353 return fTextHoverManager; | |
2354 } | |
2355 | |
2356 /** | |
2357 * Sets the creator for the hover controls. | |
2358 * | |
2359 * @param creator the hover control creator | |
2360 * @since 2.0 | |
2361 */ | |
2362 public void setHoverControlCreator(IInformationControlCreator creator) { | |
2363 fHoverControlCreator= creator; | |
2364 } | |
2365 | |
2366 /** | |
2367 * {@inheritDoc} | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
2368 * |
129 | 2369 * @since 3.4 |
2370 */ | |
159 | 2371 public void setHoverEnrichMode(ITextViewerExtension8_EnrichMode mode) { |
129 | 2372 if (fTextHoverManager is null) |
2373 return; | |
2374 fTextHoverManager.setHoverEnrichMode(mode); | |
2375 } | |
2376 | |
2377 /* | |
2378 * @see IWidgetTokenOwner#requestWidgetToken(IWidgetTokenKeeper) | |
2379 * @since 2.0 | |
2380 */ | |
2381 public bool requestWidgetToken(IWidgetTokenKeeper requester) { | |
2382 if (fTextWidget !is null) { | |
2383 if (fWidgetTokenKeeper !is null) { | |
2384 if (fWidgetTokenKeeper is requester) | |
2385 return true; | |
2386 if (fWidgetTokenKeeper.requestWidgetToken(this)) { | |
2387 fWidgetTokenKeeper= requester; | |
2388 return true; | |
2389 } | |
2390 } else { | |
2391 fWidgetTokenKeeper= requester; | |
2392 return true; | |
2393 } | |
2394 } | |
2395 return false; | |
2396 } | |
2397 | |
2398 /* | |
2399 * @see dwtx.jface.text.IWidgetTokenOwnerExtension#requestWidgetToken(dwtx.jface.text.IWidgetTokenKeeper, int) | |
2400 * @since 3.0 | |
2401 */ | |
2402 public bool requestWidgetToken(IWidgetTokenKeeper requester, int priority) { | |
2403 if (fTextWidget !is null) { | |
2404 if (fWidgetTokenKeeper !is null) { | |
2405 | |
2406 if (fWidgetTokenKeeper is requester) | |
2407 return true; | |
2408 | |
2409 bool accepted= false; | |
138 | 2410 if ( cast(IWidgetTokenKeeperExtension)fWidgetTokenKeeper ) { |
134 | 2411 IWidgetTokenKeeperExtension extension= cast(IWidgetTokenKeeperExtension) fWidgetTokenKeeper; |
129 | 2412 accepted= extension.requestWidgetToken(this, priority); |
2413 } else { | |
2414 accepted= fWidgetTokenKeeper.requestWidgetToken(this); | |
2415 } | |
2416 | |
2417 if (accepted) { | |
2418 fWidgetTokenKeeper= requester; | |
2419 return true; | |
2420 } | |
2421 | |
2422 } else { | |
2423 fWidgetTokenKeeper= requester; | |
2424 return true; | |
2425 } | |
2426 } | |
2427 return false; | |
2428 } | |
2429 | |
2430 /* | |
2431 * @see IWidgetTokenOwner#releaseWidgetToken(IWidgetTokenKeeper) | |
2432 * @since 2.0 | |
2433 */ | |
2434 public void releaseWidgetToken(IWidgetTokenKeeper tokenKeeper) { | |
2435 if (fWidgetTokenKeeper is tokenKeeper) | |
2436 fWidgetTokenKeeper= null; | |
2437 } | |
2438 | |
2439 | |
2440 //---- Selection | |
2441 | |
2442 /* | |
2443 * @see ITextViewer#getSelectedRange() | |
2444 */ | |
2445 public Point getSelectedRange() { | |
2446 | |
2447 if (!redraws() && fViewerState !is null) | |
2448 return fViewerState.getSelection(); | |
2449 | |
2450 if (fTextWidget !is null) { | |
2451 Point p= fTextWidget.getSelectionRange(); | |
2452 p= widgetSelection2ModelSelection(p); | |
2453 if (p !is null) | |
2454 return p; | |
2455 } | |
2456 | |
2457 return new Point(-1, -1); | |
2458 } | |
2459 | |
2460 /* | |
2461 * @see ITextViewer#setSelectedRange(int, int) | |
2462 */ | |
2463 public void setSelectedRange(int selectionOffset, int selectionLength) { | |
2464 | |
2465 if (!redraws()) { | |
2466 if (fViewerState !is null) | |
2467 fViewerState.updateSelection(selectionOffset, selectionLength); | |
2468 return; | |
2469 } | |
2470 | |
2471 if (fTextWidget is null) | |
2472 return; | |
2473 | |
2474 IRegion widgetSelection= modelRange2ClosestWidgetRange(new Region(selectionOffset, selectionLength)); | |
2475 if (widgetSelection !is null) { | |
2476 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
2477 int[] selectionRange= [ widgetSelection.getOffset(), widgetSelection.getLength() ]; |
129 | 2478 validateSelectionRange(selectionRange); |
2479 if (selectionRange[0] >= 0) { | |
2480 fTextWidget.setSelectionRange(selectionRange[0], selectionRange[1]); | |
2481 selectionChanged(selectionRange[0], selectionRange[1]); | |
2482 } | |
2483 } | |
2484 } | |
2485 | |
2486 /** | |
2487 * Validates and adapts the given selection range if it is not a valid | |
2488 * widget selection. The widget selection is invalid if it starts or ends | |
2489 * inside a multi-character line delimiter. If so, the selection is adapted to | |
2490 * start <b>after</b> the divided line delimiter and to end <b>before</b> | |
2491 * the divided line delimiter. The parameter passed in is changed in-place | |
2492 * when being adapted. An adaptation to <code>[-1, -1]</code> indicates | |
2493 * that the selection range could not be validated. | |
2494 * Subclasses may reimplement this method. | |
2495 * | |
2496 * @param selectionRange selectionRange[0] is the offset, selectionRange[1] | |
2497 * the length of the selection to validate. | |
2498 * @since 2.0 | |
2499 */ | |
2500 protected void validateSelectionRange(int[] selectionRange) { | |
2501 | |
2502 IDocument document= getVisibleDocument(); | |
2503 if (document is null) { | |
2504 selectionRange[0]= -1; | |
2505 selectionRange[1]= -1; | |
2506 return; | |
2507 } | |
2508 | |
2509 int documentLength= document.getLength(); | |
2510 int offset= selectionRange[0]; | |
2511 int length= selectionRange[1]; | |
2512 | |
2513 if (length < 0) { | |
2514 length= - length; | |
2515 offset -= length; | |
2516 } | |
2517 | |
2518 if (offset <0) | |
2519 offset= 0; | |
2520 | |
2521 if (offset > documentLength) | |
2522 offset= documentLength; | |
2523 | |
2524 int delta= (offset + length) - documentLength; | |
2525 if (delta > 0) | |
2526 length -= delta; | |
2527 | |
2528 try { | |
2529 | |
2530 int lineNumber= document.getLineOfOffset(offset); | |
2531 IRegion lineInformation= document.getLineInformation(lineNumber); | |
2532 | |
2533 int lineEnd= lineInformation.getOffset() + lineInformation.getLength(); | |
2534 delta= offset - lineEnd; | |
2535 if (delta > 0) { | |
2536 // in the middle of a multi-character line delimiter | |
2537 offset= lineEnd; | |
2538 String delimiter= document.getLineDelimiter(lineNumber); | |
2539 if (delimiter !is null) | |
162 | 2540 offset += delimiter.length; |
129 | 2541 } |
2542 | |
2543 int end= offset + length; | |
2544 lineInformation= document.getLineInformationOfOffset(end); | |
2545 lineEnd= lineInformation.getOffset() + lineInformation.getLength(); | |
2546 delta= end - lineEnd; | |
2547 if (delta > 0) { | |
2548 // in the middle of a multi-character line delimiter | |
2549 length -= delta; | |
2550 } | |
2551 | |
2552 } catch (BadLocationException x) { | |
2553 selectionRange[0]= -1; | |
2554 selectionRange[1]= -1; | |
2555 return; | |
2556 } | |
2557 | |
2558 if (selectionRange[1] < 0) { | |
2559 selectionRange[0]= offset + length; | |
2560 selectionRange[1]= -length; | |
2561 } else { | |
2562 selectionRange[0]= offset; | |
2563 selectionRange[1]= length; | |
2564 } | |
2565 } | |
2566 | |
2567 /* | |
2568 * @see Viewer#setSelection(ISelection) | |
2569 */ | |
2570 public void setSelection(ISelection selection, bool reveal) { | |
138 | 2571 if ( cast(ITextSelection)selection ) { |
134 | 2572 ITextSelection s= cast(ITextSelection) selection; |
129 | 2573 setSelectedRange(s.getOffset(), s.getLength()); |
2574 if (reveal) | |
2575 revealRange(s.getOffset(), s.getLength()); | |
2576 } | |
2577 } | |
2578 | |
2579 /* | |
2580 * @see Viewer#getSelection() | |
2581 */ | |
2582 public ISelection getSelection() { | |
2583 Point p= getSelectedRange(); | |
2584 if (p.x is -1 || p.y is -1) | |
2585 return TextSelection.emptySelection(); | |
2586 | |
2587 return new TextSelection(getDocument(), p.x, p.y); | |
2588 } | |
2589 | |
2590 /* | |
2591 * @see ITextViewer#getSelectionProvider() | |
2592 */ | |
2593 public ISelectionProvider getSelectionProvider() { | |
2594 return this; | |
2595 } | |
2596 | |
2597 /* | |
2598 * @see dwtx.jface.text.IPostSelectionProvider#addPostSelectionChangedListener(dwtx.jface.viewers.ISelectionChangedListener) | |
2599 * @since 3.0 | |
2600 */ | |
2601 public void addPostSelectionChangedListener(ISelectionChangedListener listener) { | |
2602 | |
162 | 2603 Assert.isNotNull(cast(Object)listener); |
129 | 2604 |
2605 if (fPostSelectionChangedListeners is null) | |
2606 fPostSelectionChangedListeners= new ArrayList(); | |
2607 | |
162 | 2608 if (!fPostSelectionChangedListeners.contains(cast(Object)listener)) |
2609 fPostSelectionChangedListeners.add(cast(Object)listener); | |
129 | 2610 } |
2611 | |
2612 /* | |
2613 * @see dwtx.jface.text.IPostSelectionProvider#removePostSelectionChangedListener(dwtx.jface.viewers.ISelectionChangedListener) | |
2614 * @since 3.0 | |
2615 */ | |
2616 public void removePostSelectionChangedListener(ISelectionChangedListener listener) { | |
2617 | |
162 | 2618 Assert.isNotNull(cast(Object)listener); |
129 | 2619 |
2620 if (fPostSelectionChangedListeners !is null) { | |
162 | 2621 fPostSelectionChangedListeners.remove(cast(Object)listener); |
129 | 2622 if (fPostSelectionChangedListeners.size() is 0) |
2623 fPostSelectionChangedListeners= null; | |
2624 } | |
2625 } | |
2626 | |
2627 /** | |
2628 * Get the text widget's display. | |
2629 * | |
2630 * @return the display or <code>null</code> if the display cannot be retrieved or if the display is disposed | |
2631 * @since 3.0 | |
2632 */ | |
2633 private Display getDisplay() { | |
2634 if (fTextWidget is null || fTextWidget.isDisposed()) | |
2635 return null; | |
2636 | |
2637 Display display= fTextWidget.getDisplay(); | |
2638 if (display !is null && display.isDisposed()) | |
2639 return null; | |
2640 | |
2641 return display; | |
2642 } | |
2643 | |
2644 /** | |
2645 * Starts a timer to send out a post selection changed event. | |
2646 * | |
2647 * @param fireEqualSelection <code>true</code> iff the event must be fired if the selection does not change | |
2648 * @since 3.0 | |
2649 */ | |
156 | 2650 private void queuePostSelectionChanged(bool fireEqualSelection) { |
129 | 2651 Display display= getDisplay(); |
2652 if (display is null) | |
2653 return; | |
2654 | |
2655 fNumberOfPostSelectionChangedEvents[0]++; | |
135 | 2656 display.timerExec(getEmptySelectionChangedEventDelay(), new class() Runnable { |
162 | 2657 const int id; |
2658 this(){ | |
2659 id = fNumberOfPostSelectionChangedEvents[0]; | |
2660 } | |
129 | 2661 public void run() { |
2662 if (id is fNumberOfPostSelectionChangedEvents[0]) { | |
2663 // Check again because this is executed after the delay | |
2664 if (getDisplay() !is null) { | |
2665 Point selection= fTextWidget.getSelectionRange(); | |
2666 if (selection !is null) { | |
2667 IRegion r= widgetRange2ModelRange(new Region(selection.x, selection.y)); | |
162 | 2668 if (fireEqualSelection || (r !is null && !(cast(Object)r).opEquals(cast(Object)fLastSentPostSelectionChange)) || r is null) { |
129 | 2669 fLastSentPostSelectionChange= r; |
2670 firePostSelectionChanged(selection.x, selection.y); | |
2671 } | |
2672 } | |
2673 } | |
2674 } | |
2675 } | |
2676 }); | |
2677 } | |
2678 | |
2679 /** | |
2680 * Sends out a text selection changed event to all registered post selection changed listeners. | |
2681 * | |
2682 * @param offset the offset of the newly selected range in the visible document | |
2683 * @param length the length of the newly selected range in the visible document | |
2684 * @since 3.0 | |
2685 */ | |
2686 protected void firePostSelectionChanged(int offset, int length) { | |
2687 if (redraws()) { | |
2688 IRegion r= widgetRange2ModelRange(new Region(offset, length)); | |
2689 ISelection selection= r !is null ? new TextSelection(getDocument(), r.getOffset(), r.getLength()) : TextSelection.emptySelection(); | |
2690 SelectionChangedEvent event= new SelectionChangedEvent(this, selection); | |
2691 firePostSelectionChanged(event); | |
2692 } | |
2693 } | |
2694 | |
2695 /** | |
2696 * Sends out a text selection changed event to all registered listeners and | |
2697 * registers the selection changed event to be sent out to all post selection | |
2698 * listeners. | |
2699 * | |
2700 * @param offset the offset of the newly selected range in the visible document | |
2701 * @param length the length of the newly selected range in the visible document | |
2702 */ | |
2703 protected void selectionChanged(int offset, int length) { | |
2704 queuePostSelectionChanged(true); | |
2705 fireSelectionChanged(offset, length); | |
2706 } | |
2707 | |
2708 /** | |
2709 * Sends out a text selection changed event to all registered listeners. | |
2710 * | |
2711 * @param offset the offset of the newly selected range in the visible document | |
2712 * @param length the length of the newly selected range in the visible document | |
2713 * @since 3.0 | |
2714 */ | |
2715 protected void fireSelectionChanged(int offset, int length) { | |
2716 if (redraws()) { | |
2717 IRegion r= widgetRange2ModelRange(new Region(offset, length)); | |
162 | 2718 if ((r !is null && !(cast(Object)r).opEquals(cast(Object)fLastSentSelectionChange)) || r is null) { |
129 | 2719 fLastSentSelectionChange= r; |
2720 ISelection selection= r !is null ? new TextSelection(getDocument(), r.getOffset(), r.getLength()) : TextSelection.emptySelection(); | |
2721 SelectionChangedEvent event= new SelectionChangedEvent(this, selection); | |
2722 fireSelectionChanged(event); | |
2723 } | |
2724 } | |
2725 } | |
2726 | |
2727 /** | |
2728 * Sends the given event to all registered post selection changed listeners. | |
2729 * | |
2730 * @param event the selection event | |
2731 * @since 3.0 | |
2732 */ | |
2733 private void firePostSelectionChanged(SelectionChangedEvent event) { | |
2734 List listeners= fPostSelectionChangedListeners; | |
2735 if (listeners !is null) { | |
2736 listeners= new ArrayList(listeners); | |
2737 for (int i= 0; i < listeners.size(); i++) { | |
134 | 2738 ISelectionChangedListener l= cast(ISelectionChangedListener) listeners.get(i); |
129 | 2739 l.selectionChanged(event); |
2740 } | |
2741 } | |
2742 } | |
2743 | |
2744 /** | |
2745 * Sends out a mark selection changed event to all registered listeners. | |
2746 * | |
2747 * @param offset the offset of the mark selection in the visible document, the offset is <code>-1</code> if the mark was cleared | |
2748 * @param length the length of the mark selection, may be negative if the caret is before the mark. | |
2749 * @since 2.0 | |
2750 */ | |
2751 protected void markChanged(int offset, int length) { | |
2752 if (redraws()) { | |
2753 | |
2754 if (offset !is -1) { | |
2755 IRegion r= widgetRange2ModelRange(new Region(offset, length)); | |
2756 offset= r.getOffset(); | |
2757 length= r.getLength(); | |
2758 } | |
2759 | |
2760 ISelection selection= new MarkSelection(getDocument(), offset, length); | |
2761 SelectionChangedEvent event= new SelectionChangedEvent(this, selection); | |
2762 fireSelectionChanged(event); | |
2763 } | |
2764 } | |
2765 | |
2766 | |
2767 //---- Text listeners | |
2768 | |
2769 /* | |
2770 * @see ITextViewer#addTextListener(ITextListener) | |
2771 */ | |
2772 public void addTextListener(ITextListener listener) { | |
2773 | |
162 | 2774 Assert.isNotNull(cast(Object)listener); |
129 | 2775 |
2776 if (fTextListeners is null) | |
2777 fTextListeners= new ArrayList(); | |
2778 | |
162 | 2779 if (!fTextListeners.contains(cast(Object)listener)) |
2780 fTextListeners.add(cast(Object)listener); | |
129 | 2781 } |
2782 | |
2783 /* | |
2784 * @see ITextViewer#removeTextListener(ITextListener) | |
2785 */ | |
2786 public void removeTextListener(ITextListener listener) { | |
2787 | |
162 | 2788 Assert.isNotNull(cast(Object)listener); |
129 | 2789 |
2790 if (fTextListeners !is null) { | |
162 | 2791 fTextListeners.remove(cast(Object)listener); |
129 | 2792 if (fTextListeners.size() is 0) |
2793 fTextListeners= null; | |
2794 } | |
2795 } | |
2796 | |
2797 /** | |
2798 * Informs all registered text listeners about the change specified by the | |
2799 * widget command. This method does not use a robust iterator. | |
2800 * | |
2801 * @param cmd the widget command translated into a text event sent to all text listeners | |
2802 */ | |
2803 protected void updateTextListeners(WidgetCommand cmd) { | |
2804 List textListeners= fTextListeners; | |
2805 if (textListeners !is null) { | |
2806 textListeners= new ArrayList(textListeners); | |
2807 DocumentEvent event= cmd.event; | |
138 | 2808 if ( cast(SlaveDocumentEvent)event ) |
134 | 2809 event= (cast(SlaveDocumentEvent) event).getMasterEvent(); |
129 | 2810 |
2811 TextEvent e= new TextEvent(cmd.start, cmd.length, cmd.text, cmd.preservedText, event, redraws()); | |
2812 for (int i= 0; i < textListeners.size(); i++) { | |
134 | 2813 ITextListener l= cast(ITextListener) textListeners.get(i); |
129 | 2814 l.textChanged(e); |
2815 } | |
2816 } | |
2817 } | |
2818 | |
2819 //---- Text input listeners | |
2820 | |
2821 /* | |
2822 * @see ITextViewer#addTextInputListener(ITextInputListener) | |
2823 */ | |
2824 public void addTextInputListener(ITextInputListener listener) { | |
2825 | |
162 | 2826 Assert.isNotNull(cast(Object)listener); |
129 | 2827 |
2828 if (fTextInputListeners is null) | |
2829 fTextInputListeners= new ArrayList(); | |
2830 | |
162 | 2831 if (!fTextInputListeners.contains(cast(Object)listener)) |
2832 fTextInputListeners.add(cast(Object)listener); | |
129 | 2833 } |
2834 | |
2835 /* | |
2836 * @see ITextViewer#removeTextInputListener(ITextInputListener) | |
2837 */ | |
2838 public void removeTextInputListener(ITextInputListener listener) { | |
2839 | |
162 | 2840 Assert.isNotNull(cast(Object)listener); |
129 | 2841 |
2842 if (fTextInputListeners !is null) { | |
162 | 2843 fTextInputListeners.remove(cast(Object)listener); |
129 | 2844 if (fTextInputListeners.size() is 0) |
2845 fTextInputListeners= null; | |
2846 } | |
2847 } | |
2848 | |
2849 /** | |
2850 * Informs all registered text input listeners about the forthcoming input change, | |
2851 * This method does not use a robust iterator. | |
2852 * | |
2853 * @param oldInput the old input document | |
2854 * @param newInput the new input document | |
2855 */ | |
2856 protected void fireInputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) { | |
2857 List listener= fTextInputListeners; | |
2858 if (listener !is null) { | |
2859 for (int i= 0; i < listener.size(); i++) { | |
134 | 2860 ITextInputListener l= cast(ITextInputListener) listener.get(i); |
129 | 2861 l.inputDocumentAboutToBeChanged(oldInput, newInput); |
2862 } | |
2863 } | |
2864 } | |
2865 | |
2866 /** | |
2867 * Informs all registered text input listeners about the successful input change, | |
2868 * This method does not use a robust iterator. | |
2869 * | |
2870 * @param oldInput the old input document | |
2871 * @param newInput the new input document | |
2872 */ | |
2873 protected void fireInputDocumentChanged(IDocument oldInput, IDocument newInput) { | |
2874 List listener= fTextInputListeners; | |
2875 if (listener !is null) { | |
2876 for (int i= 0; i < listener.size(); i++) { | |
134 | 2877 ITextInputListener l= cast(ITextInputListener) listener.get(i); |
129 | 2878 l.inputDocumentChanged(oldInput, newInput); |
2879 } | |
2880 } | |
2881 } | |
2882 | |
2883 //---- Document | |
2884 | |
2885 /* | |
2886 * @see Viewer#getInput() | |
2887 */ | |
2888 public Object getInput() { | |
162 | 2889 return cast(Object)getDocument(); |
129 | 2890 } |
2891 | |
2892 /* | |
2893 * @see ITextViewer#getDocument() | |
2894 */ | |
2895 public IDocument getDocument() { | |
2896 return fDocument; | |
2897 } | |
2898 | |
2899 /* | |
2900 * @see Viewer#setInput(Object) | |
2901 */ | |
2902 public void setInput(Object input) { | |
2903 | |
2904 IDocument document= null; | |
138 | 2905 if ( cast(IDocument)input ) |
134 | 2906 document= cast(IDocument) input; |
129 | 2907 |
2908 setDocument(document); | |
2909 } | |
2910 | |
2911 /* | |
2912 * @see ITextViewer#setDocument(IDocument) | |
2913 */ | |
2914 public void setDocument(IDocument document) { | |
2915 | |
2916 fReplaceTextPresentation= true; | |
2917 fireInputDocumentAboutToBeChanged(fDocument, document); | |
2918 | |
2919 IDocument oldDocument= fDocument; | |
2920 fDocument= document; | |
2921 | |
2922 setVisibleDocument(fDocument); | |
2923 | |
2924 resetPlugins(); | |
162 | 2925 inputChanged(cast(Object)fDocument, cast(Object)oldDocument); |
129 | 2926 |
2927 fireInputDocumentChanged(oldDocument, fDocument); | |
2928 fLastSentSelectionChange= null; | |
2929 fReplaceTextPresentation= false; | |
2930 } | |
2931 | |
2932 /* | |
2933 * @see ITextViewer#setDocument(IDocument, int, int) | |
2934 */ | |
2935 public void setDocument(IDocument document, int modelRangeOffset, int modelRangeLength) { | |
2936 | |
2937 fReplaceTextPresentation= true; | |
2938 fireInputDocumentAboutToBeChanged(fDocument, document); | |
2939 | |
2940 IDocument oldDocument= fDocument; | |
2941 fDocument= document; | |
2942 | |
2943 try { | |
2944 | |
2945 IDocument slaveDocument= createSlaveDocument(document); | |
2946 updateSlaveDocument(slaveDocument, modelRangeOffset, modelRangeLength); | |
2947 setVisibleDocument(slaveDocument); | |
2948 | |
2949 } catch (BadLocationException x) { | |
2950 throw new IllegalArgumentException(JFaceTextMessages.getString("TextViewer.error.invalid_visible_region_1")); //$NON-NLS-1$ | |
2951 } | |
2952 | |
2953 resetPlugins(); | |
162 | 2954 inputChanged(cast(Object)fDocument, cast(Object)oldDocument); |
129 | 2955 |
2956 fireInputDocumentChanged(oldDocument, fDocument); | |
2957 fLastSentSelectionChange= null; | |
2958 fReplaceTextPresentation= false; | |
2959 } | |
2960 | |
2961 /** | |
2962 * Creates a slave document for the given document if there is a slave document manager | |
2963 * associated with this viewer. | |
2964 * | |
2965 * @param document the master document | |
2966 * @return the newly created slave document | |
2967 * @since 2.1 | |
2968 */ | |
2969 protected IDocument createSlaveDocument(IDocument document) { | |
2970 ISlaveDocumentManager manager= getSlaveDocumentManager(); | |
2971 if (manager !is null) { | |
2972 if (manager.isSlaveDocument(document)) | |
2973 return document; | |
2974 return manager.createSlaveDocument(document); | |
2975 } | |
2976 return document; | |
2977 } | |
2978 | |
2979 /** | |
2980 * Sets the given slave document to the specified range of its master document. | |
2981 * | |
2982 * @param visibleDocument the slave document | |
2983 * @param visibleRegionOffset the offset of the master document range | |
2984 * @param visibleRegionLength the length of the master document range | |
2985 * @return <code>true</code> if the slave has been adapted successfully | |
2986 * @throws BadLocationException in case the specified range is not valid in the master document | |
2987 * @since 2.1 | |
2988 * @deprecated use <code>updateSlaveDocument</code> instead | |
2989 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
135
diff
changeset
|
2990 protected bool updateVisibleDocument(IDocument visibleDocument, int visibleRegionOffset, int visibleRegionLength) { |
138 | 2991 if ( cast(ChildDocument)visibleDocument ) { |
134 | 2992 ChildDocument childDocument= cast(ChildDocument) visibleDocument; |
129 | 2993 |
2994 IDocument document= childDocument.getParentDocument(); | |
2995 int line= document.getLineOfOffset(visibleRegionOffset); | |
2996 int offset= document.getLineOffset(line); | |
2997 int length= (visibleRegionOffset - offset) + visibleRegionLength; | |
2998 | |
2999 Position parentRange= childDocument.getParentDocumentRange(); | |
3000 if (offset !is parentRange.getOffset() || length !is parentRange.getLength()) { | |
3001 childDocument.setParentDocumentRange(offset, length); | |
3002 return true; | |
3003 } | |
3004 } | |
3005 return false; | |
3006 } | |
3007 | |
3008 /** | |
3009 * Updates the given slave document to show the specified range of its master document. | |
3010 * | |
3011 * @param slaveDocument the slave document | |
3012 * @param modelRangeOffset the offset of the master document range | |
3013 * @param modelRangeLength the length of the master document range | |
3014 * @return <code>true</code> if the slave has been adapted successfully | |
3015 * @throws BadLocationException in case the specified range is not valid in the master document | |
3016 * @since 3.0 | |
3017 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
135
diff
changeset
|
3018 protected bool updateSlaveDocument(IDocument slaveDocument, int modelRangeOffset, int modelRangeLength) { |
129 | 3019 return updateVisibleDocument(slaveDocument, modelRangeOffset, modelRangeLength); |
3020 } | |
3021 | |
3022 | |
3023 | |
3024 //---- View ports | |
3025 | |
3026 /** | |
3027 * Initializes all listeners and structures required to set up view port listeners. | |
3028 */ | |
3029 private void initializeViewportUpdate() { | |
3030 | |
3031 if (fViewportGuard !is null) | |
3032 return; | |
3033 | |
3034 if (fTextWidget !is null) { | |
3035 | |
3036 fViewportGuard= new ViewportGuard(); | |
3037 fLastTopPixel= -1; | |
3038 | |
3039 fTextWidget.addKeyListener(fViewportGuard); | |
3040 fTextWidget.addMouseListener(fViewportGuard); | |
3041 | |
3042 fScroller= fTextWidget.getVerticalBar(); | |
3043 if (fScroller !is null) | |
3044 fScroller.addSelectionListener(fViewportGuard); | |
3045 } | |
3046 } | |
3047 | |
3048 /** | |
3049 * Removes all listeners and structures required to set up view port listeners. | |
3050 */ | |
3051 private void removeViewPortUpdate() { | |
3052 | |
3053 if (fTextWidget !is null) { | |
3054 | |
3055 fTextWidget.removeKeyListener(fViewportGuard); | |
3056 fTextWidget.removeMouseListener(fViewportGuard); | |
3057 | |
3058 if (fScroller !is null && !fScroller.isDisposed()) { | |
3059 fScroller.removeSelectionListener(fViewportGuard); | |
3060 fScroller= null; | |
3061 } | |
3062 | |
3063 fViewportGuard= null; | |
3064 } | |
3065 } | |
3066 | |
3067 /* | |
3068 * @see ITextViewer#addViewportListener(IViewportListener) | |
3069 */ | |
3070 public void addViewportListener(IViewportListener listener) { | |
3071 | |
3072 if (fViewportListeners is null) { | |
3073 fViewportListeners= new ArrayList(); | |
3074 initializeViewportUpdate(); | |
3075 } | |
3076 | |
162 | 3077 if (!fViewportListeners.contains(cast(Object)listener)) |
3078 fViewportListeners.add(cast(Object)listener); | |
129 | 3079 } |
3080 | |
3081 /* | |
3082 * @see ITextViewer#removeViewportListener(IVewportListener) | |
3083 */ | |
3084 public void removeViewportListener(IViewportListener listener) { | |
3085 if (fViewportListeners !is null) | |
162 | 3086 fViewportListeners.remove(cast(Object)listener); |
129 | 3087 } |
3088 | |
3089 /** | |
3090 * Checks whether the view port changed and if so informs all registered | |
3091 * listeners about the change. | |
3092 * | |
3093 * @param origin describes under which circumstances this method has been called. | |
3094 * | |
3095 * @see IViewportListener | |
3096 */ | |
3097 protected void updateViewportListeners(int origin) { | |
3098 | |
3099 if (redraws()) { | |
3100 int topPixel= fTextWidget.getTopPixel(); | |
3101 if (topPixel >= 0 && topPixel !is fLastTopPixel) { | |
3102 if (fViewportListeners !is null) { | |
3103 for (int i= 0; i < fViewportListeners.size(); i++) { | |
134 | 3104 IViewportListener l= cast(IViewportListener) fViewportListeners.get(i); |
129 | 3105 l.viewportChanged(topPixel); |
3106 } | |
3107 } | |
3108 fLastTopPixel= topPixel; | |
3109 } | |
3110 } | |
3111 } | |
3112 | |
3113 //---- scrolling and revealing | |
3114 | |
3115 /* | |
3116 * @see ITextViewer#getTopIndex() | |
3117 */ | |
3118 public int getTopIndex() { | |
3119 | |
3120 if (fTextWidget !is null) { | |
3121 int top= fTextWidget.getTopIndex(); | |
3122 return widgetLine2ModelLine(top); | |
3123 } | |
3124 | |
3125 return -1; | |
3126 } | |
3127 | |
3128 /* | |
3129 * @see ITextViewer#setTopIndex(int) | |
3130 */ | |
3131 public void setTopIndex(int index) { | |
3132 | |
3133 if (fTextWidget !is null) { | |
3134 | |
3135 int widgetLine= modelLine2WidgetLine(index); | |
3136 if (widgetLine is -1) | |
3137 widgetLine= getClosestWidgetLineForModelLine(index); | |
3138 | |
3139 if (widgetLine > -1) { | |
3140 fTextWidget.setTopIndex(widgetLine); | |
3141 updateViewportListeners(INTERNAL); | |
3142 } | |
3143 } | |
3144 } | |
3145 | |
3146 /** | |
3147 * Returns the number of lines that can fully fit into the viewport. This is computed by | |
3148 * dividing the widget's client area height by the widget's line height. The result is only | |
3149 * accurate if the widget does not use variable line heights - for that reason, clients should | |
3150 * not use this method any longer and use the client area height of the text widget to find out | |
3151 * how much content fits into it. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3152 * |
129 | 3153 * @return the view port height in lines |
3154 * @deprecated as of 3.2 | |
3155 */ | |
3156 protected int getVisibleLinesInViewport() { | |
3157 if (fTextWidget !is null) { | |
3158 Rectangle clArea= fTextWidget.getClientArea(); | |
3159 if (!clArea.isEmpty()) | |
3160 return clArea.height / fTextWidget.getLineHeight(); | |
3161 } | |
3162 return -1; | |
3163 } | |
3164 | |
3165 /* | |
3166 * @see ITextViewer#getBottomIndex() | |
3167 */ | |
3168 public int getBottomIndex() { | |
3169 | |
3170 if (fTextWidget is null) | |
3171 return -1; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3172 |
129 | 3173 int widgetBottom= JFaceTextUtil.getBottomIndex(fTextWidget); |
3174 return widgetLine2ModelLine(widgetBottom); | |
3175 } | |
3176 | |
3177 /* | |
3178 * @see ITextViewer#getTopIndexStartOffset() | |
3179 */ | |
3180 public int getTopIndexStartOffset() { | |
3181 | |
3182 if (fTextWidget !is null) { | |
3183 int top= fTextWidget.getTopIndex(); | |
3184 try { | |
3185 top= getVisibleDocument().getLineOffset(top); | |
3186 return widgetOffset2ModelOffset(top); | |
3187 } catch (BadLocationException ex) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3188 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3189 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.getTopIndexStartOffset")); //$NON-NLS-1$ |
129 | 3190 } |
3191 } | |
3192 | |
3193 return -1; | |
3194 } | |
3195 | |
3196 /* | |
3197 * @see ITextViewer#getBottomIndexEndOffset() | |
3198 */ | |
3199 public int getBottomIndexEndOffset() { | |
3200 try { | |
3201 | |
3202 IRegion line= getDocument().getLineInformation(getBottomIndex()); | |
3203 int bottomEndOffset= line.getOffset() + line.getLength() - 1; | |
3204 | |
3205 IRegion coverage= getModelCoverage(); | |
3206 if (coverage is null) | |
3207 return -1; | |
3208 | |
3209 int coverageEndOffset= coverage.getOffset() + coverage.getLength() - 1; | |
3210 return Math.min(coverageEndOffset, bottomEndOffset); | |
3211 | |
3212 } catch (BadLocationException ex) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3213 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3214 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.getBottomIndexEndOffset")); //$NON-NLS-1$ |
129 | 3215 return getDocument().getLength() - 1; |
3216 } | |
3217 } | |
3218 | |
3219 /* | |
3220 * @see ITextViewer#revealRange(int, int) | |
3221 */ | |
3222 public void revealRange(int start, int length) { | |
3223 | |
3224 if (fTextWidget is null || !redraws()) | |
3225 return; | |
3226 | |
3227 IRegion modelRange= new Region(start, length); | |
3228 IRegion widgetRange= modelRange2ClosestWidgetRange(modelRange); | |
3229 if (widgetRange !is null) { | |
3230 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3231 int[] range= [ widgetRange.getOffset(), widgetRange.getLength() ]; |
129 | 3232 validateSelectionRange(range); |
3233 if (range[0] >= 0) | |
3234 internalRevealRange(range[0], range[0] + range[1]); | |
3235 | |
3236 } else { | |
3237 | |
3238 IRegion coverage= getModelCoverage(); | |
3239 int cursor= (coverage is null || start < coverage.getOffset()) ? 0 : getVisibleDocument().getLength(); | |
3240 internalRevealRange(cursor, cursor); | |
3241 } | |
3242 } | |
3243 | |
3244 /** | |
3245 * Reveals the given range of the visible document. | |
3246 * | |
3247 * @param start the start offset of the range | |
3248 * @param end the end offset of the range | |
3249 */ | |
3250 protected void internalRevealRange(int start, int end) { | |
3251 | |
3252 try { | |
3253 | |
3254 IDocument doc= getVisibleDocument(); | |
3255 | |
3256 int startLine= doc.getLineOfOffset(start); | |
3257 int endLine= doc.getLineOfOffset(end); | |
3258 | |
3259 int top= fTextWidget.getTopIndex(); | |
3260 if (top > -1) { | |
3261 | |
3262 // scroll vertically | |
3263 int bottom= JFaceTextUtil.getBottomIndex(fTextWidget); | |
3264 int lines= bottom - top; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3265 |
129 | 3266 // if the widget is not scrollable as it is displaying the entire content |
3267 // setTopIndex won't have any effect. | |
3268 | |
3269 if (startLine >= top && startLine <= bottom && endLine >= top && endLine <= bottom ) { | |
3270 | |
3271 // do not scroll at all as it is already visible | |
3272 | |
3273 } else { | |
3274 | |
3275 int delta= Math.max(0, lines - (endLine - startLine)); | |
3276 fTextWidget.setTopIndex(startLine - delta/3); | |
3277 updateViewportListeners(INTERNAL); | |
3278 } | |
3279 | |
3280 // scroll horizontally | |
3281 | |
3282 if (endLine < startLine) { | |
3283 endLine += startLine; | |
3284 startLine= endLine - startLine; | |
3285 endLine -= startLine; | |
3286 } | |
3287 | |
3288 int startPixel= -1; | |
3289 int endPixel= -1; | |
3290 | |
3291 if (endLine > startLine) { | |
3292 // reveal the beginning of the range in the start line | |
3293 IRegion extent= getExtent(start, start); | |
3294 startPixel= extent.getOffset() + fTextWidget.getHorizontalPixel(); | |
3295 endPixel= startPixel; | |
3296 | |
3297 } else { | |
3298 IRegion extent= getExtent(start, end); | |
3299 startPixel= extent.getOffset() + fTextWidget.getHorizontalPixel(); | |
3300 endPixel= startPixel + extent.getLength(); | |
3301 } | |
3302 | |
3303 int visibleStart= fTextWidget.getHorizontalPixel(); | |
3304 int visibleEnd= visibleStart + fTextWidget.getClientArea().width; | |
3305 | |
3306 // scroll only if not yet visible | |
3307 if (startPixel < visibleStart || visibleEnd < endPixel) { | |
3308 | |
3309 // set buffer zone to 10 pixels | |
3310 int bufferZone= 10; | |
3311 | |
3312 int newOffset= visibleStart; | |
3313 | |
3314 int visibleWidth= visibleEnd - visibleStart; | |
3315 int selectionPixelWidth= endPixel - startPixel; | |
3316 | |
3317 if (startPixel < visibleStart) | |
3318 newOffset= startPixel; | |
3319 else if (selectionPixelWidth + bufferZone < visibleWidth) | |
3320 newOffset= endPixel + bufferZone - visibleWidth; | |
3321 else | |
3322 newOffset= startPixel; | |
3323 | |
134 | 3324 float index= (cast(float)newOffset) / (cast(float)getAverageCharWidth()); |
129 | 3325 |
162 | 3326 fTextWidget.setHorizontalIndex(cast(int)Math.round(index)); |
129 | 3327 } |
3328 | |
3329 } | |
3330 } catch (BadLocationException e) { | |
3331 throw new IllegalArgumentException(JFaceTextMessages.getString("TextViewer.error.invalid_range")); //$NON-NLS-1$ | |
3332 } | |
3333 } | |
3334 | |
3335 /** | |
3336 * Returns the width of the text when being drawn into this viewer's widget. | |
3337 * | |
3338 * @param text the string to measure | |
3339 * @return the width of the presentation of the given string | |
3340 * @deprecated use <code>getWidthInPixels(int, int)</code> instead | |
3341 */ | |
3342 final protected int getWidthInPixels(String text) { | |
3343 GC gc= new GC(fTextWidget); | |
3344 gc.setFont(fTextWidget.getFont()); | |
3345 Point extent= gc.textExtent(text); | |
3346 gc.dispose(); | |
3347 return extent.x; | |
3348 } | |
3349 | |
3350 /** | |
3351 * Returns the region covered by the given start and end offset. | |
3352 * The result is relative to the upper left corner of the widget | |
3353 * client area. | |
3354 * | |
3355 * @param start offset relative to the start of this viewer's view port | |
3356 * 0 <= offset <= getCharCount() | |
3357 * @param end offset relative to the start of this viewer's view port | |
3358 * 0 <= offset <= getCharCount() | |
3359 * @return the region covered by start and end offset | |
3360 */ | |
3361 final protected IRegion getExtent(int start, int end) { | |
3362 if (end > 0 && start < end) { | |
3363 Rectangle bounds= fTextWidget.getTextBounds(start, end - 1); | |
3364 return new Region(bounds.x, bounds.width); | |
3365 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3366 |
129 | 3367 return new Region(fTextWidget.getLocationAtOffset(start).x, 0); |
3368 } | |
3369 | |
3370 /** | |
3371 * Returns the width of the representation of a text range in the | |
3372 * visible region of the viewer's document as drawn in this viewer's | |
3373 * widget. | |
3374 * | |
3375 * @param offset the offset of the text range in the visible region | |
3376 * @param length the length of the text range in the visible region | |
3377 * @return the width of the presentation of the specified text range | |
3378 * @since 2.0 | |
3379 */ | |
3380 final protected int getWidthInPixels(int offset, int length) { | |
3381 return getExtent(offset, offset + length).getLength(); | |
3382 } | |
3383 | |
3384 /** | |
3385 * Returns the average character width of this viewer's widget. | |
3386 * | |
3387 * @return the average character width of this viewer's widget | |
3388 */ | |
3389 final protected int getAverageCharWidth() { | |
3390 return JFaceTextUtil.getAverageCharWidth(getTextWidget()); | |
3391 } | |
3392 | |
3393 /* | |
3394 * @see Viewer#refresh() | |
3395 */ | |
3396 public void refresh() { | |
3397 setDocument(getDocument()); | |
3398 } | |
3399 | |
3400 //---- visible range support | |
3401 | |
3402 /** | |
3403 * Returns the slave document manager | |
3404 * | |
3405 * @return the slave document manager | |
3406 * @since 2.1 | |
3407 */ | |
3408 protected ISlaveDocumentManager getSlaveDocumentManager() { | |
3409 if (fSlaveDocumentManager is null) | |
3410 fSlaveDocumentManager= createSlaveDocumentManager(); | |
3411 return fSlaveDocumentManager; | |
3412 } | |
3413 | |
3414 /** | |
3415 * Creates a new slave document manager. This implementation always | |
3416 * returns a <code>ChildDocumentManager</code>. | |
3417 * | |
3418 * @return ISlaveDocumentManager | |
3419 * @since 2.1 | |
3420 */ | |
3421 protected ISlaveDocumentManager createSlaveDocumentManager() { | |
3422 return new ChildDocumentManager(); | |
3423 } | |
3424 | |
3425 /* | |
3426 * @see dwtx.jface.text.ITextViewer#invalidateTextPresentation() | |
3427 */ | |
3428 public final void invalidateTextPresentation() { | |
3429 if (fVisibleDocument !is null) { | |
3430 fWidgetCommand.event= null; | |
3431 fWidgetCommand.start= 0; | |
3432 fWidgetCommand.length= fVisibleDocument.getLength(); | |
3433 fWidgetCommand.text= fVisibleDocument.get(); | |
3434 updateTextListeners(fWidgetCommand); | |
3435 } | |
3436 } | |
3437 | |
3438 /** | |
3439 * Invalidates the given range of the text presentation. | |
3440 * | |
3441 * @param offset the offset of the range to be invalidated | |
3442 * @param length the length of the range to be invalidated | |
3443 * @since 2.1 | |
3444 */ | |
3445 public final void invalidateTextPresentation(int offset, int length) { | |
3446 if (fVisibleDocument !is null) { | |
3447 | |
3448 IRegion widgetRange= modelRange2WidgetRange(new Region(offset, length)); | |
3449 if (widgetRange !is null) { | |
3450 | |
3451 fWidgetCommand.event= null; | |
3452 fWidgetCommand.start= widgetRange.getOffset(); | |
3453 fWidgetCommand.length= widgetRange.getLength(); | |
3454 | |
3455 try { | |
3456 fWidgetCommand.text= fVisibleDocument.get(widgetRange.getOffset(), widgetRange.getLength()); | |
3457 updateTextListeners(fWidgetCommand); | |
3458 } catch (BadLocationException x) { | |
3459 // can not happen because of previous checking | |
3460 } | |
3461 } | |
3462 } | |
3463 } | |
3464 | |
3465 /** | |
3466 * Initializes the text widget with the visual document and | |
3467 * invalidates the overall presentation. | |
3468 */ | |
3469 private void initializeWidgetContents() { | |
3470 | |
3471 if (fTextWidget !is null && fVisibleDocument !is null) { | |
3472 | |
3473 // set widget content | |
3474 if (fDocumentAdapter is null) | |
3475 fDocumentAdapter= createDocumentAdapter(); | |
3476 | |
3477 fDocumentAdapter.setDocument(fVisibleDocument); | |
3478 fTextWidget.setContent(fDocumentAdapter); | |
3479 | |
3480 // invalidate presentation | |
3481 invalidateTextPresentation(); | |
3482 } | |
3483 } | |
3484 | |
3485 /** | |
3486 * Frees the given document if it is a slave document. | |
3487 * | |
3488 * @param slave the potential slave document | |
3489 * @since 3.0 | |
3490 */ | |
3491 protected void freeSlaveDocument(IDocument slave) { | |
3492 ISlaveDocumentManager manager= getSlaveDocumentManager(); | |
3493 if (manager !is null && manager.isSlaveDocument(slave)) | |
3494 manager.freeSlaveDocument(slave); | |
3495 } | |
3496 | |
3497 /** | |
3498 * Sets this viewer's visible document. The visible document represents the | |
3499 * visible region of the viewer's input document. | |
3500 * | |
3501 * @param document the visible document | |
3502 */ | |
3503 protected void setVisibleDocument(IDocument document) { | |
3504 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3505 if (fVisibleDocument is document && cast(ChildDocument)fVisibleDocument ) { |
129 | 3506 // optimization for new child documents |
3507 return; | |
3508 } | |
3509 | |
3510 if (fVisibleDocument !is null) { | |
3511 if (fVisibleDocumentListener !is null) | |
3512 fVisibleDocument.removeDocumentListener(fVisibleDocumentListener); | |
3513 if (fVisibleDocument !is document) | |
3514 freeSlaveDocument(fVisibleDocument); | |
3515 } | |
3516 | |
3517 fVisibleDocument= document; | |
3518 initializeDocumentInformationMapping(fVisibleDocument); | |
3519 | |
3520 initializeWidgetContents(); | |
3521 | |
3522 fFindReplaceDocumentAdapter= null; | |
3523 if (fVisibleDocument !is null && fVisibleDocumentListener !is null) | |
3524 fVisibleDocument.addDocumentListener(fVisibleDocumentListener); | |
3525 } | |
3526 | |
3527 /** | |
3528 * Hook method called when the visible document is about to be changed. | |
3529 * <p> | |
3530 * Subclasses may override. | |
3531 * | |
3532 * @param event the document event | |
3533 * @since 3.0 | |
3534 */ | |
3535 protected void handleVisibleDocumentAboutToBeChanged(DocumentEvent event) { | |
3536 } | |
3537 | |
3538 /** | |
3539 * Hook method called when the visible document has been changed. | |
3540 * <p> | |
3541 * Subclasses may override. | |
3542 * | |
3543 * @param event the document event | |
3544 * @since 3.0 | |
3545 */ | |
3546 protected void handleVisibleDocumentChanged(DocumentEvent event) { | |
3547 } | |
3548 | |
3549 /** | |
3550 * Initializes the document information mapping between the given slave document and | |
3551 * its master document. | |
3552 * | |
3553 * @param visibleDocument the slave document | |
3554 * @since 2.1 | |
3555 */ | |
3556 protected void initializeDocumentInformationMapping(IDocument visibleDocument) { | |
3557 ISlaveDocumentManager manager= getSlaveDocumentManager(); | |
3558 fInformationMapping= manager is null ? null : manager.createMasterSlaveMapping(visibleDocument); | |
3559 } | |
3560 | |
3561 /** | |
3562 * Returns the viewer's visible document. | |
3563 * | |
3564 * @return the viewer's visible document | |
3565 */ | |
3566 protected IDocument getVisibleDocument() { | |
3567 return fVisibleDocument; | |
3568 } | |
3569 | |
3570 /** | |
3571 * Returns the offset of the visible region. | |
3572 * | |
3573 * @return the offset of the visible region | |
3574 */ | |
3575 protected int _getVisibleRegionOffset() { | |
3576 | |
3577 IDocument document= getVisibleDocument(); | |
138 | 3578 if ( cast(ChildDocument)document ) { |
134 | 3579 ChildDocument cdoc= cast(ChildDocument) document; |
129 | 3580 return cdoc.getParentDocumentRange().getOffset(); |
3581 } | |
3582 | |
3583 return 0; | |
3584 } | |
162 | 3585 package int _getVisibleRegionOffset_package() { |
3586 return _getVisibleRegionOffset(); | |
3587 } | |
129 | 3588 |
3589 /* | |
3590 * @see ITextViewer#getVisibleRegion() | |
3591 */ | |
3592 public IRegion getVisibleRegion() { | |
3593 | |
3594 IDocument document= getVisibleDocument(); | |
138 | 3595 if ( cast(ChildDocument)document ) { |
134 | 3596 Position p= (cast(ChildDocument) document).getParentDocumentRange(); |
129 | 3597 return new Region(p.getOffset(), p.getLength()); |
3598 } | |
3599 | |
3600 return new Region(0, document is null ? 0 : document.getLength()); | |
3601 } | |
3602 | |
3603 /* | |
3604 * @see ITextViewer#overlapsWithVisibleRegion(int, int) | |
3605 */ | |
3606 public bool overlapsWithVisibleRegion(int start, int length) { | |
3607 IDocument document= getVisibleDocument(); | |
138 | 3608 if ( cast(ChildDocument)document ) { |
134 | 3609 ChildDocument cdoc= cast(ChildDocument) document; |
129 | 3610 return cdoc.getParentDocumentRange().overlapsWith(start, length); |
3611 } else if (document !is null) { | |
3612 int size= document.getLength(); | |
3613 return (start >= 0 && length >= 0 && start + length <= size); | |
3614 } | |
3615 return false; | |
3616 } | |
3617 | |
3618 /* | |
3619 * @see ITextViewer#setVisibleRegion(int, int) | |
3620 */ | |
3621 public void setVisibleRegion(int start, int length) { | |
3622 | |
3623 IRegion region= getVisibleRegion(); | |
3624 if (start is region.getOffset() && length is region.getLength()) { | |
3625 // nothing to change | |
3626 return; | |
3627 } | |
3628 | |
3629 setRedraw(false); | |
3630 try { | |
3631 | |
3632 IDocument slaveDocument= createSlaveDocument(getVisibleDocument()); | |
3633 if (updateSlaveDocument(slaveDocument, start, length)) | |
3634 setVisibleDocument(slaveDocument); | |
3635 | |
3636 } catch (BadLocationException x) { | |
3637 throw new IllegalArgumentException(JFaceTextMessages.getString("TextViewer.error.invalid_visible_region_2")); //$NON-NLS-1$ | |
3638 } finally { | |
3639 setRedraw(true); | |
3640 } | |
3641 } | |
3642 | |
3643 /* | |
3644 * @see ITextViewer#resetVisibleRegion() | |
3645 */ | |
3646 public void resetVisibleRegion() { | |
3647 ISlaveDocumentManager manager= getSlaveDocumentManager(); | |
3648 if (manager !is null) { | |
3649 IDocument slave= getVisibleDocument(); | |
3650 IDocument master= manager.getMasterDocument(slave); | |
3651 if (master !is null) { | |
3652 setVisibleDocument(master); | |
3653 manager.freeSlaveDocument(slave); | |
3654 } | |
3655 } | |
3656 } | |
3657 | |
3658 | |
3659 //-------------------------------------- | |
3660 | |
3661 /* | |
3662 * @see ITextViewer#setTextDoubleClickStrategy(ITextDoubleClickStrategy, String) | |
3663 */ | |
3664 public void setTextDoubleClickStrategy(ITextDoubleClickStrategy strategy, String contentType) { | |
3665 | |
3666 if (strategy !is null) { | |
3667 if (fDoubleClickStrategies is null) | |
3668 fDoubleClickStrategies= new HashMap(); | |
162 | 3669 fDoubleClickStrategies.put(stringcast(contentType), cast(Object)strategy); |
129 | 3670 } else if (fDoubleClickStrategies !is null) |
3671 fDoubleClickStrategies.remove(contentType); | |
3672 } | |
3673 | |
3674 /** | |
3675 * Selects from the given map the one which is registered under | |
3676 * the content type of the partition in which the given offset is located. | |
3677 * | |
3678 * @param plugins the map from which to choose | |
3679 * @param offset the offset for which to find the plug-in | |
3680 * @return the plug-in registered under the offset's content type | |
3681 */ | |
3682 protected Object selectContentTypePlugin(int offset, Map plugins) { | |
3683 try { | |
3684 return selectContentTypePlugin(TextUtilities.getContentType(getDocument(), getDocumentPartitioning(), offset, true), plugins); | |
3685 } catch (BadLocationException x) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3686 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3687 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.selectContentTypePlugin")); //$NON-NLS-1$ |
129 | 3688 } |
3689 return null; | |
3690 } | |
3691 | |
3692 /** | |
3693 * Selects from the given <code>plug-ins</code> this one which is | |
3694 * registered for the given content <code>type</code>. | |
3695 * | |
3696 * @param type the type to be used as lookup key | |
3697 * @param plugins the table to be searched | |
3698 * @return the plug-in in the map for the given content type | |
3699 */ | |
3700 private Object selectContentTypePlugin(String type, Map plugins) { | |
3701 | |
3702 if (plugins is null) | |
3703 return null; | |
3704 | |
3705 return plugins.get(type); | |
3706 } | |
3707 | |
3708 /** | |
3709 * Hook called on receipt of a <code>VerifyEvent</code>. The event has | |
3710 * been translated into a <code>DocumentCommand</code> which can now be | |
3711 * manipulated by interested parties. By default, the hook forwards the command | |
3712 * to the installed instances of <code>IAutoEditStrategy</code>. | |
3713 * | |
3714 * @param command the document command representing the verify event | |
3715 */ | |
3716 protected void customizeDocumentCommand(DocumentCommand command) { | |
3717 if (isIgnoringAutoEditStrategies()) | |
3718 return; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3719 |
129 | 3720 IDocument document= getDocument(); |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3721 |
129 | 3722 if (fTabsToSpacesConverter !is null) |
3723 fTabsToSpacesConverter.customizeDocumentCommand(document, command); | |
3724 | |
134 | 3725 List strategies= cast(List) selectContentTypePlugin(command.offset, fAutoIndentStrategies); |
129 | 3726 if (strategies is null) |
3727 return; | |
3728 | |
3729 switch (strategies.size()) { | |
3730 // optimization | |
3731 case 0: | |
3732 break; | |
3733 | |
3734 case 1: | |
134 | 3735 (cast(IAutoEditStrategy) strategies.iterator().next()).customizeDocumentCommand(document, command); |
129 | 3736 break; |
3737 | |
3738 // make iterator robust against adding/removing strategies from within strategies | |
3739 default: | |
3740 strategies= new ArrayList(strategies); | |
3741 for (final Iterator iterator= strategies.iterator(); iterator.hasNext(); ) | |
134 | 3742 (cast(IAutoEditStrategy) iterator.next()).customizeDocumentCommand(document, command); |
129 | 3743 |
3744 break; | |
3745 } | |
3746 } | |
3747 | |
3748 /** | |
3749 * Handles the verify event issued by the viewer's text widget. | |
3750 * | |
3751 * @see VerifyListener#verifyText(VerifyEvent) | |
3752 * @param e the verify event | |
3753 */ | |
3754 protected void handleVerifyEvent(VerifyEvent e) { | |
3755 | |
3756 if (fEventConsumer !is null) { | |
3757 fEventConsumer.processEvent(e); | |
3758 if (!e.doit) | |
3759 return; | |
3760 } | |
3761 | |
3762 IRegion modelRange= event2ModelRange(e); | |
3763 fDocumentCommand.setEvent(e, modelRange); | |
3764 customizeDocumentCommand(fDocumentCommand); | |
3765 if (!fDocumentCommand.fillEvent(e, modelRange)) { | |
3766 | |
3767 bool compoundChange= fDocumentCommand.getCommandCount() > 1; | |
3768 try { | |
3769 | |
3770 fVerifyListener.forward(false); | |
3771 | |
3772 if (compoundChange && fUndoManager !is null) | |
3773 fUndoManager.beginCompoundChange(); | |
3774 | |
3775 fDocumentCommand.execute(getDocument()); | |
3776 | |
3777 if (fTextWidget !is null) { | |
3778 int documentCaret= fDocumentCommand.caretOffset; | |
3779 if (documentCaret is -1) { | |
3780 // old behavior of document command | |
3781 documentCaret= fDocumentCommand.offset + (fDocumentCommand.text is null ? 0 : fDocumentCommand.text.length()); | |
3782 } | |
3783 | |
3784 int widgetCaret= modelOffset2WidgetOffset(documentCaret); | |
3785 if (widgetCaret is -1) { | |
3786 // try to move it to the closest spot | |
3787 IRegion region= getModelCoverage(); | |
3788 if (region !is null) { | |
3789 if (documentCaret <= region.getOffset()) | |
3790 widgetCaret= 0; | |
3791 else if (documentCaret >= region.getOffset() + region.getLength()) | |
3792 widgetCaret= getVisibleRegion().getLength(); | |
3793 } | |
3794 } | |
3795 | |
3796 if (widgetCaret !is -1) { | |
3797 // there is a valid widget caret | |
3798 fTextWidget.setCaretOffset(widgetCaret); | |
3799 } | |
3800 | |
3801 fTextWidget.showSelection(); | |
3802 } | |
3803 } catch (BadLocationException x) { | |
3804 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3805 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3806 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.verifyText")); //$NON-NLS-1$ |
129 | 3807 |
3808 } finally { | |
3809 | |
3810 if (compoundChange && fUndoManager !is null) | |
3811 fUndoManager.endCompoundChange(); | |
3812 | |
3813 fVerifyListener.forward(true); | |
3814 | |
3815 } | |
3816 } | |
3817 } | |
3818 | |
3819 //---- text manipulation | |
3820 | |
3821 /** | |
3822 * Returns whether the marked region of this viewer is empty. | |
3823 * | |
3824 * @return <code>true</code> if the marked region of this viewer is empty, otherwise <code>false</code> | |
3825 * @since 2.0 | |
3826 */ | |
3827 private bool isMarkedRegionEmpty() { | |
3828 return | |
3829 fTextWidget is null || | |
3830 fMarkPosition is null || | |
3831 fMarkPosition.isDeleted() || | |
3832 modelRange2WidgetRange(fMarkPosition) is null; | |
3833 } | |
3834 | |
3835 /* | |
3836 * @see ITextViewer#canDoOperation(int) | |
3837 */ | |
3838 public bool canDoOperation(int operation) { | |
3839 | |
3840 if (fTextWidget is null || !redraws()) | |
3841 return false; | |
3842 | |
3843 switch (operation) { | |
3844 case CUT: | |
3845 return isEditable() &&(fTextWidget.getSelectionCount() > 0 || !isMarkedRegionEmpty()); | |
3846 case COPY: | |
3847 return fTextWidget.getSelectionCount() > 0 || !isMarkedRegionEmpty(); | |
3848 case DELETE: | |
3849 case PASTE: | |
3850 return isEditable(); | |
3851 case SELECT_ALL: | |
3852 return true; | |
3853 case SHIFT_LEFT: | |
3854 case SHIFT_RIGHT: | |
3855 return isEditable() && fIndentChars !is null && areMultipleLinesSelected(); | |
3856 case PREFIX: | |
3857 case STRIP_PREFIX: | |
3858 return isEditable() && fDefaultPrefixChars !is null; | |
3859 case UNDO: | |
3860 return fUndoManager !is null && fUndoManager.undoable(); | |
3861 case REDO: | |
3862 return fUndoManager !is null && fUndoManager.redoable(); | |
3863 case PRINT: | |
3864 return isPrintable(); | |
3865 } | |
3866 | |
3867 return false; | |
3868 } | |
3869 | |
3870 /* | |
3871 * @see ITextViewer#doOperation(int) | |
3872 */ | |
3873 public void doOperation(int operation) { | |
3874 | |
3875 if (fTextWidget is null || !redraws()) | |
3876 return; | |
3877 | |
3878 Point selection= null; | |
3879 | |
3880 switch (operation) { | |
3881 | |
3882 case UNDO: | |
3883 if (fUndoManager !is null) { | |
3884 ignoreAutoEditStrategies(true); | |
3885 fUndoManager.undo(); | |
3886 ignoreAutoEditStrategies(false); | |
3887 } | |
3888 break; | |
3889 case REDO: | |
3890 if (fUndoManager !is null) { | |
3891 ignoreAutoEditStrategies(true); | |
3892 fUndoManager.redo(); | |
3893 ignoreAutoEditStrategies(false); | |
3894 } | |
3895 break; | |
3896 case CUT: | |
3897 if (fTextWidget.getSelectionCount() is 0) | |
3898 copyMarkedRegion(true); | |
3899 else | |
3900 fTextWidget.cut(); | |
3901 | |
3902 selection= fTextWidget.getSelectionRange(); | |
3903 fireSelectionChanged(selection.x, selection.y); | |
3904 | |
3905 break; | |
3906 case COPY: | |
3907 if (fTextWidget.getSelectionCount() is 0) | |
3908 copyMarkedRegion(false); | |
3909 else | |
3910 fTextWidget.copy(); | |
3911 break; | |
3912 case PASTE: | |
3913 // ignoreAutoEditStrategies(true); | |
3914 fTextWidget.paste(); | |
3915 selection= fTextWidget.getSelectionRange(); | |
3916 fireSelectionChanged(selection.x, selection.y); | |
3917 // ignoreAutoEditStrategies(false); | |
3918 break; | |
3919 case DELETE: | |
3920 fTextWidget.invokeAction(ST.DELETE_NEXT); | |
3921 selection= fTextWidget.getSelectionRange(); | |
3922 fireSelectionChanged(selection.x, selection.y); | |
3923 break; | |
3924 case SELECT_ALL: { | |
3925 if (getDocument() !is null) | |
3926 setSelectedRange(0, getDocument().getLength()); | |
3927 break; | |
3928 } | |
3929 case SHIFT_RIGHT: | |
3930 shift(false, true, false); | |
3931 break; | |
3932 case SHIFT_LEFT: | |
3933 shift(false, false, false); | |
3934 break; | |
3935 case PREFIX: | |
3936 shift(true, true, true); | |
3937 break; | |
3938 case STRIP_PREFIX: | |
3939 shift(true, false, true); | |
3940 break; | |
3941 case PRINT: | |
3942 print(); | |
3943 break; | |
3944 } | |
3945 } | |
3946 | |
3947 /** | |
3948 * Tells this viewer whether the registered auto edit strategies should be ignored. | |
3949 * | |
3950 * @param ignore <code>true</code> if the strategies should be ignored. | |
3951 * @since 2.1 | |
3952 */ | |
3953 protected void ignoreAutoEditStrategies(bool ignore) { | |
3954 if (fIgnoreAutoIndent is ignore) | |
3955 return; | |
3956 | |
3957 fIgnoreAutoIndent= ignore; | |
3958 | |
3959 IDocument document= getDocument(); | |
138 | 3960 if ( cast(IDocumentExtension2)document ) { |
134 | 3961 IDocumentExtension2 extension= cast(IDocumentExtension2) document; |
129 | 3962 if (ignore) |
3963 extension.ignorePostNotificationReplaces(); | |
3964 else | |
3965 extension.acceptPostNotificationReplaces(); | |
3966 } | |
3967 } | |
162 | 3968 package void ignoreAutoEditStrategies_package(bool ignore) { |
3969 return ignoreAutoEditStrategies(ignore); | |
3970 } | |
129 | 3971 |
3972 /** | |
3973 * Returns whether this viewer ignores the registered auto edit strategies. | |
3974 * | |
3975 * @return <code>true</code> if the strategies are ignored | |
3976 * @since 2.1 | |
3977 */ | |
3978 protected bool isIgnoringAutoEditStrategies() { | |
3979 return fIgnoreAutoIndent; | |
3980 } | |
3981 | |
3982 /* | |
3983 * @see ITextOperationTargetExtension#enableOperation(int, bool) | |
3984 * @since 2.0 | |
3985 */ | |
3986 public void enableOperation(int operation, bool enable) { | |
3987 /* | |
3988 * NO-OP by default. | |
3989 * Will be changed to regularly disable the known operations. | |
3990 */ | |
3991 } | |
3992 | |
3993 /** | |
3994 * Copies/cuts the marked region. | |
3995 * | |
3996 * @param delete <code>true</code> if the region should be deleted rather than copied. | |
3997 * @since 2.0 | |
3998 */ | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
3999 protected void copyMarkedRegion(bool delete_) { |
129 | 4000 |
4001 if (fTextWidget is null) | |
4002 return; | |
4003 | |
4004 if (fMarkPosition is null || fMarkPosition.isDeleted() || modelRange2WidgetRange(fMarkPosition) is null) | |
4005 return; | |
4006 | |
4007 int widgetMarkOffset= modelOffset2WidgetOffset(fMarkPosition.offset); | |
4008 Point selection= fTextWidget.getSelection(); | |
4009 if (selection.x <= widgetMarkOffset) | |
4010 fTextWidget.setSelection(selection.x, widgetMarkOffset); | |
4011 else | |
4012 fTextWidget.setSelection(widgetMarkOffset, selection.x); | |
4013 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4014 if (delete_) { |
129 | 4015 fTextWidget.cut(); |
4016 } else { | |
4017 fTextWidget.copy(); | |
4018 fTextWidget.setSelection(selection.x); // restore old cursor position | |
4019 } | |
4020 } | |
4021 | |
4022 /** | |
4023 * Deletes the current selection. If the selection has the length 0 | |
4024 * the selection is automatically extended to the right - either by 1 | |
4025 * or by the length of line delimiter if at the end of a line. | |
4026 * | |
4027 * @deprecated use <code>StyledText.invokeAction</code> instead | |
4028 */ | |
4029 protected void deleteText() { | |
4030 fTextWidget.invokeAction(ST.DELETE_NEXT); | |
4031 } | |
4032 | |
4033 /** | |
4034 * A block is selected if the character preceding the start of the | |
4035 * selection is a new line character. | |
4036 * | |
4037 * @return <code>true</code> if a block is selected | |
4038 */ | |
4039 protected bool isBlockSelected() { | |
4040 | |
4041 Point s= getSelectedRange(); | |
4042 if (s.y is 0) | |
4043 return false; | |
4044 | |
4045 try { | |
4046 | |
4047 IDocument document= getDocument(); | |
4048 int line= document.getLineOfOffset(s.x); | |
4049 int start= document.getLineOffset(line); | |
4050 return (s.x is start); | |
4051 | |
4052 } catch (BadLocationException x) { | |
4053 } | |
4054 | |
4055 return false; | |
4056 } | |
4057 | |
4058 /** | |
4059 * Returns <code>true</code> if one line is completely selected or if multiple lines are selected. | |
4060 * Being completely selected means that all characters except the new line characters are | |
4061 * selected. | |
4062 * | |
4063 * @return <code>true</code> if one or multiple lines are selected | |
4064 * @since 2.0 | |
4065 */ | |
4066 protected bool areMultipleLinesSelected() { | |
4067 Point s= getSelectedRange(); | |
4068 if (s.y is 0) | |
4069 return false; | |
4070 | |
4071 try { | |
4072 | |
4073 IDocument document= getDocument(); | |
4074 int startLine= document.getLineOfOffset(s.x); | |
4075 int endLine= document.getLineOfOffset(s.x + s.y); | |
4076 IRegion line= document.getLineInformation(startLine); | |
4077 return startLine !is endLine || (s.x is line.getOffset() && s.y is line.getLength()); | |
4078 | |
4079 } catch (BadLocationException x) { | |
4080 } | |
4081 | |
4082 return false; | |
4083 } | |
4084 | |
4085 /** | |
4086 * Returns the index of the first line whose start offset is in the given text range. | |
4087 * | |
4088 * @param region the text range in characters where to find the line | |
4089 * @return the first line whose start index is in the given range, -1 if there is no such line | |
4090 */ | |
4091 private int getFirstCompleteLineOfRegion(IRegion region) { | |
4092 | |
4093 try { | |
4094 | |
4095 IDocument d= getDocument(); | |
4096 | |
4097 int startLine= d.getLineOfOffset(region.getOffset()); | |
4098 | |
4099 int offset= d.getLineOffset(startLine); | |
4100 if (offset >= region.getOffset()) | |
4101 return startLine; | |
4102 | |
4103 offset= d.getLineOffset(startLine + 1); | |
4104 return (offset > region.getOffset() + region.getLength() ? -1 : startLine + 1); | |
4105 | |
4106 } catch (BadLocationException x) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4107 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4108 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.getFirstCompleteLineOfRegion")); //$NON-NLS-1$ |
129 | 4109 } |
4110 | |
4111 return -1; | |
4112 } | |
4113 | |
4114 | |
4115 /** | |
4116 * Creates a region describing the text block (something that starts at | |
4117 * the beginning of a line) completely containing the current selection. | |
4118 * | |
4119 * @param selection the selection to use | |
4120 * @return the region describing the text block comprising the given selection | |
4121 * @since 2.0 | |
4122 */ | |
4123 private IRegion getTextBlockFromSelection(Point selection) { | |
4124 | |
4125 try { | |
4126 IDocument document= getDocument(); | |
4127 IRegion line= document.getLineInformationOfOffset(selection.x); | |
4128 int length= selection.y is 0 ? line.getLength() : selection.y + (selection.x - line.getOffset()); | |
4129 return new Region(line.getOffset(), length); | |
4130 | |
4131 } catch (BadLocationException x) { | |
4132 } | |
4133 | |
4134 return null; | |
4135 } | |
4136 | |
4137 /** | |
4138 * Shifts a text block to the right or left using the specified set of prefix characters. | |
4139 * The prefixes must start at the beginning of the line. | |
4140 * | |
4141 * @param useDefaultPrefixes says whether the configured default or indent prefixes should be used | |
4142 * @param right says whether to shift to the right or the left | |
4143 * | |
4144 * @deprecated use shift(bool, bool, bool) instead | |
4145 */ | |
4146 protected void shift(bool useDefaultPrefixes, bool right) { | |
4147 shift(useDefaultPrefixes, right, false); | |
4148 } | |
4149 | |
4150 /** | |
4151 * Shifts a text block to the right or left using the specified set of prefix characters. | |
4152 * If white space should be ignored the prefix characters must not be at the beginning of | |
4153 * the line when shifting to the left. There may be whitespace in front of the prefixes. | |
4154 * | |
4155 * @param useDefaultPrefixes says whether the configured default or indent prefixes should be used | |
4156 * @param right says whether to shift to the right or the left | |
4157 * @param ignoreWhitespace says whether whitespace in front of prefixes is allowed | |
4158 * @since 2.0 | |
4159 */ | |
4160 protected void shift(bool useDefaultPrefixes, bool right, bool ignoreWhitespace) { | |
4161 if (fUndoManager !is null) | |
4162 fUndoManager.beginCompoundChange(); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4163 |
129 | 4164 IDocument d= getDocument(); |
4165 Map partitioners= null; | |
4166 DocumentRewriteSession rewriteSession= null; | |
4167 try { | |
4168 Point selection= getSelectedRange(); | |
4169 IRegion block= getTextBlockFromSelection(selection); | |
4170 ITypedRegion[] regions= TextUtilities.computePartitioning(d, getDocumentPartitioning(), block.getOffset(), block.getLength(), false); | |
4171 | |
4172 int lineCount= 0; | |
4173 int[] lines= new int[regions.length * 2]; // [start line, end line, start line, end line, ...] | |
4174 for (int i= 0, j= 0; i < regions.length; i++, j+= 2) { | |
4175 // start line of region | |
4176 lines[j]= getFirstCompleteLineOfRegion(regions[i]); | |
4177 // end line of region | |
4178 int length= regions[i].getLength(); | |
4179 int offset= regions[i].getOffset() + length; | |
4180 if (length > 0) | |
4181 offset--; | |
4182 lines[j + 1]= (lines[j] is -1 ? -1 : d.getLineOfOffset(offset)); | |
4183 lineCount += lines[j + 1] - lines[j] + 1; | |
4184 } | |
4185 | |
138 | 4186 if ( cast(IDocumentExtension4)d ) { |
134 | 4187 IDocumentExtension4 extension= cast(IDocumentExtension4) d; |
129 | 4188 rewriteSession= extension.startRewriteSession(DocumentRewriteSessionType.SEQUENTIAL); |
4189 } else { | |
4190 setRedraw(false); | |
4191 startSequentialRewriteMode(true); | |
4192 } | |
4193 if (lineCount >= 20) | |
4194 partitioners= TextUtilities.removeDocumentPartitioners(d); | |
4195 | |
4196 // Perform the shift operation. | |
4197 Map map= (useDefaultPrefixes ? fDefaultPrefixChars : fIndentChars); | |
4198 for (int i= 0, j= 0; i < regions.length; i++, j += 2) { | |
156 | 4199 String[] prefixes= stringArrayFromObject(selectContentTypePlugin(regions[i].getType(), map)); |
129 | 4200 if (prefixes !is null && prefixes.length > 0 && lines[j] >= 0 && lines[j + 1] >= 0) { |
4201 if (right) | |
4202 shiftRight(lines[j], lines[j + 1], prefixes[0]); | |
4203 else | |
4204 shiftLeft(lines[j], lines[j + 1], prefixes, ignoreWhitespace); | |
4205 } | |
4206 } | |
4207 | |
4208 } catch (BadLocationException x) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4209 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4210 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.shift_1")); //$NON-NLS-1$ |
129 | 4211 |
4212 } finally { | |
4213 | |
4214 if (partitioners !is null) | |
4215 TextUtilities.addDocumentPartitioners(d, partitioners); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4216 |
138 | 4217 if ( cast(IDocumentExtension4)d ) { |
134 | 4218 IDocumentExtension4 extension= cast(IDocumentExtension4) d; |
129 | 4219 extension.stopRewriteSession(rewriteSession); |
4220 } else { | |
4221 stopSequentialRewriteMode(); | |
4222 setRedraw(true); | |
4223 } | |
4224 | |
4225 if (fUndoManager !is null) | |
4226 fUndoManager.endCompoundChange(); | |
4227 } | |
4228 } | |
4229 | |
4230 /** | |
4231 * Shifts the specified lines to the right inserting the given prefix | |
4232 * at the beginning of each line | |
4233 * | |
4234 * @param prefix the prefix to be inserted | |
4235 * @param startLine the first line to shift | |
4236 * @param endLine the last line to shift | |
4237 * @since 2.0 | |
4238 */ | |
4239 private void shiftRight(int startLine, int endLine, String prefix) { | |
4240 | |
4241 try { | |
4242 | |
4243 IDocument d= getDocument(); | |
4244 while (startLine <= endLine) { | |
4245 d.replace(d.getLineOffset(startLine++), 0, prefix); | |
4246 } | |
4247 | |
4248 } catch (BadLocationException x) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4249 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4250 System.out_.println("TextViewer.shiftRight: BadLocationException"); //$NON-NLS-1$ |
129 | 4251 } |
4252 } | |
4253 | |
4254 /** | |
4255 * Shifts the specified lines to the right or to the left. On shifting to the right | |
4256 * it insert <code>prefixes[0]</code> at the beginning of each line. On shifting to the | |
4257 * left it tests whether each of the specified lines starts with one of the specified | |
4258 * prefixes and if so, removes the prefix. | |
4259 * | |
4260 * @param startLine the first line to shift | |
4261 * @param endLine the last line to shift | |
4262 * @param prefixes the prefixes to be used for shifting | |
4263 * @param ignoreWhitespace <code>true</code> if whitespace should be ignored, <code>false</code> otherwise | |
4264 * @since 2.0 | |
4265 */ | |
4266 private void shiftLeft(int startLine, int endLine, String[] prefixes, bool ignoreWhitespace) { | |
4267 | |
4268 IDocument d= getDocument(); | |
4269 | |
4270 try { | |
4271 | |
4272 IRegion[] occurrences= new IRegion[endLine - startLine + 1]; | |
4273 | |
4274 // find all the first occurrences of prefix in the given lines | |
4275 for (int i= 0; i < occurrences.length; i++) { | |
4276 | |
4277 IRegion line= d.getLineInformation(startLine + i); | |
4278 String text= d.get(line.getOffset(), line.getLength()); | |
4279 | |
4280 int index= -1; | |
4281 int[] found= TextUtilities.indexOf(prefixes, text, 0); | |
4282 if (found[0] !is -1) { | |
4283 if (ignoreWhitespace) { | |
4284 String s= d.get(line.getOffset(), found[0]); | |
4285 s= s.trim(); | |
4286 if (s.length() is 0) | |
4287 index= line.getOffset() + found[0]; | |
4288 } else if (found[0] is 0) | |
4289 index= line.getOffset(); | |
4290 } | |
4291 | |
4292 if (index > -1) { | |
4293 // remember where prefix is in line, so that it can be removed | |
4294 int length= prefixes[found[1]].length(); | |
4295 if (length is 0 && !ignoreWhitespace && line.getLength() > 0) { | |
4296 // found a non-empty line which cannot be shifted | |
4297 return; | |
4298 } | |
4299 occurrences[i]= new Region(index, length); | |
4300 } else { | |
4301 // found a line which cannot be shifted | |
4302 return; | |
4303 } | |
4304 } | |
4305 | |
4306 // OK - change the document | |
4307 int decrement= 0; | |
4308 for (int i= 0; i < occurrences.length; i++) { | |
4309 IRegion r= occurrences[i]; | |
4310 d.replace(r.getOffset() - decrement, r.getLength(), ""); //$NON-NLS-1$ | |
4311 decrement += r.getLength(); | |
4312 } | |
4313 | |
4314 } catch (BadLocationException x) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4315 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4316 System.out_.println("TextViewer.shiftLeft: BadLocationException"); //$NON-NLS-1$ |
129 | 4317 } |
4318 } | |
4319 | |
4320 /** | |
4321 * Returns whether the shown text can be printed. | |
4322 * | |
4323 * @return the viewer's printable mode | |
4324 */ | |
4325 protected bool isPrintable() { | |
4326 /* | |
4327 * 1GK7Q10: ITPUI:WIN98 - internal error after invoking print at editor view | |
4328 * Changed from returning true to testing the length of the printer queue | |
4329 */ | |
4330 PrinterData[] printerList= Printer.getPrinterList(); | |
4331 return (printerList !is null && printerList.length > 0); | |
4332 } | |
4333 | |
4334 /** | |
4335 * {@inheritDoc} | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4336 * |
129 | 4337 * @since 3.4 |
4338 */ | |
4339 public void print(StyledTextPrintOptions options) { | |
4340 final PrintDialog dialog= new PrintDialog(fTextWidget.getShell(), DWT.PRIMARY_MODAL); | |
4341 final PrinterData data= dialog.open(); | |
4342 | |
4343 if (data !is null) { | |
4344 final Printer printer= new Printer(data); | |
4345 final Runnable styledTextPrinter= fTextWidget.print(printer, options); | |
4346 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4347 void threadrun() { |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4348 styledTextPrinter.run(); |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4349 printer.dispose(); |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4350 } |
172 | 4351 JThread printingThread= new JThread( &threadrun ); |
4352 printingThread.setName( "Printing" ); //$NON-NLS-1$ | |
129 | 4353 printingThread.start(); |
4354 } | |
4355 } | |
4356 | |
4357 /** | |
4358 * Brings up a print dialog and calls <code>printContents(Printer)</code> | |
4359 * which performs the actual print. | |
4360 */ | |
4361 protected void print() { | |
4362 StyledTextPrintOptions options= new StyledTextPrintOptions(); | |
4363 options.printTextFontStyle= true; | |
4364 options.printTextForeground= true; | |
4365 print(options); | |
4366 } | |
4367 | |
4368 //------ find support | |
4369 | |
4370 /** | |
4371 * Adheres to the contract of {@link IFindReplaceTarget#canPerformFind()}. | |
4372 * | |
4373 * @return <code>true</code> if find can be performed, <code>false</code> otherwise | |
4374 */ | |
4375 protected bool canPerformFind() { | |
4376 IDocument d= getVisibleDocument(); | |
4377 return (fTextWidget !is null && d !is null && d.getLength() > 0); | |
4378 } | |
4379 | |
4380 /** | |
4381 * Adheres to the contract of {@link IFindReplaceTarget#findAndSelect(int, String, bool, bool, bool)}. | |
4382 * | |
4383 * @param startPosition the start position | |
4384 * @param findString the find string specification | |
4385 * @param forwardSearch the search direction | |
4386 * @param caseSensitive <code>true</code> if case sensitive, <code>false</code> otherwise | |
4387 * @param wholeWord <code>true</code> if match must be whole words, <code>false</code> otherwise | |
4388 * @return the model offset of the first match | |
4389 * @deprecated as of 3.0 use {@link #findAndSelect(int, String, bool, bool, bool, bool)} | |
4390 */ | |
4391 protected int findAndSelect(int startPosition, String findString, bool forwardSearch, bool caseSensitive, bool wholeWord) { | |
4392 try { | |
4393 return findAndSelect(startPosition, findString, forwardSearch, caseSensitive, wholeWord, false); | |
4394 } catch (IllegalStateException ex) { | |
4395 return -1; | |
4396 } catch (PatternSyntaxException ex) { | |
4397 return -1; | |
4398 } | |
4399 } | |
4400 | |
4401 /** | |
4402 * Adheres to the contract of | |
4403 * {@link dwtx.jface.text.IFindReplaceTargetExtension3#findAndSelect(int, String, bool, bool, bool, bool)}. | |
4404 * | |
4405 * @param startPosition the start position | |
4406 * @param findString the find string specification | |
4407 * @param forwardSearch the search direction | |
4408 * @param caseSensitive <code>true</code> if case sensitive, <code>false</code> otherwise | |
4409 * @param wholeWord <code>true</code> if matches must be whole words, <code>false</code> otherwise | |
4410 * @param regExSearch <code>true</code> if <code>findString</code> is a regular expression, <code>false</code> otherwise | |
4411 * @return the model offset of the first match | |
4412 * | |
4413 */ | |
4414 protected int findAndSelect(int startPosition, String findString, bool forwardSearch, bool caseSensitive, bool wholeWord, bool regExSearch) { | |
4415 if (fTextWidget is null) | |
4416 return -1; | |
4417 | |
4418 try { | |
4419 | |
4420 int widgetOffset= (startPosition is -1 ? startPosition : modelOffset2WidgetOffset(startPosition)); | |
4421 FindReplaceDocumentAdapter adapter= getFindReplaceDocumentAdapter(); | |
4422 IRegion matchRegion= adapter.find(widgetOffset, findString, forwardSearch, caseSensitive, wholeWord, regExSearch); | |
4423 if (matchRegion !is null) { | |
4424 int widgetPos= matchRegion.getOffset(); | |
4425 int length= matchRegion.getLength(); | |
4426 | |
4427 // Prevents setting of widget selection with line delimiters at beginning or end | |
4428 char startChar= adapter.charAt(widgetPos); | |
4429 char endChar= adapter.charAt(widgetPos+length-1); | |
4430 bool borderHasLineDelimiter= startChar is '\n' || startChar is '\r' || endChar is '\n' || endChar is '\r'; | |
162 | 4431 bool redraws_= redraws(); |
4432 if (borderHasLineDelimiter && redraws_) | |
129 | 4433 setRedraw(false); |
4434 | |
4435 if (redraws()) { | |
4436 fTextWidget.setSelectionRange(widgetPos, length); | |
4437 internalRevealRange(widgetPos, widgetPos + length); | |
4438 selectionChanged(widgetPos, length); | |
4439 } else { | |
4440 setSelectedRange(widgetOffset2ModelOffset(widgetPos), length); | |
162 | 4441 if (redraws_) |
129 | 4442 setRedraw(true); |
4443 } | |
4444 | |
4445 return widgetOffset2ModelOffset(widgetPos); | |
4446 } | |
4447 | |
4448 } catch (BadLocationException x) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4449 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4450 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.findAndSelect")); //$NON-NLS-1$ |
129 | 4451 } |
4452 | |
4453 return -1; | |
4454 } | |
4455 | |
4456 /** | |
4457 * Adheres to the contract of {@link dwtx.jface.text.IFindReplaceTargetExtension3#findAndSelect(int, String, bool, bool, bool, bool)}. | |
4458 * | |
4459 * @param startPosition the start position | |
4460 * @param findString the find string specification | |
4461 * @param forwardSearch the search direction | |
4462 * @param caseSensitive <code>true</code> if case sensitive, <code>false</code> otherwise | |
4463 * @param wholeWord <code>true</code> if matches must be whole words, <code>false</code> otherwise | |
4464 * @param rangeOffset the search scope offset | |
4465 * @param rangeLength the search scope length | |
4466 * @param regExSearch <code>true</code> if <code>findString</code> is a regular expression, <code>false</code> otherwise | |
4467 * @return the model offset of the first match | |
4468 * @since 3.0 | |
4469 */ | |
4470 protected int findAndSelectInRange(int startPosition, String findString, bool forwardSearch, bool caseSensitive, bool wholeWord, int rangeOffset, int rangeLength, bool regExSearch) { | |
4471 if (fTextWidget is null) | |
4472 return -1; | |
4473 | |
4474 try { | |
4475 | |
4476 int modelOffset; | |
4477 if (forwardSearch && (startPosition is -1 || startPosition < rangeOffset)) { | |
4478 modelOffset= rangeOffset; | |
4479 } else if (!forwardSearch && (startPosition is -1 || startPosition > rangeOffset + rangeLength)) { | |
4480 modelOffset= rangeOffset + rangeLength; | |
4481 } else { | |
4482 modelOffset= startPosition; | |
4483 } | |
4484 | |
4485 int widgetOffset= modelOffset2WidgetOffset(modelOffset); | |
4486 if (widgetOffset is -1) | |
4487 return -1; | |
4488 | |
4489 FindReplaceDocumentAdapter adapter= getFindReplaceDocumentAdapter(); | |
4490 IRegion matchRegion= adapter.find(widgetOffset, findString, forwardSearch, caseSensitive, wholeWord, regExSearch); | |
4491 int widgetPos= -1; | |
4492 int length= 0; | |
4493 if (matchRegion !is null) { | |
4494 widgetPos= matchRegion.getOffset(); | |
4495 length= matchRegion.getLength(); | |
4496 } | |
4497 int modelPos= widgetPos is -1 ? -1 : widgetOffset2ModelOffset(widgetPos); | |
4498 | |
4499 if (widgetPos !is -1 && (modelPos < rangeOffset || modelPos + length > rangeOffset + rangeLength)) | |
4500 widgetPos= -1; | |
4501 | |
4502 if (widgetPos > -1) { | |
4503 | |
4504 // Prevents setting of widget selection with line delimiters at beginning or end | |
4505 char startChar= adapter.charAt(widgetPos); | |
4506 char endChar= adapter.charAt(widgetPos+length-1); | |
4507 bool borderHasLineDelimiter= startChar is '\n' || startChar is '\r' || endChar is '\n' || endChar is '\r'; | |
162 | 4508 bool redraws_= redraws(); |
4509 if (borderHasLineDelimiter && redraws_) | |
129 | 4510 setRedraw(false); |
4511 | |
4512 if (redraws()) { | |
4513 fTextWidget.setSelectionRange(widgetPos, length); | |
4514 internalRevealRange(widgetPos, widgetPos + length); | |
4515 selectionChanged(widgetPos, length); | |
4516 } else { | |
4517 setSelectedRange(modelPos, length); | |
162 | 4518 if (redraws_) |
129 | 4519 setRedraw(true); |
4520 } | |
4521 | |
4522 return modelPos; | |
4523 } | |
4524 | |
4525 | |
4526 } catch (BadLocationException x) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4527 if (TRACE_ERRORS) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4528 System.out_.println(JFaceTextMessages.getString("TextViewer.error.bad_location.findAndSelect")); //$NON-NLS-1$ |
129 | 4529 } |
4530 | |
4531 return -1; | |
4532 } | |
4533 | |
4534 //---------- text presentation support | |
4535 | |
4536 /* | |
4537 * @see ITextViewer#setTextColor(Color) | |
4538 */ | |
4539 public void setTextColor(Color color) { | |
4540 if (color !is null) | |
4541 setTextColor(color, 0, getDocument().getLength(), true); | |
4542 } | |
4543 | |
4544 /* | |
4545 * @see ITextViewer#setTextColor(Color, start, length, bool) | |
4546 */ | |
4547 public void setTextColor(Color color, int start, int length, bool controlRedraw) { | |
4548 if (fTextWidget !is null) { | |
4549 | |
4550 StyleRange s= new StyleRange(); | |
4551 s.foreground= color; | |
4552 s.start= start; | |
4553 s.length= length; | |
4554 | |
4555 s= modelStyleRange2WidgetStyleRange(s); | |
4556 if (s !is null) { | |
4557 if (controlRedraw) | |
4558 fTextWidget.setRedraw(false); | |
4559 try { | |
4560 fTextWidget.setStyleRange(s); | |
4561 } finally { | |
4562 if (controlRedraw) | |
4563 fTextWidget.setRedraw(true); | |
4564 } | |
4565 } | |
4566 } | |
4567 } | |
4568 | |
4569 /** | |
4570 * Adds the given presentation to the viewer's style information. | |
4571 * | |
4572 * @param presentation the presentation to be added | |
4573 */ | |
4574 private void addPresentation(TextPresentation presentation) { | |
4575 | |
4576 StyleRange range= presentation.getDefaultStyleRange(); | |
4577 if (range !is null) { | |
4578 | |
4579 range= modelStyleRange2WidgetStyleRange(range); | |
4580 if (range !is null) | |
4581 fTextWidget.setStyleRange(range); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4582 |
129 | 4583 ArrayList ranges= new ArrayList(presentation.getDenumerableRanges()); |
4584 Iterator e= presentation.getNonDefaultStyleRangeIterator(); | |
4585 while (e.hasNext()) { | |
134 | 4586 range= cast(StyleRange) e.next(); |
129 | 4587 range= modelStyleRange2WidgetStyleRange(range); |
4588 if (range !is null) | |
4589 ranges.add(range); | |
4590 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4591 |
129 | 4592 if (!ranges.isEmpty()) |
156 | 4593 fTextWidget.replaceStyleRanges(0, 0, arraycast!(StyleRange)(ranges.toArray())); |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
4594 |
129 | 4595 } else { |
4596 IRegion region= modelRange2WidgetRange(presentation.getCoverage()); | |
4597 if (region is null) | |
4598 return; | |
4599 | |
4600 List list= new ArrayList(presentation.getDenumerableRanges()); | |
4601 Iterator e= presentation.getAllStyleRangeIterator(); | |
4602 while (e.hasNext()) { | |
134 | 4603 range= cast(StyleRange) e.next(); |
129 | 4604 range= modelStyleRange2WidgetStyleRange(range); |
4605 if (range !is null) | |
4606 list.add(range); | |
4607 } | |
4608 | |
4609 if (!list.isEmpty()) { | |
4610 StyleRange[] ranges= new StyleRange[list.size()]; | |
4611 list.toArray(ranges); | |
4612 fTextWidget.replaceStyleRanges(region.getOffset(), region.getLength(), ranges); | |
4613 } | |
4614 } | |
4615 } | |
4616 | |
4617 /** | |
4618 * Applies the given presentation to the given text widget. Helper method. | |
4619 * | |
4620 * @param presentation the style information | |
4621 * @since 2.1 | |
4622 */ | |
4623 private void applyTextPresentation(TextPresentation presentation) { | |
4624 | |
4625 List list= new ArrayList(presentation.getDenumerableRanges()); | |
4626 Iterator e= presentation.getAllStyleRangeIterator(); | |
4627 while (e.hasNext()) { | |
134 | 4628 StyleRange range= cast(StyleRange) e.next(); |
129 | 4629 range= modelStyleRange2WidgetStyleRange(range); |
4630 if (range !is null) | |
4631 list.add(range); | |
4632 } | |
4633 | |
4634 if (!list.isEmpty()) { | |
4635 StyleRange[] ranges= new StyleRange[list.size()]; | |
4636 list.toArray(ranges); | |
4637 fTextWidget.setStyleRanges(ranges); | |
4638 } | |
4639 } | |
4640 | |
4641 /** | |
4642 * Returns the visible region if it is not equal to the whole document. | |
4643 * Otherwise returns <code>null</code>. | |
4644 * | |
4645 * @return the viewer's visible region if smaller than input document, otherwise <code>null</code> | |
4646 */ | |
4647 protected IRegion _internalGetVisibleRegion() { | |
4648 | |
4649 IDocument document= getVisibleDocument(); | |
138 | 4650 if ( cast(ChildDocument)document ) { |
134 | 4651 Position p= (cast(ChildDocument) document).getParentDocumentRange(); |
129 | 4652 return new Region(p.getOffset(), p.getLength()); |
4653 } | |
4654 | |
4655 return null; | |
4656 } | |
4657 | |
4658 /* | |
4659 * @see ITextViewer#changeTextPresentation(TextPresentation, bool) | |
4660 */ | |
4661 public void changeTextPresentation(TextPresentation presentation, bool controlRedraw) { | |
4662 | |
4663 if (presentation is null || !redraws()) | |
4664 return; | |
4665 | |
4666 if (fTextWidget is null) | |
4667 return; | |
4668 | |
4669 | |
4670 /* | |
4671 * Call registered text presentation listeners | |
4672 * and let them apply their presentation. | |
4673 */ | |
4674 if (fTextPresentationListeners !is null) { | |
4675 ArrayList listeners= new ArrayList(fTextPresentationListeners); | |
4676 for (int i= 0, size= listeners.size(); i < size; i++) { | |
134 | 4677 ITextPresentationListener listener= cast(ITextPresentationListener)listeners.get(i); |
129 | 4678 listener.applyTextPresentation(presentation); |
4679 } | |
4680 } | |
4681 | |
4682 if (presentation.isEmpty()) | |
4683 return; | |
4684 | |
4685 if (controlRedraw) | |
4686 fTextWidget.setRedraw(false); | |
4687 | |
4688 if (fReplaceTextPresentation) | |
4689 applyTextPresentation(presentation); | |
4690 else | |
4691 addPresentation(presentation); | |
4692 | |
4693 if (controlRedraw) | |
4694 fTextWidget.setRedraw(true); | |
4695 } | |
4696 | |
4697 /* | |
4698 * @see ITextViewer#getFindReplaceTarget() | |
4699 */ | |
4700 public IFindReplaceTarget getFindReplaceTarget() { | |
4701 if (fFindReplaceTarget is null) | |
4702 fFindReplaceTarget= new FindReplaceTarget(); | |
4703 return fFindReplaceTarget; | |
4704 } | |
4705 | |
4706 /** | |
4707 * Returns the find/replace document adapter. | |
4708 * | |
4709 * @return the find/replace document adapter. | |
4710 * @since 3.0 | |
4711 */ | |
4712 protected FindReplaceDocumentAdapter getFindReplaceDocumentAdapter() { | |
4713 if (fFindReplaceDocumentAdapter is null) | |
4714 fFindReplaceDocumentAdapter= new FindReplaceDocumentAdapter(getVisibleDocument()); | |
4715 return fFindReplaceDocumentAdapter; | |
4716 } | |
4717 | |
4718 /* | |
4719 * @see ITextViewer#getTextOperationTarget() | |
4720 */ | |
4721 public ITextOperationTarget getTextOperationTarget() { | |
4722 return this; | |
4723 } | |
4724 | |
4725 /* | |
4726 * @see ITextViewerExtension#appendVerifyKeyListener(VerifyKeyListener) | |
4727 * @since 2.0 | |
4728 */ | |
4729 public void appendVerifyKeyListener(VerifyKeyListener listener) { | |
4730 int index= fVerifyKeyListenersManager.numberOfListeners(); | |
4731 fVerifyKeyListenersManager.insertListener(listener, index); | |
4732 } | |
4733 | |
4734 /* | |
4735 * @see ITextViewerExtension#prependVerifyKeyListener(VerifyKeyListener) | |
4736 * @since 2.0 | |
4737 */ | |
4738 public void prependVerifyKeyListener(VerifyKeyListener listener) { | |
4739 fVerifyKeyListenersManager.insertListener(listener, 0); | |
4740 | |
4741 } | |
4742 | |
4743 /* | |
4744 * @see ITextViewerExtension#removeVerifyKeyListener(VerifyKeyListener) | |
4745 * @since 2.0 | |
4746 */ | |
4747 public void removeVerifyKeyListener(VerifyKeyListener listener) { | |
4748 fVerifyKeyListenersManager.removeListener(listener); | |
4749 } | |
4750 | |
4751 /* | |
4752 * @see ITextViewerExtension#getMark() | |
4753 * @since 2.0 | |
4754 */ | |
4755 public int getMark() { | |
4756 return fMarkPosition is null || fMarkPosition.isDeleted() ? -1 : fMarkPosition.getOffset(); | |
4757 } | |
4758 | |
4759 /* | |
4760 * @see ITextViewerExtension#setMark(int) | |
4761 * @since 2.0 | |
4762 */ | |
4763 public void setMark(int offset) { | |
4764 | |
4765 // clear | |
4766 if (offset is -1) { | |
4767 if (fMarkPosition !is null && !fMarkPosition.isDeleted()) { | |
4768 | |
4769 IDocument document= getDocument(); | |
4770 if (document !is null) | |
4771 document.removePosition(fMarkPosition); | |
4772 } | |
4773 | |
4774 fMarkPosition= null; | |
4775 | |
4776 markChanged(-1, 0); | |
4777 | |
4778 // set | |
4779 } else { | |
4780 | |
4781 IDocument document= getDocument(); | |
4782 if (document is null) { | |
4783 fMarkPosition= null; | |
4784 return; | |
4785 } | |
4786 | |
4787 if (fMarkPosition !is null) | |
4788 document.removePosition(fMarkPosition); | |
4789 | |
4790 fMarkPosition= null; | |
4791 | |
4792 try { | |
4793 | |
4794 Position position= new Position(offset); | |
4795 document.addPosition(MARK_POSITION_CATEGORY, position); | |
4796 fMarkPosition= position; | |
4797 | |
4798 } catch (BadLocationException e) { | |
4799 return; | |
4800 } catch (BadPositionCategoryException e) { | |
4801 return; | |
4802 } | |
4803 | |
4804 markChanged(modelOffset2WidgetOffset(fMarkPosition.offset), 0); | |
4805 } | |
4806 } | |
4807 | |
4808 /* | |
4809 * @see Viewer#inputChanged(Object, Object) | |
4810 * @since 2.0 | |
4811 */ | |
4812 protected void inputChanged(Object newInput, Object oldInput) { | |
4813 | |
134 | 4814 IDocument oldDocument= cast(IDocument) oldInput; |
129 | 4815 if (oldDocument !is null) { |
4816 | |
4817 if (fMarkPosition !is null && !fMarkPosition.isDeleted()) | |
4818 oldDocument.removePosition(fMarkPosition); | |
4819 | |
4820 try { | |
4821 oldDocument.removePositionUpdater(fMarkPositionUpdater); | |
4822 oldDocument.removePositionCategory(MARK_POSITION_CATEGORY); | |
4823 | |
4824 } catch (BadPositionCategoryException e) { | |
4825 } | |
4826 } | |
4827 | |
4828 fMarkPosition= null; | |
4829 | |
138 | 4830 if ( cast(IDocumentExtension4)oldDocument ) { |
134 | 4831 IDocumentExtension4 document= cast(IDocumentExtension4) oldDocument; |
129 | 4832 document.removeDocumentRewriteSessionListener(fDocumentRewriteSessionListener); |
4833 } | |
4834 | |
4835 super.inputChanged(newInput, oldInput); | |
4836 | |
138 | 4837 if ( cast(IDocumentExtension4)newInput ) { |
134 | 4838 IDocumentExtension4 document= cast(IDocumentExtension4) newInput; |
129 | 4839 document.addDocumentRewriteSessionListener(fDocumentRewriteSessionListener); |
4840 } | |
4841 | |
134 | 4842 IDocument newDocument= cast(IDocument) newInput; |
129 | 4843 if (newDocument !is null) { |
4844 newDocument.addPositionCategory(MARK_POSITION_CATEGORY); | |
4845 newDocument.addPositionUpdater(fMarkPositionUpdater); | |
4846 } | |
4847 } | |
4848 | |
4849 /** | |
4850 * Informs all text listeners about the change of the viewer's redraw state. | |
4851 * @since 2.0 | |
4852 */ | |
4853 private void fireRedrawChanged() { | |
4854 fWidgetCommand.start= 0; | |
4855 fWidgetCommand.length= 0; | |
4856 fWidgetCommand.text= null; | |
4857 fWidgetCommand.event= null; | |
4858 updateTextListeners(fWidgetCommand); | |
4859 } | |
4860 | |
4861 /** | |
4862 * Enables the redrawing of this text viewer. | |
4863 * @since 2.0 | |
4864 */ | |
4865 protected void enabledRedrawing() { | |
4866 enabledRedrawing(-1); | |
4867 } | |
4868 /** | |
4869 * Enables the redrawing of this text viewer. | |
4870 * | |
4871 * @param topIndex the top index to be set or <code>-1</code> | |
4872 * @since 3.0 | |
4873 */ | |
4874 protected void enabledRedrawing(int topIndex) { | |
138 | 4875 if ( cast(IDocumentAdapterExtension)fDocumentAdapter ) { |
134 | 4876 IDocumentAdapterExtension extension= cast(IDocumentAdapterExtension) fDocumentAdapter; |
129 | 4877 StyledText textWidget= getTextWidget(); |
4878 if (textWidget !is null && !textWidget.isDisposed()) { | |
4879 extension.resumeForwardingDocumentChanges(); | |
4880 if (topIndex > -1) { | |
4881 try { | |
4882 setTopIndex(topIndex); | |
4883 } catch (IllegalArgumentException x) { | |
4884 // changes don't allow for the previous top pixel | |
4885 } | |
4886 } | |
4887 } | |
4888 } | |
4889 | |
4890 if (fViewerState !is null) { | |
4891 fViewerState.restore(topIndex is -1); | |
4892 fViewerState= null; | |
4893 } | |
4894 | |
4895 if (fTextWidget !is null && !fTextWidget.isDisposed()) | |
4896 fTextWidget.setRedraw(true); | |
4897 | |
4898 fireRedrawChanged(); | |
4899 } | |
4900 | |
4901 /** | |
4902 * Disables the redrawing of this text viewer. Subclasses may extend. | |
4903 * @since 2.0 | |
4904 */ | |
4905 protected void disableRedrawing() { | |
4906 if (fViewerState is null) | |
4907 fViewerState= new ViewerState(); | |
4908 | |
138 | 4909 if ( cast(IDocumentAdapterExtension)fDocumentAdapter ) { |
134 | 4910 IDocumentAdapterExtension extension= cast(IDocumentAdapterExtension) fDocumentAdapter; |
129 | 4911 extension.stopForwardingDocumentChanges(); |
4912 } | |
4913 | |
4914 if (fTextWidget !is null && !fTextWidget.isDisposed()) | |
4915 fTextWidget.setRedraw(false); | |
4916 | |
4917 fireRedrawChanged(); | |
4918 } | |
4919 | |
4920 /* | |
4921 * @see ITextViewerExtension#setRedraw(bool) | |
4922 * @since 2.0 | |
4923 */ | |
4924 public final void setRedraw(bool redraw) { | |
4925 setRedraw(redraw, -1); | |
4926 } | |
4927 | |
4928 /** | |
4929 * Basically same functionality as | |
4930 * <code>ITextViewerExtension.setRedraw(bool)</code>. Adds a way for | |
4931 * subclasses to pass in a desired top index that should be used when | |
4932 * <code>redraw</code> is <code>true</code>. If <code>topIndex</code> | |
4933 * is -1, this method is identical to | |
4934 * <code>ITextViewerExtension.setRedraw(bool)</code>. | |
4935 * | |
4936 * @see ITextViewerExtension#setRedraw(bool) | |
4937 * | |
4938 * @param redraw | |
4939 * @param topIndex | |
4940 * @since 3.0 | |
4941 */ | |
4942 protected final void setRedraw(bool redraw, int topIndex) { | |
4943 if (!redraw) { | |
4944 | |
4945 ++ fRedrawCounter; | |
4946 if (fRedrawCounter is 1) | |
4947 disableRedrawing(); | |
4948 | |
4949 } else { | |
4950 -- fRedrawCounter; | |
4951 if (fRedrawCounter is 0) { | |
4952 if (topIndex is -1) | |
4953 enabledRedrawing(); | |
4954 else | |
4955 enabledRedrawing(topIndex); | |
4956 } | |
4957 } | |
4958 } | |
4959 | |
4960 /** | |
4961 * Returns whether this viewer redraws itself. | |
4962 * | |
4963 * @return <code>true</code> if this viewer redraws itself | |
4964 * @since 2.0 | |
4965 */ | |
4966 protected final bool redraws() { | |
4967 return fRedrawCounter <= 0; | |
4968 } | |
4969 | |
4970 /** | |
4971 * Starts the sequential rewrite mode of the viewer's document. | |
4972 * | |
4973 * @param normalized <code>true</code> if the rewrite is performed from the start to the end of the document | |
4974 * @since 2.0 | |
4975 * @deprecated since 3.1 use {@link IDocumentExtension4#startRewriteSession(DocumentRewriteSessionType)} instead | |
4976 */ | |
4977 protected final void startSequentialRewriteMode(bool normalized) { | |
4978 IDocument document= getDocument(); | |
138 | 4979 if ( cast(IDocumentExtension)document ) { |
134 | 4980 IDocumentExtension extension= cast(IDocumentExtension) document; |
129 | 4981 extension.startSequentialRewrite(normalized); |
4982 } | |
4983 } | |
4984 | |
4985 /** | |
4986 * Sets the sequential rewrite mode of the viewer's document. | |
4987 * | |
4988 * @since 2.0 | |
4989 * @deprecated since 3.1 use {@link IDocumentExtension4#stopRewriteSession(DocumentRewriteSession)} instead | |
4990 */ | |
4991 protected final void stopSequentialRewriteMode() { | |
4992 IDocument document= getDocument(); | |
138 | 4993 if ( cast(IDocumentExtension)document ) { |
134 | 4994 IDocumentExtension extension= cast(IDocumentExtension) document; |
129 | 4995 extension.stopSequentialRewrite(); |
4996 } | |
4997 } | |
4998 | |
4999 /* | |
5000 * @see dwtx.jface.text.ITextViewerExtension#getRewriteTarget() | |
5001 * @since 2.0 | |
5002 */ | |
5003 public IRewriteTarget getRewriteTarget() { | |
5004 if (fRewriteTarget is null) | |
5005 fRewriteTarget= new RewriteTarget(); | |
5006 return fRewriteTarget; | |
5007 } | |
5008 | |
5009 /* | |
5010 * @see dwtx.jface.text.ITextViewerExtension2#getCurrentTextHover() | |
5011 */ | |
5012 public ITextHover getCurrentTextHover() { | |
5013 if (fTextHoverManager is null) | |
5014 return null; | |
162 | 5015 return fTextHoverManager.getCurrentTextHover_package(); |
129 | 5016 } |
5017 | |
5018 /* | |
5019 * @see dwtx.jface.text.ITextViewerExtension2#getHoverEventLocation() | |
5020 */ | |
5021 public Point getHoverEventLocation() { | |
5022 if (fTextHoverManager is null) | |
5023 return null; | |
162 | 5024 return fTextHoverManager.getHoverEventLocation_package(); |
129 | 5025 } |
5026 | |
5027 /** | |
5028 * Returns the paint manager of this viewer. | |
5029 * | |
5030 * @return the paint manager of this viewer | |
5031 * @since 2.1 | |
5032 */ | |
5033 protected PaintManager getPaintManager() { | |
5034 if (fPaintManager is null) | |
5035 fPaintManager= new PaintManager(this); | |
5036 return fPaintManager; | |
5037 } | |
5038 | |
5039 /** | |
5040 * Adds the given painter to this viewer. If the painter is already registered | |
5041 * this method is without effect. | |
5042 * | |
5043 * @param painter the painter to be added | |
5044 * @since 2.1 | |
5045 */ | |
5046 public void addPainter(IPainter painter) { | |
5047 getPaintManager().addPainter(painter); | |
5048 } | |
5049 | |
5050 /** | |
5051 * Removes the given painter from this viewer. If the painter has previously not been | |
5052 * added to this viewer this method is without effect. | |
5053 * | |
5054 * @param painter the painter to be removed | |
5055 * @since 2.1 | |
5056 */ | |
5057 public void removePainter(IPainter painter) { | |
5058 getPaintManager().removePainter(painter); | |
5059 } | |
5060 | |
5061 // ----------------------------------- conversions ------------------------------------------------------- | |
5062 | |
5063 /** | |
5064 * Implements the contract of {@link ITextViewerExtension5#modelLine2WidgetLine(int)}. | |
5065 * | |
5066 * @param modelLine the model line | |
5067 * @return the corresponding widget line or <code>-1</code> | |
5068 * @since 2.1 | |
5069 */ | |
5070 public int modelLine2WidgetLine(int modelLine) { | |
5071 if (fInformationMapping is null) | |
5072 return modelLine; | |
5073 | |
5074 try { | |
5075 return fInformationMapping.toImageLine(modelLine); | |
5076 } catch (BadLocationException x) { | |
5077 } | |
5078 | |
5079 return -1; | |
5080 } | |
5081 | |
5082 /** | |
5083 * Implements the contract of {@link ITextViewerExtension5#modelOffset2WidgetOffset(int)}. | |
5084 * | |
5085 * @param modelOffset the model offset | |
5086 * @return the corresponding widget offset or <code>-1</code> | |
5087 * @since 2.1 | |
5088 */ | |
5089 public int modelOffset2WidgetOffset(int modelOffset) { | |
5090 if (fInformationMapping is null) | |
5091 return modelOffset; | |
5092 | |
5093 try { | |
5094 return fInformationMapping.toImageOffset(modelOffset); | |
5095 } catch (BadLocationException x) { | |
5096 } | |
5097 | |
5098 return -1; | |
5099 } | |
5100 | |
5101 /** | |
5102 * Implements the contract of {@link ITextViewerExtension5#modelRange2WidgetRange(IRegion)}. | |
5103 * | |
5104 * @param modelRange the model range | |
5105 * @return the corresponding widget range or <code>null</code> | |
5106 * @since 2.1 | |
5107 */ | |
5108 public IRegion modelRange2WidgetRange(IRegion modelRange) { | |
5109 if (fInformationMapping is null) | |
5110 return modelRange; | |
5111 | |
5112 try { | |
5113 | |
5114 if (modelRange.getLength() < 0) { | |
5115 Region reversed= new Region(modelRange.getOffset() + modelRange.getLength(), -modelRange.getLength()); | |
5116 IRegion result= fInformationMapping.toImageRegion(reversed); | |
5117 if (result !is null) | |
5118 return new Region(result.getOffset() + result.getLength(), -result.getLength()); | |
5119 } | |
5120 return fInformationMapping.toImageRegion(modelRange); | |
5121 | |
5122 } catch (BadLocationException x) { | |
5123 } | |
5124 | |
5125 return null; | |
5126 } | |
5127 | |
5128 /** | |
5129 * Similar to {@link #modelRange2WidgetRange(IRegion)}, but more forgiving: | |
5130 * if <code>modelRange</code> describes a region entirely hidden in the | |
5131 * image, then this method returns the zero-length region at the offset of | |
5132 * the folded region. | |
5133 * | |
5134 * @param modelRange the model range | |
5135 * @return the corresponding widget range, or <code>null</code> | |
5136 * @since 3.1 | |
5137 */ | |
5138 protected IRegion modelRange2ClosestWidgetRange(IRegion modelRange) { | |
138 | 5139 if (!( cast(IDocumentInformationMappingExtension2)fInformationMapping )) |
129 | 5140 return modelRange2WidgetRange(modelRange); |
5141 | |
5142 try { | |
5143 if (modelRange.getLength() < 0) { | |
5144 Region reversed= new Region(modelRange.getOffset() + modelRange.getLength(), -modelRange.getLength()); | |
134 | 5145 IRegion result= (cast(IDocumentInformationMappingExtension2) fInformationMapping).toClosestImageRegion(reversed); |
129 | 5146 if (result !is null) |
5147 return new Region(result.getOffset() + result.getLength(), -result.getLength()); | |
5148 } | |
134 | 5149 return (cast(IDocumentInformationMappingExtension2) fInformationMapping).toClosestImageRegion(modelRange); |
129 | 5150 |
5151 } catch (BadLocationException x) { | |
5152 } | |
5153 | |
5154 return null; | |
5155 } | |
5156 | |
5157 /** | |
5158 * Implements the contract of {@link ITextViewerExtension5#widgetLine2ModelLine(int)}. | |
5159 * | |
5160 * @param widgetLine the widget line | |
5161 * @return the corresponding model line | |
5162 * @since 2.1 | |
5163 */ | |
5164 public int widgetlLine2ModelLine(int widgetLine) { | |
5165 return widgetLine2ModelLine(widgetLine); | |
5166 } | |
5167 | |
5168 /** | |
5169 * Implements the contract of {@link ITextViewerExtension5#widgetLine2ModelLine(int)}. | |
5170 * | |
5171 * @param widgetLine the widget line | |
5172 * @return the corresponding model line or <code>-1</code> | |
5173 * @since 3.0 | |
5174 */ | |
5175 public int widgetLine2ModelLine(int widgetLine) { | |
5176 if (fInformationMapping is null) | |
5177 return widgetLine; | |
5178 | |
5179 try { | |
5180 return fInformationMapping.toOriginLine(widgetLine); | |
5181 } catch (BadLocationException x) { | |
5182 } | |
5183 | |
5184 return -1; | |
5185 } | |
5186 | |
5187 /** | |
5188 * Implements the contract of {@link ITextViewerExtension5#widgetOffset2ModelOffset(int)}. | |
5189 * | |
5190 * @param widgetOffset the widget offset | |
5191 * @return the corresponding model offset or <code>-1</code> | |
5192 * @since 2.1 | |
5193 */ | |
5194 public int widgetOffset2ModelOffset(int widgetOffset) { | |
5195 if (fInformationMapping is null) | |
5196 return widgetOffset; | |
5197 | |
5198 try { | |
5199 return fInformationMapping.toOriginOffset(widgetOffset); | |
5200 } catch (BadLocationException x) { | |
5201 if (widgetOffset is getVisibleDocument().getLength()) { | |
5202 IRegion coverage= fInformationMapping.getCoverage(); | |
5203 return coverage.getOffset() + coverage.getLength(); | |
5204 } | |
5205 } | |
5206 | |
5207 return -1; | |
5208 } | |
5209 | |
5210 /** | |
5211 * Implements the contract of {@link ITextViewerExtension5#widgetRange2ModelRange(IRegion)}. | |
5212 * | |
5213 * @param widgetRange the widget range | |
5214 * @return the corresponding model range or <code>null</code> | |
5215 * @since 2.1 | |
5216 */ | |
5217 public IRegion widgetRange2ModelRange(IRegion widgetRange) { | |
5218 if (fInformationMapping is null) | |
5219 return widgetRange; | |
5220 | |
5221 try { | |
5222 | |
5223 if (widgetRange.getLength() < 0) { | |
5224 Region reveresed= new Region(widgetRange.getOffset() + widgetRange.getLength(), -widgetRange.getLength()); | |
5225 IRegion result= fInformationMapping.toOriginRegion(reveresed); | |
5226 return new Region(result.getOffset() + result.getLength(), -result.getLength()); | |
5227 } | |
5228 | |
5229 return fInformationMapping.toOriginRegion(widgetRange); | |
5230 | |
5231 } catch (BadLocationException x) { | |
5232 int modelOffset= widgetOffset2ModelOffset(widgetRange.getOffset()); | |
5233 if (modelOffset > -1) { | |
5234 int modelEndOffset= widgetOffset2ModelOffset(widgetRange.getOffset() + widgetRange.getLength()); | |
5235 if (modelEndOffset > -1) | |
5236 return new Region(modelOffset, modelEndOffset - modelOffset); | |
5237 } | |
5238 } | |
5239 | |
5240 return null; | |
5241 } | |
5242 | |
5243 /** | |
5244 * Implements the contract of {@link ITextViewerExtension5#getModelCoverage()}. | |
5245 * | |
5246 * @return the model coverage | |
5247 * @since 2.1 | |
5248 */ | |
5249 public IRegion getModelCoverage() { | |
5250 if (fInformationMapping is null) { | |
5251 IDocument document= getDocument(); | |
5252 if (document is null) | |
5253 return null; | |
5254 return new Region(0, document.getLength()); | |
5255 } | |
5256 | |
5257 return fInformationMapping.getCoverage(); | |
5258 } | |
5259 | |
5260 /** | |
5261 * Returns the line of the widget whose corresponding line in the viewer's document | |
5262 * is closest to the given line in the viewer's document or <code>-1</code>. | |
5263 * | |
5264 * @param modelLine the line in the viewer's document | |
5265 * @return the line in the widget that corresponds best to the given line in the viewer's document or <code>-1</code> | |
5266 * @since 2.1 | |
5267 */ | |
5268 protected int getClosestWidgetLineForModelLine(int modelLine) { | |
5269 if (fInformationMapping is null) | |
5270 return modelLine; | |
5271 | |
5272 try { | |
5273 return fInformationMapping.toClosestImageLine(modelLine); | |
5274 } catch (BadLocationException x) { | |
5275 } | |
5276 | |
5277 return -1; | |
5278 } | |
5279 | |
5280 /** | |
5281 * Translates a style range given relative to the viewer's document into style | |
5282 * ranges relative to the viewer's widget or <code>null</code>. | |
5283 * | |
5284 * @param range the style range in the coordinates of the viewer's document | |
5285 * @return the style range in the coordinates of the viewer's widget or <code>null</code> | |
5286 * @since 2.1 | |
5287 */ | |
5288 protected StyleRange modelStyleRange2WidgetStyleRange(StyleRange range) { | |
5289 IRegion region= modelRange2WidgetRange(new Region(range.start, range.length)); | |
5290 if (region !is null) { | |
134 | 5291 StyleRange result= cast(StyleRange) range.clone(); |
129 | 5292 result.start= region.getOffset(); |
5293 result.length= region.getLength(); | |
5294 return result; | |
5295 } | |
5296 return null; | |
5297 } | |
5298 | |
5299 /** | |
5300 * Same as {@link #modelRange2WidgetRange(IRegion)} just for a {@link dwtx.jface.text.Position}. | |
5301 * | |
5302 * @param modelPosition the position describing a range in the viewer's document | |
5303 * @return a region describing a range in the viewer's widget | |
5304 * @since 2.1 | |
5305 */ | |
5306 protected IRegion modelRange2WidgetRange(Position modelPosition) { | |
5307 return modelRange2WidgetRange(new Region(modelPosition.getOffset(), modelPosition.getLength())); | |
5308 } | |
5309 | |
5310 /** | |
5311 * Translates the widget region of the given verify event into | |
5312 * the corresponding region of the viewer's document. | |
5313 * | |
5314 * @param event the verify event | |
5315 * @return the region of the viewer's document corresponding to the verify event | |
5316 * @since 2.1 | |
5317 */ | |
5318 protected IRegion event2ModelRange(VerifyEvent event) { | |
5319 | |
5320 Region region= null; | |
5321 if (event.start <= event.end) | |
5322 region= new Region(event.start, event.end - event.start); | |
5323 else | |
5324 region= new Region(event.end, event.start - event.end); | |
5325 | |
5326 return widgetRange2ModelRange(region); | |
5327 } | |
5328 | |
5329 /** | |
5330 * Translates the given widget selection into the corresponding region | |
5331 * of the viewer's document or returns <code>null</code> if this fails. | |
5332 * | |
5333 * @param widgetSelection the widget selection | |
5334 * @return the region of the viewer's document corresponding to the widget selection or <code>null</code> | |
5335 * @since 2.1 | |
5336 */ | |
5337 protected Point widgetSelection2ModelSelection(Point widgetSelection) { | |
5338 IRegion region= new Region(widgetSelection.x, widgetSelection.y); | |
5339 region= widgetRange2ModelRange(region); | |
5340 return region is null ? null : new Point(region.getOffset(), region.getLength()); | |
5341 } | |
5342 | |
5343 /** | |
5344 * Translates the given selection range of the viewer's document into | |
5345 * the corresponding widget range or returns <code>null</code> of this fails. | |
5346 * | |
5347 * @param modelSelection the selection range of the viewer's document | |
5348 * @return the widget range corresponding to the selection range or <code>null</code> | |
5349 * @since 2.1 | |
5350 */ | |
5351 protected Point modelSelection2WidgetSelection(Point modelSelection) { | |
5352 if (fInformationMapping is null) | |
5353 return modelSelection; | |
5354 | |
5355 try { | |
5356 IRegion region= new Region(modelSelection.x, modelSelection.y); | |
5357 region= fInformationMapping.toImageRegion(region); | |
5358 if (region !is null) | |
5359 return new Point(region.getOffset(), region.getLength()); | |
5360 } catch (BadLocationException x) { | |
5361 } | |
5362 | |
5363 return null; | |
5364 } | |
5365 | |
5366 /** | |
5367 * Implements the contract of {@link ITextViewerExtension5#widgetLineOfWidgetOffset(int)}. | |
5368 * | |
5369 * @param widgetOffset the widget offset | |
5370 * @return the corresponding widget line or <code>-1</code> | |
5371 * @since 2.1 | |
5372 */ | |
5373 public int widgetLineOfWidgetOffset(int widgetOffset) { | |
5374 IDocument document= getVisibleDocument(); | |
5375 if (document !is null) { | |
5376 try { | |
5377 return document.getLineOfOffset(widgetOffset); | |
5378 } catch (BadLocationException e) { | |
5379 } | |
5380 } | |
5381 return -1; | |
5382 } | |
5383 | |
5384 /* | |
5385 * @see dwtx.jface.text.ITextViewerExtension4#moveFocusToWidgetToken() | |
5386 * @since 3.0 | |
5387 */ | |
5388 public bool moveFocusToWidgetToken() { | |
138 | 5389 if ( cast(IWidgetTokenKeeperExtension)fWidgetTokenKeeper ) { |
134 | 5390 IWidgetTokenKeeperExtension extension= cast(IWidgetTokenKeeperExtension) fWidgetTokenKeeper; |
129 | 5391 return extension.setFocus(this); |
5392 } | |
5393 return false; | |
5394 } | |
5395 | |
5396 /** | |
5397 * Sets the document partitioning of this viewer. The partitioning is used by this viewer to | |
5398 * access partitioning information of the viewers input document. | |
5399 * | |
5400 * @param partitioning the partitioning name | |
5401 * @since 3.0 | |
5402 */ | |
5403 public void setDocumentPartitioning(String partitioning) { | |
5404 fPartitioning= partitioning; | |
5405 } | |
5406 | |
5407 /** | |
5408 * Returns the document partitioning for this viewer. | |
5409 * | |
5410 * @return the document partitioning for this viewer | |
5411 * @since 3.0 | |
5412 */ | |
5413 protected String getDocumentPartitioning() { | |
5414 return fPartitioning; | |
5415 } | |
5416 | |
5417 //---- Text presentation listeners ---- | |
5418 | |
5419 /* | |
5420 * @see ITextViewerExtension4#addTextPresentationListener(ITextPresentationListener) | |
5421 * @since 3.0 | |
5422 */ | |
5423 public void addTextPresentationListener(ITextPresentationListener listener) { | |
5424 | |
162 | 5425 Assert.isNotNull(cast(Object)listener); |
129 | 5426 |
5427 if (fTextPresentationListeners is null) | |
5428 fTextPresentationListeners= new ArrayList(); | |
5429 | |
162 | 5430 if (!fTextPresentationListeners.contains(cast(Object)listener)) |
5431 fTextPresentationListeners.add(cast(Object)listener); | |
129 | 5432 } |
5433 | |
5434 /* | |
5435 * @see ITextViewerExtension4#removeTextPresentationListener(ITextPresentationListener) | |
5436 * @since 3.0 | |
5437 */ | |
5438 public void removeTextPresentationListener(ITextPresentationListener listener) { | |
5439 | |
162 | 5440 Assert.isNotNull(cast(Object)listener); |
129 | 5441 |
5442 if (fTextPresentationListeners !is null) { | |
162 | 5443 fTextPresentationListeners.remove(cast(Object)listener); |
129 | 5444 if (fTextPresentationListeners.size() is 0) |
5445 fTextPresentationListeners= null; | |
5446 } | |
5447 } | |
5448 | |
5449 /* | |
5450 * @see dwtx.jface.text.IEditingSupportRegistry#registerHelper(dwtx.jface.text.IEditingSupport) | |
5451 * @since 3.1 | |
5452 */ | |
5453 public void register(IEditingSupport helper) { | |
5454 Assert.isLegal(helper !is null); | |
162 | 5455 fEditorHelpers.add(cast(Object)helper); |
129 | 5456 } |
5457 | |
5458 /* | |
5459 * @see dwtx.jface.text.IEditingSupportRegistry#deregisterHelper(dwtx.jface.text.IEditingSupport) | |
5460 * @since 3.1 | |
5461 */ | |
5462 public void unregister(IEditingSupport helper) { | |
162 | 5463 fEditorHelpers.remove(cast(Object)helper); |
129 | 5464 } |
5465 | |
5466 /* | |
5467 * @see dwtx.jface.text.IEditingSupportRegistry#getCurrentHelpers() | |
5468 * @since 3.1 | |
5469 */ | |
5470 public IEditingSupport[] getRegisteredSupports() { | |
156 | 5471 return arraycast!(IEditingSupport)( fEditorHelpers.toArray()); |
129 | 5472 } |
5473 | |
5474 /* | |
5475 * @see dwtx.jface.text.ITextViewerExtension6#setHyperlinkDetectors(dwtx.jface.text.hyperlink.IHyperlinkDetector[], int) | |
5476 * @since 3.1 | |
5477 */ | |
5478 public void setHyperlinkDetectors(IHyperlinkDetector[] hyperlinkDetectors, int eventStateMask) { | |
5479 if (fHyperlinkDetectors !is null) { | |
5480 for (int i= 0; i < fHyperlinkDetectors.length; i++) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
5481 if (cast(IHyperlinkDetectorExtension)fHyperlinkDetectors[i] ) |
134 | 5482 (cast(IHyperlinkDetectorExtension)fHyperlinkDetectors[i]).dispose(); |
129 | 5483 } |
5484 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
5485 |
129 | 5486 bool enable= hyperlinkDetectors !is null && hyperlinkDetectors.length > 0; |
5487 fHyperlinkStateMask= eventStateMask; | |
5488 fHyperlinkDetectors= hyperlinkDetectors; | |
5489 if (enable) { | |
5490 if (fHyperlinkManager !is null) { | |
5491 fHyperlinkManager.setHyperlinkDetectors(fHyperlinkDetectors); | |
5492 fHyperlinkManager.setHyperlinkStateMask(fHyperlinkStateMask); | |
5493 } | |
5494 ensureHyperlinkManagerInstalled(); | |
5495 } else { | |
5496 if (fHyperlinkManager !is null) | |
5497 fHyperlinkManager.uninstall(); | |
5498 fHyperlinkManager= null; | |
5499 } | |
5500 } | |
5501 | |
5502 /** | |
5503 * Sets the hyperlink presenter. | |
5504 * <p> | |
5505 * This is only valid as long as the hyperlink manager hasn't | |
5506 * been created yet. | |
5507 * </p> | |
5508 * | |
5509 * @param hyperlinkPresenter the hyperlink presenter | |
5510 * @throws IllegalStateException if the hyperlink manager has already been created | |
5511 * @since 3.1 | |
5512 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
135
diff
changeset
|
5513 public void setHyperlinkPresenter(IHyperlinkPresenter hyperlinkPresenter) { |
129 | 5514 if (fHyperlinkManager !is null) |
5515 throw new IllegalStateException(); | |
5516 | |
5517 fHyperlinkPresenter= hyperlinkPresenter; | |
5518 ensureHyperlinkManagerInstalled(); | |
5519 } | |
5520 | |
5521 /** | |
5522 * Ensures that the hyperlink manager has been | |
5523 * installed if a hyperlink detector is available. | |
5524 * | |
5525 * @since 3.1 | |
5526 */ | |
5527 private void ensureHyperlinkManagerInstalled() { | |
5528 if (fHyperlinkDetectors !is null && fHyperlinkDetectors.length > 0 && fHyperlinkPresenter !is null && fHyperlinkManager is null) { | |
159 | 5529 HyperlinkManager.DETECTION_STRATEGY strategy= fHyperlinkPresenter.canShowMultipleHyperlinks() ? HyperlinkManager.ALL : HyperlinkManager.FIRST; |
129 | 5530 fHyperlinkManager= new HyperlinkManager(strategy); |
5531 fHyperlinkManager.install(this, fHyperlinkPresenter, fHyperlinkDetectors, fHyperlinkStateMask); | |
5532 } | |
5533 } | |
5534 | |
5535 /* | |
5536 * @see dwtx.jface.text.ITextViewerExtension7#setTabsToSpacesConverter(dwtx.jface.text.IAutoEditStrategy) | |
5537 * @since 3.3 | |
5538 */ | |
5539 public void setTabsToSpacesConverter(IAutoEditStrategy converter) { | |
5540 fTabsToSpacesConverter= converter; | |
5541 } | |
5542 | |
5543 } |