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